import { useEffect, useRef, useState } from 'react' import { useNavigate, useSearchParams } from 'react-router-dom' import { Badge, Button, DataTable, EmptyState, ErrorState, FilterChips, PageHeader, Skeleton, Toolbar } from '@sims/ui' import { getClients, CLIENT_STATUSES, type ClientStatus, type DocStatus } from '../api' import { ClientFormDialog } from '../components/ClientFormDialog' /** Tiny fetch-render hook shared by the HQ pages (same shape as backoffice live.tsx). */ export function useData(loader: () => Promise, deps: unknown[] = []): { data?: T; error?: string; reload: () => void } { const [data, setData] = useState() const [error, setError] = useState() const seqRef = useRef(0) const reload = () => { setError(undefined) const mySeq = ++seqRef.current loader() .then((d) => { if (seqRef.current === mySeq) setData(d) }) .catch((e: Error) => { if (seqRef.current === mySeq) setError(e.message) }) } // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(reload, deps) return { data, error, reload } } type Tone = 'ok' | 'warn' | 'err' | 'accent' export const CLIENT_TONE: Record = { lead: 'accent', active: 'ok', dormant: 'warn', lost: 'err', } export const DOC_TONE: Record = { draft: 'warn', sent: 'accent', accepted: 'ok', invoiced: 'accent', part_paid: 'warn', paid: 'ok', lost: 'err', cancelled: 'err', } /** Client registry — search, list, inline create; row click opens the Client 360°. */ export function Clients() { const nav = useNavigate() const [q, setQ] = useState('') // D18 WS-F: the old book's two working filters, applied server-side. const [district, setDistrict] = useState('') const [sector, setSector] = useState('') const { data, error, reload } = useData( () => getClients(q, { district, sector }), [q, district, sector], ) const [sp, setSp] = useSearchParams() const [creating, setCreating] = useState(sp.get('new') === '1') const [statusFilter, setStatusFilter] = useState('all') useEffect(() => { if (sp.get('new') === '1') { setCreating(true); setSp({}, { replace: true }) } }, [sp, setSp]) const counts = new Map() for (const c of data ?? []) counts.set(c.status, (counts.get(c.status) ?? 0) + 1) const shown = (data ?? []).filter((c) => statusFilter === 'all' || c.status === statusFilter) const why = [ q !== '' ? ` matching “${q}”` : '', statusFilter !== 'all' ? ` with status ${statusFilter}` : '', ].filter((x) => x !== '').join('') return (
setCreating((v) => !v)}>New client} /> setQ(e.target.value)} /> setDistrict(e.target.value)} /> setSector(e.target.value)} /> ({ key: s, label: s, count: counts.get(s) ?? 0 })), ]} /> {shown.length} shown setCreating(false)} onSaved={(c) => { reload(); nav(`/clients/${c.id}`) }} /> {error !== undefined && } {data === undefined && error === undefined ? : shown.length === 0 ? No clients{why !== '' ? why : ' yet — add the first one'}. : ( nav(`/clients/${shown[i]!.id}`)} rows={shown.map((c) => ({ code: c.code, name: c.name, status: {c.status}, state: c.stateCode, contact: c.contacts[0]?.email ?? c.contacts[0]?.phone ?? '—', }))} /> )}
) }