import './CustomTimePicker.styles.scss'; import { Color } from '@signozhq/design-tokens'; import { Button } from 'antd'; import logEvent from 'api/common/logEvent'; import cx from 'classnames'; import DatePickerV2 from 'components/DatePickerV2/DatePickerV2'; import { DATE_TIME_FORMATS } from 'constants/dateTimeFormats'; import { QueryParams } from 'constants/query'; import ROUTES from 'constants/routes'; import { DateTimeRangeType } from 'container/TopNav/CustomDateTimeModal'; import { LexicalContext, Option, RelativeDurationSuggestionOptions, } from 'container/TopNav/DateTimeSelectionV2/config'; import dayjs from 'dayjs'; import { Clock, PenLine } from 'lucide-react'; import { useTimezone } from 'providers/Timezone'; import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useState, } from 'react'; import { useLocation } from 'react-router-dom'; import { getCustomTimeRanges } from 'utils/customTimeRangeUtils'; import TimezonePicker from './TimezonePicker'; interface CustomTimePickerPopoverContentProps { options: any[]; setIsOpen: Dispatch>; customDateTimeVisible: boolean; setCustomDTPickerVisible: Dispatch>; onCustomDateHandler: ( dateTimeRange: DateTimeRangeType, lexicalContext?: LexicalContext, ) => void; onSelectHandler: (label: string, value: string) => void; onGoLive: () => void; selectedTime: string; activeView: 'datetime' | 'timezone'; setActiveView: Dispatch>; isOpenedFromFooter: boolean; setIsOpenedFromFooter: Dispatch>; onExitLiveLogs: () => void; } interface RecentlyUsedDateTimeRange { label: string; value: number; timestamp: number; from: string; to: string; } // eslint-disable-next-line sonarjs/cognitive-complexity function CustomTimePickerPopoverContent({ options, setIsOpen, customDateTimeVisible, setCustomDTPickerVisible, onCustomDateHandler, onSelectHandler, onGoLive, selectedTime, activeView, setActiveView, isOpenedFromFooter, setIsOpenedFromFooter, onExitLiveLogs, }: CustomTimePickerPopoverContentProps): JSX.Element { const { pathname } = useLocation(); const isLogsExplorerPage = useMemo(() => pathname === ROUTES.LOGS_EXPLORER, [ pathname, ]); const url = new URLSearchParams(window.location.search); let panelTypeFromURL = url.get(QueryParams.panelTypes); try { panelTypeFromURL = JSON.parse(panelTypeFromURL as string); } catch { // fallback → leave as-is } const isLogsListView = panelTypeFromURL !== 'table' && panelTypeFromURL !== 'graph'; // we do not select list view in the url const { timezone } = useTimezone(); const activeTimezoneOffset = timezone.offset; const [recentlyUsedTimeRanges, setRecentlyUsedTimeRanges] = useState< RecentlyUsedDateTimeRange[] >([]); const handleExitLiveLogs = useCallback((): void => { if (isLogsExplorerPage) { onExitLiveLogs(); } }, [isLogsExplorerPage, onExitLiveLogs]); useEffect(() => { if (!customDateTimeVisible) { const customTimeRanges = getCustomTimeRanges(); const formattedCustomTimeRanges: RecentlyUsedDateTimeRange[] = customTimeRanges.map( (range) => ({ label: `${dayjs(range.from) .tz(timezone.value) .format(DATE_TIME_FORMATS.DD_MMM_YYYY_HH_MM_SS)} - ${dayjs(range.to) .tz(timezone.value) .format(DATE_TIME_FORMATS.DD_MMM_YYYY_HH_MM_SS)}`, from: range.from, to: range.to, value: range.timestamp, timestamp: range.timestamp, }), ); setRecentlyUsedTimeRanges(formattedCustomTimeRanges); } }, [customDateTimeVisible, timezone.value]); function getTimeChips(options: Option[]): JSX.Element { return (
{options.map((option) => ( ))}
); } const handleTimezoneHintClick = (): void => { setActiveView('timezone'); setIsOpenedFromFooter(true); logEvent( 'DateTimePicker: Timezone picker opened from time range picker footer', { page: pathname, }, ); }; if (activeView === 'timezone') { return (
); } const handleGoLive = (): void => { onGoLive(); setIsOpen(false); }; return ( <>
{!customDateTimeVisible && (
{isLogsExplorerPage && isLogsListView && ( )} {options.map((option) => ( ))}
)}
{customDateTimeVisible ? ( ) : (
RELATIVE TIMES
{getTimeChips(RelativeDurationSuggestionOptions)}
RECENTLY USED
{recentlyUsedTimeRanges.map((range: RecentlyUsedDateTimeRange) => (
{ if (e.key === 'Enter' || e.key === ' ') { handleExitLiveLogs(); onCustomDateHandler([dayjs(range.from), dayjs(range.to)]); setIsOpen(false); } }} key={range.value} onClick={(): void => { handleExitLiveLogs(); onCustomDateHandler([dayjs(range.from), dayjs(range.to)]); setIsOpen(false); }} > {range.label}
))}
)}
Current timezone
); } export default CustomTimePickerPopoverContent;