"use client" import { useMemo, useState } from "react" import { toast } from "sonner" import { MoreVertical, Pencil, Plus, Trash2 } from "lucide-react" import { AgendaCalendarDialog } from "@/components/agenda/agenda-calendar-dialog" import { CalendarViewDialog, ExternalCalendarDialog, } from "@/components/agenda/agenda-calendars-settings" import { AgendaMiniMonth } from "@/components/agenda/agenda-mini-month" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" import { Button } from "@/components/ui/button" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { Skeleton } from "@/components/ui/skeleton" import { useDeleteAgendaCalendar } from "@/lib/api/hooks/use-calendar-mutations" import { useLabels } from "@/lib/api/hooks/use-folder-label-queries" import { useMailAccounts } from "@/lib/api/hooks/use-mail-queries" import { ALL_AGENDAS_VIEW_LABEL, isExternalCalendarId, isReservedAgendaViewName } from "@/lib/agenda/agenda-calendar-visibility" import { calendarColor } from "@/lib/agenda/agenda-events" import { useAgendaSettingsStore, useAgendaUIStore } from "@/lib/agenda/agenda-store" import type { AgendaCalendar } from "@/lib/agenda/agenda-types" import { useMergedAgendaCalendars } from "@/lib/agenda/use-visible-agenda-calendars" import { useIsMobile } from "@/hooks/use-mobile" import { cn } from "@/lib/utils" export function AgendaSidebar({ selectedDate, onSelectDate, onCreateEvent, }: { selectedDate: Date onSelectDate: (date: Date) => void onCreateEvent: () => void }) { const isMobile = useIsMobile() const sidebarCollapsed = useAgendaUIStore((s) => s.sidebarCollapsed) const setSidebarCollapsed = useAgendaUIStore((s) => s.setSidebarCollapsed) const hiddenIds = useAgendaSettingsStore((s) => s.hiddenCalendarIds) const weekStart = useAgendaSettingsStore((s) => s.weekStart) const calendarViews = useAgendaSettingsStore((s) => s.calendarViews) const activeCalendarViewId = useAgendaSettingsStore((s) => s.activeCalendarViewId) const setActiveCalendarViewId = useAgendaSettingsStore((s) => s.setActiveCalendarViewId) const toggleCalendar = useAgendaSettingsStore((s) => s.toggleCalendarVisible) const { data: accounts = [] } = useMailAccounts() const { data: labels = [] } = useLabels() const { calendars, isLoading } = useMergedAgendaCalendars() const deleteMutation = useDeleteAgendaCalendar() const [calendarDialogOpen, setCalendarDialogOpen] = useState(false) const [editingCalendar, setEditingCalendar] = useState(null) const [deletingCalendar, setDeletingCalendar] = useState(null) const [externalDialogOpen, setExternalDialogOpen] = useState(false) const [viewDialogOpen, setViewDialogOpen] = useState(false) const calendarOptions = useMemo( () => (calendars ?? []).map((calendar) => ({ id: calendar.id, label: calendar.display_name, color: calendarColor(calendar), })), [calendars], ) const labelOptions = useMemo( () => labels.map((label) => ({ id: label.id, label: label.name })), [labels], ) const open = !sidebarCollapsed const confirmDelete = async () => { if (!deletingCalendar) return try { await deleteMutation.mutateAsync({ id: deletingCalendar.id }) toast.success(`Agenda « ${deletingCalendar.display_name} » supprimé`) } catch { toast.error("Impossible de supprimer cet agenda") } finally { setDeletingCalendar(null) } } return ( <> { setCalendarDialogOpen(false) setExternalDialogOpen(true) } } /> { if (!o) setDeletingCalendar(null) }} > Supprimer « {deletingCalendar?.display_name} » ? Tous les événements de cet agenda seront définitivement supprimés. Annuler void confirmDelete()} > Supprimer ) }