|
| 1 | +/** |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +import { Button } from 'components/redpanda-ui/components/button'; |
| 13 | +import { Calendar } from 'components/redpanda-ui/components/calendar'; |
| 14 | +import { Input } from 'components/redpanda-ui/components/input'; |
| 15 | +import { Popover, PopoverContent, PopoverTrigger } from 'components/redpanda-ui/components/popover'; |
| 16 | +import { cn } from 'components/redpanda-ui/lib/utils'; |
| 17 | +import { CalendarIcon } from 'lucide-react'; |
| 18 | +import { useMemo, useState } from 'react'; |
| 19 | + |
| 20 | +const DATE_PART_LENGTH = 10; // YYYY-MM-DD |
| 21 | +const TIME_PART_LENGTH = 8; // HH:mm:ss |
| 22 | + |
| 23 | +const pad = (n: number, len = 2) => String(n).padStart(len, '0'); |
| 24 | + |
| 25 | +const toLocalDate = (utcMs: number) => new Date(utcMs); |
| 26 | + |
| 27 | +const formatDateLabel = (utcMs: number) => { |
| 28 | + const d = toLocalDate(utcMs); |
| 29 | + return `${pad(d.getFullYear(), 4)}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; |
| 30 | +}; |
| 31 | + |
| 32 | +const formatTimeLabel = (utcMs: number) => { |
| 33 | + const d = toLocalDate(utcMs); |
| 34 | + return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`; |
| 35 | +}; |
| 36 | + |
| 37 | +const composeUtcMs = (datePart: string, timePart: string): number | null => { |
| 38 | + if (datePart.length !== DATE_PART_LENGTH) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + const normalizedTime = timePart.length === 5 ? `${timePart}:00` : timePart; |
| 42 | + if (normalizedTime.length !== TIME_PART_LENGTH) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + const ms = Date.parse(`${datePart}T${normalizedTime}`); |
| 46 | + return Number.isFinite(ms) ? ms : null; |
| 47 | +}; |
| 48 | + |
| 49 | +type DateTimeInputProps = { |
| 50 | + value: number | undefined; |
| 51 | + onChange: (utcMs: number) => void; |
| 52 | + disabled?: boolean; |
| 53 | + className?: string; |
| 54 | + 'data-testid'?: string; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Local date+time picker. Replaces `<DateTimeInput>` from `@redpanda-data/ui`, |
| 59 | + * which depended on date-fns-tz@2 and forced a build-time shim. Uses the |
| 60 | + * Calendar + Popover + Input registry components and works in the user's local |
| 61 | + * timezone — value/onChange remain UTC milliseconds for parity with the old API. |
| 62 | + */ |
| 63 | +export const DateTimeInput = ({ value, onChange, disabled, className, ...rest }: DateTimeInputProps) => { |
| 64 | + const [open, setOpen] = useState(false); |
| 65 | + const effectiveValue = value ?? Date.now(); |
| 66 | + |
| 67 | + const dateLabel = useMemo(() => formatDateLabel(effectiveValue), [effectiveValue]); |
| 68 | + const timeLabel = useMemo(() => formatTimeLabel(effectiveValue), [effectiveValue]); |
| 69 | + |
| 70 | + const setDate = (next: Date | undefined) => { |
| 71 | + if (!next) { |
| 72 | + return; |
| 73 | + } |
| 74 | + const ms = composeUtcMs( |
| 75 | + `${pad(next.getFullYear(), 4)}-${pad(next.getMonth() + 1)}-${pad(next.getDate())}`, |
| 76 | + timeLabel |
| 77 | + ); |
| 78 | + if (ms !== null) { |
| 79 | + onChange(ms); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + const setTime = (nextTime: string) => { |
| 84 | + const ms = composeUtcMs(dateLabel, nextTime); |
| 85 | + if (ms !== null) { |
| 86 | + onChange(ms); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className={cn('flex items-center gap-2', className)} data-testid={rest['data-testid']}> |
| 92 | + <Popover onOpenChange={setOpen} open={open}> |
| 93 | + <PopoverTrigger> |
| 94 | + <Button |
| 95 | + className={cn('w-[160px] justify-start font-normal', !value && 'text-muted-foreground')} |
| 96 | + disabled={disabled} |
| 97 | + type="button" |
| 98 | + variant="outline" |
| 99 | + > |
| 100 | + <CalendarIcon className="mr-2 size-4" /> |
| 101 | + {dateLabel} |
| 102 | + </Button> |
| 103 | + </PopoverTrigger> |
| 104 | + <PopoverContent align="start" className="w-auto p-0"> |
| 105 | + <Calendar |
| 106 | + mode="single" |
| 107 | + onSelect={(d) => { |
| 108 | + setDate(d); |
| 109 | + setOpen(false); |
| 110 | + }} |
| 111 | + selected={toLocalDate(effectiveValue)} |
| 112 | + /> |
| 113 | + </PopoverContent> |
| 114 | + </Popover> |
| 115 | + <Input |
| 116 | + className="w-[120px]" |
| 117 | + disabled={disabled} |
| 118 | + onChange={(e) => setTime(e.target.value)} |
| 119 | + step={1} |
| 120 | + type="time" |
| 121 | + value={timeLabel} |
| 122 | + /> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +}; |
0 commit comments