"use client" import { useEffect, useState } from "react" import { format } from "date-fns" import { fr } from "date-fns/locale" import { toast } from "sonner" import { X } from "lucide-react" import { AgendaFloatingCard, type AnchorRect } from "@/components/agenda/agenda-floating-card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { draftToApiEvent, useCreateAgendaEvent, } from "@/lib/api/hooks/use-calendar-mutations" import { formatEventTime } from "@/lib/agenda/agenda-date" import { calendarColor } from "@/lib/agenda/agenda-events" import type { AgendaCalendar, AgendaEventDraft, } from "@/lib/agenda/agenda-types" export interface AgendaQuickCreateState { start: Date end: Date allDay: boolean anchor: AnchorRect } export function AgendaQuickCreate({ state, calendars, defaultCalendarId, userEmail, onClose, onMoreOptions, }: { state: AgendaQuickCreateState | null calendars: AgendaCalendar[] defaultCalendarId: string userEmail?: string onClose: () => void onMoreOptions: (draft: AgendaEventDraft) => void }) { const [title, setTitle] = useState("") const [calendarId, setCalendarId] = useState(defaultCalendarId) const createMutation = useCreateAgendaEvent() useEffect(() => { if (state) { setTitle("") setCalendarId(defaultCalendarId) } }, [state, defaultCalendarId]) if (!state) return null const draft: AgendaEventDraft = { title, start: state.start, end: state.end, allDay: state.allDay, calendarId, } const dateLabel = format(state.start, "EEEE d MMMM", { locale: fr }).replace( /^./, (c) => c.toUpperCase(), ) const timeLabel = state.allDay ? "Toute la journée" : `${formatEventTime(state.start)} – ${formatEventTime(state.end)}` const save = async () => { try { const apiEvent = draftToApiEvent(draft) if (userEmail) apiEvent.organizer = userEmail await createMutation.mutateAsync({ calendarId, event: apiEvent }) toast.success("Événement créé") onClose() } catch { toast.error("Impossible de créer l'événement") } } return (
setTitle(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") void save() }} className="h-11 rounded-none border-0 border-b-2 border-border/60 !bg-transparent px-1 !text-lg shadow-none focus-visible:border-primary focus-visible:ring-0" />
{dateLabel} {timeLabel}
) }