feat(design): migrate the Incidents list to TableCard with server-side views, sort, filters and paging
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
export const PAGE_SIZE = 25
|
||||
export const OPEN_STATUSES = ['reported', 'triaged', 'investigating', 'capa_pending', 'verification'] as const
|
||||
|
||||
export type SortKey = 'reported_at' | 'reference_no' | 'status'
|
||||
export type ListView = 'all' | 'open' | 'closed'
|
||||
|
||||
export interface ListParams {
|
||||
page: number
|
||||
q: string
|
||||
status: string
|
||||
type: string
|
||||
siteId: string
|
||||
truckId: string
|
||||
view: ListView
|
||||
sort: SortKey
|
||||
dir: 'asc' | 'desc'
|
||||
}
|
||||
|
||||
type Raw = {
|
||||
page?: string; q?: string; status?: string; type?: string; site_id?: string; truck_id?: string
|
||||
view?: string; sort?: string; dir?: string
|
||||
}
|
||||
|
||||
const SORT_KEYS: SortKey[] = ['reported_at', 'reference_no', 'status']
|
||||
const VIEWS: ListView[] = ['all', 'open', 'closed']
|
||||
|
||||
export function parseListParams(raw: Raw): ListParams {
|
||||
const page = Number.parseInt(raw.page ?? '1', 10)
|
||||
return {
|
||||
page: Number.isFinite(page) && page > 0 ? page : 1,
|
||||
q: raw.q ?? '',
|
||||
status: raw.status ?? '',
|
||||
type: raw.type ?? '',
|
||||
siteId: raw.site_id ?? '',
|
||||
truckId: raw.truck_id ?? '',
|
||||
view: VIEWS.includes(raw.view as ListView) ? (raw.view as ListView) : 'all',
|
||||
sort: SORT_KEYS.includes(raw.sort as SortKey) ? (raw.sort as SortKey) : 'reported_at',
|
||||
dir: raw.dir === 'asc' ? 'asc' : 'desc',
|
||||
}
|
||||
}
|
||||
|
||||
export function nextSort(
|
||||
current: { key: SortKey; dir: 'asc' | 'desc' },
|
||||
key: SortKey,
|
||||
): { key: SortKey; dir: 'asc' | 'desc' } {
|
||||
if (current.key === key) return { key, dir: current.dir === 'asc' ? 'desc' : 'asc' }
|
||||
return { key, dir: key === 'reported_at' ? 'desc' : 'asc' }
|
||||
}
|
||||
|
||||
export function withParams(search: string, updates: Record<string, string | null>): string {
|
||||
const p = new URLSearchParams(search)
|
||||
for (const [k, v] of Object.entries(updates)) {
|
||||
if (v) p.set(k, v)
|
||||
else p.delete(k)
|
||||
}
|
||||
if (!('page' in updates)) p.delete('page')
|
||||
const s = p.toString()
|
||||
return s ? `?${s}` : ''
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import { and, asc, desc, eq, ilike, inArray, or, sql, aliasedTable } from 'drizzle-orm'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { incidents, sites, trucks, users, zones } from '@/lib/db/schema'
|
||||
import { OPEN_STATUSES, PAGE_SIZE, type ListParams } from './list-params'
|
||||
import type { Incident, SiteOption, TruckOption } from './types'
|
||||
|
||||
export interface IncidentListData {
|
||||
incidents: Incident[]
|
||||
total: number
|
||||
counts: { all: number; open: number; closed: number }
|
||||
sites: SiteOption[]
|
||||
trucks: TruckOption[]
|
||||
}
|
||||
|
||||
export async function loadIncidentList(p: ListParams): Promise<IncidentListData> {
|
||||
const reporter = aliasedTable(users, 'reporter')
|
||||
|
||||
const base = []
|
||||
if (p.q) base.push(or(ilike(incidents.referenceNo, `%${p.q}%`), ilike(incidents.description, `%${p.q}%`)))
|
||||
if (p.type) base.push(eq(incidents.incidentType, p.type as never))
|
||||
if (p.siteId) base.push(eq(incidents.siteId, p.siteId))
|
||||
if (p.truckId) base.push(eq(incidents.truckId, p.truckId))
|
||||
|
||||
const scoped = [...base]
|
||||
if (p.status) scoped.push(eq(incidents.status, p.status as never))
|
||||
if (p.view === 'open') scoped.push(inArray(incidents.status, [...OPEN_STATUSES] as never))
|
||||
if (p.view === 'closed') scoped.push(eq(incidents.status, 'closed' as never))
|
||||
|
||||
const where = scoped.length ? and(...scoped) : undefined
|
||||
const baseWhere = base.length ? and(...base) : undefined
|
||||
|
||||
const sortColumn = { reported_at: incidents.reportedAt, reference_no: incidents.referenceNo, status: incidents.status }[p.sort]
|
||||
const orderBy = p.dir === 'asc' ? asc(sortColumn) : desc(sortColumn)
|
||||
|
||||
const [[countRow], rows, statusCounts, siteRows, truckRows] = await Promise.all([
|
||||
asAdmin(db => db.select({ count: sql<number>`count(*)` }).from(incidents).where(where)),
|
||||
asAdmin(db =>
|
||||
db.select({
|
||||
id: incidents.id,
|
||||
referenceNo: incidents.referenceNo,
|
||||
incidentType: incidents.incidentType,
|
||||
status: incidents.status,
|
||||
severity: incidents.severity,
|
||||
reportedAt: incidents.reportedAt,
|
||||
siteName: sites.name,
|
||||
zoneName: zones.name,
|
||||
reporterName: reporter.name,
|
||||
truckNo: trucks.truckNo,
|
||||
})
|
||||
.from(incidents)
|
||||
.leftJoin(sites, eq(incidents.siteId, sites.id))
|
||||
.leftJoin(zones, eq(incidents.zoneId, zones.id))
|
||||
.leftJoin(reporter, eq(incidents.reportedBy, reporter.id))
|
||||
.leftJoin(trucks, eq(incidents.truckId, trucks.id))
|
||||
.where(where)
|
||||
.orderBy(orderBy)
|
||||
.limit(PAGE_SIZE)
|
||||
.offset((p.page - 1) * PAGE_SIZE)
|
||||
),
|
||||
asAdmin(db =>
|
||||
db.select({ status: incidents.status, count: sql<number>`count(*)` })
|
||||
.from(incidents)
|
||||
.where(baseWhere)
|
||||
.groupBy(incidents.status)
|
||||
),
|
||||
asAdmin(db => db.select({ id: sites.id, name: sites.name }).from(sites).orderBy(asc(sites.name))),
|
||||
asAdmin(db =>
|
||||
db.select({ id: trucks.id, truckNo: trucks.truckNo })
|
||||
.from(trucks)
|
||||
.where(eq(trucks.active, true))
|
||||
.orderBy(asc(trucks.truckNo))
|
||||
),
|
||||
])
|
||||
|
||||
const byStatus = new Map(statusCounts.map(r => [r.status as string, Number(r.count)]))
|
||||
const all = [...byStatus.values()].reduce((a, b) => a + b, 0)
|
||||
const open = OPEN_STATUSES.reduce((sum, s) => sum + (byStatus.get(s) ?? 0), 0)
|
||||
|
||||
return {
|
||||
incidents: rows.map(r => ({
|
||||
id: r.id,
|
||||
reference_no: r.referenceNo,
|
||||
incident_type: r.incidentType,
|
||||
status: r.status,
|
||||
severity: r.severity,
|
||||
reported_at: r.reportedAt.toISOString(),
|
||||
sites: r.siteName ? { name: r.siteName } : null,
|
||||
zones: r.zoneName ? { name: r.zoneName } : null,
|
||||
trucks: r.truckNo ? { truck_no: r.truckNo } : null,
|
||||
reporter: r.reporterName ? { name: r.reporterName } : null,
|
||||
})),
|
||||
total: Number(countRow?.count ?? 0),
|
||||
counts: { all, open, closed: byStatus.get('closed') ?? 0 },
|
||||
sites: siteRows.map(s => ({ id: s.id, name: s.name })),
|
||||
trucks: truckRows.map(t => ({ id: t.id, truck_no: t.truckNo })),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
export type Incident = {
|
||||
id: string
|
||||
reference_no: string | null
|
||||
incident_type: string
|
||||
status: string
|
||||
severity: number | null
|
||||
reported_at: string
|
||||
sites: { name: string } | null
|
||||
zones: { name: string } | null
|
||||
trucks: { truck_no: string } | null
|
||||
reporter: { name: string } | null
|
||||
}
|
||||
|
||||
export interface SiteOption { id: string; name: string }
|
||||
export interface TruckOption { id: string; truck_no: string }
|
||||
Reference in New Issue
Block a user