Files
Homelab-manager/web/src/hooks/useStackedTables.ts
T
bobbanandClaude Sonnet 5.5 9965d94036 Phone card layout, status symbols, breadcrumbs, integration cards and server trends
Tables stack into labelled cards on a phone. Green, red and amber badges carry
a check, cross or triangle as well as their colour. Pages inside a menu group
show a breadcrumb, with the thing's name on a server's page. Integrations are
cards with an Open button and a live status (working, failing, not checked yet,
disabled). Agent-reported servers keep a short history of load, memory and disk
use (migration 0019, one sample per ten minutes for 35 days) and show it as a
Trends card with a days-until-full forecast for a filling disk.

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

55 lines
2.1 KiB
TypeScript

import { useEffect } from "react";
const SORT_GLYPHS = /[↕↑↓▲▼⇅⇵]/g;
/** Writes each column's heading onto its cells (data-label), so the phone layout can show "Status: running" beside each value. */
function labelTable(table: HTMLTableElement): void {
const headRow = table.tHead?.rows[table.tHead.rows.length - 1];
if (!headRow) return;
const labels: string[] = [];
for (const th of Array.from(headRow.cells)) {
const text = (th.textContent ?? "").replace(SORT_GLYPHS, "").replace(/\s+/g, " ").trim();
for (let i = 0; i < Math.max(1, th.colSpan); i++) labels.push(text);
}
for (const body of Array.from(table.tBodies)) {
for (const row of Array.from(body.rows)) {
let col = 0;
for (const cell of Array.from(row.cells)) {
const label = cell.colSpan > 1 ? "" : (labels[col] ?? "");
if (label) {
if (cell.getAttribute("data-label") !== label) cell.setAttribute("data-label", label);
} else if (cell.hasAttribute("data-label")) {
cell.removeAttribute("data-label");
}
col += Math.max(1, cell.colSpan);
}
}
}
}
/**
* On a phone the wide tables are drawn as a stack of cards (see styles.css), which needs every cell to know its column's
* heading. This keeps that up to date as pages load, sort, filter and page — without each table having to do it.
*/
export function useStackedTables(): void {
useEffect(() => {
const root = document.querySelector(".page-body");
if (!root) return;
// A short timer rather than an animation frame: frames stop while the tab is in the background, and the labels should not.
let timer: ReturnType<typeof setTimeout> | null = null;
const run = () => {
timer = null;
root.querySelectorAll<HTMLTableElement>("table.card-table").forEach(labelTable);
};
const observer = new MutationObserver(() => {
if (!timer) timer = setTimeout(run, 30);
});
observer.observe(root, { childList: true, subtree: true });
run();
return () => {
observer.disconnect();
if (timer) clearTimeout(timer);
};
}, []);
}