Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion front/src/applications/editor/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { ChevronLeft, ChevronRight } from '@osrd-project/ui-icons';
import { skipToken } from '@reduxjs/toolkit/query';
import cx from 'classnames';
import { isNil, toInteger } from 'lodash';
import { useTranslation } from 'react-i18next';
Expand All @@ -20,14 +21,15 @@ import useSwitchTypes from 'applications/editor/tools/switchEdition/useSwitchTyp
import type { switchProps } from 'applications/editor/tools/switchProps';
import type { CommonToolState } from 'applications/editor/tools/types';
import { centerMapOnObject, selectEntities } from 'applications/editor/tools/utils';
import type { ObjectType } from 'common/api/osrdEditoastApi';
import { osrdEditoastApi, type ObjectType } from 'common/api/osrdEditoastApi';
import useCheckUserPrivileges from 'common/authorization/hooks/useCheckUserPrivileges';
import useProtectedAction from 'common/authorization/hooks/useProtectedAction';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import { LoaderState } from 'common/Loaders';
import MapButtons from 'common/Map/Buttons/MapButtons';
import MapSearch from 'common/Map/Search/MapSearch';
import { MapContextProvider } from 'common/Map/useMapContext';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useInfraActions, useInfraID, useOsrdActions } from 'common/osrdContext';
import useInfra from 'modules/infra/useInfra';
import { useMapSettings, useMapSettingsActions } from 'reducers/commonMap';
Expand Down Expand Up @@ -310,6 +312,20 @@ const Editor = () => {
}
}, [urlInfra]);

const { data: infraBbox } = osrdEditoastApi.endpoints.getInfraByInfraIdBbox.useQuery(
infraID ? { infraId: infraID } : skipToken
);
useMemo(() => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo() is supposed to return a value. Here, no value is returned.

setViewport() does not integrate well with React here, unfortunately. Ideally the return value of useMemo() would be used as the initial viewport.

As-is, this patch results in a glitch: the map is first displayed with an arbitrary default viewport, then it's updated later on with the infra's bbox. Ideally the map would not show up until we have the infra's bbox.

if (infraBbox === undefined) return;
const { min_lat, min_lon, max_lat, max_lon } = infraBbox!;

const newViewport = computeBBoxViewport([min_lon, min_lat, max_lon, max_lat], viewport, {
padding: 64,
});

setViewport(newViewport);
}, [infraBbox]);

// Lifecycle events on tools:
useEffect(() => {
if (toolAndState.tool.onMount) toolAndState.tool.onMount(extendedContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useRef, useState, type PropsWithChildren } from 'react';

import { skipToken } from '@reduxjs/toolkit/query';
import type { Feature, Position } from 'geojson';
import { useTranslation } from 'react-i18next';
import type { MapLayerMouseEvent, MapRef } from 'react-map-gl/maplibre';
Expand All @@ -16,6 +17,7 @@ import MapButtons from 'common/Map/Buttons/MapButtons';
import PathStepMarker, { PATH_STEP_MARKER_STATE } from 'common/Map/components/PathStepMarker';
import { SnappedMarker } from 'common/Map/Layers';
import { MapContextProvider } from 'common/Map/useMapContext';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useInfraID } from 'common/osrdContext';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import Itinerary from 'modules/simulationResult/components/SimulationResultsMap/RenderItinerary';
Expand Down Expand Up @@ -103,6 +105,25 @@ const ItineraryModalMap = ({
const lastRealStepIndex = pathSteps ? pathSteps.length - (lastStepHasLocation ? 1 : 2) : -1;

const isNewPlacement = isMapSelectionMode;
const locatedStepsCount = useMemo(
() => pathSteps?.filter((step) => step.location !== null).length ?? 0,
[pathSteps]
);

const { data: infraBbox } = osrdEditoastApi.endpoints.getInfraByInfraIdBbox.useQuery(
infraID ? { infraId: infraID } : skipToken
);

useMemo(() => {
if (infraBbox === undefined) return;
if (locatedStepsCount >= 2) return;
const { min_lat, min_lon, max_lat, max_lon } = infraBbox!;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nit: I think you can get rid of this ! by checking if infraBBox is falsy two lines earlier (if (!infraBbox) return false; instead of if (infraBbox === undefined) return false;.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not nit because otherwise, if infraBbox is null (for an empty infra), we got a blank page.
We got the same problem on the three files.


const newViewport = computeBBoxViewport([min_lon, min_lat, max_lon, max_lat], viewport, {
padding: 64,
});
updateViewport(newViewport);
}, [infraBbox, locatedStepsCount]);

useEffect(() => {
if (!isMapSelectionMode) {
Expand Down
25 changes: 25 additions & 0 deletions front/src/applications/referenceMap/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useCallback, useMemo, useRef } from 'react';

import { skipToken } from '@reduxjs/toolkit/query';
import type { MapLibreEvent } from 'maplibre-gl';
import type { MapRef } from 'react-map-gl/maplibre';

import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import BaseMap from 'common/Map/BaseMap';
import MapButtons from 'common/Map/Buttons/MapButtons';
import { MapContextProvider } from 'common/Map/useMapContext';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useInfraID } from 'common/osrdContext';
import { useParams } from 'react-router-dom';
import { useMapSettings, useMapSettingsActions } from 'reducers/commonMap';
import type { MapSettings, Viewport } from 'reducers/commonMap/types';
import { syncReferenceMapRouterViewport, updateReferenceMapViewport } from 'reducers/referenceMap';
Expand Down Expand Up @@ -58,6 +62,27 @@ const Map = () => {
[layersSettings.track_sections]
);

const { urlLat } = useParams();
const skipNextInfraAutoFocusRef = useRef(urlLat !== undefined);
const { data: infraBbox } = osrdEditoastApi.endpoints.getInfraByInfraIdBbox.useQuery(
infraID ? { infraId: infraID } : skipToken
);
useMemo(() => {
if (infraBbox === undefined) return;
if (skipNextInfraAutoFocusRef.current) {
skipNextInfraAutoFocusRef.current = false;
return;
}

const { min_lat, min_lon, max_lat, max_lon } = infraBbox!;

const newViewport = computeBBoxViewport([min_lon, min_lat, max_lon, max_lat], viewport, {
padding: 64,
});

updateViewportChange(newViewport);
}, [infraBbox]);

return (
<main className="mastcontainer mastcontainer-map">
<MapContextProvider
Expand Down
Loading