diff --git a/frontend/src/container/TopNav/AutoRefresh/config.ts b/frontend/src/container/TopNav/AutoRefresh/config.ts index cefd0c8bf1..dfd3134e0e 100644 --- a/frontend/src/container/TopNav/AutoRefresh/config.ts +++ b/frontend/src/container/TopNav/AutoRefresh/config.ts @@ -1,3 +1,7 @@ +import GetMinMax, { GetMinMaxPayload } from 'lib/getMinMax'; + +import { Time } from '../DateTimeSelection/config'; + export const options: IOptions[] = [ { label: 'off', @@ -61,3 +65,12 @@ export interface IOptions { key: string; value: number; } + +export const getMinMax = ( + selectedTime: Time, + minTime: number, + maxTime: number, +): GetMinMaxPayload => + selectedTime !== 'custom' + ? GetMinMax(selectedTime) + : GetMinMax(selectedTime, [minTime, maxTime]); diff --git a/frontend/src/container/TopNav/AutoRefresh/index.tsx b/frontend/src/container/TopNav/AutoRefresh/index.tsx index aa6a74793c..ba0d4bb3e6 100644 --- a/frontend/src/container/TopNav/AutoRefresh/index.tsx +++ b/frontend/src/container/TopNav/AutoRefresh/index.tsx @@ -12,7 +12,6 @@ import { CheckboxChangeEvent } from 'antd/lib/checkbox'; import get from 'api/browser/localstorage/get'; import set from 'api/browser/localstorage/set'; import { DASHBOARD_TIME_IN_DURATION } from 'constants/app'; -import dayjs from 'dayjs'; import useUrlQuery from 'hooks/useUrlQuery'; import _omit from 'lodash-es/omit'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; @@ -28,21 +27,19 @@ import { } from 'types/actions/globalTime'; import { GlobalReducer } from 'types/reducer/globalTime'; -import { options } from './config'; +import { getMinMax, options } from './config'; import { ButtonContainer, Container } from './styles'; function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { - const { - minTime: initialMinTime, - selectedTime, - isAutoRefreshDisabled, - } = useSelector((state) => state.globalTime); + const globalTime = useSelector( + (state) => state.globalTime, + ); const { pathname } = useLocation(); - const isDisabled = useMemo(() => disabled || isAutoRefreshDisabled, [ - isAutoRefreshDisabled, - disabled, - ]); + const isDisabled = useMemo( + () => disabled || globalTime.isAutoRefreshDisabled, + [globalTime.isAutoRefreshDisabled, disabled], + ); const localStorageData = JSON.parse(get(DASHBOARD_TIME_IN_DURATION) || '{}'); @@ -89,14 +86,18 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { } if (selectedOption !== 'off' && selectedValue) { - const min = initialMinTime / 1000000; + const { maxTime, minTime } = getMinMax( + globalTime.selectedTime, + globalTime.minTime, + globalTime.maxTime, + ); dispatch({ type: UPDATE_TIME_INTERVAL, payload: { - maxTime: dayjs().valueOf() * 1000000, - minTime: dayjs(min).subtract(selectedValue, 'second').valueOf() * 1000000, - selectedTime, + maxTime, + minTime, + selectedTime: globalTime.selectedTime, }, }); } diff --git a/frontend/src/lib/getMinMax.ts b/frontend/src/lib/getMinMax.ts index a597314799..320d509131 100644 --- a/frontend/src/lib/getMinMax.ts +++ b/frontend/src/lib/getMinMax.ts @@ -53,7 +53,7 @@ const GetMinMax = ( }; }; -interface GetMinMaxPayload { +export interface GetMinMaxPayload { minTime: GlobalReducer['minTime']; maxTime: GlobalReducer['maxTime']; }