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:
@@ -0,0 +1,74 @@
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import { ArrowDown, ArrowUp, ArrowUpDown } from "lucide-react";
|
||||
|
||||
export type SortDir = "asc" | "desc";
|
||||
type Value = string | number | boolean | null | undefined;
|
||||
/** How to read one column's value, and which way it sorts when first picked (default: A–Z / smallest first). */
|
||||
export type SortColumn<T> = { get: (row: T) => Value; first?: SortDir };
|
||||
export type Sort<K extends string> = { key: K; dir: SortDir; toggle: (k: K) => void; set: (k: K, dir: SortDir) => void };
|
||||
|
||||
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
||||
|
||||
/** Empty values always go last, whichever way the column is sorted. */
|
||||
function compare(a: Value, b: Value, dir: SortDir) {
|
||||
const emptyA = a == null || a === "";
|
||||
const emptyB = b == null || b === "";
|
||||
if (emptyA || emptyB) return emptyA === emptyB ? 0 : emptyA ? 1 : -1;
|
||||
const n = typeof a === "string" && typeof b === "string" ? collator.compare(a, b) : Number(a) - Number(b);
|
||||
return dir === "asc" ? n : -n;
|
||||
}
|
||||
|
||||
function remembered<K extends string>(storageKey: string | undefined, keys: K[]): { key: K; dir: SortDir } | null {
|
||||
if (!storageKey) return null;
|
||||
try {
|
||||
const saved = JSON.parse(localStorage.getItem(`sort:${storageKey}`) || "null");
|
||||
if (saved && keys.includes(saved.key) && (saved.dir === "asc" || saved.dir === "desc")) return saved;
|
||||
} catch { /* private window or blocked storage: use the default */ }
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A list sorted by the column the user picked (Hub 2.13.0). Clicking the same column again turns it
|
||||
* round. With `storageKey` the choice is remembered in this browser.
|
||||
*/
|
||||
export function useSort<T, K extends string>(items: T[], columns: Record<K, SortColumn<T>>, initial: { key: NoInfer<K>; dir: SortDir }, storageKey?: string) {
|
||||
const [state, setState] = useState(initial);
|
||||
useEffect(() => {
|
||||
const saved = remembered(storageKey, Object.keys(columns) as K[]);
|
||||
if (saved) setState(saved);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- read once; `columns` is a fixed table
|
||||
}, [storageKey]);
|
||||
const set = (key: K, dir: SortDir) => {
|
||||
setState({ key, dir });
|
||||
if (storageKey) try { localStorage.setItem(`sort:${storageKey}`, JSON.stringify({ key, dir })); } catch { /* not remembered */ }
|
||||
};
|
||||
const toggle = (key: K) => set(key, key === state.key ? (state.dir === "asc" ? "desc" : "asc") : columns[key].first || "asc");
|
||||
const column = columns[state.key];
|
||||
// A stable sort, so rows that tie keep the server's order (newest first).
|
||||
const rows = useMemo(() => [...items].sort((a, b) => compare(column.get(a), column.get(b), state.dir)), [items, column, state.dir]);
|
||||
const sort: Sort<K> = { key: state.key, dir: state.dir, toggle, set };
|
||||
return { rows, sort };
|
||||
}
|
||||
|
||||
/** A table heading that sorts by its column when clicked. */
|
||||
export function SortTh<K extends string>({ sort, k, children, className = "p-3", right }: { sort: Sort<K>; k: K; children: React.ReactNode; className?: string; right?: boolean }) {
|
||||
const active = sort.key === k;
|
||||
const Icon = !active ? ArrowUpDown : sort.dir === "asc" ? ArrowUp : ArrowDown;
|
||||
return (
|
||||
<th className={`${className} ${right ? "text-right" : ""}`} aria-sort={active ? (sort.dir === "asc" ? "ascending" : "descending") : undefined}>
|
||||
<button type="button" onClick={() => sort.toggle(k)} className={`inline-flex items-center gap-1 whitespace-nowrap hover:text-slate-900 ${active ? "text-slate-700" : ""} ${right ? "flex-row-reverse" : ""}`}>
|
||||
{children}
|
||||
<Icon className={`h-3 w-3 ${active ? "" : "opacity-40"}`} aria-hidden="true" />
|
||||
</button>
|
||||
</th>
|
||||
);
|
||||
}
|
||||
|
||||
/** The same choice as a drop-down, for lists that aren't tables. `options` are [column, direction, label]. */
|
||||
export function SortSelect<K extends string>({ sort, options, className = "" }: { sort: Sort<K>; options: [K, SortDir, string][]; className?: string }) {
|
||||
return (
|
||||
<select className={`input w-auto py-1 text-xs ${className}`} aria-label="Sort by" value={`${sort.key}:${sort.dir}`} onChange={e => { const [k, d] = e.target.value.split(":"); sort.set(k as K, d as SortDir); }}>
|
||||
{options.map(([k, d, label]) => <option key={`${k}:${d}`} value={`${k}:${d}`}>{label}</option>)}
|
||||
</select>
|
||||
);
|
||||
}
|
||||
@@ -76,7 +76,7 @@ export type Stats = {
|
||||
linkedProducts?: number;
|
||||
};
|
||||
|
||||
export type InstallEvent = { id: string; type: string; level: Problem["level"]; message: string; createdAt: string; resolvedAt: string | null; install?: { id: string; name: string; url: string } };
|
||||
export type InstallEvent = { id: string; type: string; level: Problem["level"]; message: string; createdAt: string; resolvedAt: string | null; clearedAt?: string | null; install?: { id: string; name: string; url: string } };
|
||||
|
||||
// AI (Cross Events 2.0). Amounts are in cents (rand), with fractions.
|
||||
export type AiStatus = { enabled: boolean; capReached: boolean; capCents: number; usedCents: number; usedPercent: number; renewsOn: string };
|
||||
|
||||
Reference in New Issue
Block a user