import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { pushModal, useOnPushModal } from '@/modals'; import { cn } from '@/utils/cn'; import { bind } from 'bind-event-listener'; import { CalendarIcon } from 'lucide-react'; import { useCallback, useEffect, useRef } from 'react'; import { shouldIgnoreKeypress } from '@/utils/should-ignore-keypress'; import { timeWindows } from '@openpanel/constants'; import type { IChartRange } from '@openpanel/validation'; import { endOfDay, format, startOfDay } from 'date-fns'; type Props = { value: IChartRange; onChange: (value: IChartRange) => void; onStartDateChange: (date: string) => void; onEndDateChange: (date: string) => void; endDate: string | null; startDate: string | null; className?: string; }; export function TimeWindowPicker({ value, onChange, startDate, onStartDateChange, endDate, onEndDateChange, className, }: Props) { const isDateRangerPickerOpen = useRef(false); useOnPushModal('DateRangerPicker', (open) => { isDateRangerPickerOpen.current = open; }); const timeWindow = timeWindows[value ?? '30d']; const handleCustom = useCallback(() => { pushModal('DateRangerPicker', { onChange: ({ startDate, endDate }) => { onStartDateChange(format(startOfDay(startDate), 'yyyy-MM-dd HH:mm:ss')); onEndDateChange(format(endOfDay(endDate), 'yyyy-MM-dd HH:mm:ss')); onChange('custom'); }, startDate: startDate ? new Date(startDate) : undefined, endDate: endDate ? new Date(endDate) : undefined, }); }, [startDate, endDate]); useEffect(() => { return bind(document, { type: 'keydown', listener(event) { if (shouldIgnoreKeypress(event)) { return; } if (isDateRangerPickerOpen.current) { return; } const match = Object.values(timeWindows).find( (tw) => event.key === tw.shortcut.toLowerCase(), ); if (match?.key === 'custom') { handleCustom(); } else if (match) { onChange(match.key); } }, }); }, [handleCustom]); return ( Time window onChange(timeWindows['30min'].key)}> {timeWindows['30min'].label} {timeWindows['30min'].shortcut} onChange(timeWindows.lastHour.key)}> {timeWindows.lastHour.label} {timeWindows.lastHour.shortcut} onChange(timeWindows.today.key)}> {timeWindows.today.label} {timeWindows.today.shortcut} onChange(timeWindows.yesterday.key)}> {timeWindows.yesterday.label} {timeWindows.yesterday.shortcut} onChange(timeWindows['7d'].key)}> {timeWindows['7d'].label} {timeWindows['7d'].shortcut} onChange(timeWindows['30d'].key)}> {timeWindows['30d'].label} {timeWindows['30d'].shortcut} onChange(timeWindows['6m'].key)}> {timeWindows['6m'].label} {timeWindows['6m'].shortcut} onChange(timeWindows['12m'].key)}> {timeWindows['12m'].label} {timeWindows['12m'].shortcut} onChange(timeWindows.monthToDate.key)} > {timeWindows.monthToDate.label} {timeWindows.monthToDate.shortcut} onChange(timeWindows.lastMonth.key)}> {timeWindows.lastMonth.label} {timeWindows.lastMonth.shortcut} onChange(timeWindows.yearToDate.key)} > {timeWindows.yearToDate.label} {timeWindows.yearToDate.shortcut} onChange(timeWindows.lastYear.key)}> {timeWindows.lastYear.label} {timeWindows.lastYear.shortcut} handleCustom()}> {timeWindows.custom.label} {timeWindows.custom.shortcut} ); }