({
localStorageKey,
defaultValue = DEFAULT_TIME_PERIOD,
timePeriodOptions = DEFAULT_OPTIONS,
}: {
localStorageKey: string;
defaultValue?: string;
timePeriodOptions?: TimePeriodOptions;
})
| 31 | export const DEFAULT_TIME_PERIOD = Object.keys(DEFAULT_OPTIONS)[0]; |
| 32 | |
| 33 | export const useTimePeriodMinutes = ({ |
| 34 | localStorageKey, |
| 35 | defaultValue = DEFAULT_TIME_PERIOD, |
| 36 | timePeriodOptions = DEFAULT_OPTIONS, |
| 37 | }: { |
| 38 | localStorageKey: string; |
| 39 | defaultValue?: string; |
| 40 | timePeriodOptions?: TimePeriodOptions; |
| 41 | }) => { |
| 42 | const [timePeriodMinutes, setTimePeriodMinutes] = React.useState(() => { |
| 43 | let value = defaultValue; |
| 44 | if (storageAvailable("localStorage")) { |
| 45 | value = window.localStorage.getItem(localStorageKey) ?? defaultValue; |
| 46 | } |
| 47 | return timePeriodFromUrl(value, timePeriodOptions); |
| 48 | }); |
| 49 | |
| 50 | React.useEffect(() => { |
| 51 | if (storageAvailable("localStorage")) { |
| 52 | window.localStorage.setItem( |
| 53 | localStorageKey, |
| 54 | timePeriodMinutes.toString(), |
| 55 | ); |
| 56 | } |
| 57 | }, [timePeriodMinutes, localStorageKey]); |
| 58 | |
| 59 | return [timePeriodMinutes, setTimePeriodMinutes] as const; |
| 60 | }; |
| 61 | |
| 62 | const parseTimePeriod = ( |
| 63 | periodString: string | null, |
no test coverage detected