"use client"; /* eslint-disable @next/next/no-img-element */ import { Check, CircleAlert, Download, Search, Send } from "lucide-react"; import Link from "next/link"; import { useMemo, useState } from "react"; import { parseAmount } from "@/lib/consistency"; import { useDecisions, type Decision } from "@/lib/decisions"; import type { ReceiptRecord } from "@/lib/types"; import { Banner, Button, Card, Pill, Segmented } from "./ui"; type Filter = "all" | "accepted" | "review" | "mine"; type Status = "accepted" | "review" | "approved" | "sent"; const STATUS: Record = { accepted: { label: "Auto-accepted", tone: "good", icon: }, review: { label: "Needs review", tone: "warn", icon: }, approved: { label: "Approved by you", tone: "good", icon: }, sent: { label: "Sent to review", tone: "warn", icon: }, }; function statusOf(r: ReceiptRecord, d?: Decision): Status { if (d) return d.status === "approved" ? "approved" : "sent"; return r.flagged ? "review" : "accepted"; } export function QueueTable({ receipts }: { receipts: ReceiptRecord[] }) { const { decisions } = useDecisions(); const [filter, setFilter] = useState("all"); const [query, setQuery] = useState(""); const rows = useMemo( () => receipts.map((r) => { const d = decisions[r.id]; const reading = d?.edited ?? r.pred; const passed = r.checks.filter((c) => c.passed).length; return { r, status: statusOf(r, d), items: reading?.items?.length ?? 0, total: reading?.total ?? "", names: (reading?.items ?? []).map((it) => it.name ?? "").join(" ").toLowerCase(), rules: r.checks.length ? `${passed}/${r.checks.length}` : "–", rulesOk: passed === r.checks.length, }; }), [receipts, decisions], ); const counts = { accepted: rows.filter((x) => x.status === "accepted").length, review: rows.filter((x) => x.status === "review").length, mine: rows.filter((x) => x.status === "approved" || x.status === "sent").length, }; const shown = rows.filter((x) => { if (filter === "accepted" && x.status !== "accepted") return false; if (filter === "review" && x.status !== "review") return false; if (filter === "mine" && !(x.status === "approved" || x.status === "sent")) return false; const q = query.trim().toLowerCase(); return !q || x.names.includes(q) || `#${x.r.id}` === q || String(x.r.id) === q; }); const exportCsv = () => { const header = ["receipt", "status", "total", "total_number", "items", "rules_passed", "matches_human_label"]; const lines = shown.map((x) => [x.r.id, STATUS[x.status].label, `"${x.total.replace(/"/g, '""')}"`, parseAmount(x.total) ?? "", x.items, x.rules, x.r.exact ? "yes" : "no"].join(","), ); const blob = new Blob([[header.join(","), ...lines].join("\n")], { type: "text/csv" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `sumproof-receipts-${filter}.csv`; a.click(); URL.revokeObjectURL(url); }; return ( <> {counts.accepted} auto-accepted {counts.review} need review {counts.mine > 0 && {counts.mine} decided by you} } meta="The 100 sample receipts as SumProof read them. Only the ones whose numbers do not add up need a person to check." right={ } />
{shown.map((x) => { const s = STATUS[x.status]; const href = `/?sample=${x.r.id}`; const first = (x.r.pred?.items ?? []).slice(0, 2).map((it) => it.name).filter(Boolean).join(", "); return ( ); })} {shown.length === 0 && ( )}
Receipt First items Items Total Rules vs human label Status
#{x.r.id} {first || "–"} {x.items} {x.total || "–"} {x.rules} {x.r.exact ? matches : differs} {s.icon} {s.label}
No receipts match this filter.

"vs human label" compares every field with CORD's annotation; most differences are item-name spellings, not amounts. Your decisions are saved in this browser only.

); }