Skip to content

Commit

Permalink
front: move allWaypoints to ManageTrainScheduleContextType
Browse files Browse the repository at this point in the history
This value should be updated every time pathSteps or
suggestedOperationalPoints change. Instead of manually updating it
when touching these two values, memoize it.

Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Dec 23, 2024
1 parent ada2dae commit 3505153
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createContext, useContext, useMemo, type ReactNode, useState } from 'react';

import { compact } from 'lodash';
import { useSelector } from 'react-redux';

import type { InfraWithState } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { RangedValue } from 'common/types';
import getPathVoltages from 'modules/pathfinding/helpers/getPathVoltages';
import usePathfinding from 'modules/pathfinding/hooks/usePathfinding';
import type { PathfindingState } from 'modules/pathfinding/types';
import { upsertPathStepsInOPs } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { PathStep } from 'reducers/osrdconf/types';

import type { ManageTrainSchedulePathProperties } from '../types';
Expand All @@ -16,6 +22,8 @@ type ManageTrainScheduleContextType = {
launchPathfinding: (pathSteps: (PathStep | null)[]) => void;
pathfindingState: PathfindingState;
infraInfo: { infra?: InfraWithState; reloadCount: number };
/** Operational points along the path (including origin and destination) and vias added by clicking on map */
allWaypoints?: SuggestedOP[];
} | null;

const ManageTrainScheduleContext = createContext<ManageTrainScheduleContextType>(null);
Expand All @@ -25,6 +33,9 @@ type ManageTrainScheduleContextProviderProps = { children: ReactNode };
export const ManageTrainScheduleContextProvider = ({
children,
}: ManageTrainScheduleContextProviderProps) => {
const { getPathSteps } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);

const [pathProperties, setPathProperties] = useState<ManageTrainSchedulePathProperties>();

const { launchPathfinding, pathfindingState, infraInfo } = usePathfinding(setPathProperties);
Expand All @@ -34,6 +45,11 @@ export const ManageTrainScheduleContextProvider = ({
[pathProperties]
);

const allWaypoints = useMemo(() => {
if (!pathProperties) return undefined;
return upsertPathStepsInOPs(pathProperties.suggestedOperationalPoints, compact(pathSteps));
}, [pathProperties?.suggestedOperationalPoints, pathSteps]);

const providedContext = useMemo(
() => ({
pathProperties,
Expand All @@ -42,6 +58,7 @@ export const ManageTrainScheduleContextProvider = ({
launchPathfinding,
pathfindingState,
infraInfo,
allWaypoints,
}),
[
pathProperties,
Expand All @@ -50,6 +67,7 @@ export const ManageTrainScheduleContextProvider = ({
launchPathfinding,
pathfindingState,
infraInfo,
allWaypoints,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import {
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import buildOpSearchQuery from 'modules/operationalPoint/helpers/buildOpSearchQuery';
import {
formatSuggestedOperationalPoints,
matchPathStepAndOp,
upsertPathStepsInOPs,
} from 'modules/pathfinding/utils';
import { formatSuggestedOperationalPoints, matchPathStepAndOp } from 'modules/pathfinding/utils';
import { getSupportedElectrification, isThermal } from 'modules/rollingStock/helpers/electric';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import computeBasePathStep from 'modules/trainschedule/helpers/computeBasePathStep';
Expand Down Expand Up @@ -213,14 +209,11 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
});
}

const allWaypoints = upsertPathStepsInOPs(suggestedOperationalPoints, updatedPathSteps);

return {
pathProperties: {
electrifications,
geometry,
suggestedOperationalPoints,
allWaypoints,
length: pathfindingResult.length,
trackSectionRanges: pathfindingResult.track_section_ranges,
},
Expand Down
2 changes: 0 additions & 2 deletions front/src/applications/operationalStudies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ export type ManageTrainSchedulePathProperties = {
electrifications: NonNullable<PathProperties['electrifications']>;
geometry: NonNullable<PathProperties['geometry']>;
suggestedOperationalPoints: SuggestedOP[];
/** Operational points along the path (including origin and destination) and vias added by clicking on map */
allWaypoints: SuggestedOP[];
length: number;
trackSectionRanges: NonNullable<PathfindingResultSuccess['track_section_ranges']>;
incompatibleConstraints?: IncompatibleConstraints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ManageTrainScheduleProps = {

const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const { pathProperties, voltageRanges } = useManageTrainScheduleContext();
const { pathProperties, voltageRanges, allWaypoints } = useManageTrainScheduleContext();

const { getOrigin, getDestination, getPathSteps, getConstraintDistribution, getStartTime } =
useOsrdConfSelectors();
Expand Down Expand Up @@ -117,7 +117,7 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
// If pathProperties is defined we know that pathSteps won't have any null values
content: (
<TimesStopsInput
allWaypoints={pathProperties?.allWaypoints}
allWaypoints={allWaypoints}
startTime={new Date(startTime)}
pathSteps={compact(pathSteps)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const Itinerary = ({ shouldManageStopDuration }: ItineraryProps) => {
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const { openModal } = useModal();

const { pathProperties, setPathProperties, launchPathfinding } = useManageTrainScheduleContext();
const { pathProperties, setPathProperties, launchPathfinding, allWaypoints } =
useManageTrainScheduleContext();

const zoomToFeaturePoint = (lngLat?: Position) => {
if (lngLat) {
Expand Down Expand Up @@ -122,15 +123,15 @@ const Itinerary = ({ shouldManageStopDuration }: ItineraryProps) => {
>
<Route />
</button>
{pathProperties && pathProperties.suggestedOperationalPoints && (
{allWaypoints && (
<button
data-testid="add-waypoints-button"
className="col ml-1 my-1 text-white btn bg-info btn-sm"
type="button"
onClick={() =>
openModal(
<ModalSuggestedVias
suggestedVias={pathProperties.allWaypoints}
suggestedVias={allWaypoints}
launchPathfinding={launchPathfinding}
/>
)
Expand Down
9 changes: 1 addition & 8 deletions front/src/modules/pathfinding/hooks/usePathfinding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useState } from 'react';

import { compact, isObject } from 'lodash';
import { isObject } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

Expand All @@ -18,7 +18,6 @@ import {
formatSuggestedOperationalPoints,
getPathfindingQuery,
matchPathStepAndOp,
upsertPathStepsInOPs,
} from 'modules/pathfinding/utils';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
Expand Down Expand Up @@ -161,16 +160,10 @@ const usePathfinding = (
}
dispatch(updatePathSteps(updatedPathSteps));

const allWaypoints = upsertPathStepsInOPs(
suggestedOperationalPoints,
compact(updatedPathSteps)
);

setPathProperties({
electrifications,
geometry,
suggestedOperationalPoints,
allWaypoints,
length: pathResult.length,
trackSectionRanges: pathResult.track_section_ranges,
incompatibleConstraints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default function commonConfBuilder() {
],
},
suggestedOperationalPoints: [],
allWaypoints: [],
length: 1169926000,
trackSectionRanges: [],
}),
Expand Down

0 comments on commit 3505153

Please sign in to comment.