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>
This commit is contained in:
1 parent
92726568b7
commit
846af02851
50 files changed
+4434
-233
No files matched your search
@@ -30,6 +30,84 @@ export function warrantyStatus(warrantyUntil: string | null, now: Date = new Dat
|
||||
return days <= warnDays ? "expiring" : "ok";
|
||||
}
|
||||
|
||||
// ─── Lifecycle planning ─────────────────────────────────────────────────────
|
||||
|
||||
export interface LifecycleItem {
|
||||
id: number;
|
||||
name: string;
|
||||
kind: string;
|
||||
/** YYYY-MM-DD — when it ages out. */
|
||||
endsOn: string;
|
||||
/** What that date is: the end of support the maker gives (or you set), or, with none set, the end of the warranty. */
|
||||
basis: "support" | "warranty";
|
||||
daysLeft: number;
|
||||
/** What replacing it is expected to cost, and where that figure comes from; null when there is none. */
|
||||
cost: number | null;
|
||||
costBasis: "replacement" | "purchase" | null;
|
||||
}
|
||||
|
||||
export interface LifecycleYear {
|
||||
year: number;
|
||||
items: LifecycleItem[];
|
||||
/** The sum of the figures that exist. */
|
||||
totalCost: number;
|
||||
/** How many have no figure, so the total is understood to be at least that. */
|
||||
unpriced: number;
|
||||
}
|
||||
|
||||
export interface LifecycleReport {
|
||||
/** Already past their date. */
|
||||
past: LifecycleItem[];
|
||||
/** The years from this one on. */
|
||||
years: LifecycleYear[];
|
||||
/** Further off than the years shown. */
|
||||
later: LifecycleItem[];
|
||||
/** With no date at all: no end of support and no warranty. */
|
||||
undated: { id: number; name: string; kind: string }[];
|
||||
}
|
||||
|
||||
type LifecycleSource = { id: number; name: string; kind: string; endOfSupport: string | null; warrantyUntil: string | null; replacementCost: number | null; purchasePrice: number | null };
|
||||
|
||||
/**
|
||||
* What ages out when, for planning what to replace. The date is the end of support where there is one, otherwise the end of the warranty (said
|
||||
* so, since that is only a stand-in); the cost is the replacement cost where there is one, otherwise what it was bought for. Things already past
|
||||
* their date, those in each of the next `yearsAhead` calendar years, those later, and those with no date at all are kept apart.
|
||||
*/
|
||||
export function lifecycleReport(assets: LifecycleSource[], now: Date = new Date(), yearsAhead = 5): LifecycleReport {
|
||||
const thisYear = now.getUTCFullYear();
|
||||
const years: LifecycleYear[] = Array.from({ length: yearsAhead }, (_, i) => ({ year: thisYear + i, items: [], totalCost: 0, unpriced: 0 }));
|
||||
const past: LifecycleItem[] = [];
|
||||
const later: LifecycleItem[] = [];
|
||||
const undated: LifecycleReport["undated"] = [];
|
||||
for (const a of assets) {
|
||||
const support = a.endOfSupport && isRealDate(a.endOfSupport) ? a.endOfSupport : null;
|
||||
const warranty = a.warrantyUntil && isRealDate(a.warrantyUntil) ? a.warrantyUntil : null;
|
||||
const endsOn = support ?? warranty;
|
||||
if (!endsOn) {
|
||||
undated.push({ id: a.id, name: a.name, kind: a.kind });
|
||||
continue;
|
||||
}
|
||||
const cost = a.replacementCost ?? a.purchasePrice ?? null;
|
||||
const item: LifecycleItem = { id: a.id, name: a.name, kind: a.kind, endsOn, basis: support ? "support" : "warranty", daysLeft: warrantyDaysLeft(endsOn, now) ?? 0, cost, costBasis: a.replacementCost !== null ? "replacement" : a.purchasePrice !== null ? "purchase" : null };
|
||||
if (item.daysLeft < 0) past.push(item);
|
||||
else {
|
||||
const bucket = years.find((y) => y.year === Number(endsOn.slice(0, 4)));
|
||||
if (bucket) bucket.items.push(item);
|
||||
else later.push(item);
|
||||
}
|
||||
}
|
||||
const byDate = (x: LifecycleItem, y: LifecycleItem) => x.endsOn.localeCompare(y.endsOn) || x.name.localeCompare(y.name);
|
||||
past.sort(byDate);
|
||||
later.sort(byDate);
|
||||
for (const y of years) {
|
||||
y.items.sort(byDate);
|
||||
y.totalCost = Math.round(y.items.reduce((n, i) => n + (i.cost ?? 0), 0));
|
||||
y.unpriced = y.items.filter((i) => i.cost === null).length;
|
||||
}
|
||||
undated.sort((a, b) => a.name.localeCompare(b.name));
|
||||
return { past, years, later, undated };
|
||||
}
|
||||
|
||||
/** Kilowatt-hours a steady draw uses in a year. */
|
||||
export function yearlyKwh(watts: number): number {
|
||||
return Math.round((watts / 1000) * 24 * 365 * 100) / 100;
|
||||
|
||||
@@ -185,11 +185,16 @@ export async function buildDigest(now: Date = new Date()): Promise<Digest> {
|
||||
expiring.push({ daysLeft: d.daysLeft, text: d.daysLeft < 0 ? `Domain ${d.name} expired ${plural(Math.abs(d.daysLeft), "day")} ago` : `Domain ${d.name} expires in ${plural(d.daysLeft, "day")} (${d.expiresAt})` });
|
||||
}
|
||||
// A warranty about to end is worth knowing while there is still time to claim on it; one that ended long ago is not news.
|
||||
for (const a of await db.select({ name: assets.name, warrantyUntil: assets.warrantyUntil }).from(assets)) {
|
||||
for (const a of await db.select({ name: assets.name, warrantyUntil: assets.warrantyUntil, endOfSupport: assets.endOfSupport }).from(assets)) {
|
||||
const days = warrantyDaysLeft(a.warrantyUntil, now);
|
||||
if (days !== null && days >= -30 && days <= 60) {
|
||||
expiring.push({ daysLeft: days, text: days < 0 ? `Warranty on ${a.name} ended ${plural(Math.abs(days), "day")} ago` : `Warranty on ${a.name} ends in ${plural(days, "day")} (${a.warrantyUntil})` });
|
||||
}
|
||||
// The end of support gives more notice: it is when something has to be replaced, not claimed on.
|
||||
const supportDays = warrantyDaysLeft(a.endOfSupport, now);
|
||||
if (supportDays !== null && supportDays >= -30 && supportDays <= 90) {
|
||||
expiring.push({ daysLeft: supportDays, text: supportDays < 0 ? `Support for ${a.name} ended ${plural(Math.abs(supportDays), "day")} ago` : `Support for ${a.name} ends in ${plural(supportDays, "day")} (${a.endOfSupport})` });
|
||||
}
|
||||
}
|
||||
if (expiring.length > 0) {
|
||||
expiring.sort((a, b) => a.daysLeft - b.daysLeft);
|
||||
|
||||
Reference in new issue
Block a user