'use client' import { CalendarIcon } from 'lucide-react' import { useState } from 'react' import { ptBR } from 'react-day-picker/locale' import { Button } from '@/components/ui/button' import { Calendar } from '@/components/ui/Calendar' import { Drawer, DrawerContent, DrawerFooter, DrawerHeader, DrawerTitle, } from '@/components/ui/Drawer' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/Popover' import { floorToMinuteStep, formatBahiaCivilDate, formatBahiaCivilDateLabel, hourOptions, minuteOptionsForStep, parseBahiaDateTimeInput, } from '@/lib/campaignTime' import { cn } from '@/lib/utils' /** * C97/C123 — the date+time control for the agenda overlay. C123 split it: the * day part is a calendar trigger (popover on desktop, nested bottom sheet on * mobile) and the Hora/Minuto selects render INLINE beside it, always visible * — nothing hides below the calendar anymore. The value contract stays the * civil `YYYY-MM-DDTHH:mm` string, so parse/validation on submit is untouched. * 24h by construction: the trigger formats the civil date directly and the * selects list 00–23 / 00–45, so no browser locale or Intl hour cycle can * inject AM/PM. The parent renders the surrounding Field/FieldError; this * component is the control itself. * * C104 — `timeVisible={false}` is the all-day mode: only the date trigger * renders, the time selects disappear. */ export const ActivityDateTimeField = ({ id, value, onValueChange, invalid = false, errorId, isNarrow = false, label, required = false, timeVisible = true, }: { id: string value: string onValueChange: (value: string) => void invalid?: boolean /** FieldError id to announce on focus — parity with the input it replaces. */ errorId?: string /** Renders the calendar picker as a nested bottom sheet instead of a popover. */ isNarrow?: boolean /** Field name used in the sheet header and the time-select a11y labels (e.g. "Início"). */ label?: string /** Marks the required fields with a visible asterisk on the narrow trigger. */ required?: boolean /** When false, hide the time selects (all-day mode). */ timeVisible?: boolean }) => { const [open, setOpen] = useState(false) const civil = floorToMinuteStep(value) const datePart = civil.slice(0, 10) // A draft without a startAt (tour drafts) renders the controls empty: the // selects default to 00:00 and picking a date seeds the time part. const timePart = civil ? civil.slice(11) : '00:00' const hour = timePart.slice(0, 2) const minute = timePart.slice(3, 5) const setDate = (date: Date | undefined) => { if (!date) return onValueChange(`${formatBahiaCivilDate(date)}T${timePart}`) } // Time edits are a no-op until a date exists: a draft without a startAt // (tour drafts) must not emit a corrupt civil string like `T09:00`. const setHour = (next: string) => { if (!datePart) return onValueChange(`${datePart}T${next}:${minute}`) } const setMinute = (next: string) => { if (!datePart) return onValueChange(`${datePart}T${hour}:${next}`) } const selectedInstant = parseBahiaDateTimeInput( `${datePart || formatBahiaCivilDate(new Date())}T12:00`, ) const dateTrigger = ( ) const calendarContent = selectedInstant ? ( ) : null const timeSelects = timeVisible ? (
{( [ ['Hora', hour, setHour], ['Minuto', minute, setMinute], ] as const ).map(([part, value, onChange]) => (
{!isNarrow ? ( {part} ) : null} onChange(event.target.value)} className={cn( 'w-full **:data-[slot=native-select]:min-h-11', isNarrow && '**:data-[slot=native-select]:min-h-11 **:data-[slot=native-select]:rounded-none **:data-[slot=native-select]:border-0 **:data-[slot=native-select]:bg-transparent **:data-[slot=native-select]:px-1', )} > {(part === 'Hora' ? hourOptions : minuteOptionsForStep()).map((option) => ( {part === 'Hora' ? `${option}h` : option} ))}
))}
) : null return (
{isNarrow ? ( <> {dateTrigger} {label ? `${label} — ` : ''} {selectedInstant ? formatBahiaCivilDate(new Date(selectedInstant)) : datePart}
{calendarContent}
) : ( {dateTrigger} {calendarContent} )}
{timeSelects}
) }