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:
1 parent
906220040e
commit
44e393f172
50 files changed
+6114
-258
No files matched your search
+47
-5
@@ -23,15 +23,45 @@ export function parseFavorites(stored: string | null): string[] {
|
||||
}
|
||||
}
|
||||
|
||||
/** What is stored for the dashboard, read back as a layout. Anything that isn't one is the default layout rather than an error. */
|
||||
export function parseDashboardLayout(stored: string | null): DashboardLayout {
|
||||
const empty: DashboardLayout = { hidden: [], order: {} };
|
||||
if (!stored) return empty;
|
||||
try {
|
||||
const value = JSON.parse(stored);
|
||||
const parsed = dashboardSchema.safeParse(value);
|
||||
return parsed.success ? parsed.data : empty;
|
||||
} catch {
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
meRouter.get("/", requireAuth, (req, res) => {
|
||||
const { id, email, name, role, oidcSub, favorites } = req.currentUser!;
|
||||
res.json({ user: { id, sub: oidcSub, email, name, role, favorites: parseFavorites(favorites) } });
|
||||
const { id, email, name, role, oidcSub, favorites, dashboardLayout } = req.currentUser!;
|
||||
res.json({ user: { id, sub: oidcSub, email, name, role, favorites: parseFavorites(favorites), dashboard: parseDashboardLayout(dashboardLayout) } });
|
||||
});
|
||||
|
||||
// Paths inside the app only: a slash, then letters, digits, dashes, underscores and further slashes. Which of them are real pages is
|
||||
// the menu's business; a favourite for a page that is gone, or that this person's role can't open, is simply not shown.
|
||||
// Paths inside the app only: a slash, then letters, digits, dashes, underscores and further slashes — and, for a saved view of a page,
|
||||
// a short query of the characters a browser leaves as they are or percent-encodes (the search words, a status, a sort). Which of them
|
||||
// are real pages is the menu's business; a favourite for a page that is gone, or that this person's role can't open, is simply not shown.
|
||||
const favoritesSchema = z.object({
|
||||
paths: z.array(z.string().regex(/^\/[A-Za-z0-9\-_/]{0,80}$/, "Not a page path")).max(MAX_FAVORITES),
|
||||
paths: z.array(z.string().regex(/^\/[A-Za-z0-9\-_/]{0,80}(\?[A-Za-z0-9*\-._+%=&]{1,160})?$/, "Not a page path")).max(MAX_FAVORITES),
|
||||
});
|
||||
|
||||
/** The most cards a layout can name: more than any dashboard has. */
|
||||
const MAX_LAYOUT_CARDS = 60;
|
||||
|
||||
export interface DashboardLayout {
|
||||
/** Card labels the person hid. */
|
||||
hidden: string[];
|
||||
/** Position of a card among its neighbours; lower comes first. */
|
||||
order: Record<string, number>;
|
||||
}
|
||||
|
||||
const cardName = z.string().min(1).max(60);
|
||||
const dashboardSchema = z.object({
|
||||
hidden: z.array(cardName).max(MAX_LAYOUT_CARDS),
|
||||
order: z.record(cardName, z.number().int().min(0).max(1000)).refine((o) => Object.keys(o).length <= MAX_LAYOUT_CARDS, "Too many cards"),
|
||||
});
|
||||
|
||||
// Replaces the signed-in person's pinned pages. Only ever their own.
|
||||
@@ -69,3 +99,15 @@ meRouter.post("/changes/seen", requireAuth, asyncHandler(async (req, res) => {
|
||||
await db.update(users).set({ lastLookedAt: at }).where(eq(users.id, req.currentUser!.id));
|
||||
res.json({ lastLookedAt: at });
|
||||
}));
|
||||
|
||||
// Replaces how the signed-in person arranged the dashboard. Only ever their own.
|
||||
meRouter.put("/dashboard", requireAuth, asyncHandler(async (req, res) => {
|
||||
const parsed = dashboardSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "Name the cards to hide and the order of the rest.", details: parsed.error.flatten() });
|
||||
}
|
||||
const layout: DashboardLayout = { hidden: [...new Set(parsed.data.hidden)], order: parsed.data.order };
|
||||
const empty = layout.hidden.length === 0 && Object.keys(layout.order).length === 0;
|
||||
await db.update(users).set({ dashboardLayout: empty ? null : JSON.stringify(layout) }).where(eq(users.id, req.currentUser!.id));
|
||||
res.json({ dashboard: layout });
|
||||
}));
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Router } from "express";
|
||||
import { NOT_FOUND, resolveShareLink } from "../services/shareLinks.js";
|
||||
import { buildPublicWall } from "../services/publicWall.js";
|
||||
import { rateLimit } from "../utils/rateLimit.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const publicWallRouter = Router();
|
||||
|
||||
// No sign-in here: the secret in the address is the whole of the access, so this reads nothing but the wall's picture, and answers a
|
||||
// link that doesn't work — never made, ended, or taken back — in the same words.
|
||||
const limiter = rateLimit({ windowMs: 60_000, max: 60, message: "Too many requests. Wait a minute." });
|
||||
|
||||
publicWallRouter.get("/:token", limiter, asyncHandler(async (req, res) => {
|
||||
const link = await resolveShareLink(req.params.token);
|
||||
if (!link) return res.status(404).json(NOT_FOUND);
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.setHeader("X-Robots-Tag", "noindex, nofollow");
|
||||
res.json(await buildPublicWall(link.detail));
|
||||
}));
|
||||
@@ -0,0 +1,61 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { shareLinkDetails, shareLinks } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { createShareLink, publicShareLink } from "../services/shareLinks.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const shareLinksRouter = Router();
|
||||
|
||||
// Making a link that anyone can open without signing in is an administrator's decision.
|
||||
shareLinksRouter.use(requireAuth, requireRole("admin"));
|
||||
|
||||
/** More links than anyone needs; each is something to keep track of and take back. */
|
||||
const MAX_LINKS = 20;
|
||||
|
||||
const createSchema = z.object({
|
||||
label: z.string().trim().min(1).max(60),
|
||||
detail: z.enum(shareLinkDetails),
|
||||
expiresInDays: z.number().int().min(1).max(365).nullable(),
|
||||
});
|
||||
|
||||
shareLinksRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const rows = await db.select().from(shareLinks).orderBy(shareLinks.createdAt);
|
||||
res.json({ links: rows.map((r) => publicShareLink(r)) });
|
||||
}));
|
||||
|
||||
// The secret part of the address is in this response and nowhere else, ever: only a hash of it is kept.
|
||||
shareLinksRouter.post("/", asyncHandler(async (req, res) => {
|
||||
const parsed = createSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "Give the link a name, say how much it shows, and how long it lasts.", details: parsed.error.flatten() });
|
||||
}
|
||||
const existing = await db.select({ id: shareLinks.id }).from(shareLinks);
|
||||
if (existing.length >= MAX_LINKS) {
|
||||
return res.status(400).json({ error: "too_many", message: `There are already ${MAX_LINKS} share links. Remove one you no longer use first.` });
|
||||
}
|
||||
const actor = req.currentUser!;
|
||||
const { link, token } = await createShareLink({ ...parsed.data, createdBy: actor.name ?? actor.email ?? actor.oidcSub });
|
||||
await recordAudit({
|
||||
actor,
|
||||
category: "share_link",
|
||||
action: "create",
|
||||
targetType: "share_link",
|
||||
targetId: link.id,
|
||||
detail: { label: link.label, detail: link.detail, expiresAt: link.expiresAt },
|
||||
});
|
||||
res.status(201).json({ link: publicShareLink(link), token, path: `/share/wall/${token}` });
|
||||
}));
|
||||
|
||||
shareLinksRouter.delete("/:id", asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [link] = await db.select().from(shareLinks).where(eq(shareLinks.id, id)).limit(1);
|
||||
if (!link) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(shareLinks).where(eq(shareLinks.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "share_link", action: "delete", targetType: "share_link", targetId: id, detail: { label: link.label } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
Reference in new issue
Block a user