Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 34 additions & 1 deletion front/src/applications/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,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 @@ -57,8 +58,10 @@ const Editor = () => {
useMapSettingsActions();

const mapRef = useRef<MapRef>(null);
const focusedInfraIdRef = useRef<number | undefined>(undefined);
const { urlInfra } = useParams();
const infraID = useInfraID();
const [getInfraByInfraIdBbox] = osrdEditoastApi.endpoints.getInfraByInfraIdBbox.useLazyQuery();
const [searchParams, setSearchParams] = useSearchParams();
const isLoading = useSelector(getIsLoading);
const isLocked = useSelector(getInfraLockStatus);
Expand Down Expand Up @@ -310,6 +313,36 @@ const Editor = () => {
}
}, [urlInfra]);

useEffect(() => {

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.

useEffect() isn't a great thing to use here. useQuery() + useMemo() would be better. Redux usage makes things a bit painful here.

if (isNil(infraID) || focusedInfraIdRef.current === infraID) {
return;
}

focusedInfraIdRef.current = infraID;

void getInfraByInfraIdBbox({ infraId: infraID })
.unwrap()
.then((infraBbox) => {

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.

async/await should be preferred over the callback-based Promise API.

if (!infraBbox) {
return;
}

const mapContainer = mapRef.current?.getContainer();
const newViewport = computeBBoxViewport(
[infraBbox.min_lon, infraBbox.min_lat, infraBbox.max_lon, infraBbox.max_lat],
viewport,
{
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 100,
}
);

setViewport(newViewport);
})
.catch(() => undefined);
Comment thread
flomonster marked this conversation as resolved.
Outdated
}, [infraID, getInfraByInfraIdBbox, setViewport, viewport]);

// 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
Expand Up @@ -16,6 +16,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 @@ -81,6 +82,7 @@ const ItineraryModalMap = ({
} = useMapSettingsActions();

const mapRef = useRef<MapRef | null>(null);
const focusedInfraIdRef = useRef<number | undefined>(undefined);

const [hoveredOperationalPointId, setHoveredOperationalPointId] = useState<string>();
const [isDraggingMarker, setIsDraggingMarker] = useState(false);
Expand All @@ -98,11 +100,16 @@ const ItineraryModalMap = ({

const [getInfraObjectEntity] =
osrdEditoastApi.endpoints.postInfraByInfraIdObjectsAndObjectType.useLazyQuery();
const [getInfraByInfraIdBbox] = osrdEditoastApi.endpoints.getInfraByInfraIdBbox.useLazyQuery();

const lastStepHasLocation = !!pathSteps?.at(-1)?.location;
const lastRealStepIndex = pathSteps ? pathSteps.length - (lastStepHasLocation ? 1 : 2) : -1;

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

useEffect(() => {
if (!isMapSelectionMode) {
Expand Down Expand Up @@ -314,6 +321,42 @@ const ItineraryModalMap = ({
return result;
}, [layersSettings, isMapSelectionMode]);

useEffect(() => {
if (pathProperties?.geometry) {
focusedInfraIdRef.current = undefined;
return;
}

// Initial state: focus on infra until a valid itinerary can drive map framing.
if (!infraID || locatedStepsCount >= 2 || focusedInfraIdRef.current === infraID) {
return;
}

focusedInfraIdRef.current = infraID;

void getInfraByInfraIdBbox({ infraId: infraID })
.unwrap()
.then((infraBbox) => {
if (!infraBbox) {
return;
}

const mapContainer = mapRef.current?.getContainer();
const newViewport = computeBBoxViewport(
[infraBbox.min_lon, infraBbox.min_lat, infraBbox.max_lon, infraBbox.max_lat],
viewport,
{
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 100,
}
);

dispatch(updateViewport(newViewport));
})
.catch(() => undefined);
}, [infraID, locatedStepsCount, pathProperties?.geometry, getInfraByInfraIdBbox, viewport]);

return (
<MapContextProvider
infraId={infraID}
Expand Down
47 changes: 46 additions & 1 deletion front/src/applications/referenceMap/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useCallback, useMemo, useRef } from 'react';
import { useCallback, useEffect, useMemo, useRef } from 'react';

import type { MapLibreEvent } from 'maplibre-gl';
import type { MapRef } from 'react-map-gl/maplibre';
import { useParams } from 'react-router-dom';

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 { useMapSettings, useMapSettingsActions } from 'reducers/commonMap';
import type { MapSettings, Viewport } from 'reducers/commonMap/types';
Expand All @@ -22,8 +25,14 @@ const Map = () => {
useMapSettingsActions();

const infraID = useInfraID();
const { urlLat, urlLon, urlZoom, urlBearing, urlPitch } = useParams();

const mapRef = useRef<MapRef | null>(null);
const focusedInfraIdRef = useRef<number | undefined>(undefined);
const skipNextInfraAutoFocusRef = useRef(
Boolean(urlLat && urlLon && urlZoom && urlBearing && urlPitch)
);
const [getInfraByInfraIdBbox] = osrdEditoastApi.endpoints.getInfraByInfraIdBbox.useLazyQuery();

const updateMapSettings = useCallback(
(value: Partial<MapSettings>) => {
Expand Down Expand Up @@ -58,6 +67,42 @@ const Map = () => {
[layersSettings.track_sections]
);

useEffect(() => {
if (!infraID || focusedInfraIdRef.current === infraID) {
return;
}

if (skipNextInfraAutoFocusRef.current) {
skipNextInfraAutoFocusRef.current = false;
focusedInfraIdRef.current = infraID;
return;
}

focusedInfraIdRef.current = infraID;

void getInfraByInfraIdBbox({ infraId: infraID })
.unwrap()
.then((infraBbox) => {
if (!infraBbox) {
return;
}

const mapContainer = mapRef.current?.getContainer();
const newViewport = computeBBoxViewport(
[infraBbox.min_lon, infraBbox.min_lat, infraBbox.max_lon, infraBbox.max_lat],
viewport,
{
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 100,
}
);

updateViewportChange(newViewport);
})
.catch(() => undefined);
}, [infraID, getInfraByInfraIdBbox, updateViewportChange, viewport]);

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