wrolpi/app/src/components/Calculators.js

66 lines
2.8 KiB
JavaScript
Raw Normal View History

import React from "react";
2024-06-25 17:05:41 -06:00
import {Breadcrumbs, useTitle} from "./Common";
import {Button, Icon} from "./Theme";
import {useCalcQuery} from "../hooks/customHooks";
2024-06-20 09:51:29 -06:00
import {TemperatureCalculator} from "./calculators/TemperatureCalculator";
2024-06-25 17:05:41 -06:00
import {ElectricalCalculators} from "./calculators/ElectricalCalculators";
2024-06-20 09:51:29 -06:00
import {DipoleAntennaCalculator} from "./calculators/HamCalculators";
import {Link} from "react-router";
import {RatioCalculators} from "./calculators/RatioCalculator";
import {QRCodeCalculator} from "./calculators/QRCodeCalculator";
import {DriveCalculator} from "./calculators/DriveCalculator";
import {WaterCalculator} from "./calculators/WaterCalculator";
Rebuild Inventory as a config-only system with food catalog and calculators Replace the DB-backed inventory module with a config-only design and build out food-storage tooling around it. Inventory (config-only, no database) - Each inventory is a YAML file at config/inventory/<slug>.yaml, loaded once into memory and the single source of truth. New reusable MultiFileConfig (wrolpi/common.py) manages a directory of per-entity files. - Inventory types (food/fuel/tool) seed a customizable field schema (text/number/quantity/date/select/location/calories). Unit math/aggregation moved to the frontend (mathjs); Pint dropped from the backend. - Minimal API: GET / (all inventories full), POST /, PUT /<slug> (whole- inventory save with version-based optimistic concurrency; 404 for missing), DELETE. Legacy inventories.yaml / old tables migrate to per-inventory files (pound->lb), and an alembic migration drops the tables. Frontend - New app/src/components/inventory/ package: spreadsheet-style table with Tab/Enter keyboard entry, sortable columns, expired-row highlighting, customizable field editor, and pooled cross-inventory location autocomplete. - A single useInventories store loads everything once and persists whole inventories optimistically; list/items/summary/locations derive client-side. - Summary groups items (default by Category), totals size x count, shows a calories column, and embeds a ration estimate (how long the stock lasts). Food catalog - Shared, editable catalog (config/inventory/catalog.yaml) seeded from catalog_defaults.yaml (~60 generic items, total calories per package) with merge-keep-edits + tombstones. GET/PUT /api/inventory/catalog, a management modal, and entry-row pre-fill of category/size/calories from a name match. Calculators - Generic Food Storage planner (like the Water calculator): household + editable per-person/year amounts -> total to store over a chosen duration, with women/children weighted by DGA calorie shares and an imperial/metric toggle. Also fixes ConfigFile.save() to accept nested file_names. Backend pytest and frontend jest suites updated/added throughout; alembic check clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 14:20:10 -06:00
import {FoodStorageCalculator} from "./calculators/FoodStorageCalculator";
export const useCalculators = () => {
const [calc, setCalc] = useCalcQuery();
const calculators = [
{key: 'ratio', icon: 'th large', button: 'Ratio', contents: <RatioCalculators/>},
2024-06-25 17:05:41 -06:00
{key: 'electrical', icon: 'lightning', button: 'Electrical', contents: <ElectricalCalculators/>},
{key: 'drive', icon: 'cogs', button: 'Drive Ratio', contents: <DriveCalculator/>},
{key: 'water', icon: 'tint', button: 'Water Storage', contents: <WaterCalculator/>},
Rebuild Inventory as a config-only system with food catalog and calculators Replace the DB-backed inventory module with a config-only design and build out food-storage tooling around it. Inventory (config-only, no database) - Each inventory is a YAML file at config/inventory/<slug>.yaml, loaded once into memory and the single source of truth. New reusable MultiFileConfig (wrolpi/common.py) manages a directory of per-entity files. - Inventory types (food/fuel/tool) seed a customizable field schema (text/number/quantity/date/select/location/calories). Unit math/aggregation moved to the frontend (mathjs); Pint dropped from the backend. - Minimal API: GET / (all inventories full), POST /, PUT /<slug> (whole- inventory save with version-based optimistic concurrency; 404 for missing), DELETE. Legacy inventories.yaml / old tables migrate to per-inventory files (pound->lb), and an alembic migration drops the tables. Frontend - New app/src/components/inventory/ package: spreadsheet-style table with Tab/Enter keyboard entry, sortable columns, expired-row highlighting, customizable field editor, and pooled cross-inventory location autocomplete. - A single useInventories store loads everything once and persists whole inventories optimistically; list/items/summary/locations derive client-side. - Summary groups items (default by Category), totals size x count, shows a calories column, and embeds a ration estimate (how long the stock lasts). Food catalog - Shared, editable catalog (config/inventory/catalog.yaml) seeded from catalog_defaults.yaml (~60 generic items, total calories per package) with merge-keep-edits + tombstones. GET/PUT /api/inventory/catalog, a management modal, and entry-row pre-fill of category/size/calories from a name match. Calculators - Generic Food Storage planner (like the Water calculator): household + editable per-person/year amounts -> total to store over a chosen duration, with women/children weighted by DGA calorie shares and an imperial/metric toggle. Also fixes ConfigFile.save() to accept nested file_names. Backend pytest and frontend jest suites updated/added throughout; alembic check clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 14:20:10 -06:00
{key: 'ration', icon: 'food', button: 'Food Storage', contents: <FoodStorageCalculator/>},
2024-06-20 09:51:29 -06:00
{key: 'antenna', icon: 'signal', button: 'Antenna', contents: <DipoleAntennaCalculator/>},
2024-06-25 17:05:41 -06:00
{key: 'temperature', icon: 'thermometer', button: 'Temperature', contents: <TemperatureCalculator/>},
{key: 'qrCode', icon: 'qrcode', button: 'QR Code', contents: <QRCodeCalculator/>},
];
const activeCalculator = calc ? calculators.find(i => i.key === calc) : null;
const calculatorLinks = calculators.map(i => {
const {button, icon, key} = i;
2024-06-25 17:05:41 -06:00
return <Link key={key} to={`/more/calculators?calc=${key}`}>
<Button key={key} style={{margin: '0.5em'}}>
2024-06-25 17:05:41 -06:00
{icon && <Icon name={icon}/>}
{button}
</Button>
</Link>
});
return {calc, calculators, activeCalculator, calculatorLinks}
}
export function CalculatorsPage() {
const {activeCalculator, calculatorLinks} = useCalculators();
const {icon, button, title, contents} = activeCalculator || {};
const name = activeCalculator ? title || button + ' Calculator' : 'Calculators';
useTitle(name);
const body = activeCalculator ? <span>{contents}</span> : calculatorLinks;
2024-06-25 17:05:41 -06:00
// Make first breadcrumb a link back to Calculators only when a calculator is active.
const crumbs = activeCalculator
? [{text: 'Calculators', link: '/more/calculators'}, {text: name, icon: icon}]
: [{text: 'Calculators'}];
return <>
2024-06-25 17:05:41 -06:00
<Breadcrumbs crumbs={crumbs} size='huge'/>
<div style={{marginTop: '1em'}}>
{body}
</div>
</>
}