mams_frontend/src/features/view/headers.tsx

74 lines
3.9 KiB
TypeScript

import type { ReactNode } from 'react';
import { Avatar } from '@mui/material';
import { PlateBadge } from '../../components/PlateBadge';
export interface Header {
title: string;
subtitle?: string;
lead?: ReactNode;
status?: string;
}
type Data = Record<string, unknown>;
const s = (v: unknown) => String(v ?? '').trim();
const join = (...parts: unknown[]) => parts.map(s).filter(Boolean).join(' · ');
/** Entity-specific page headers; anything not listed falls back to the entity caption. */
/** label(field, code): the translated dropdown label of a field value, as the form shows it */
type Label = (field: string, code: unknown) => string;
/**
* The plate as the user reads it ("5759 و ك"): the backend's ready-made grid text when the record has one, else the number
* with its code, whatever the screen calls the fields. Blacklist records keep it as "<code> <number>" in ENTITY_VALUE.
*/
export function plateOf(d: Data, rtl: boolean): string {
const L = rtl ? 'AR' : 'EN';
const O = rtl ? 'EN' : 'AR';
const ready = [`PLATE_NUMBER_GRID_${L}`, `FLEET_PLATE_NUMBER_GRID_${L}`, `PLATE_NO_GRID_${L}`, `PLATE_NUMBER_GRID_${O}`, `FLEET_PLATE_NUMBER_GRID_${O}`, `PLATE_NO_GRID_${O}`]
.map((k) => s(d[k]))
.find(Boolean);
if (ready) return ready;
const num = s(d.PLATE_NUMBER) || s(d.FLEET_PLATE_NUMBER);
const code = [`PLATE_CODE_${L}`, `FLEET_PLATE_CODE_${L}`, 'PLATE_CODE', 'PLATE_CODE_AR', 'PLATE_CODE_EN', 'FLEET_PLATE_CODE_AR'].map((k) => s(d[k])).find(Boolean) ?? '';
if (num) return `${num} ${code}`.trim();
return s(d.ENTITY_VALUE);
}
/** Vehicle records and the records about a vehicle lead with the plate badge instead of a name or an id. */
const plateHeader = (d: Data, rtl: boolean, fallback: string): Header | null => {
const plate = plateOf(d, rtl);
return plate ? { lead: <PlateBadge text={plate} />, title: fallback, status: s(d.STATUS_CODE) } : null;
};
const PLATE_ENTITIES = ['RFID_TAGS', 'TRAILERS', 'EQUIPMENTS', 'BUSES', 'CARS', 'AFFILIATION', 'RENTED_TRUCK', 'SUBSCRIPTIONS', 'TROUBLE_SHOOTING', 'SANCTIONS', 'VIOLATIONS', 'BLACKLISTS', 'PARKING_YARD', 'FLEET_PENDING_FINANCIAL_TRANSACTIONS'];
const HEADERS: Record<string, (d: Data, rtl: boolean, label: Label) => Header> = {
TRUCKS: (d, rtl, label) => ({
lead: <PlateBadge text={s(rtl ? d.PLATE_NUMBER_GRID_AR : d.PLATE_NUMBER_GRID_EN) || s(d.PLATE_NUMBER)} />,
title: s(d.OWNER_NAME) || s(d.COMPANY_NAME),
subtitle: join(label('TRUCK_TYPE', d.TRUCK_TYPE), label('USE_DESCRIPTION', d.USE_DESCRIPTION)),
status: s(d.STATUS_CODE),
}),
// the request number identifies it; the company name on these records is often "unknown"
FLEET_REQUEST: (d) => ({ title: s(d.FLEET_REQUEST_NUMBER), status: s(d.STATUS_CODE) }),
USERS: (d) => ({
lead: <Avatar sx={{ width: 56, height: 56, bgcolor: 'primary.main' }}>{s(d.NAME).slice(0, 1).toUpperCase()}</Avatar>,
title: s(d.NAME),
subtitle: join(d.EMAIL, d.MOBILE),
status: s(d.STATUS_CODE),
}),
};
// for entities without a custom header: the first name-like value the record has
const NAME_KEYS = ['NAME', 'NAME_AR', 'FULL_NAME', 'COMPANY_NAME', 'OWNER_NAME', 'SUBJECT', 'TITLE', 'LETTER_TITLE', 'PLATE_NUMBER', 'PERMIT_NUMBER', 'VIOLATION_NUM', 'CODE'];
/** Page titles that differ from the module's menu caption. */
const VIEW_TITLE: Record<string, { en: string; ar: string }> = { DEPOSITS: { en: 'Deposit request', ar: 'طلب الإيداع' } };
export const getHeader = (entity: string, d: Data, rtl: boolean, label: Label, fallbackTitle: string): Header => {
const fallback = VIEW_TITLE[entity] ? (rtl ? VIEW_TITLE[entity].ar : VIEW_TITLE[entity].en) : fallbackTitle;
const custom = HEADERS[entity]?.(d, rtl, label) ?? (PLATE_ENTITIES.includes(entity) ? plateHeader(d, rtl, fallback) : null);
if (custom) return custom;
const name = NAME_KEYS.map((k) => s(d[k])).find(Boolean);
return { title: name || fallback, subtitle: name ? fallback : undefined, status: s(d.STATUS_CODE) };
};