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 | null = null; const run = () => { timer = null; root.querySelectorAll("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); }; }, []); }