import { redirect } from 'next/navigation' import { createClient } from '@/lib/supabase/server' import { PageHeader, PageTitle } from '@/components/ui/page-title' import { Badge } from '@/components/ui/badge' import { Empty, EmptyHeader, EmptyMedia, EmptyTitle, EmptyDescription } from '@/components/ui/empty' import { CalendarCheck } from 'lucide-react' import { listAttendance } from '@/lib/actions' export const dynamic = 'force-dynamic' type Row = { id: string name: string isHost: boolean status: 'present' | 'checked_out' | 'stale' checkedInAt: string checkedOutAt: string | null checkInNote: string | null checkOutNote: string | null } const STATUS: Record = { present: { label: 'Here now', cls: 'text-primary' }, checked_out: { label: 'Checked out', cls: 'text-muted-foreground' }, stale: { label: 'No checkout', cls: 'text-amber-600' }, } function dur(inAt: string, outAt: string | null): string { const end = outAt ? new Date(outAt).getTime() : Date.now() const mins = Math.max(0, Math.round((end - new Date(inAt).getTime()) / 60000)) if (mins < 60) return `${mins}m` const h = Math.floor(mins / 60) return `${h}h ${mins % 60}m` } export default async function AttendancePage() { const supabase = await createClient() const { data: { user } } = await supabase.auth.getUser() if (!user) redirect('/login') const res = await listAttendance() const rows = ('data' in res ? res.data : []) as Row[] return ( <> Attendance
{rows.length === 0 ? ( No visits recorded yet When members check in from the dashboard, their visits appear here. ) : (
{rows.map(r => { const s = STATUS[r.status] return (
{r.name} {r.isHost && Host} {s.label}
{(r.checkInNote || r.checkOutNote) && (

{r.checkInNote ? `in: ${r.checkInNote}` : ''} {r.checkInNote && r.checkOutNote ? ' ยท ' : ''} {r.checkOutNote ? `out: ${r.checkOutNote}` : ''}

)}
{new Date(r.checkedInAt).toLocaleString()}
{r.checkedOutAt ? `${dur(r.checkedInAt, r.checkedOutAt)} visit` : r.status === 'present' ? `${dur(r.checkedInAt, null)} so far` : 'open'}
) })}
)}
) }