File size: 8,339 Bytes
0c52aa7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | "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<Status, { label: string; tone: "good" | "warn"; icon: React.ReactNode }> = {
accepted: { label: "Auto-accepted", tone: "good", icon: <Check size={13} /> },
review: { label: "Needs review", tone: "warn", icon: <CircleAlert size={13} /> },
approved: { label: "Approved by you", tone: "good", icon: <Check size={13} /> },
sent: { label: "Sent to review", tone: "warn", icon: <Send size={12} /> },
};
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<Filter>("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 (
<>
<Banner
crumbs={[{ label: "SumProof", href: "/" }, { label: "Receipt inbox" }]}
title="Receipt inbox"
pills={
<>
<Pill tone="good">{counts.accepted} auto-accepted</Pill>
<Pill tone="warn">{counts.review} need review</Pill>
{counts.mine > 0 && <Pill tone="dark">{counts.mine} decided by you</Pill>}
</>
}
meta="The 100 sample receipts as SumProof read them. Only the ones whose numbers do not add up need a person to check."
right={
<Button tone="accent" onClick={exportCsv}>
<Download size={16} /> Export CSV
</Button>
}
/>
<Card>
<div className="mb-4 flex flex-wrap items-center gap-3">
<Segmented
label="Filter receipts"
value={filter}
onChange={setFilter}
options={[
{ value: "all", label: `All (${rows.length})` },
{ value: "accepted", label: `Auto-accepted (${counts.accepted})` },
{ value: "review", label: `Needs review (${counts.review})` },
{ value: "mine", label: `Decided by you (${counts.mine})` },
]}
/>
<label htmlFor="queue-search" className="relative ml-auto w-full sm:w-64">
<span className="sr-only">Search item names or receipt number</span>
<Search size={16} className="pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-muted" />
<input
id="queue-search"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Item name or #id"
className="h-10 w-full rounded-xl border border-line bg-surface-solid pr-3 pl-9 text-[13.5px] text-ink focus:border-accent focus:outline-none"
/>
</label>
</div>
<div className="relative overflow-x-auto rounded-xl border border-line bg-surface-solid">
<table className="w-full min-w-[720px] text-[13.5px]">
<thead>
<tr className="border-b border-line text-left text-[11.5px] tracking-wide text-muted uppercase">
<th className="px-3 py-2.5 font-semibold">Receipt</th>
<th className="px-3 py-2.5 font-semibold">First items</th>
<th className="px-3 py-2.5 text-right font-semibold">Items</th>
<th className="px-3 py-2.5 text-right font-semibold">Total</th>
<th className="px-3 py-2.5 text-right font-semibold">Rules</th>
<th className="px-3 py-2.5 font-semibold">vs human label</th>
<th className="px-3 py-2.5 font-semibold">Status</th>
</tr>
</thead>
<tbody className="tabular">
{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 (
<tr key={x.r.id} className="border-b border-line transition-colors last:border-0 hover:bg-sunken">
<td className="px-3 py-2">
<Link href={href} className="flex items-center gap-3 font-semibold text-ink hover:text-accent-deep">
<img src={`/data/${x.r.image}`} alt="" loading="lazy" className="h-12 w-9 rounded-md border border-line object-cover object-top" />
#{x.r.id}
</Link>
</td>
<td className="max-w-[260px] truncate px-3 py-2 text-muted">{first || "–"}</td>
<td className="px-3 py-2 text-right">{x.items}</td>
<td className="px-3 py-2 text-right font-semibold">{x.total || "–"}</td>
<td className={`px-3 py-2 text-right font-semibold ${x.rulesOk ? "text-good" : "text-warn"}`}>{x.rules}</td>
<td className="px-3 py-2">{x.r.exact ? <Pill tone="good">matches</Pill> : <Pill>differs</Pill>}</td>
<td className="px-3 py-2">
<Link href={href}>
<Pill tone={s.tone}>
{s.icon} {s.label}
</Pill>
</Link>
</td>
</tr>
);
})}
{shown.length === 0 && (
<tr>
<td colSpan={7} className="px-3 py-8 text-center text-muted">
No receipts match this filter.
</td>
</tr>
)}
</tbody>
</table>
</div>
<p className="mt-3 text-[12.5px] text-muted">
"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.
</p>
</Card>
</>
);
}
|