Files

60 lines
1.8 KiB
TypeScript

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}` : ''
}