import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { api, type LifecycleItem, type LifecycleReport } from "../api/client"; import EmptyState from "../components/EmptyState"; import { ErrorNotice } from "../components/Notice"; import { BlockSkeleton } from "../components/Skeleton"; import { downloadCsv } from "../utils/csv"; import { readableError } from "../utils/errors"; import { KIND_LABELS, money } from "../utils/inventory"; import type { AssetKind } from "../api/client"; const YEARS = [3, 5, 7, 10]; const kindLabel = (k: string) => KIND_LABELS[k as AssetKind] ?? k; function Items({ items, currency, showYear }: { items: LifecycleItem[]; currency: string; showYear?: boolean }) { return (
{items.map((i) => ( ))}
Hardware {showYear ? "Date" : "Ages out"} Because Replacement
{i.name} {kindLabel(i.kind)} {i.endsOn} · {i.daysLeft < 0 ? `${-i.daysLeft} d ago` : i.daysLeft === 0 ? "today" : `in ${i.daysLeft} d`} {i.basis === "support" ? "end of support" : "warranty ends (no end of support set)"} {i.cost === null ? ( — ) : ( <> {money(i.cost, currency)} {i.costBasis === "purchase" && (bought for)} )}
); } /** * What ages out, year by year, for planning what to replace and roughly what it will cost. The date is the end of support where there is one (the * warranty's end stands in where there is not, and says so); the cost is the replacement cost, or what it was bought for. */ export default function InventoryLifecycle() { const [years, setYears] = useState(5); const [report, setReport] = useState(null); const [error, setError] = useState(null); useEffect(() => { setReport(null); api.inventory .lifecycle(years) .then((r) => { setReport(r); setError(null); }) .catch((err) => setError(readableError(err))); }, [years]); const currency = report?.currency ?? ""; const dated = report ? report.past.length + report.later.length + report.years.reduce((n, y) => n + y.items.length, 0) : 0; function exportCsv() { if (!report) return; const rows: (string | number)[][] = []; const add = (group: string, i: LifecycleItem) => rows.push([group, i.name, kindLabel(i.kind), i.endsOn, i.basis === "support" ? "end of support" : "warranty", i.cost ?? "", i.costBasis ?? ""]); report.past.forEach((i) => add("Already past", i)); for (const y of report.years) y.items.forEach((i) => add(String(y.year), i)); report.later.forEach((i) => add("Later", i)); report.undated.forEach((u) => rows.push(["No date", u.name, kindLabel(u.kind), "", "", "", ""])); downloadCsv("lifecycle.csv", ["When", "Hardware", "Kind", "Ages out", "Because", "Replacement cost", "Cost is"], rows); } return ( <>

Lifecycle

Back to the inventory

What ages out, year by year, and roughly what replacing it will cost. Give a piece of hardware an end of support and a replacement cost in the inventory; until it has an end of support, the end of its warranty stands in, and until it has a replacement cost, what it was bought for.

{!report && !error && } {report && dated === 0 && (
)} {report && report.past.length > 0 && (

Already past ({report.past.length})

)} {report && dated > 0 && report.years.map((y) => (

{y.year} ({y.items.length})

{y.items.length === 0 ? ( "nothing ages out" ) : ( <> about {money(y.totalCost, currency)} {y.unpriced > 0 && ` and ${y.unpriced} with no figure`} )}
{y.items.length > 0 && }
))} {report && report.later.length > 0 && (

Later ({report.later.length})

)} {report && dated > 0 && report.undated.length > 0 && (

No date yet ({report.undated.length})

These have neither an end of support nor a warranty end, so they are not in the plan.
{report.undated.map((u) => ( {u.name} ))}
)} ); }