Add saved filter views, a customisable dashboard, server comparison, row keyboard navigation and share links

- Table search, status, tag and sort live in the address; Save view pins one as a favourite.
- Dashboard cards can be moved and hidden per user (users.dashboard_layout, migration 0022).
- Compare two to four servers' load, memory or disk on one chart.
- j/k move through table rows that open a detail panel.
- Share links open a read-only wall without signing in, numbers only or with names
  (share_links, migration 0023); the wall is split into a shared view over two data sources.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-16 22:15:29 +02:00
1 parent 906220040e
commit 44e393f172
50 files changed
+6114 -258

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
import { useSyncExternalStore } from "react";
import { api, type DashboardLayout } from "../api/client";
import { readableError } from "./errors";
import { toast } from "./toast";
// How this person arranged the dashboard cards. It is kept on their user record on the server, so it follows them to any browser;
// here it is kept in step so every card, and the Customise bar, see the same layout at once.
const EMPTY: DashboardLayout = { hidden: [], order: {} };
/** Where a card goes when nothing has been said about it: after every card that has been placed, in the order it was drawn. */
export const UNPLACED = 1000;
interface State {
layout: DashboardLayout;
/** The person is arranging the cards: hidden ones show (dimmed) and each card has its buttons. */
editing: boolean;
}
let state: State = { layout: EMPTY, editing: false };
const listeners = new Set<() => void>();
/** Only the answer to the latest change may overwrite the layout, however the replies are ordered. */
let latest = 0;
function set(next: State) {
state = next;
for (const l of listeners) l();
}
function subscribe(listener: () => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
/** Loads the layout the server sent with the signed-in user. */
export function initDashboardLayout(layout: DashboardLayout | undefined): void {
set({ ...state, layout: layout ? { hidden: [...layout.hidden], order: { ...layout.order } } : EMPTY });
}
export function useDashboardLayout(): State {
return useSyncExternalStore(subscribe, () => state);
}
export function setEditingDashboard(editing: boolean): void {
set({ ...state, editing });
}
/** Shown at once; put back, with a message, if the server can't save it. */
async function save(next: DashboardLayout): Promise<void> {
const before = state.layout;
set({ ...state, layout: next });
const mine = ++latest;
try {
const res = await api.me.setDashboard(next);
if (mine === latest) set({ ...state, layout: res.dashboard });
} catch (err) {
if (mine === latest) {
set({ ...state, layout: before });
toast.error(`Couldn't save the dashboard layout: ${readableError(err)}`);
}
}
}
export function setCardHidden(label: string, hidden: boolean): Promise<void> {
const { layout } = state;
const rest = layout.hidden.filter((l) => l !== label);
return save({ ...layout, hidden: hidden ? [...rest, label] : rest });
}
/**
* Moves a card one place earlier or later among the cards beside it (the ones in the same row). Every card of that row is given its
* place at the same time — a card is only ever swapped with its neighbour — so a row is either left as drawn or fully placed.
*/
export function moveCard(card: HTMLElement, direction: -1 | 1): Promise<void> {
const row = card.closest(".row");
if (!row) return Promise.resolve();
const cards = [...row.children].filter((el): el is HTMLElement => el instanceof HTMLElement && el.dataset.card !== undefined);
const placed = cards
.map((el, index) => ({ label: el.dataset.card!, index }))
.sort((a, b) => (state.layout.order[a.label] ?? UNPLACED) - (state.layout.order[b.label] ?? UNPLACED) || a.index - b.index)
.map((c) => c.label);
const at = placed.indexOf(card.dataset.card!);
const to = at + direction;
if (at === -1 || to < 0 || to >= placed.length) return Promise.resolve();
[placed[at], placed[to]] = [placed[to], placed[at]];
const order = { ...state.layout.order };
placed.forEach((label, i) => (order[label] = i));
return save({ ...state.layout, order });
}
export function resetDashboardLayout(): Promise<void> {
return save(EMPTY);
}
+52
View File
@@ -0,0 +1,52 @@
// A "saved view" is a page with its filters, pinned like a favourite: "/servers?status=offline". The address is the whole of it — the
// search words, the status or tag chosen, the sort — so a view can also be bookmarked or sent to someone, and it needs no storage of its own.
/** The address parts that narrow or order a table. Anything else in an address (an id to edit, say) is not part of a view. */
const VIEW_PARAMS = ["q", "status", "tag", "sort", "dir"] as const;
/** Only these make a page worth saving as a view: sorting alone does, a lone `dir` doesn't. */
const NARROWING = new Set(["q", "status", "tag", "sort"]);
/** "/servers?q=elsa" → ["/servers", "?q=elsa"]; a plain page has an empty second part. */
export function splitView(path: string): [string, string] {
const at = path.indexOf("?");
return at === -1 ? [path, ""] : [path.slice(0, at), path.slice(at)];
}
export function isSavedView(path: string): boolean {
return path.includes("?");
}
/** The part of an address that makes up a view, in a fixed order, so the same view always comes out as the same text. */
export function canonicalSearch(search: string): string {
const from = new URLSearchParams(search);
const to = new URLSearchParams();
for (const key of VIEW_PARAMS) for (const value of from.getAll(key)) if (value !== "") to.append(key, value);
const text = to.toString();
return text ? `?${text}` : "";
}
/** Whether an address has anything that narrows or orders the table, i.e. is worth saving. */
export function hasViewParams(search: string): boolean {
const from = new URLSearchParams(search);
return [...from.keys()].some((k) => NARROWING.has(k) && from.get(k) !== "");
}
const STATUS_WORDS: Record<string, string> = { offline: "not reporting", online: "reporting", failing: "failing", working: "working", disabled: "disabled" };
/** A short phrase for the menu: `“elsa” · not reporting · tag backup · by name ↓`. */
export function describeView(search: string): string {
const p = new URLSearchParams(search);
const parts: string[] = [];
const q = p.get("q");
if (q) parts.push(`“${q}”`);
const status = p.get("status");
if (status) parts.push(STATUS_WORDS[status] ?? status);
for (const t of p.getAll("tag")) parts.push(`tag ${t}`);
const sort = p.get("sort");
if (sort) parts.push(`by ${sort.replace(/([A-Z])/g, " $1").toLowerCase()} ${p.get("dir") === "desc" ? "↓" : "↑"}`);
return parts.join(" · ");
}
/** The longest view that can be pinned: the same limit the server keeps. */
export const MAX_VIEW_QUERY = 160;