|
| 1 | +import * as React from 'react'; |
| 2 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 3 | +// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version |
| 4 | +import { useDispatch, useSelector } from 'react-redux'; |
| 5 | +import { useLocation } from 'react-router-dom-v5-compat'; |
| 6 | +import { |
| 7 | + setInterval as setIntervalAction, |
| 8 | + setTimespan as setTimespanAction, |
| 9 | +} from '../../redux/actions/pipelines-overview-filters'; |
| 10 | +import { |
| 11 | + getPipelinesOverviewInterval, |
| 12 | + getPipelinesOverviewTimespan, |
| 13 | +} from '../../redux/selectors/pipelines-overview-filters'; |
| 14 | +import { useQueryParams } from '../pipelines-overview/utils'; |
| 15 | + |
| 16 | +export const usePersistedTimespanWithUrl = ( |
| 17 | + defaultValue: number, |
| 18 | + options: { |
| 19 | + displayFormat: (v: number) => string; |
| 20 | + loadFormat: (v: string) => number; |
| 21 | + options: Record<string, any>; |
| 22 | + }, |
| 23 | + namespace: string, |
| 24 | +) => { |
| 25 | + const dispatch = useDispatch(); |
| 26 | + const reduxTimespan = useSelector(getPipelinesOverviewTimespan); |
| 27 | + const location = useLocation(); |
| 28 | + const prevNamespaceRef = React.useRef(namespace); |
| 29 | + const [timespan, setTimespanValue] = React.useState(() => { |
| 30 | + const urlParams = new URLSearchParams(location.search); |
| 31 | + const urlValue = urlParams.has('timerange') |
| 32 | + ? urlParams.get('timerange') |
| 33 | + : null; |
| 34 | + if (urlValue) { |
| 35 | + return options.loadFormat(urlValue); |
| 36 | + } |
| 37 | + return reduxTimespan ?? defaultValue; |
| 38 | + }); |
| 39 | + |
| 40 | + // Reset to default when namespace changes |
| 41 | + React.useEffect(() => { |
| 42 | + if (prevNamespaceRef.current !== namespace) { |
| 43 | + prevNamespaceRef.current = namespace; |
| 44 | + setTimespanValue(defaultValue); |
| 45 | + } |
| 46 | + }, [namespace, defaultValue]); |
| 47 | + |
| 48 | + // Persist to Redux whenever value changes |
| 49 | + React.useEffect(() => { |
| 50 | + dispatch(setTimespanAction(timespan)); |
| 51 | + }, [timespan, dispatch]); |
| 52 | + |
| 53 | + useQueryParams({ |
| 54 | + key: 'timerange', |
| 55 | + value: timespan, |
| 56 | + setValue: setTimespanValue, |
| 57 | + defaultValue: timespan, |
| 58 | + ...options, |
| 59 | + }); |
| 60 | + |
| 61 | + return [timespan, setTimespanValue]; |
| 62 | +}; |
| 63 | + |
| 64 | +export const usePersistedIntervalWithUrl = ( |
| 65 | + defaultValue: number, |
| 66 | + options: { |
| 67 | + displayFormat: (v: number | null) => string; |
| 68 | + loadFormat: (v: string) => number | null; |
| 69 | + options: Record<string, any>; |
| 70 | + }, |
| 71 | + namespace: string, |
| 72 | +) => { |
| 73 | + const dispatch = useDispatch(); |
| 74 | + const reduxInterval = useSelector(getPipelinesOverviewInterval); |
| 75 | + const location = useLocation(); |
| 76 | + const prevNamespaceRef = React.useRef(namespace); |
| 77 | + const [interval, setIntervalValue] = React.useState(() => { |
| 78 | + const urlParams = new URLSearchParams(location.search); |
| 79 | + const urlValue = urlParams.has('refreshinterval') |
| 80 | + ? urlParams.get('refreshinterval') |
| 81 | + : null; |
| 82 | + if (urlValue) { |
| 83 | + return options.loadFormat(urlValue); |
| 84 | + } |
| 85 | + // to handle refresh interval "off" state |
| 86 | + return reduxInterval !== undefined ? reduxInterval : defaultValue; |
| 87 | + }); |
| 88 | + |
| 89 | + // Reset to default when namespace changes |
| 90 | + React.useEffect(() => { |
| 91 | + if (prevNamespaceRef.current !== namespace) { |
| 92 | + prevNamespaceRef.current = namespace; |
| 93 | + setIntervalValue(defaultValue); |
| 94 | + } |
| 95 | + }, [namespace, defaultValue]); |
| 96 | + |
| 97 | + // Persist to Redux whenever value changes |
| 98 | + React.useEffect(() => { |
| 99 | + dispatch(setIntervalAction(interval)); |
| 100 | + }, [interval, dispatch]); |
| 101 | + |
| 102 | + useQueryParams({ |
| 103 | + key: 'refreshinterval', |
| 104 | + value: interval, |
| 105 | + setValue: setIntervalValue, |
| 106 | + defaultValue: interval, |
| 107 | + ...options, |
| 108 | + }); |
| 109 | + |
| 110 | + return [interval, setIntervalValue]; |
| 111 | +}; |
0 commit comments