export const dynamic = 'force-dynamic' import Link from 'next/link' import { createClient } from '@/lib/supabase/server' import { redirect } from 'next/navigation' import { getSession } from '@/lib/auth/get-session' const STATUS_LABELS: Record = { reported: 'Reported', triaged: 'Triaged', investigating: 'Investigating', capa_pending: 'CAPA Pending', verification: 'Verification', closed: 'Closed', } const STATUS_COLORS: Record = { reported: 'bg-gray-100 text-gray-600', triaged: 'bg-yellow-100 text-yellow-800', investigating: 'bg-blue-100 text-blue-700', capa_pending: 'bg-orange-100 text-orange-700', verification: 'bg-purple-100 text-purple-700', closed: 'bg-green-100 text-green-700', } const TYPE_LABELS: Record = { injury: 'Injury', near_miss: 'Near Miss', hazard: 'Hazard', asset_damage: 'Asset Damage', environmental: 'Environmental', security: 'Security', fire: 'Fire', } export default async function ReporterPage() { const session = await getSession() if (!session) redirect('/login') const supabase = await createClient() const { data: incidents } = await supabase .from('incidents') .select('id, reference_no, incident_type, status, reported_at, severity, sites (name)') .eq('reported_by', session.sub) .order('reported_at', { ascending: false }) const rows = incidents ?? [] const openCount = rows.filter(r => r.status !== 'closed').length const closedCount = rows.filter(r => r.status === 'closed').length return (

My Reports

Welcome, {session.name}

+ New Report

{rows.length}

Total Submitted

{openCount}

In Progress

{closedCount}

Closed

{rows.length === 0 ? (

No incidents reported yet.

Submit your first report →
) : (
{rows.map(inc => { const siteName = (inc.sites as unknown as { name: string } | null)?.name return (

{inc.reference_no ?? inc.id.slice(0, 8)}

{TYPE_LABELS[inc.incident_type] ?? inc.incident_type} {siteName ? ` · ${siteName}` : ''} {inc.severity ? ` · Severity ${inc.severity}` : ''}

{inc.reported_at && (

{new Date(inc.reported_at as string).toLocaleDateString('en-MY', { day: 'numeric', month: 'short', year: 'numeric', })}

)}
{STATUS_LABELS[inc.status] ?? inc.status}
) })}
)}
) }