import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { formatINR } from '@sims/domain' import { Badge, DataTable, EmptyState, ErrorState, FilterChips, PageHeader, Skeleton, Toolbar } from '@sims/ui' import { getDocuments, DOC_STATUSES, type DocStatus, type DocType } from '../api' import { useData, DOC_TONE } from './Clients' const PAGE_SIZE = 50 const TYPE_LABEL: Record = { QUOTATION: 'Quotation', PROFORMA: 'Proforma', INVOICE: 'Invoice', CREDIT_NOTE: 'Credit note', RECEIPT: 'Receipt', } const TYPE_CHIPS: { key: string; label: string }[] = [ { key: 'all', label: 'All' }, { key: 'QUOTATION', label: 'Quotations' }, { key: 'PROFORMA', label: 'Proformas' }, { key: 'INVOICE', label: 'Invoices' }, { key: 'CREDIT_NOTE', label: 'Credit notes' }, { key: 'RECEIPT', label: 'Receipts' }, ] /** * Documents register (go-live cluster WS-D): every document across all clients, * newest first, server-paginated with an honest total (rule 8). Type chips and * the status filter compose server-side; a row opens the document view. */ export function Documents() { const nav = useNavigate() const [type, setType] = useState('all') const [status, setStatus] = useState('') const [page, setPage] = useState(1) const list = useData( () => getDocuments({ ...(type !== 'all' ? { type: type as DocType } : {}), ...(status !== '' ? { status: status as DocStatus } : {}), page, pageSize: PAGE_SIZE, }), [type, status, page], ) const data = list.data const docs = data?.documents const totalPages = data !== undefined ? Math.max(1, Math.ceil(data.total / data.pageSize)) : 1 const from = data !== undefined && data.total > 0 ? (data.page - 1) * data.pageSize + 1 : 0 const to = data !== undefined ? Math.min(data.page * data.pageSize, data.total) : 0 const why = [ type !== 'all' ? ` of type ${TYPE_LABEL[type as DocType].toLowerCase()}` : '', status !== '' ? ` with status ${status}` : '', ].filter((x) => x !== '').join('') return (
{ setType(k); setPage(1) }} /> {data !== undefined && {data.total} total} {list.error !== undefined && } {list.error !== undefined ? null : docs === undefined ? : docs.length === 0 ? No documents{why !== '' ? why : ' yet'}. : ( <> nav(`/documents/${docs[i]!.id}`)} rows={docs.map((d) => ({ no: d.docNo ?? '— draft —', type: TYPE_LABEL[d.docType], client: d.clientName, date: d.docDate, status: {d.status}, amount: formatINR(d.payablePaise), }))} /> Showing {from}–{to} of {data!.total} Page {data!.page} / {totalPages} )}
) }