Files
Homelab-manager/web/src/pages/InventoryRacks.tsx
T
bobbanandClaude Sonnet 5.5 18ceed62e8 Racks, notes and to-do, OS support, suggested dependencies, Nginx Proxy Manager and Home Assistant
- Racks page and a network diagram (UniFi tree with servers, printable)
- Notes on servers and devices; To do list shown in Coming up and My work
- Agents report the operating system (Linux 7, Windows 6); end-of-life badge and alert
- Suggested dependencies from where a service is reached
- Nginx Proxy Manager integration: proxy hosts page, certificate and host alerts, suggested checks
- Home Assistant integration: watch chosen entities, history chart, limit and unavailable alerts
- TimeChart gets a "value" scale that fits negatives; docs updated

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 21:47:56 +02:00

141 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { api } from "../api/client";
import EmptyState from "../components/EmptyState";
import { ErrorNotice } from "../components/Notice";
import { BlockSkeleton } from "../components/Skeleton";
import { readableError } from "../utils/errors";
import { KIND_LABELS } from "../utils/inventory";
type Rack = Awaited<ReturnType<typeof api.inventory.racks>>["racks"][number];
const UNIT_H = 24;
const KIND_COLOR: Record<string, string> = { server: "#4299e1", storage: "#38b2ac", network: "#48bb78", power: "#f59f00", client: "#a78bfa", other: "#8896ab" };
/** One rack, from the bottom (unit 1) up, as it stands: each piece where it says it sits, empty units between, a clash in red. */
function RackDrawing({ rack }: { rack: Rack }) {
const height = rack.units * UNIT_H;
const yOf = (unit: number) => height - unit * UNIT_H; // top edge of a unit
return (
<div className="rack-drawing">
<svg viewBox={`0 0 260 ${height + 4}`} width="100%" role="img" aria-label={`${rack.name}: ${rack.slots.length} pieces in ${rack.units} units${rack.slots.some((s) => s.clash) ? ", with a clash" : ""}`} style={{ maxWidth: 300 }}>
<rect x="26" y="1" width="228" height={height + 2} fill="none" stroke="currentColor" strokeOpacity="0.5" strokeWidth="2" rx="3" />
{Array.from({ length: rack.units }, (_, i) => i + 1).map((u) => (
<g key={u}>
<line x1="26" x2="254" y1={yOf(u) + 1} y2={yOf(u) + 1} stroke="currentColor" strokeOpacity="0.1" />
<text x="22" y={yOf(u) + UNIT_H / 2 + 4} textAnchor="end" fontSize="10" fill="currentColor" fillOpacity="0.55">
{u}
</text>
</g>
))}
{rack.slots.map((s) => {
const h = (s.to - s.from + 1) * UNIT_H;
const y = yOf(s.to);
return (
<g key={s.assetId}>
<title>{`${s.name}${s.model ? ` — ${s.model}` : ""} (${KIND_LABELS[s.kind as keyof typeof KIND_LABELS] ?? s.kind}, ${s.from === s.to ? `unit ${s.from}` : `units ${s.from}–${s.to}`}${s.powerWatts !== null ? `, ${s.powerWatts} W` : ""})${s.clash ? " — another piece is in the same unit" : ""}`}</title>
<rect x="29" y={y + 2} width="222" height={h - 3} rx="3" fill={KIND_COLOR[s.kind] ?? KIND_COLOR.other} fillOpacity="0.28" stroke={s.clash ? "#f03e3e" : KIND_COLOR[s.kind] ?? KIND_COLOR.other} strokeWidth={s.clash ? 2.5 : 1.5} />
<text x="38" y={y + Math.min(h, UNIT_H) / 2 + 4} fontSize="12" fill="currentColor" fontWeight="600">
{s.name.length > 30 ? `${s.name.slice(0, 29)}…` : s.name}
</text>
{h >= UNIT_H * 2 && s.model && (
<text x="38" y={y + UNIT_H / 2 + 20} fontSize="10.5" fill="currentColor" fillOpacity="0.75">
{s.model.length > 34 ? `${s.model.slice(0, 33)}…` : s.model}
</text>
)}
</g>
);
})}
</svg>
</div>
);
}
/**
* The racks, drawn from the inventory: each piece of hardware that says which unit it sits in (U12, or U12-13) and where (the rack's name in its location). Edit a
* piece's location and unit on the Inventory page and it moves here. Print it for the door of the cabinet.
*/
export default function InventoryRacks() {
const [racks, setRacks] = useState<Rack[] | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
api.inventory
.racks()
.then((res) => setRacks(res.racks))
.catch((err) => setError(readableError(err)));
}, []);
return (
<>
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
<h2 className="page-title mb-0">Racks</h2>
<div className="ms-auto btn-list no-print">
<button type="button" className="btn btn-outline-secondary" onClick={() => window.print()} disabled={!racks || racks.length === 0}>
Print
</button>
<Link to="/inventory" className="btn btn-outline-secondary">
Hardware
</Link>
</div>
</div>
<div className="text-secondary mb-3 no-print">
Each piece of hardware that gives a unit (&ldquo;U12&rdquo;, or &ldquo;U12-13&rdquo; for a tall one) and the rack&rsquo;s name as its location is drawn where it sits, from the bottom up. Two pieces in the same unit are shown in red.
</div>
<ErrorNotice error={error} />
{racks === null && !error && <BlockSkeleton lines={4} />}
{racks !== null && racks.length === 0 && (
<div className="card">
<EmptyState title="No racks to draw yet" hint={<>On the <Link to="/inventory">Hardware</Link> page, give a piece a location (the rack&rsquo;s name) and a rack unit such as U12, and it appears here.</>} />
</div>
)}
<div className="row row-cards">
{racks?.map((rack) => (
<div className="col-md-6 col-xl-4" key={rack.name}>
<div className="card h-100">
<div className="card-header">
<h3 className="card-title">{rack.name}</h3>
<div className="card-actions text-secondary small">
{rack.slots.length} {rack.slots.length === 1 ? "piece" : "pieces"} · {rack.usedUnits} of {rack.units} units{rack.watts > 0 ? ` · ${rack.watts} W` : ""}
</div>
</div>
<div className="card-body">
<RackDrawing rack={rack} />
{rack.slots.some((s) => s.clash) && (
<div className="alert alert-danger mt-3 mb-0">
Clash: {rack.slots.filter((s) => s.clash).map((s) => `${s.name} (${s.from === s.to ? `U${s.from}` : `U${s.from}-${s.to}`})`).join(", ")} claim the same unit. Correct one of them on the Hardware page.
</div>
)}
{rack.unplaced.length > 0 && (
<div className="mt-3 small">
<span className="text-secondary">Named here but not in a unit: </span>
{rack.unplaced.map((u, i) => (
<span key={u.assetId}>
{i > 0 && ", "}
<Link to={`/inventory?asset=${u.assetId}`}>{u.name}</Link>
</span>
))}
</div>
)}
<ul className="list-unstyled small mt-3 mb-0 d-print-none">
{[...rack.slots].reverse().map((s) => (
<li key={s.assetId}>
<span className="text-secondary">{s.from === s.to ? `U${s.from}` : `U${s.from}–${s.to}`}</span> <Link to={`/inventory?asset=${s.assetId}`}>{s.name}</Link>
{s.serverId !== null && (
<>
{" "}
· <Link to={`/servers/${s.serverId}`}>server</Link>
</>
)}
</li>
))}
</ul>
</div>
</div>
</div>
))}
</div>
</>
);
}