Converts all 18 server component page files from Supabase client queries to Drizzle ORM using asAdmin. Adds getSession() + redirect to the three pages (hse/incidents, hse/incidents/[id], hse/dashboard) that lacked it. Maps snake_case component prop shapes explicitly where required. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
import { redirect } from 'next/navigation'
|
|
import { asAdmin } from '@/lib/db/with-user'
|
|
import { getSession } from '@/lib/auth/get-session'
|
|
import { zones, sites, trucks } from '@/lib/db/schema'
|
|
import { eq, asc } from 'drizzle-orm'
|
|
import { ReportForm } from '@/components/incidents/report-form'
|
|
import { LanguageSwitcher } from '@/components/language-switcher'
|
|
import { OfflineSync } from '@/components/incidents/offline-sync'
|
|
|
|
interface Props {
|
|
searchParams: Promise<{ zone?: string; truck_id?: string }>
|
|
}
|
|
|
|
export default async function ReportPage({ searchParams }: Props) {
|
|
const { zone, truck_id } = await searchParams
|
|
const session = await getSession()
|
|
if (!session) redirect(`/login?redirect=/report${zone ? `?zone=${zone}` : ''}`)
|
|
|
|
let zoneName: string | null = null
|
|
let siteName: string | null = null
|
|
|
|
if (zone) {
|
|
const [zd] = await asAdmin(db =>
|
|
db.select({
|
|
name: zones.name,
|
|
siteName: sites.name,
|
|
})
|
|
.from(zones)
|
|
.leftJoin(sites, eq(zones.siteId, sites.id))
|
|
.where(eq(zones.qrCodeToken, zone))
|
|
.limit(1)
|
|
)
|
|
if (zd) {
|
|
zoneName = zd.name
|
|
siteName = zd.siteName ?? null
|
|
}
|
|
}
|
|
|
|
const truckRows = await asAdmin(db =>
|
|
db.select({
|
|
id: trucks.id,
|
|
truckNo: trucks.truckNo,
|
|
carrier: trucks.carrier,
|
|
})
|
|
.from(trucks)
|
|
.where(eq(trucks.active, true))
|
|
.orderBy(asc(trucks.truckNo))
|
|
)
|
|
|
|
const truckList = truckRows.map(t => ({
|
|
id: t.id,
|
|
truck_no: t.truckNo,
|
|
carrier: t.carrier ?? null,
|
|
}))
|
|
|
|
return (
|
|
<main className="min-h-screen bg-gray-50 py-6 px-4 max-w-lg mx-auto">
|
|
<div className="mb-6">
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-2xl font-bold text-gray-900">{/* will be translated via ReportForm */}Report an Incident</h1>
|
|
<LanguageSwitcher />
|
|
</div>
|
|
{zoneName ? (
|
|
<p className="text-sm text-gray-600 mt-1">
|
|
{siteName ?? 'Unknown Site'} — {zoneName}
|
|
</p>
|
|
) : (
|
|
<p className="text-sm text-amber-600 mt-1">No zone detected — zone will not be recorded</p>
|
|
)}
|
|
</div>
|
|
<ReportForm
|
|
zoneToken={zone ?? null}
|
|
zoneName={zoneName}
|
|
siteName={siteName}
|
|
trucks={truckList}
|
|
initialTruckId={truck_id ?? null}
|
|
/>
|
|
<OfflineSync />
|
|
</main>
|
|
)
|
|
}
|