'use client' import { useRouter } from 'next/navigation' import { format, isToday, isTomorrow, parseISO, startOfDay } from 'date-fns' import { toZonedTime } from 'date-fns-tz' import { Plus, Calendar, MapPin, Clock, ChevronRight, CalendarDays } from 'lucide-react' import { useLiveQuery } from 'dexie-react-hooks' import { db } from '@/lib/sync' import { Card, LoadingState, EmptyState, Button } from '@/components/ui' import { Header, PageContainer } from '@/components/layout/header' import { useApp } from '../provider' const TIMEZONE = 'Australia/Perth' export default function AppointmentsPage() { const router = useRouter() const { currentWorkspace } = useApp() const appointments = useLiveQuery( () => db.appointments .where('workspaceId') .equals(currentWorkspace.id) .and((a) => !a.deletedAt) .sortBy('datetime'), [currentWorkspace.id] ) // Group appointments by date const groupedAppointments = appointments?.reduce( (groups, appt) => { const date = toZonedTime(parseISO(appt.datetime), TIMEZONE) const dateKey = format(startOfDay(date), 'yyyy-MM-dd') if (!groups[dateKey]) { groups[dateKey] = { date, appointments: [], } } groups[dateKey].appointments.push(appt) return groups }, {} as Record ) const sortedDates = Object.keys(groupedAppointments || {}).sort() const formatDateHeader = (date: Date) => { if (isToday(date)) return 'Today' if (isTomorrow(date)) return 'Tomorrow' return format(date, 'EEEE, MMMM d') } if (!appointments) { return ( <>
, label: 'Calendar view', onClick: () => router.push('/appointments/calendar'), }} /> ) } return ( <>
, label: 'Calendar view', onClick: () => router.push('/appointments/calendar'), }} /> {appointments.length === 0 ? ( router.push('/appointments/new'), }} /> ) : (
{sortedDates.map((dateKey) => { const group = groupedAppointments![dateKey] const isPast = group.date < startOfDay(new Date()) return (

{formatDateHeader(group.date)}

{group.appointments.map((appt) => ( router.push(`/appointments/${appt.id}`)} className={`${isPast ? 'opacity-60' : ''}`} >

{appt.title}

{format( toZonedTime(parseISO(appt.datetime), TIMEZONE), 'h:mm a' )}

{appt.location && (

{appt.location}

)}
))}
) })}
)} {/* Add appointment FAB */}
) }