Hub 2.13.0: clear alerts by hand, sortable billing tables, no stray tab scrollbars

- Alerts: Clear one or Clear all. A cleared alert leaves the open list and dashboard count
  but stays open to the checks, so it isn't re-raised or re-emailed until the problem goes
  away and comes back. Audited. Migration 20261123120000_alert_cleared.
- Billing invoices, payments, churches and a customer's invoices/payments can be sorted by
  column (remembered per browser). Invoices show the due date in its own column.
- Tabs: the grey rule is an inset shadow, so the active underline no longer overflows the
  row by a pixel and makes it scroll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 10:20:23 +02:00
co-authored by Claude Opus 5.5
parent 7ae95f737c
commit be27b4f5f0
15 changed files with 229 additions and 27 deletions
+29 -3
View File
@@ -2,29 +2,51 @@
import React, { useEffect, useState } from "react";
import Link from "next/link";
import { Check } from "lucide-react";
import { api, errorText } from "@/lib/api";
import { ago, fmtDate } from "@/lib/format";
import { useServerPaged } from "@/lib/paging";
import type { InstallEvent } from "@/lib/types";
import { Badge, Loading, PageTitle, Pager, SearchInput } from "@/components/ui";
import { Badge, Loading, Message, PageTitle, Pager, SearchInput } from "@/components/ui";
/** Open alerts, and the history of everything that happened to installs, a page at a time (Hub 2.6.0: paged on the server). */
export default function AlertsPage() {
const [openOnly, setOpenOnly] = useState(true);
const [query, setQuery] = useState("");
const [q, setQ] = useState("");
const [msg, setMsg] = useState<{ type: "ok" | "err"; text: string } | null>(null);
const [busy, setBusy] = useState(false);
useEffect(() => { const t = setTimeout(() => setQ(query.trim()), 300); return () => clearTimeout(t); }, [query]);
const paged = useServerPaged<InstallEvent>("/api/admin/events", { open: openOnly ? "true" : "", q }, r => r.events as InstallEvent[]);
const events = paged.rows;
const error = paged.error;
// Hub 2.13.0: clear an alert by hand. It comes back only if the problem goes away and returns.
const clear = async (path: string, done: (r: { cleared?: number }) => string) => {
setBusy(true);
try {
const r = await api<{ cleared?: number }>(path, { method: "POST" });
setMsg({ type: "ok", text: done(r) });
paged.reload();
} catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(false); }
};
const clearOne = (e: InstallEvent) => clear(`/api/admin/events/${e.id}/clear`, () => `Cleared: ${e.message}`);
const clearAll = () => {
if (!confirm(`Clear all ${paged.total} open alert${paged.total === 1 ? "" : "s"}? Each is raised again only if its problem goes away and comes back.`)) return;
clear("/api/admin/events/clear", r => `${r.cleared ?? 0} alert${r.cleared === 1 ? "" : "s"} cleared.`);
};
const isOpen = (e: InstallEvent) => e.level !== "info" && !e.resolvedAt && !e.clearedAt;
return (
<>
<PageTitle title="Alerts" subtitle="Checked every 5 minutes. New warnings and critical alerts are emailed once (Settings → Alerts → Alert email)." />
<PageTitle title="Alerts" subtitle="Checked every 5 minutes. New warnings and critical alerts are emailed once (Settings → Alerts → Alert email). A cleared alert stays away until its problem is fixed and comes back." />
<div className="mb-4 flex flex-wrap gap-2">
<button className={openOnly ? "btn-primary" : "btn-secondary"} onClick={() => setOpenOnly(true)}>Open</button>
<button className={!openOnly ? "btn-primary" : "btn-secondary"} onClick={() => setOpenOnly(false)}>Everything</button>
{openOnly && !q && paged.total > 1 && <button className="btn-secondary" disabled={busy} onClick={clearAll}><Check className="h-4 w-4" /> Clear all</button>}
<SearchInput className="w-full sm:ml-auto sm:w-72" value={query} onChange={setQuery} placeholder="Search alerts or install" />
</div>
{msg && <div className="mb-4"><Message msg={msg} /></div>}
{error && <p className="text-sm text-red-700">{error}</p>}
{!events ? <Loading /> : events.length === 0 && !q ? <p className="text-sm text-slate-500">{openOnly ? "No open alerts. 🎉" : "Nothing yet."}</p> : (
<div className="card overflow-x-auto p-0">
@@ -37,7 +59,11 @@ export default function AlertsPage() {
<td className="p-3 whitespace-nowrap text-xs text-slate-500">{fmtDate(e.createdAt)}</td>
<td className="p-3">{e.install ? <Link href={`/installs/${e.install.id}`} className="text-indigo-700 hover:underline">{e.install.name}</Link> : "—"}</td>
<td className="p-3"><Badge value={e.level} /> <span className="ml-1">{e.message}</span></td>
<td className="p-3 text-xs text-slate-500">{e.level === "info" ? "" : e.resolvedAt ? `resolved ${ago(e.resolvedAt)}` : "open"}</td>
<td className="p-3 whitespace-nowrap text-xs text-slate-500">
{isOpen(e)
? <span className="inline-flex items-center gap-3">open <button type="button" className="font-medium text-indigo-700 hover:underline disabled:opacity-50" disabled={busy} onClick={() => clearOne(e)} title="Clear this alert; it's raised again only if the problem goes away and comes back">Clear</button></span>
: e.level === "info" ? "" : e.resolvedAt ? `resolved ${ago(e.resolvedAt)}` : `cleared ${ago(e.clearedAt)}`}
</td>
</tr>
))}
</tbody>
+39 -8
View File
@@ -9,6 +9,7 @@ import { Loading, Message, PageTitle, Pager, SearchInput, Stat, Tabs } from "@/c
import { CYCLE, METHOD, StateBadge, day, money, todayYmd, type BillingCycle, type Invoice, type Payment } from "@/lib/billing";
import { RecordPayment } from "@/components/billing/RecordPayment";
import { PRODUCT_NAMES, byProduct, type Product } from "@/lib/products";
import { SortTh, useSort, type SortColumn } from "@/lib/sort";
type Msg = { type: "ok" | "err"; text: string } | null;
type Overview = { drafts: number; approved: number; owingCents: number; overdueCents: number; overdue: number; receivedThisMonthCents: number };
@@ -30,6 +31,31 @@ const FILTERS = [
["overdue", "Overdue"], ["paid", "Paid"], ["void", "Void"],
] as const;
// Hub 2.13.0: the columns each table can be sorted by (click a heading).
const INVOICE_SORT = {
number: { get: i => i.number, first: "desc" },
church: { get: i => i.customer?.name },
date: { get: i => i.issueDate, first: "desc" },
due: { get: i => i.dueDate, first: "desc" },
total: { get: i => i.totalCents, first: "desc" },
balance: { get: i => (i.status === "sent" ? i.balanceCents : null), first: "desc" },
state: { get: i => i.state },
} satisfies Record<string, SortColumn<Invoice>>;
const PAYMENT_SORT = {
receipt: { get: p => p.receiptNumber, first: "desc" },
church: { get: p => p.customer?.name },
received: { get: p => p.paidOn, first: "desc" },
invoice: { get: p => p.invoice?.number },
amount: { get: p => p.amountCents, first: "desc" },
sent: { get: p => p.receiptSentAt, first: "desc" },
} satisfies Record<string, SortColumn<Payment>>;
const ACCOUNT_SORT = {
church: { get: a => a.name },
founding: { get: a => (a.founding ? 1 : 0), first: "desc" },
billing: { get: a => (a.billingCycle === "none" ? null : a.nextInvoiceDate) },
owing: { get: a => a.balanceCents || null, first: "desc" },
} satisfies Record<string, SortColumn<Account>>;
/** Billing: invoices (drafted automatically, approved here), payments and receipts, churches and founding status. */
export default function BillingPage() {
const [tab, setTab] = useState<Tab>("invoices");
@@ -81,8 +107,12 @@ export default function BillingPage() {
const shownInvoices = useMemo(() => (invoices || []).filter(i => matches(query, i.number, i.customer?.name, i.plan, i.recipientEmail)), [invoices, query]);
const shownPayments = useMemo(() => (payments || []).filter(p => matches(query, p.receiptNumber, p.customer?.name, p.reference, p.invoice?.number)), [payments, query]);
const shownAccounts = useMemo(() => (accounts || []).filter(a => matches(query, a.name, a.billingEmail, ...a.licences.map(l => l.plan))), [accounts, query]);
const invPaged = usePaged(shownInvoices, `${query}|${filter}`, 25);
const payPaged = usePaged(shownPayments, query, 25);
const invSorted = useSort(shownInvoices, INVOICE_SORT, { key: "date", dir: "desc" }, "billing-invoices");
const paySorted = useSort(shownPayments, PAYMENT_SORT, { key: "received", dir: "desc" }, "billing-payments");
const accSorted = useSort(shownAccounts, ACCOUNT_SORT, { key: "church", dir: "asc" }, "billing-churches");
const invSort = invSorted.sort, paySort = paySorted.sort, accSort = accSorted.sort;
const invPaged = usePaged(invSorted.rows, `${query}|${filter}|${invSort.key}|${invSort.dir}`, 25);
const payPaged = usePaged(paySorted.rows, `${query}|${paySort.key}|${paySort.dir}`, 25);
const founding = (accounts || []).filter(a => a.founding).length;
return (
@@ -125,20 +155,21 @@ export default function BillingPage() {
{tab === "invoices" && (!invoices ? <Loading /> : (
<div className="card overflow-x-auto p-0">
<table className="w-full text-sm">
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><th className="p-3">Invoice</th><th className="p-3">Church</th><th className="p-3">Date</th><th className="p-3 text-right">Total</th><th className="p-3 text-right">Balance</th><th className="p-3">State</th><th className="p-3" /></tr></thead>
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><SortTh sort={invSort} k="number">Invoice</SortTh><SortTh sort={invSort} k="church">Church</SortTh><SortTh sort={invSort} k="date">Date</SortTh><SortTh sort={invSort} k="due">Due</SortTh><SortTh sort={invSort} k="total" right>Total</SortTh><SortTh sort={invSort} k="balance" right>Balance</SortTh><SortTh sort={invSort} k="state">State</SortTh><th className="p-3" /></tr></thead>
<tbody>
{invPaged.rows.map(i => (
<tr key={i.id} className="border-t border-slate-100">
<td className="p-3"><Link className="font-medium text-indigo-700 hover:underline" href={`/billing/invoices/${i.id}`}>{i.number}</Link>{i.planChanges.map(pc => <div key={`${pc.product}-${pc.to}`} className="text-xs text-amber-700">{pc.product && pc.product !== "events" ? `${PRODUCT_NAMES[pc.product]}: ` : ""}{pc.from} → {pc.to}</div>)}{i.warnings.length > 0 && i.status === "draft" && <div className="text-xs text-amber-700">Check {i.warnings.length === 1 ? "1 note" : `${i.warnings.length} notes`}</div>}</td>
<td className="p-3">{i.customer?.name}{i.customer?.founding && <Star className="ml-1 inline h-3.5 w-3.5 fill-amber-400 text-amber-400" aria-label="Founding church" />}<div className="text-xs text-slate-500">{i.plan || "One-off"}</div></td>
<td className="p-3 whitespace-nowrap">{day(i.issueDate)}<div className="text-xs text-slate-500">due {day(i.dueDate)}</div></td>
<td className="p-3 whitespace-nowrap">{day(i.issueDate)}</td>
<td className="p-3 whitespace-nowrap">{day(i.dueDate)}</td>
<td className="p-3 text-right tabular-nums">{money(i.totalCents)}</td>
<td className="p-3 text-right tabular-nums">{i.status === "sent" ? money(i.balanceCents) : "—"}</td>
<td className="p-3"><StateBadge state={i.state} short /></td>
<td className="p-3 text-right"><button className="text-slate-500 hover:text-indigo-700" title="Open the PDF" onClick={() => openPdf(`/api/admin/billing/invoices/${i.id}/pdf`).catch(e => setMsg({ type: "err", text: errorText(e) }))}><FileText className="h-4 w-4" /></button></td>
</tr>
))}
{shownInvoices.length === 0 && <tr><td colSpan={7} className="p-6 text-center text-slate-500">No invoices{filter || query ? " match" : " yet. Set up billing on a customer's licence and the drafts appear here 14 days before each invoice date"}.</td></tr>}
{shownInvoices.length === 0 && <tr><td colSpan={8} className="p-6 text-center text-slate-500">No invoices{filter || query ? " match" : " yet. Set up billing on a customer's licence and the drafts appear here 14 days before each invoice date"}.</td></tr>}
</tbody>
</table>
<div className="p-3"><Pager {...invPaged} /></div>
@@ -148,7 +179,7 @@ export default function BillingPage() {
{tab === "payments" && (!payments ? <Loading /> : (
<div className="card overflow-x-auto p-0">
<table className="w-full text-sm">
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><th className="p-3">Receipt</th><th className="p-3">Church</th><th className="p-3">Received</th><th className="p-3">For</th><th className="p-3 text-right">Amount</th><th className="p-3">Receipt sent</th><th className="p-3" /></tr></thead>
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><SortTh sort={paySort} k="receipt">Receipt</SortTh><SortTh sort={paySort} k="church">Church</SortTh><SortTh sort={paySort} k="received">Received</SortTh><SortTh sort={paySort} k="invoice">For</SortTh><SortTh sort={paySort} k="amount" right>Amount</SortTh><SortTh sort={paySort} k="sent">Receipt sent</SortTh><th className="p-3" /></tr></thead>
<tbody>
{payPaged.rows.map(p => (
<tr key={p.id} className="border-t border-slate-100">
@@ -173,9 +204,9 @@ export default function BillingPage() {
<p className="mb-3 text-sm text-slate-600">{founding} founding church{founding === 1 ? "" : "es"}. Founding churches pay the founding price (35% less) on every tier for as long as they stay. Set billing up on the church&apos;s page: one invoice covers all its products.</p>
<div className="card overflow-x-auto p-0">
<table className="w-full text-sm">
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><th className="p-3">Church</th><th className="p-3">Founding</th><th className="p-3">Products</th><th className="p-3">Billing</th><th className="p-3 text-right">Owing</th></tr></thead>
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><SortTh sort={accSort} k="church">Church</SortTh><SortTh sort={accSort} k="founding">Founding</SortTh><th className="p-3">Products</th><SortTh sort={accSort} k="billing">Billing</SortTh><SortTh sort={accSort} k="owing" right>Owing</SortTh></tr></thead>
<tbody>
{shownAccounts.map(a => (
{accSorted.rows.map(a => (
<tr key={a.id} className="border-t border-slate-100 align-top">
<td className="p-3"><Link className="font-medium text-indigo-700 hover:underline" href={`/customers/${a.id}#billing`}>{a.name}</Link><div className="text-xs text-slate-500">{a.billingEmail || ""}</div></td>
<td className="p-3">
@@ -267,7 +267,7 @@ export default function InstallPage() {
<span className="w-40 shrink-0 text-xs text-slate-500">{fmtDate(e.createdAt)}</span>
<Badge value={e.level} />
<span>{e.message}</span>
{e.level !== "info" && <span className="text-xs text-slate-400">{e.resolvedAt ? `resolved ${ago(e.resolvedAt)}` : "open"}</span>}
{e.level !== "info" && <span className="text-xs text-slate-400">{e.resolvedAt ? `resolved ${ago(e.resolvedAt)}` : e.clearedAt ? `cleared ${ago(e.clearedAt)}` : "open"}</span>}
</li>
))}
</ul>