Add CSV/PDF export and a search filter to Inventory

New Export tab with two client-side formats (no new dependencies):
- CSV: the raw items table (RFC-4180 quoting, UTF-8 BOM for Excel), generated
  entirely in the browser.  The download anchor is attached to the DOM and the
  object URL is revoked on the next tick for cross-browser robustness.
- PDF: a printable full-inventory table followed by a group-by summary table,
  via the browser's print dialog ("Save as PDF").  The summary's grouping is
  chosen in the Export tab.

Search filter: a box on its own line under the inventory selector filters the
active inventory across every column's displayed value (case-insensitive;
whitespace-separated terms are AND-ed).  It narrows the whole view — Items
table, Summary aggregation, and Export/PDF — while edits/adds in the table
still operate on the full inventory, so filtering never drops data.

Summary/export grouping:
- The group-by aggregation is extracted into a shared summarize.js (field-role
  detection + count-aware totals via units.js + calories), now the single
  source of truth for the Summary tab, the PDF summary, and the ration
  calculator.  findCaloriesKey/findCountKey moved here from RationCalculator
  (re-exported for back-compat).
- The Sum selector now offers number and calories fields in addition to
  quantity: number/calories are summed as a plain column total, while quantity
  stays count-aware and unit-compacted and the dedicated Calories column stays
  count-aware.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-06-19 11:00:01 -06:00
parent cc5afe3fbc
commit 947ee01b25
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
13 changed files with 835 additions and 102 deletions

View file

@ -48,7 +48,7 @@ function compareByField(a, b, field) {
* inventory (the page holds every inventory and saves the changed one). A persistent "new item" row sits at the
* bottom: Tab moves between fields and Enter submits the item then refocuses the first field.
*/
export function InventoryTable({slug, fields, items, locations, catalog, onChange}) {
export function InventoryTable({slug, fields, items, locations, catalog, search, onChange}) {
const [draft, setDraft] = useState(() => emptyItem(fields));
const [edits, setEdits] = useState({}); // itemId -> edited copy
const [selected, setSelected] = useState(new Set());
@ -120,6 +120,9 @@ export function InventoryTable({slug, fields, items, locations, catalog, onChang
return sort.dir === 'desc' ? sorted.reverse() : sorted;
}, [items, fields, sort]);
// The search filters which rows are shown; mutations below still operate on the full `items` prop.
const visibleItems = useMemo(() => filterItems(displayItems, fields, search), [displayItems, fields, search]);
const focusFirst = () => {
const node = firstInputRef.current;
if (node && node.focus) {
@ -213,7 +216,7 @@ export function InventoryTable({slug, fields, items, locations, catalog, onChang
</TableRow>
</TableHeader>
<TableBody>
{displayItems.map(item => {
{visibleItems.map(item => {
const editing = edits[item.id];
const expired = isExpired(item);
return <TableRow key={item.id} negative={expired}>
@ -239,6 +242,12 @@ export function InventoryTable({slug, fields, items, locations, catalog, onChang
</TableCell>)}
</TableRow>;
})}
{search && visibleItems.length === 0 && (items || []).length > 0 &&
<TableRow>
<TableCell colSpan={sortedFields.length + 1} style={{opacity: 0.7}}>
No items match "{search}".
</TableCell>
</TableRow>}
</TableBody>
<TableFooter>
{/* The persistent new-item entry row. */}
@ -289,3 +298,21 @@ export function isItemExpired(item, fields) {
return !isNaN(t) && t < start.getTime();
});
}
/**
* Filter items by a free-text search across every column's displayed value (using the same formatting as the
* table, so quantities match as "30 lb", dates as "2030-01-01", etc.). Case-insensitive; whitespace-separated
* terms are AND-ed. A blank search returns all items. Shared by the Items table, Summary, and export views.
*/
export function filterItems(items, fields, search) {
const query = (search || '').trim().toLowerCase();
if (!query) {
return items || [];
}
const terms = query.split(/\s+/);
const cols = [...(fields || [])].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
return (items || []).filter(item => {
const haystack = cols.map(f => formatValue(item, f)).join(' ').toLowerCase();
return terms.every(term => haystack.includes(term));
});
}