Files
Homelab-manager/web/src/pages/InventoryLifecycle.tsx
T
bobbanandClaude Sonnet 5.5 846af02851 Lifecycle planning, and consistent empty and loading states
- Inventory: end of support and replacement cost per asset; a Lifecycle page showing what ages out year by year and what replacing it will cost; support ending within 90 days in the weekly digest
- Lists: a shared empty state saying what is missing, why, and the one thing to do; "nothing matches" with a clear button; skeleton rows instead of empty-looking lists while loading

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-18 23:18:42 +02:00

189 lines
7.7 KiB
TypeScript

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 (
<div className="table-responsive">
<table className="table table-vcenter card-table table-stack">
<thead>
<tr>
<th>Hardware</th>
<th>{showYear ? "Date" : "Ages out"}</th>
<th>Because</th>
<th className="text-end">Replacement</th>
</tr>
</thead>
<tbody>
{items.map((i) => (
<tr key={i.id}>
<td className="stack-wide">
<Link to={`/inventory?asset=${i.id}`}>{i.name}</Link> <span className="text-secondary small">{kindLabel(i.kind)}</span>
</td>
<td>
{i.endsOn}
<span className="text-secondary small"> · {i.daysLeft < 0 ? `${-i.daysLeft} d ago` : i.daysLeft === 0 ? "today" : `in ${i.daysLeft} d`}</span>
</td>
<td className="text-secondary">{i.basis === "support" ? "end of support" : "warranty ends (no end of support set)"}</td>
<td className="text-end stack-label" data-label="Replacement">
{i.cost === null ? (
<span className="text-secondary">—</span>
) : (
<>
{money(i.cost, currency)}
{i.costBasis === "purchase" && <span className="text-secondary small" title="No replacement cost is set, so what it was bought for stands in"> (bought for)</span>}
</>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
/**
* 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<LifecycleReport | null>(null);
const [error, setError] = useState<string | null>(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 (
<>
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
<h2 className="page-title mb-0">Lifecycle</h2>
<div className="ms-auto d-flex gap-2">
<select className="form-select w-auto" aria-label="How many years to show" value={years} onChange={(e) => setYears(Number(e.target.value))}>
{YEARS.map((y) => (
<option key={y} value={y}>
Next {y} years
</option>
))}
</select>
<button className="btn btn-outline-secondary" onClick={exportCsv} disabled={!report}>
Export CSV
</button>
<Link to="/inventory" className="btn">
Back to the inventory
</Link>
</div>
</div>
<p className="text-secondary">
What ages out, year by year, and roughly what replacing it will cost. Give a piece of hardware an <strong>end of support</strong> and a <strong>replacement cost</strong> 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.
</p>
<ErrorNotice error={error} />
{!report && !error && <BlockSkeleton lines={5} />}
{report && dated === 0 && (
<div className="card">
<EmptyState
title="Nothing has a date yet"
hint="Edit a piece of hardware and give it an end of support (or a warranty end), and what ages out is listed here by year, with what replacing it will cost."
action={{ label: "Go to the inventory", to: "/inventory" }}
/>
</div>
)}
{report && report.past.length > 0 && (
<div className="card mb-3 border-red">
<div className="card-header">
<h3 className="card-title text-red">Already past ({report.past.length})</h3>
</div>
<Items items={report.past} currency={currency} showYear />
</div>
)}
{report &&
dated > 0 &&
report.years.map((y) => (
<div className="card mb-3" key={y.year}>
<div className="card-header">
<h3 className="card-title">
{y.year} <span className="text-secondary fw-normal">({y.items.length})</span>
</h3>
<div className="card-actions text-secondary">
{y.items.length === 0 ? (
"nothing ages out"
) : (
<>
about <strong className="text-body">{money(y.totalCost, currency)}</strong>
{y.unpriced > 0 && ` and ${y.unpriced} with no figure`}
</>
)}
</div>
</div>
{y.items.length > 0 && <Items items={y.items} currency={currency} showYear />}
</div>
))}
{report && report.later.length > 0 && (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Later ({report.later.length})</h3>
</div>
<Items items={report.later} currency={currency} showYear />
</div>
)}
{report && dated > 0 && report.undated.length > 0 && (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">No date yet ({report.undated.length})</h3>
</div>
<div className="card-body">
<div className="text-secondary small mb-2">These have neither an end of support nor a warranty end, so they are not in the plan.</div>
<div className="d-flex flex-wrap gap-2">
{report.undated.map((u) => (
<Link key={u.id} to={`/inventory?asset=${u.id}`} className="badge bg-secondary-lt text-secondary">
{u.name}
</Link>
))}
</div>
</div>
</div>
)}
</>
);
}