From 788b3e073c55c030a395a4330fb99b866316e9ea Mon Sep 17 00:00:00 2001 From: Bobban Rydh Date: Tue, 20 Oct 2026 22:46:12 +0200 Subject: [PATCH] A To do card on server pages and in a device's drawer - The open jobs about a server (or device) are listed on its page, with a line to add another already about it and a box to tick one off with Undo - GET /api/todos takes ?targetType=&targetId= to list only those about one thing - README and ROLES updated Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- ROLES.md | 2 +- server/src/routes/todos.ts | 7 +- server/src/services/todos.ts | 7 +- server/test/todosAndNotes.test.ts | 7 ++ web/src/api/client.ts | 3 +- web/src/components/TodosCard.tsx | 138 ++++++++++++++++++++++++++++++ web/src/pages/NetworkDevices.tsx | 3 + web/src/pages/ServerDetail.tsx | 2 + web/src/pages/Todo.tsx | 2 +- 10 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 web/src/components/TodosCard.tsx diff --git a/README.md b/README.md index 926c468..b4d86e8 100644 --- a/README.md +++ b/README.md @@ -861,7 +861,7 @@ support has ended, a note while it is only coming. The dates are a static table **Suggested dependencies.** Under **Map → Dependencies**, a *Suggested* card offers what the app can work out without being told: a service (Gitea, Semaphore, Uptime Kuma, phpIPAM, Home Assistant…) that is reached at a server's own address most likely runs there, so it needs that server. *Add* writes it as an ordinary dependency; *Not that* hides the suggestion (*Show the N you said no to* brings them back). Nothing is written until you add it. -**Notes and a to-do list.** Every server's page has a *Notes* card, and every network device a *Notes* button (operators), for what is worth remembering: free text with the same small formatting as runbooks +**Notes and a to-do list.** Every server's page has a *To do* card (the open jobs about it, with a line to add another and a box to tick one off) and a *Notes* card, and every network device a *Notes* button (operators; its drawer shows the device's jobs too), for what is worth remembering: free text with the same small formatting as runbooks (bold, lists, links). **Operations → To do** is a list of the small jobs — "replace the UPS battery", "move the NAS to the new switch" — each with a day it is due, a person it is handed to and a server or device it is about. A job with a day shows in **Coming up**, a job handed to someone in their **My work** (and its badge), and ticking it off, editing and removing it go in the change log. Operators and administrators write, everyone signed in reads. diff --git a/ROLES.md b/ROLES.md index 8bdba8e..7c02efe 100644 --- a/ROLES.md +++ b/ROLES.md @@ -105,7 +105,7 @@ even further, to admin only: - **Network devices** (Network → Network devices): everyone can see the devices, networks and Wi-Fi names read from UniFi controllers; **Read now** and putting a device under maintenance need operator; adding the UniFi integration itself (a key or login) is an administrator's job, like any integration. - **Dependencies**: everyone can see what needs what; adding and removing one needs operator. So does adding or hiding a *suggested* one. -- **To do and Notes** (Operations → To do, a server's *Notes*, a device's *Notes*): everyone signed in reads them; adding, editing, ticking off and removing a job, and writing a note, need operator. +- **To do and Notes** (Operations → To do, the *To do* and *Notes* cards on a server's page, a device's *Notes* drawer): everyone signed in reads them; adding, editing, ticking off and removing a job, and writing a note, need operator. - **Racks** (Inventory → Racks) and the **network diagram**: everyone signed in. A rack is made from the hardware's *where* and *rack unit*, which operators edit on the inventory page. - **Proxy hosts** (Network → Proxy hosts): everyone signed in sees what Nginx Proxy Manager serves; **Read now** needs operator; adding the integration (its login) is an administrator's job. - **Home Assistant** (Monitoring → Home Assistant): everyone signed in sees what is watched, its state and history; choosing what to watch, its limits, stopping and **Read now** need operator (the list of all diff --git a/server/src/routes/todos.ts b/server/src/routes/todos.ts index 3d087c8..9adfe1d 100644 --- a/server/src/routes/todos.ts +++ b/server/src/routes/todos.ts @@ -22,8 +22,11 @@ const fields = { targetId: z.number().int().positive().nullable().optional(), }; -todosRouter.get("/", asyncHandler(async (_req, res) => { - res.json({ todos: await listTodos(), people: await assignableUsers() }); +// `?targetType=server&targetId=3` lists only the jobs about that one (a server's page shows its own). +todosRouter.get("/", asyncHandler(async (req, res) => { + const type = TARGET_TYPES.find((t) => t === req.query.targetType); + const id = /^\d+$/.test(String(req.query.targetId ?? "")) ? Number(req.query.targetId) : NaN; + res.json({ todos: await listTodos(type && Number.isInteger(id) ? { type, id } : undefined), people: await assignableUsers() }); })); todosRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => { diff --git a/server/src/services/todos.ts b/server/src/services/todos.ts index 3d3f761..72816da 100644 --- a/server/src/services/todos.ts +++ b/server/src/services/todos.ts @@ -68,10 +68,11 @@ async function shape(rows: Row[]): Promise { } /** Open ones first, the soonest due first (those with no day last); done ones after, the latest done first. */ -export async function listTodos(): Promise { - const open = await shape(await db.select().from(todos).where(isNull(todos.doneAt))); +export async function listTodos(about?: { type: TargetType; id: number }): Promise { + const only = about ? and(eq(todos.targetType, about.type), eq(todos.targetId, about.id)) : undefined; + const open = await shape(await db.select().from(todos).where(and(isNull(todos.doneAt), only))); open.sort((a, b) => (a.dueDate ?? "9999-99-99").localeCompare(b.dueDate ?? "9999-99-99") || a.createdAt.localeCompare(b.createdAt) || a.id - b.id); - const done = await shape(await db.select().from(todos).where(isNotNull(todos.doneAt)).orderBy(desc(todos.doneAt)).limit(200)); + const done = await shape(await db.select().from(todos).where(and(isNotNull(todos.doneAt), only)).orderBy(desc(todos.doneAt)).limit(200)); return [...open, ...done]; } diff --git a/server/test/todosAndNotes.test.ts b/server/test/todosAndNotes.test.ts index 76d991c..4797b64 100644 --- a/server/test/todosAndNotes.test.ts +++ b/server/test/todosAndNotes.test.ts @@ -115,6 +115,13 @@ describe("the to-do list", () => { assert.equal(back.body.todo.doneAt, null); }); + test("a server's page asks for just the jobs about it", async () => { + const mine = (await send("GET", `/todos?targetType=server&targetId=${ids.web}`)).body.todos.map((t: any) => t.title); + assert.deepEqual(mine, ["Replace the UPS battery"], "the two with nothing to do with it are left out"); + assert.equal((await send("GET", `/todos?targetType=server&targetId=999999`)).body.todos.length, 0); + assert.equal((await send("GET", `/todos?targetType=bogus&targetId=${ids.web}`)).body.todos.length >= 3, true, "an unknown kind is no filter"); + }); + test("a job can be changed, and removed; one that is not there is a 404", async () => { const changed = await send("PATCH", `/todos/${jobId}`, { title: "Replace the UPS battery (APC)", dueDate: null, assigneeId: null }); assert.deepEqual([changed.body.todo.title, changed.body.todo.dueDate, changed.body.todo.assignee], ["Replace the UPS battery (APC)", null, null]); diff --git a/web/src/api/client.ts b/web/src/api/client.ts index 90a2e08..75bc8bd 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -2388,7 +2388,8 @@ export const api = { get: () => request("/api/my-work"), }, todos: { - list: () => request<{ todos: TodoItem[]; people: { id: number; name: string; role: string }[] }>("/api/todos"), + list: (about?: { type: "server" | "device"; id: number }) => + request<{ todos: TodoItem[]; people: { id: number; name: string; role: string }[] }>(`/api/todos${about ? `?targetType=${about.type}&targetId=${about.id}` : ""}`), create: (data: TodoInput) => request<{ todo: TodoItem }>("/api/todos", { method: "POST", body: JSON.stringify(data) }), update: (id: number, data: Partial & { done?: boolean }) => request<{ todo: TodoItem }>(`/api/todos/${id}`, { method: "PATCH", body: JSON.stringify(data) }), remove: (id: number) => request(`/api/todos/${id}`, { method: "DELETE" }), diff --git a/web/src/components/TodosCard.tsx b/web/src/components/TodosCard.tsx new file mode 100644 index 0000000..0f21b79 --- /dev/null +++ b/web/src/components/TodosCard.tsx @@ -0,0 +1,138 @@ +import { useCallback, useEffect, useState } from "react"; +import { Link } from "react-router-dom"; +import { api, type TodoItem } from "../api/client"; +import { dueWords } from "../pages/Todo"; +import { BlockSkeleton } from "./Skeleton"; +import { refreshMyWork } from "../hooks/useMyWork"; +import { readableError } from "../utils/errors"; +import { toast } from "../utils/toast"; + +/** + * The open jobs on the to-do list that are about this server or device, with a box to tick one off and (for operators) a line to add another that is already about it. + * The full list, with who each is handed to and the finished ones, is on the To do page. + */ +export default function TodosCard({ targetType, targetId, canEdit, bare = false }: { targetType: "server" | "device"; targetId: number; canEdit: boolean; /** Without the card round it (inside a drawer). */ bare?: boolean }) { + const [jobs, setJobs] = useState(null); + const [failed, setFailed] = useState(false); + const [title, setTitle] = useState(""); + const [due, setDue] = useState(""); + const [busy, setBusy] = useState(false); + + const load = useCallback(() => { + return api.todos + .list({ type: targetType, id: targetId }) + .then((res) => { + setJobs(res.todos.filter((t) => !t.doneAt)); + setFailed(false); + }) + .catch(() => setFailed(true)); + }, [targetType, targetId]); + + useEffect(() => { + setJobs(null); + void load(); + }, [load]); + + async function add(e: React.FormEvent) { + e.preventDefault(); + if (title.trim() === "") return; + setBusy(true); + try { + await api.todos.create({ title: title.trim(), dueDate: due || null, targetType, targetId }); + setTitle(""); + setDue(""); + await load(); + refreshMyWork(); + } catch (err) { + toast.error(readableError(err)); + } finally { + setBusy(false); + } + } + + async function tick(t: TodoItem) { + try { + await api.todos.update(t.id, { done: true }); + toast.success(`Done: ${t.title}`, { + label: "Undo", + onClick: async () => { + try { + await api.todos.update(t.id, { done: false }); + } catch (err) { + toast.error(readableError(err)); + } finally { + void load(); + refreshMyWork(); + } + }, + }); + await load(); + refreshMyWork(); + } catch (err) { + toast.error(readableError(err)); + } + } + + const content = ( + <> + {failed ? ( +
Couldn’t load the jobs.
+ ) : jobs === null ? ( + + ) : jobs.length === 0 ? ( +
Nothing to do about this.
+ ) : ( +
    + {jobs.map((t) => { + const d = dueWords(t); + return ( +
  • + void tick(t)} /> +
    +
    {t.title}
    + {t.detail && ( +
    + {t.detail} +
    + )} +
    + {d && {d.text}} + {t.assignee && {t.assignee.name}} +
    +
    +
  • + ); + })} +
+ )} + {canEdit && ( +
void add(e)}> + setTitle(e.target.value)} placeholder="Add a job about this…" aria-label="A job about this" /> + setDue(e.target.value)} aria-label="Due day (optional)" /> + +
+ )} +
+ All jobs +
+ + ); + + if (bare) + return ( + <> +

To do

+ {content} + + ); + return ( +
+
+

To do{jobs && jobs.length > 0 ? ` (${jobs.length})` : ""}

+
+
{content}
+
+ ); +} diff --git a/web/src/pages/NetworkDevices.tsx b/web/src/pages/NetworkDevices.tsx index f53a6b9..c34f985 100644 --- a/web/src/pages/NetworkDevices.tsx +++ b/web/src/pages/NetworkDevices.tsx @@ -10,6 +10,7 @@ import SilenceButton from "../components/SilenceButton"; import RunbookEntry from "../components/RunbookEntry"; import NetworkDiagram from "../components/NetworkDiagram"; import NotesCard from "../components/NotesCard"; +import TodosCard from "../components/TodosCard"; import DetailDrawer from "../components/DetailDrawer"; import CopyButton from "../components/CopyButton"; import EmptyState, { EmptyRow } from "../components/EmptyState"; @@ -368,6 +369,8 @@ export default function NetworkDevices({ user }: { user: CurrentUser }) { {noting && ( setNoting(null)}> +
+
)} diff --git a/web/src/pages/ServerDetail.tsx b/web/src/pages/ServerDetail.tsx index 9efd215..e0eaa79 100644 --- a/web/src/pages/ServerDetail.tsx +++ b/web/src/pages/ServerDetail.tsx @@ -16,6 +16,7 @@ import ServerPorts from "../components/ServerPorts"; import ServerTrends from "../components/ServerTrends"; import UptimeHistory from "../components/UptimeHistory"; import NotesCard from "../components/NotesCard"; +import TodosCard from "../components/TodosCard"; import ServerActivity from "../components/ServerActivity"; import TimeAgo from "../components/TimeAgo"; import UptimeRibbon from "../components/UptimeRibbon"; @@ -764,6 +765,7 @@ export default function ServerDetail({ user }: { user: CurrentUser }) { +
diff --git a/web/src/pages/Todo.tsx b/web/src/pages/Todo.tsx index b229a9d..e923aa0 100644 --- a/web/src/pages/Todo.tsx +++ b/web/src/pages/Todo.tsx @@ -17,7 +17,7 @@ type Place = { type: "server" | "device"; id: number; name: string }; const today = () => new Date().toISOString().slice(0, 10); -function dueWords(t: TodoItem): { text: string; className: string } | null { +export function dueWords(t: TodoItem): { text: string; className: string } | null { if (!t.dueDate) return null; const days = Math.round((Date.parse(`${t.dueDate}T00:00:00Z`) - Date.parse(`${today()}T00:00:00Z`)) / 86_400_000); if (t.doneAt) return { text: formatDate(new Date(`${t.dueDate}T12:00:00Z`)), className: "text-secondary" };