Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: add memoization to use scenario data and sub hooks #9833

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,43 @@ const useScenarioData = (
});
}, []);

return {
selectedTrainId,
trainScheduleSummaries,
trainSchedules,
projectionData:
trainScheduleUsedForProjection && projectionPath
? {
trainSchedule: trainScheduleUsedForProjection,
...projectionPath,
projectedTrains,
projectionLoaderData: {
allTrainsProjected,
totalTrains: timetable.train_ids.length,
},
}
: undefined,
simulationResults,
conflicts,
removeTrains,
upsertTrainSchedules,
};
return useMemo(
() => ({
selectedTrainId,
trainScheduleSummaries,
trainSchedules,
projectionData:
trainScheduleUsedForProjection && projectionPath
? {
trainSchedule: trainScheduleUsedForProjection,
...projectionPath,
projectedTrains,
projectionLoaderData: {
allTrainsProjected,
totalTrains: timetable.train_ids.length,
},
}
: undefined,
simulationResults,
conflicts,
removeTrains,
upsertTrainSchedules,
}),
[
selectedTrainId,
trainScheduleSummaries,
trainSchedules,
trainScheduleUsedForProjection,
projectionPath,
projectedTrains,
allTrainsProjected,
timetable.train_ids.length,
simulationResults,
conflicts,
removeTrains,
upsertTrainSchedules,
]
);
};

export default useScenarioData;
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useMemo } from 'react';

import { useSelector } from 'react-redux';

import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
Expand Down Expand Up @@ -50,15 +52,18 @@ const useSimulationResults = (): SimulationResultsData => {
selectedTrainPowerRestrictions: [],
};

return {
selectedTrainSchedule,
selectedTrainRollingStock: speedSpaceChart?.rollingStock,
selectedTrainPowerRestrictions: speedSpaceChart?.formattedPowerRestrictions || [],
trainSimulation: speedSpaceChart?.simulation,
pathProperties: speedSpaceChart?.formattedPathProperties,
pathLength: path?.length,
path,
};
return useMemo(
() => ({
selectedTrainSchedule,
selectedTrainRollingStock: speedSpaceChart?.rollingStock,
selectedTrainPowerRestrictions: speedSpaceChart?.formattedPowerRestrictions || [],
trainSimulation: speedSpaceChart?.simulation,
pathProperties: speedSpaceChart?.formattedPathProperties,
pathLength: path?.length,
path,
}),
[selectedTrainSchedule, speedSpaceChart, path]
);
};

export default useSimulationResults;
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo } from 'react';

import type {
LayerData,
PowerRestrictionValues,
} from '@osrd-project/ui-speedspacechart/dist/types/chartTypes';
import { useTranslation } from 'react-i18next';

import type { PathPropertiesFormatted } from 'applications/operationalStudies/types';
import { preparePathPropertiesData } from 'applications/operationalStudies/utils';
import {
osrdEditoastApi,
Expand All @@ -31,10 +26,6 @@ const useSpeedSpaceChart = (
const { t } = useTranslation('simulation');
const infraId = useInfraID();

const [formattedPathProperties, setFormattedPathProperties] = useState<PathPropertiesFormatted>();
const [formattedPowerRestrictions, setFormattedPowerRestrictions] =
useState<LayerData<PowerRestrictionValues>[]>();

const rollingStockName = trainScheduleResult?.rolling_stock_name;
const { data: rollingStock } =
osrdEditoastApi.endpoints.getRollingStockNameByRollingStockName.useQuery(
Expand All @@ -53,8 +44,8 @@ const useSpeedSpaceChart = (
]);

// retrieve and format pathfinding properties
useEffect(() => {
const getPathProperties = async () => {
const formattedPathProperties = useMemo(() => {
try {
if (
infraId &&
trainScheduleResult &&
Expand All @@ -63,28 +54,36 @@ const useSpeedSpaceChart = (
simulation?.status === 'success' &&
pathProperties
) {
const formattedPathProps = preparePathPropertiesData(
return preparePathPropertiesData(
simulation.electrical_profiles,
pathProperties,
pathfindingResult,
trainScheduleResult.path,
t
);

setFormattedPathProperties(formattedPathProps);

// Format power restrictions
const powerRestrictions = formatPowerRestrictionRangesWithHandled({
selectedTrainSchedule: trainScheduleResult,
selectedTrainRollingStock: rollingStock,
pathfindingResult,
pathProperties: formattedPathProps,
});
setFormattedPowerRestrictions(powerRestrictions);
}
};
return undefined;
} catch (err) {
return undefined;
}
}, [pathProperties, infraId, rollingStock]);

getPathProperties();
const formattedPowerRestrictions = useMemo(() => {
if (
infraId &&
trainScheduleResult &&
rollingStock &&
pathfindingResult &&
formattedPathProperties
) {
return formatPowerRestrictionRangesWithHandled({
selectedTrainSchedule: trainScheduleResult,
selectedTrainRollingStock: rollingStock,
pathfindingResult,
pathProperties: formattedPathProperties,
});
}
return undefined;
}, [pathProperties, infraId, rollingStock]);

// setup chart synchronizer
Expand All @@ -94,19 +93,23 @@ const useSpeedSpaceChart = (
}
}, [simulation, trainScheduleResult, rollingStock, departureTime]);

return trainScheduleResult &&
rollingStock &&
simulation?.status === 'success' &&
formattedPathProperties &&
departureTime
? {
rollingStock,
formattedPowerRestrictions,
simulation,
formattedPathProperties,
departureTime,
}
: null;
return useMemo(
() =>
trainScheduleResult &&
rollingStock &&
simulation?.status === 'success' &&
formattedPathProperties &&
departureTime
? {
rollingStock,
formattedPowerRestrictions,
simulation,
formattedPathProperties,
departureTime,
}
: null,
[rollingStock, formattedPowerRestrictions, simulation, formattedPathProperties, departureTime]
);
};

export default useSpeedSpaceChart;
Loading