-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: add scenario content context with cached tracks and infraid
Signed-off-by: Alice Khoudli <alice.khoudli@polytechnique.org>
- Loading branch information
Showing
14 changed files
with
292 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
front/src/applications/operationalStudies/hooks/useCachedTrackSections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useRef, useCallback } from 'react'; | ||
|
||
import { osrdEditoastApi } from 'common/api/osrdEditoastApi'; | ||
import type { InfraObjectWithGeometry } from 'common/api/osrdEditoastApi'; | ||
|
||
export default function useCachedTrackSections(infraId: number) { | ||
const trackIdsRef = useRef<Set<string>>(new Set()); | ||
const trackSectionsRef = useRef<InfraObjectWithGeometry[]>([]); | ||
const [loadInfraObject, { isLoading }] = | ||
osrdEditoastApi.endpoints.postInfraByInfraIdObjectsAndObjectType.useMutation(); | ||
|
||
const getTrackSectionsByIds = useCallback( | ||
async (requestedTrackIds: string[]) => { | ||
let fetchedSections: InfraObjectWithGeometry[] = []; | ||
const uniqueNewIds = requestedTrackIds.filter((id) => !trackIdsRef.current.has(id)); | ||
if (uniqueNewIds.length !== 0) { | ||
try { | ||
fetchedSections = await loadInfraObject({ | ||
infraId, | ||
objectType: 'TrackSection', | ||
body: uniqueNewIds, | ||
}).unwrap(); | ||
|
||
uniqueNewIds.forEach((id) => trackIdsRef.current.add(id)); | ||
trackSectionsRef.current = [...trackSectionsRef.current, ...fetchedSections]; | ||
} catch (error) { | ||
console.error('Failed to fetch track sections:', error); | ||
} | ||
} | ||
|
||
return trackSectionsRef.current.filter((section) => | ||
requestedTrackIds.includes(section.obj_id) | ||
); | ||
}, | ||
[infraId] | ||
); | ||
|
||
return { getTrackSectionsByIds, isLoading }; | ||
} |
35 changes: 35 additions & 0 deletions
35
front/src/applications/operationalStudies/hooks/useScenarioContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createContext, useContext, useMemo, type ReactNode } from 'react'; | ||
|
||
import useCachedTrackSections from 'applications/operationalStudies/hooks/useCachedTrackSections'; | ||
import type { InfraObjectWithGeometry } from 'common/api/osrdEditoastApi'; | ||
|
||
type ScenarioContextType = { | ||
getTrackSectionsByIds: (requestedTrackIds: string[]) => Promise<InfraObjectWithGeometry[]>; | ||
infraId: number; | ||
trackSectionsLoading: boolean; | ||
} | null; | ||
const ScenarioContext = createContext<ScenarioContextType>(null); | ||
|
||
type ScenarioContextProviderProps = { infraId: number; children: ReactNode }; | ||
|
||
export const ScenarioContextProvider = ({ infraId, children }: ScenarioContextProviderProps) => { | ||
const { getTrackSectionsByIds, isLoading: trackSectionsLoading } = | ||
useCachedTrackSections(infraId); | ||
const providedContext = useMemo( | ||
() => ({ | ||
getTrackSectionsByIds, | ||
infraId, | ||
trackSectionsLoading, | ||
}), | ||
[getTrackSectionsByIds, infraId, trackSectionsLoading] | ||
); | ||
return <ScenarioContext.Provider value={providedContext}>{children}</ScenarioContext.Provider>; | ||
}; | ||
|
||
export const useScenarioContext = () => { | ||
const context = useContext(ScenarioContext); | ||
if (!context) { | ||
throw new Error('useScenarioContext must be used within a TrackSectionsProvider'); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.