|
| 1 | +import { useEffect, useCallback, useMemo } from 'react'; |
| 2 | + |
| 3 | +import { useSelector } from 'react-redux'; |
| 4 | +import { useLocation, useNavigate, useParams } from 'react-router-dom'; |
| 5 | + |
| 6 | +import { updateSelectedTrainId, updateTrainIdUsedForProjection } from 'reducers/simulationResults'; |
| 7 | +import { |
| 8 | + getSelectedTrainId, |
| 9 | + getTrainIdUsedForProjection, |
| 10 | +} from 'reducers/simulationResults/selectors'; |
| 11 | +import { useAppDispatch } from 'store'; |
| 12 | + |
| 13 | +type SimulationParams = { |
| 14 | + projectId: string; |
| 15 | + studyId: string; |
| 16 | + scenarioId: string; |
| 17 | +}; |
| 18 | + |
| 19 | +const useScenarioQueryParams = () => { |
| 20 | + const location = useLocation(); |
| 21 | + const navigate = useNavigate(); |
| 22 | + const dispatch = useAppDispatch(); |
| 23 | + |
| 24 | + const { |
| 25 | + projectId: urlProjectId, |
| 26 | + studyId: urlStudyId, |
| 27 | + scenarioId: urlScenarioId, |
| 28 | + } = useParams() as SimulationParams; |
| 29 | + const localKey = `useScenarioQueryParams_project${urlProjectId}_study${urlStudyId}_scenario${urlScenarioId}`; |
| 30 | + |
| 31 | + const searchParams = useMemo(() => new URLSearchParams(location.search), [location.search]); |
| 32 | + |
| 33 | + const reduxSelectedTrainId = useSelector(getSelectedTrainId); |
| 34 | + const reduxProjectionTrainId = useSelector(getTrainIdUsedForProjection); |
| 35 | + |
| 36 | + // Helper function to get a parameter from the URL, or if absent from local storage |
| 37 | + const getParamFromUrlOrStorage = useCallback( |
| 38 | + (paramName: string) => |
| 39 | + searchParams.get(paramName) || localStorage.getItem(`${localKey}_${paramName}`) || undefined, |
| 40 | + [localKey, searchParams] |
| 41 | + ); |
| 42 | + |
| 43 | + // Helper function to get a parameter from the URL or local storage |
| 44 | + const setParamsInUrlAndStorage = useCallback( |
| 45 | + (paramName: string, paramValue: string | undefined) => { |
| 46 | + if (paramValue === undefined) { |
| 47 | + searchParams.delete(paramName); |
| 48 | + localStorage.removeItem(`${localKey}_${paramName}`); |
| 49 | + } else { |
| 50 | + searchParams.set(paramName, paramValue); |
| 51 | + localStorage.setItem(`${localKey}_${paramName}`, paramValue); |
| 52 | + } |
| 53 | + navigate(`${location.pathname}?${searchParams.toString()}`, { replace: true }); |
| 54 | + }, |
| 55 | + [localKey, searchParams, location.pathname, navigate] |
| 56 | + ); |
| 57 | + |
| 58 | + // Update the redux store on page load |
| 59 | + useEffect(() => { |
| 60 | + const selectedTrainFromUrl = getParamFromUrlOrStorage('selected_train'); |
| 61 | + const projectionFromUrl = getParamFromUrlOrStorage('projection'); |
| 62 | + |
| 63 | + if (selectedTrainFromUrl) { |
| 64 | + dispatch(updateSelectedTrainId(Number(selectedTrainFromUrl))); |
| 65 | + } |
| 66 | + if (projectionFromUrl) { |
| 67 | + dispatch(updateTrainIdUsedForProjection(Number(projectionFromUrl))); |
| 68 | + } |
| 69 | + }, [dispatch, getParamFromUrlOrStorage]); |
| 70 | + |
| 71 | + // Update the URL and local storage on redux store change |
| 72 | + useEffect(() => { |
| 73 | + if (reduxSelectedTrainId?.toString() !== getParamFromUrlOrStorage('selected_train')) { |
| 74 | + setParamsInUrlAndStorage('selected_train', reduxSelectedTrainId?.toString()); |
| 75 | + } |
| 76 | + if (reduxProjectionTrainId?.toString() !== getParamFromUrlOrStorage('projection')) { |
| 77 | + setParamsInUrlAndStorage('projection', reduxProjectionTrainId?.toString()); |
| 78 | + } |
| 79 | + }, [ |
| 80 | + reduxSelectedTrainId, |
| 81 | + reduxProjectionTrainId, |
| 82 | + setParamsInUrlAndStorage, |
| 83 | + getParamFromUrlOrStorage, |
| 84 | + ]); |
| 85 | +}; |
| 86 | + |
| 87 | +export default useScenarioQueryParams; |
0 commit comments