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 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-20 22:46:12 +02:00
1 parent 18ceed62e8
commit 788b3e073c
10 files changed
+164 -9

No files matched your search

+2 -1
View File
@@ -2388,7 +2388,8 @@ export const api = {
get: () => request<MyWork>("/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<TodoInput> & { done?: boolean }) => request<{ todo: TodoItem }>(`/api/todos/${id}`, { method: "PATCH", body: JSON.stringify(data) }),
remove: (id: number) => request<void>(`/api/todos/${id}`, { method: "DELETE" }),
+138
View File
@@ -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<TodoItem[] | null>(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 ? (
<div className="text-secondary">Couldn&rsquo;t load the jobs.</div>
) : jobs === null ? (
<BlockSkeleton lines={2} />
) : jobs.length === 0 ? (
<div className="text-secondary">Nothing to do about this.</div>
) : (
<ul className="list-group list-group-flush mb-2">
{jobs.map((t) => {
const d = dueWords(t);
return (
<li key={t.id} className="list-group-item px-0 d-flex gap-3 align-items-start">
<input type="checkbox" className="form-check-input mt-1" aria-label={`Done: ${t.title}`} disabled={!canEdit} checked={false} onChange={() => void tick(t)} />
<div className="me-auto">
<div className="fw-bold">{t.title}</div>
{t.detail && (
<div className="text-secondary small" style={{ whiteSpace: "pre-wrap" }}>
{t.detail}
</div>
)}
<div className="small d-flex flex-wrap gap-2 mt-1">
{d && <span className={d.className}>{d.text}</span>}
{t.assignee && <span className="badge bg-blue-lt text-blue">{t.assignee.name}</span>}
</div>
</div>
</li>
);
})}
</ul>
)}
{canEdit && (
<form className="d-flex flex-wrap gap-2" onSubmit={(e) => void add(e)}>
<input className="form-control flex-fill" style={{ minWidth: "12rem", width: "auto" }} maxLength={160} value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Add a job about this…" aria-label="A job about this" />
<input type="date" className="form-control w-auto" value={due} onChange={(e) => setDue(e.target.value)} aria-label="Due day (optional)" />
<button type="submit" className="btn btn-primary" disabled={busy || title.trim() === ""}>
Add
</button>
</form>
)}
<div className="small mt-2">
<Link to="/todo">All jobs</Link>
</div>
</>
);
if (bare)
return (
<>
<h4 className="mb-2">To do</h4>
{content}
</>
);
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">To do{jobs && jobs.length > 0 ? ` (${jobs.length})` : ""}</h3>
</div>
<div className="card-body">{content}</div>
</div>
);
}
+3
View File
@@ -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 && (
<DetailDrawer title={noting.name} subtitle="Notes" onClose={() => setNoting(null)}>
<NotesCard targetType="device" targetId={noting.id} canEdit={canAct} bare />
<hr />
<TodosCard targetType="device" targetId={noting.id} canEdit={canAct} bare />
</DetailDrawer>
)}
</>
+2
View File
@@ -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 }) {
</div>
</div>
<TodosCard targetType="server" targetId={server.id} canEdit={canEditTasks} />
<NotesCard targetType="server" targetId={server.id} canEdit={canEditTasks} />
<div className="card mb-3">
<div className="card-header">
+1 -1
View File
@@ -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" };