From 4f730ef4e81466868daf77e4e2eb2aba774b857b Mon Sep 17 00:00:00 2001 From: Alexis Jacomy Date: Thu, 4 Jul 2024 16:50:30 +0200 Subject: [PATCH] editor: improves switch types cache handling This commit fixes #7946. Details: - Adds a `?` to handle case where switch types have not been loaded yet in SwitchList.tsx - Adds a very simple cache to useSwitchTypes to avoid reloading switch types any time a new component mounts with that hook --- .../rangeEdition/speedSection/SwitchList.tsx | 2 +- .../tools/switchEdition/useSwitchTypes.ts | 51 ++++++++++++------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/front/src/applications/editor/tools/rangeEdition/speedSection/SwitchList.tsx b/front/src/applications/editor/tools/rangeEdition/speedSection/SwitchList.tsx index 51c9b809852..8cca0566b6d 100644 --- a/front/src/applications/editor/tools/rangeEdition/speedSection/SwitchList.tsx +++ b/front/src/applications/editor/tools/rangeEdition/speedSection/SwitchList.tsx @@ -64,7 +64,7 @@ const SwitchList: React.FC = ({ {`${t('Editor.obj-types.Switch')} ${swId}`}
- {switchPositionsByType[type].map((optPosition, posIndex) => { + {switchPositionsByType[type]?.map((optPosition, posIndex) => { const isPositionNull = optPosition === 'Any'; const isButtonIncompatible = Object.keys(selectedSwitches).length > 1 && diff --git a/front/src/applications/editor/tools/switchEdition/useSwitchTypes.ts b/front/src/applications/editor/tools/switchEdition/useSwitchTypes.ts index 30eb1471eae..cf10d33e249 100644 --- a/front/src/applications/editor/tools/switchEdition/useSwitchTypes.ts +++ b/front/src/applications/editor/tools/switchEdition/useSwitchTypes.ts @@ -6,7 +6,7 @@ import { osrdEditoastApi } from 'common/api/osrdEditoastApi'; import type { SwitchType } from './types'; -// Client prefered order +// Client preferred order const trackNodeTypeOrder = [ 'link', 'point_switch', @@ -15,30 +15,47 @@ const trackNodeTypeOrder = [ 'double_slip_switch', ]; +let switchTypesCache: Record = {}; + export default function useSwitchTypes(infraID: number | undefined) { - const [data, setData] = useState([]); + const [data, setData] = useState( + !isNil(infraID) ? switchTypesCache[infraID] || [] : [] + ); const [getInfraSwitchTypes, { isLoading, error }] = osrdEditoastApi.endpoints.getInfraByInfraIdSwitchTypes.useLazyQuery({}); + const invalidateCache = useCallback(() => { + switchTypesCache = {}; + }, [switchTypesCache]); const fetch = useCallback( async (infraId?: number) => { - // reset + // Reset state: setData([]); + + if (isNil(infraId)) { + return; + } + + // Check cache first: + if (switchTypesCache[infraId]) { + setData(switchTypesCache[infraId]); + return; + } + try { - if (!isNil(infraId)) { - const resp = getInfraSwitchTypes({ infraId }); - const result = await resp.unwrap(); - if (result) { - const orderedData = [...result] as SwitchType[]; - orderedData.sort( - (a, b) => trackNodeTypeOrder.indexOf(a.id) - trackNodeTypeOrder.indexOf(b.id) - ); - setData(orderedData); - } else { - setData([]); - } - resp.unsubscribe(); + const resp = getInfraSwitchTypes({ infraId }, true); + const result = await resp.unwrap(); + if (result) { + const orderedData = [...result] as SwitchType[]; + orderedData.sort( + (a, b) => trackNodeTypeOrder.indexOf(a.id) - trackNodeTypeOrder.indexOf(b.id) + ); + setData(orderedData); + switchTypesCache[infraId] = orderedData; + } else { + setData([]); } + resp.unsubscribe(); } catch (e) { console.error(e); } @@ -50,5 +67,5 @@ export default function useSwitchTypes(infraID: number | undefined) { fetch(infraID); }, [infraID, fetch]); - return { data, isLoading, error }; + return { data, isLoading, error, invalidateCache }; }