import { useMemo, useState } from 'react' import { CalendarDays } from 'lucide-react' import { useTranslation } from 'react-i18next' import dayjs from '@/lib/dayjs' import { cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' interface CompactDateTimeRangePickerProps { start?: Date end?: Date onChange: (range: { start?: Date; end?: Date }) => void className?: string } function toInputValue(date?: Date): string { return date ? dayjs(date).format('YYYY-MM-DDTHH:mm') : '' } function fromInputValue(value: string): Date | undefined { if (!value) return undefined const date = new Date(value) return Number.isNaN(date.getTime()) ? undefined : date } export function CompactDateTimeRangePicker({ start, end, onChange, className, }: CompactDateTimeRangePickerProps) { const { t } = useTranslation() const [open, setOpen] = useState(false) const [draftStart, setDraftStart] = useState(toInputValue(start)) const [draftEnd, setDraftEnd] = useState(toInputValue(end)) const label = useMemo(() => { if (!start && !end) return t('Date Range') const startText = start ? dayjs(start).format('YYYY-MM-DD HH:mm:ss') : '-' const endText = end ? dayjs(end).format('YYYY-MM-DD HH:mm:ss') : '-' return `${startText} ~ ${endText}` }, [end, start, t]) const handleOpenChange = (nextOpen: boolean) => { if (nextOpen) { setDraftStart(toInputValue(start)) setDraftEnd(toInputValue(end)) } setOpen(nextOpen) } const applyDraft = () => { onChange({ start: fromInputValue(draftStart), end: fromInputValue(draftEnd), }) setOpen(false) } const applyPreset = (kind: 'today' | '7d' | 'week' | '30d' | 'month') => { const now = dayjs() const presets = { today: { start: now.startOf('day').toDate(), end: now.endOf('day').toDate(), }, '7d': { start: now.subtract(6, 'day').startOf('day').toDate(), end: now.endOf('day').toDate(), }, week: { start: now.startOf('week').toDate(), end: now.endOf('week').toDate(), }, '30d': { start: now.subtract(29, 'day').startOf('day').toDate(), end: now.endOf('day').toDate(), }, month: { start: now.startOf('month').toDate(), end: now.endOf('month').toDate(), }, } const range = presets[kind] setDraftStart(toInputValue(range.start)) setDraftEnd(toInputValue(range.end)) onChange(range) setOpen(false) } return (
{t('Start Time')}
setDraftStart(e.target.value)} className='h-8 font-mono text-xs' />
~
{t('End Time')}
setDraftEnd(e.target.value)} className='h-8 font-mono text-xs' />
) }