Skip to content

Commit

Permalink
editor: improves switch types cache handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jacomyal authored and clarani committed Jul 8, 2024
1 parent 6fe9c93 commit 4dfa713
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const SwitchList: React.FC<SwitchListProps> = ({
<span className="align-self-end">{`${t('Editor.obj-types.Switch')} ${swId}`}</span>
</div>
<div className="d-flex ml-4">
{switchPositionsByType[type].map((optPosition, posIndex) => {
{switchPositionsByType[type]?.map((optPosition, posIndex) => {
const isPositionNull = optPosition === 'Any';
const isButtonIncompatible =
Object.keys(selectedSwitches).length > 1 &&
Expand Down
51 changes: 34 additions & 17 deletions front/src/applications/editor/tools/switchEdition/useSwitchTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -15,30 +15,47 @@ const trackNodeTypeOrder = [
'double_slip_switch',
];

let switchTypesCache: Record<number, SwitchType[]> = {};

export default function useSwitchTypes(infraID: number | undefined) {
const [data, setData] = useState<SwitchType[]>([]);
const [data, setData] = useState<SwitchType[]>(
!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);
}
Expand All @@ -50,5 +67,5 @@ export default function useSwitchTypes(infraID: number | undefined) {
fetch(infraID);
}, [infraID, fetch]);

return { data, isLoading, error };
return { data, isLoading, error, invalidateCache };
}

0 comments on commit 4dfa713

Please sign in to comment.