New integration kinds adguard (status, statistics, filter lists and a real DNS question over UDP; alerts for DNS down/not running, protection left off, stale lists) and grafana (alert rules firing or failing, data-source health). Optional warning for running Docker images built more than N days ago (off by default). A server's overview cards can be folded and arranged, kept per user. Migrations 0075-0077; docs, metrics and tests included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
94 lines
3.7 KiB
TypeScript
94 lines
3.7 KiB
TypeScript
import { useSyncExternalStore } from "react";
|
|
import { api, type PageLayout } from "../api/client";
|
|
import { readableError } from "./errors";
|
|
import { toast } from "./toast";
|
|
|
|
// How this person arranged a server's overview page: which cards they folded and in what order the blocks come. It is kept on their user record on the
|
|
// server, so it follows them to any browser, and is the same for every server; here it is kept in step so every block, and the Arrange bar, see the same layout.
|
|
|
|
const EMPTY: PageLayout = { folded: [], order: {} };
|
|
|
|
interface State {
|
|
layout: PageLayout;
|
|
/** The person is arranging: each block shows 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 initServerPageLayout(layout: PageLayout | undefined): void {
|
|
set({ ...state, layout: layout ? { folded: [...layout.folded], order: { ...layout.order } } : EMPTY });
|
|
}
|
|
|
|
export function useServerPageLayout(): State {
|
|
return useSyncExternalStore(subscribe, () => state);
|
|
}
|
|
|
|
export function setEditingServerPage(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: PageLayout): Promise<void> {
|
|
const before = state.layout;
|
|
set({ ...state, layout: next });
|
|
const mine = ++latest;
|
|
try {
|
|
const res = await api.me.setServerPage(next);
|
|
if (mine === latest) set({ ...state, layout: res.serverPage });
|
|
} catch (err) {
|
|
if (mine === latest) {
|
|
set({ ...state, layout: before });
|
|
toast.error(`Couldn't save how the page is arranged: ${readableError(err)}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function toggleFolded(id: string): Promise<void> {
|
|
const { layout } = state;
|
|
const folded = layout.folded.includes(id);
|
|
return save({ ...layout, folded: folded ? layout.folded.filter((f) => f !== id) : [...layout.folded, id] });
|
|
}
|
|
|
|
/**
|
|
* Moves a block one place earlier or later among the blocks of the same stack. Every block of the stack is given its place at the same time (blocks with nothing to show —
|
|
* a Hetzner card for a server that is not at Hetzner — are stepped over), so a stack is either left as drawn or fully placed.
|
|
*/
|
|
export function moveBlock(block: HTMLElement, direction: -1 | 1): Promise<void> {
|
|
const stack = block.parentElement;
|
|
if (!stack) return Promise.resolve();
|
|
const all = [...stack.children]
|
|
.filter((el): el is HTMLElement => el instanceof HTMLElement && el.dataset.arrange !== undefined)
|
|
.map((el, index) => ({ el, id: el.dataset.block!, index, shown: el.querySelector(".card") !== null }));
|
|
const placed = all.sort((a, b) => (state.layout.order[a.id] ?? Number(a.el.dataset.at)) - (state.layout.order[b.id] ?? Number(b.el.dataset.at)) || a.index - b.index);
|
|
const at = placed.findIndex((p) => p.el === block);
|
|
if (at === -1) return Promise.resolve();
|
|
let to = at + direction;
|
|
while (to >= 0 && to < placed.length && !placed[to].shown) to += direction;
|
|
if (to < 0 || to >= placed.length) return Promise.resolve();
|
|
[placed[at], placed[to]] = [placed[to], placed[at]];
|
|
const order = { ...state.layout.order };
|
|
placed.forEach((p, i) => (order[p.id] = i * 10));
|
|
return save({ ...state.layout, order });
|
|
}
|
|
|
|
export function resetServerPageLayout(): Promise<void> {
|
|
return save(EMPTY);
|
|
}
|