diff --git a/frontend/src/container/TopNav/AutoRefresh/config.ts b/frontend/src/container/TopNav/AutoRefresh/config.ts index 240b7d1f25..cefd0c8bf1 100644 --- a/frontend/src/container/TopNav/AutoRefresh/config.ts +++ b/frontend/src/container/TopNav/AutoRefresh/config.ts @@ -1,6 +1,6 @@ export const options: IOptions[] = [ { - label: '', + label: 'off', key: 'off', value: 0, }, diff --git a/frontend/src/container/TopNav/AutoRefresh/index.tsx b/frontend/src/container/TopNav/AutoRefresh/index.tsx index f81875bcc3..bc52e2cf86 100644 --- a/frontend/src/container/TopNav/AutoRefresh/index.tsx +++ b/frontend/src/container/TopNav/AutoRefresh/index.tsx @@ -1,9 +1,20 @@ -import { Select } from 'antd'; +import { CaretDownFilled } from '@ant-design/icons'; +import { + Checkbox, + Divider, + Popover, + Radio, + RadioChangeEvent, + Space, + Typography, +} from 'antd'; +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'; import { useDispatch, useSelector } from 'react-redux'; import { useLocation } from 'react-router-dom'; @@ -15,7 +26,7 @@ import { UPDATE_TIME_INTERVAL } from 'types/actions/globalTime'; import { GlobalReducer } from 'types/reducer/globalTime'; import { options } from './config'; -import { SelectContainer } from './styles'; +import { ButtonContainer, Container } from './styles'; function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { const { minTime: initialMinTime, selectedTime } = useSelector< @@ -24,8 +35,6 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { >((state) => state.globalTime); const { pathname } = useLocation(); - const params = useUrlQuery(); - const localStorageData = JSON.parse(get(DASHBOARD_TIME_IN_DURATION) || '{}'); const localStorageValue = useMemo(() => localStorageData[pathname], [ @@ -33,6 +42,16 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { localStorageData, ]); + const [isAutoRefreshEnabled, setIsAutoRefreshfreshEnabled] = useState( + Boolean(localStorageValue), + ); + + useEffect(() => { + setIsAutoRefreshfreshEnabled(Boolean(localStorageValue)); + }, [localStorageValue]); + + const params = useUrlQuery(); + const dispatch = useDispatch>(); const [selectedOption, setSelectedOption] = useState( localStorageValue || options[0].key, @@ -50,7 +69,7 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { useInterval(() => { const selectedValue = getOption?.value; - if (disabled) { + if (disabled || !isAutoRefreshEnabled) { return; } @@ -69,31 +88,70 @@ function AutoRefresh({ disabled = false }: AutoRefreshProps): JSX.Element { }, getOption?.value || 0); const onChangeHandler = useCallback( - (value: unknown) => { - if (typeof value === 'string') { - setSelectedOption(value); - params.set(DASHBOARD_TIME_IN_DURATION, value); - set( - DASHBOARD_TIME_IN_DURATION, - JSON.stringify({ ...localStorageData, [pathname]: value }), - ); - } + (event: RadioChangeEvent) => { + const selectedValue = event.target.value; + setSelectedOption(selectedValue); + params.set(DASHBOARD_TIME_IN_DURATION, selectedValue); + set( + DASHBOARD_TIME_IN_DURATION, + JSON.stringify({ ...localStorageData, [pathname]: selectedValue }), + ); + setIsAutoRefreshfreshEnabled(true); }, [params, pathname, localStorageData], ); + const onChangeAutoRefreshHandler = useCallback( + (event: CheckboxChangeEvent) => { + const { checked } = event.target; + if (!checked) { + // remove the path from localstorage + set( + DASHBOARD_TIME_IN_DURATION, + JSON.stringify(_omit(localStorageData, pathname)), + ); + } + setIsAutoRefreshfreshEnabled(checked); + }, + [localStorageData, pathname], + ); + return ( - + + Auto Refresh + + + + + Refresh Interval + + + + {options + .filter((e) => e.label !== 'off') + .map((option) => ( + + {option.label} + + ))} + + + + } > - {options.map((option) => ( - - {option.label} - - ))} - + + + + ); } diff --git a/frontend/src/container/TopNav/AutoRefresh/styles.ts b/frontend/src/container/TopNav/AutoRefresh/styles.ts index 6e8380703f..9672a346e7 100644 --- a/frontend/src/container/TopNav/AutoRefresh/styles.ts +++ b/frontend/src/container/TopNav/AutoRefresh/styles.ts @@ -1,9 +1,13 @@ -import { Select } from 'antd'; +import { Button } from 'antd'; import styled from 'styled-components'; -export const SelectContainer = styled(Select)` +export const Container = styled.div` + min-width: 8rem; +`; + +export const ButtonContainer = styled(Button)` &&& { - width: 100%; - min-width: 4rem; + padding-left: 0.5rem; + padding-right: 0.5rem; } `; diff --git a/frontend/src/container/TopNav/DateTimeSelection/index.tsx b/frontend/src/container/TopNav/DateTimeSelection/index.tsx index 9d62c36a8d..18e8dce4c8 100644 --- a/frontend/src/container/TopNav/DateTimeSelection/index.tsx +++ b/frontend/src/container/TopNav/DateTimeSelection/index.tsx @@ -19,7 +19,7 @@ import AutoRefresh from '../AutoRefresh'; import CustomDateTimeModal, { DateTimeRangeType } from '../CustomDateTimeModal'; import { getDefaultOption, getOptions, Time } from './config'; import RefreshText from './Refresh'; -import { Form, FormItem } from './styles'; +import { Form, FormContainer, FormItem } from './styles'; const { Option } = DefaultSelect; @@ -263,29 +263,31 @@ function DateTimeSelection({ layout="inline" initialValues={{ interval: selectedTime }} > - onSelectHandler(value as Time)} - value={getInputLabel(startTime, endTime, selectedTime)} - data-testid="dropDown" - > - {options.map(({ value, label }) => ( - - ))} - + + onSelectHandler(value as Time)} + value={getInputLabel(startTime, endTime, selectedTime)} + data-testid="dropDown" + > + {options.map(({ value, label }) => ( + + ))} + -