import { Button, Select as DefaultSelect } from 'antd'; import React, { useCallback, useEffect, useState } from 'react'; import { getDefaultOption, getOptions, Time } from './config'; import { Container, Form, FormItem } from './styles'; const { Option } = DefaultSelect; import getLocalStorageKey from 'api/browser/localstorage/get'; import setLocalStorageKey from 'api/browser/localstorage/set'; import { LOCAL_STORAGE } from 'constants/localStorage'; import getTimeString from 'lib/getTimeString'; import dayjs, { Dayjs } from 'dayjs'; import { connect, useSelector } from 'react-redux'; import { RouteComponentProps, withRouter } from 'react-router'; import { bindActionCreators, Dispatch } from 'redux'; import { ThunkDispatch } from 'redux-thunk'; import { GlobalTimeLoading, UpdateTimeInterval } from 'store/actions'; import { AppState } from 'store/reducers'; import AppActions from 'types/actions'; import { GlobalReducer } from 'types/reducer/globalTime'; import CustomDateTimeModal, { DateTimeRangeType } from '../CustomDateTimeModal'; import RefreshText from './Refresh'; const DateTimeSelection = ({ location, updateTimeInterval, globalTimeLoading, }: Props): JSX.Element => { const [form_dtselector] = Form.useForm(); const params = new URLSearchParams(location.search); const searchStartTime = params.get('startTime'); const searchEndTime = params.get('endTime'); const localstorageStartTime = getLocalStorageKey('startTime'); const localstorageEndTime = getLocalStorageKey('endTime'); const getTime = useCallback((): [number, number] | undefined => { if (searchEndTime && searchStartTime) { const startDate = dayjs( new Date(parseInt(getTimeString(searchStartTime), 10)), ); const endDate = dayjs(new Date(parseInt(getTimeString(searchEndTime), 10))); return [startDate.toDate().getTime() || 0, endDate.toDate().getTime() || 0]; } if (localstorageStartTime && localstorageEndTime) { const startDate = dayjs(localstorageStartTime); const endDate = dayjs(localstorageEndTime); return [startDate.toDate().getTime() || 0, endDate.toDate().getTime() || 0]; } return undefined; }, [ localstorageEndTime, localstorageStartTime, searchEndTime, searchStartTime, ]); const [startTime, setStartTime] = useState(); const [endTime, setEndTime] = useState(); const [options, setOptions] = useState(getOptions(location.pathname)); const [refreshButtonHidden, setRefreshButtonHidden] = useState(false); const [customDateTimeVisible, setCustomDTPickerVisible] = useState( false, ); const { maxTime, minTime, selectedTime } = useSelector< AppState, GlobalReducer >((state) => state.globalTime); const getDefaultTime = (pathName: string): Time => { const defaultSelectedOption = getDefaultOption(pathName); const routes = getLocalStorageKey(LOCAL_STORAGE.METRICS_TIME_IN_DURATION); if (routes !== null) { const routesObject = JSON.parse(routes || '{}'); const selectedTime = routesObject[pathName]; if (selectedTime) { return selectedTime; } } return defaultSelectedOption; }; const [selectedTimeInterval, setSelectedTimeInterval] = useState