"use client" import type { MouseEvent } from "react" import { format, isSameDay, isSameMonth } from "date-fns" import { fr } from "date-fns/locale" import { AgendaEventChip } from "@/components/agenda/agenda-event-chip" import type { AnchorRect } from "@/components/agenda/agenda-floating-card" import { viewDays } from "@/lib/agenda/agenda-date" import { eventsOnDay, isMultiDay } from "@/lib/agenda/agenda-events" import type { AgendaEvent } from "@/lib/agenda/agenda-types" import { cn } from "@/lib/utils" const MAX_CHIPS = 4 function anchorFromEvent(e: MouseEvent): AnchorRect { const rect = e.currentTarget.getBoundingClientRect() return { left: rect.left, top: rect.top, width: rect.width, height: rect.height } } export function AgendaViewMonth({ date, events, onCreateAt, onEventClick, onOpenDay, }: { date: Date events: AgendaEvent[] onCreateAt: (day: Date, anchor: AnchorRect) => void onEventClick: (event: AgendaEvent, anchor: AnchorRect) => void onOpenDay: (day: Date) => void }) { const days = viewDays("month", date) const weeks: Date[][] = [] for (let i = 0; i < days.length; i += 7) weeks.push(days.slice(i, i + 7)) const today = new Date() return (
{days.slice(0, 7).map((d) => (
{format(d, "EEE", { locale: fr }).replace(".", "")}
))}
{weeks.map((week) => week.map((day) => { const dayEvents = eventsOnDay(events, day).sort((a, b) => { const aBanner = a.allDay || isMultiDay(a) const bBanner = b.allDay || isMultiDay(b) if (aBanner !== bBanner) return aBanner ? -1 : 1 return a.start.getTime() - b.start.getTime() }) const visible = dayEvents.slice(0, MAX_CHIPS) const hidden = dayEvents.length - visible.length const isToday = isSameDay(day, today) const inMonth = isSameMonth(day, date) return (
onCreateAt(day, anchorFromEvent(e))} >
{visible.map((event) => ( { e.stopPropagation() onEventClick(event, anchorFromEvent(e)) }} /> ))} {hidden > 0 && ( )}
) }), )}
) }