"use client" import { useMemo, useState } from "react" import { useRouter } from "next/navigation" import { useQuery } from "@tanstack/react-query" import { CalendarCheck2Icon, ClipboardListIcon, ClockIcon } from "lucide-react" import { Calendar } from "@/shared/components/ui/calendar" import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card" import { Badge } from "@/shared/components/ui/badge" import { useAuthApi } from "@/shared/useApi" import { APPOINTMENT_ROUTES } from "@garage/api" import { formatEnum } from "@/shared/utils/formatters" const STATUS_COLORS: Record = { requested: "bg-yellow-100 text-yellow-800", confirmed: "bg-blue-100 text-blue-800", in_progress: "bg-purple-100 text-purple-800", completed: "bg-green-100 text-green-800", cancelled: "bg-red-100 text-red-800", } function toDateKey(d: Date): string { const y = d.getFullYear() const m = String(d.getMonth() + 1).padStart(2, "0") const day = String(d.getDate()).padStart(2, "0") return `${y}-${m}-${day}` } export function AppointmentsCalendarView() { const api = useAuthApi() const router = useRouter() const [selected, setSelected] = useState(() => new Date()) const { data, isLoading } = useQuery({ queryKey: [APPOINTMENT_ROUTES.INDEX, "calendar"], queryFn: () => api.appointments.list({ per_page: 100 } as any), }) const appointments: any[] = (data as any)?.data ?? [] const byDate = useMemo(() => { const map: Record = {} for (const a of appointments) { const key = String(a.date ?? "").slice(0, 10) if (!key) continue ;(map[key] ??= []).push(a) } return map }, [appointments]) const bookedDays = useMemo( () => Object.keys(byDate).map((k) => new Date(`${k}T00:00:00`)), [byDate], ) const selectedKey = selected ? toDateKey(selected) : "" const dayAppointments = byDate[selectedKey] ?? [] return (
{selected ? selected.toLocaleDateString(undefined, { weekday: "long", year: "numeric", month: "long", day: "numeric", }) : "Select a day"} {isLoading &&

Loading…

} {!isLoading && dayAppointments.length === 0 && (

No appointments on this day.

)} {dayAppointments.map((a) => { const jobCardId = a.job_card_id ?? a.job_card?.id return ( ) })}
) }