'use client' import { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' import { List, Plus } from 'lucide-react' import { useLiveQuery } from 'dexie-react-hooks' import { db } from '@/lib/sync' import { Card, LoadingState, Button } from '@/components/ui' import { Header, PageContainer } from '@/components/layout/header' import { CalendarMonth } from '@/components/calendar/CalendarMonth' import { useApp } from '../../provider' export default function CalendarPage() { const router = useRouter() const { currentWorkspace } = useApp() const [currentMonth, setCurrentMonth] = useState(new Date()) const [selectedDate, setSelectedDate] = useState(new Date()) const [loading, setLoading] = useState(true) const [serverAppointments, setServerAppointments] = useState([]) // Fetch from IndexedDB for offline support const localAppointments = useLiveQuery( () => db.appointments .where('workspaceId') .equals(currentWorkspace.id) .and((a) => !a.deletedAt) .toArray(), [currentWorkspace.id] ) // Also fetch from server for latest data useEffect(() => { async function fetchAppointments() { try { const response = await fetch( `/api/workspaces/${currentWorkspace.id}/appointments` ) if (response.ok) { const data = await response.json() setServerAppointments(data.appointments) } } catch (err) { console.error('Failed to fetch appointments:', err) } finally { setLoading(false) } } fetchAppointments() }, [currentWorkspace.id]) // Prefer server data if available const appointments = serverAppointments.length > 0 ? serverAppointments : localAppointments || [] if (loading && !localAppointments) { return ( <>
, label: 'List view', onClick: () => router.push('/appointments'), }} /> ) } return ( <>
, label: 'List view', onClick: () => router.push('/appointments'), }} /> ({ id: a.id, title: a.title, datetime: a.datetime, location: a.location, }))} selectedDate={selectedDate} onDateSelect={setSelectedDate} onMonthChange={setCurrentMonth} currentMonth={currentMonth} /> {/* Add appointment FAB */}
) }