wrolpi/app/src/components/inventory/InventoryExportPanel.js

61 lines
2.8 KiB
JavaScript
Raw Normal View History

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>
2026-06-19 11:00:01 -06:00
import React from "react";
import {Select} from "semantic-ui-react";
import {Button, Header, Icon, Segment} from "../Theme";
import {downloadCSV, inventoryExportFilename, toCSV} from "./inventoryExport";
import {groupFieldsOf, summableFieldsOf} from "./summarize";
const fieldOptions = (fs) => fs.map(f => ({key: f.key, value: f.key, text: f.label}));
/**
* The "Export" tab: explains each export format and provides the action to produce it. CSV is the raw items table,
* generated entirely in the browser. "PDF" opens the print dialog over the hidden printable view (see
* InventoryPrint), which includes a summary table grouped by the field chosen here.
*/
export function InventoryExportPanel({name, fields, items, groupKey, sumKey, onGroupKey, onSumKey}) {
const count = (items || []).length;
const groupFields = groupFieldsOf(fields);
const summableFields = summableFieldsOf(fields);
const exportCSV = () =>
downloadCSV(inventoryExportFilename(name, 'csv'), toCSV(fields, items));
return <>
<Segment>
<Header as='h3'><Icon name='file alternate outline'/> CSV</Header>
<p>
A comma-separated spreadsheet of all {count} item{count === 1 ? '' : 's'} and every field, openable
in Excel, Numbers, or Google Sheets. Generated entirely on this device.
</p>
<Button primary onClick={exportCSV} disabled={count === 0}>
<Icon name='download'/> Download CSV
</Button>
</Segment>
<Segment>
<Header as='h3'><Icon name='file pdf outline'/> PDF</Header>
<p>
A printable table of the whole inventory, followed by a summary grouped by the field you choose.
This opens your browser's print dialog — choose "Save as PDF" as the destination to get a PDF file,
or print it on paper.
</p>
{groupFields.length > 0 &&
<div style={{marginBottom: '1em', display: 'flex', gap: '1em', flexWrap: 'wrap'}}>
<span>
Summary group:{' '}
<Select options={fieldOptions(groupFields)} value={groupKey || ''}
onChange={(e, data) => onGroupKey(data.value)}/>
</span>
{summableFields.length > 0 &&
<span>
Sum:{' '}
<Select clearable options={fieldOptions(summableFields)} value={sumKey || ''}
onChange={(e, data) => onSumKey(data.value || undefined)}/>
</span>}
</div>}
<Button onClick={() => window.print()} disabled={count === 0}>
<Icon name='print'/> Print / Save as PDF
</Button>
</Segment>
</>;
}