- 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>
321 lines
14 KiB
TypeScript
321 lines
14 KiB
TypeScript
import { useEffect, useMemo, useState } from "react";
|
|
import { Link, useSearchParams } from "react-router-dom";
|
|
import { api, type CurrentUser, type TodoInput, type TodoItem } from "../api/client";
|
|
import DetailDrawer from "../components/DetailDrawer";
|
|
import EmptyState from "../components/EmptyState";
|
|
import { ErrorNotice } from "../components/Notice";
|
|
import { BlockSkeleton } from "../components/Skeleton";
|
|
import TimeAgo from "../components/TimeAgo";
|
|
import { refreshMyWork } from "../hooks/useMyWork";
|
|
import { formatDate } from "../utils/date";
|
|
import { readableError } from "../utils/errors";
|
|
import { toast } from "../utils/toast";
|
|
import { removeWithUndo } from "../utils/undoable";
|
|
|
|
type Person = { id: number; name: string; role: string };
|
|
type Place = { type: "server" | "device"; id: number; name: string };
|
|
|
|
const today = () => new Date().toISOString().slice(0, 10);
|
|
|
|
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" };
|
|
if (days < 0) return { text: `${-days} ${days === -1 ? "day" : "days"} overdue`, className: "text-red" };
|
|
if (days === 0) return { text: "due today", className: "text-yellow" };
|
|
if (days === 1) return { text: "due tomorrow", className: "text-yellow" };
|
|
return { text: `due ${formatDate(new Date(`${t.dueDate}T12:00:00Z`))}`, className: days <= 7 ? "text-yellow" : "text-secondary" };
|
|
}
|
|
|
|
/** The same form to make a job and to change one. */
|
|
function JobForm({ initial, people, places, busy, submitLabel, onSubmit, onCancel }: { initial: TodoInput & { targetKey?: string }; people: Person[]; places: Place[]; busy: boolean; submitLabel: string; onSubmit: (v: TodoInput) => void; onCancel?: () => void }) {
|
|
const [title, setTitle] = useState(initial.title);
|
|
const [detail, setDetail] = useState(initial.detail ?? "");
|
|
const [due, setDue] = useState(initial.dueDate ?? "");
|
|
const [who, setWho] = useState(initial.assigneeId ? String(initial.assigneeId) : "");
|
|
const [about, setAbout] = useState(initial.targetType && initial.targetId ? `${initial.targetType}:${initial.targetId}` : "");
|
|
return (
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
const [type, id] = about.split(":");
|
|
onSubmit({ title: title.trim(), detail: detail.trim() || null, dueDate: due || null, assigneeId: who ? Number(who) : null, targetType: about ? (type as "server" | "device") : null, targetId: about ? Number(id) : null });
|
|
}}
|
|
>
|
|
<div className="row g-3">
|
|
<div className="col-12">
|
|
<label className="form-label" htmlFor="todo-title">
|
|
What is to be done
|
|
</label>
|
|
<input id="todo-title" className="form-control" maxLength={160} required value={title} onChange={(e) => setTitle(e.target.value)} placeholder="e.g. Replace the UPS battery" autoFocus />
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor="todo-due">
|
|
By <span className="text-secondary">(optional)</span>
|
|
</label>
|
|
<input id="todo-due" type="date" className="form-control" value={due} onChange={(e) => setDue(e.target.value)} />
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor="todo-who">
|
|
For <span className="text-secondary">(optional)</span>
|
|
</label>
|
|
<select id="todo-who" className="form-select" value={who} onChange={(e) => setWho(e.target.value)}>
|
|
<option value="">Nobody in particular</option>
|
|
{people.map((p) => (
|
|
<option key={p.id} value={p.id}>
|
|
{p.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor="todo-about">
|
|
About <span className="text-secondary">(optional)</span>
|
|
</label>
|
|
<select id="todo-about" className="form-select" value={about} onChange={(e) => setAbout(e.target.value)}>
|
|
<option value="">Nothing in particular</option>
|
|
<optgroup label="Servers">
|
|
{places.filter((p) => p.type === "server").map((p) => (
|
|
<option key={`s${p.id}`} value={`server:${p.id}`}>
|
|
{p.name}
|
|
</option>
|
|
))}
|
|
</optgroup>
|
|
{places.some((p) => p.type === "device") && (
|
|
<optgroup label="Network devices">
|
|
{places.filter((p) => p.type === "device").map((p) => (
|
|
<option key={`d${p.id}`} value={`device:${p.id}`}>
|
|
{p.name}
|
|
</option>
|
|
))}
|
|
</optgroup>
|
|
)}
|
|
</select>
|
|
</div>
|
|
<div className="col-12">
|
|
<label className="form-label" htmlFor="todo-detail">
|
|
Details <span className="text-secondary">(optional)</span>
|
|
</label>
|
|
<textarea id="todo-detail" className="form-control" rows={3} maxLength={2000} value={detail} onChange={(e) => setDetail(e.target.value)} />
|
|
</div>
|
|
</div>
|
|
<div className="d-flex gap-2 mt-3">
|
|
<button type="submit" className="btn btn-primary" disabled={busy || title.trim() === ""}>
|
|
{busy ? "Saving…" : submitLabel}
|
|
</button>
|
|
{onCancel && (
|
|
<button type="button" className="btn" onClick={onCancel}>
|
|
Cancel
|
|
</button>
|
|
)}
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The small jobs of a homelab, written down: what, by when, for whom and about which server or device. A job with a day is in Coming up; one handed to somebody is in
|
|
* their My work. Ticking one off keeps it (under Done), and it can be brought back.
|
|
*/
|
|
export default function Todo({ user }: { user: CurrentUser }) {
|
|
const canEdit = user.role === "admin" || user.role === "operator";
|
|
const [params, setParams] = useSearchParams();
|
|
const mineOnly = params.get("who") === "me";
|
|
const [list, setList] = useState<TodoItem[] | null>(null);
|
|
const [people, setPeople] = useState<Person[]>([]);
|
|
const [places, setPlaces] = useState<Place[]>([]);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [editing, setEditing] = useState<TodoItem | null>(null);
|
|
const [showDone, setShowDone] = useState(false);
|
|
|
|
function load() {
|
|
api.todos
|
|
.list()
|
|
.then((res) => {
|
|
setList(res.todos);
|
|
setPeople(res.people);
|
|
})
|
|
.catch((err) => setError(readableError(err)));
|
|
refreshMyWork();
|
|
}
|
|
useEffect(load, []);
|
|
useEffect(() => {
|
|
// What a job can be about: the servers, and the network devices when there are any.
|
|
Promise.all([api.servers.list().catch(() => null), api.network.overview().catch(() => null)]).then(([s, n]) =>
|
|
setPlaces([...(s?.servers ?? []).map((x) => ({ type: "server" as const, id: x.id, name: x.name })), ...(n?.devices ?? []).map((x) => ({ type: "device" as const, id: x.id, name: x.name }))]),
|
|
);
|
|
}, []);
|
|
|
|
const visible = useMemo(() => (list ?? []).filter((t) => !mineOnly || t.assignee?.id === user.id), [list, mineOnly, user.id]);
|
|
const open = visible.filter((t) => !t.doneAt);
|
|
const done = visible.filter((t) => t.doneAt);
|
|
|
|
async function create(v: TodoInput) {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await api.todos.create(v);
|
|
toast.success("Added.");
|
|
load();
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
async function save(t: TodoItem, v: TodoInput) {
|
|
setBusy(true);
|
|
try {
|
|
await api.todos.update(t.id, v);
|
|
setEditing(null);
|
|
toast.success("Saved.");
|
|
load();
|
|
} catch (err) {
|
|
toast.error(readableError(err));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
async function tick(t: TodoItem, finished: boolean) {
|
|
try {
|
|
await api.todos.update(t.id, { done: finished });
|
|
if (finished) {
|
|
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 {
|
|
load();
|
|
}
|
|
},
|
|
});
|
|
}
|
|
load();
|
|
} catch (err) {
|
|
toast.error(readableError(err));
|
|
}
|
|
}
|
|
|
|
async function remove(t: TodoItem) {
|
|
setEditing(null);
|
|
await removeWithUndo({
|
|
remove: () => api.todos.remove(t.id),
|
|
restore: () => api.todos.create({ title: t.title, detail: t.detail, dueDate: t.dueDate, assigneeId: t.assignee?.id ?? null, targetType: t.target?.type ?? null, targetId: t.target?.id ?? null }).then((r) => (t.doneAt ? api.todos.update(r.todo.id, { done: true }) : r)),
|
|
message: `Removed "${t.title}".`,
|
|
reload: load,
|
|
});
|
|
}
|
|
|
|
const row = (t: TodoItem) => {
|
|
const due = dueWords(t);
|
|
return (
|
|
<li key={t.id} className="list-group-item d-flex gap-3 align-items-start">
|
|
<input type="checkbox" className="form-check-input mt-1" aria-label={t.doneAt ? `Bring back: ${t.title}` : `Done: ${t.title}`} checked={!!t.doneAt} disabled={!canEdit} onChange={() => void tick(t, !t.doneAt)} />
|
|
<div className="me-auto">
|
|
<div className={t.doneAt ? "text-decoration-line-through text-secondary" : "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">
|
|
{due && <span className={due.className}>{due.text}</span>}
|
|
{t.assignee && <span className="badge bg-blue-lt text-blue">{t.assignee.name}</span>}
|
|
{t.target && <Link to={t.target.type === "server" ? `/servers/${t.target.id}` : "/network/devices"}>{t.target.name}</Link>}
|
|
{t.doneAt && (
|
|
<span className="text-secondary">
|
|
done <TimeAgo value={t.doneAt} />
|
|
{t.doneBy ? ` by ${t.doneBy}` : ""}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
{canEdit && (
|
|
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={() => setEditing(t)}>
|
|
Edit
|
|
</button>
|
|
)}
|
|
</li>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
|
<h2 className="page-title mb-0">To do</h2>
|
|
<div className="ms-auto btn-group btn-group-sm" role="group" aria-label="Whose jobs">
|
|
<button type="button" className={`btn ${!mineOnly ? "btn-primary" : "btn-outline-secondary"}`} aria-pressed={!mineOnly} onClick={() => setParams({})}>
|
|
Everyone’s
|
|
</button>
|
|
<button type="button" className={`btn ${mineOnly ? "btn-primary" : "btn-outline-secondary"}`} aria-pressed={mineOnly} onClick={() => setParams({ who: "me" })}>
|
|
Handed to me
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="text-secondary mb-3">
|
|
The small jobs: replace the battery, move the NAS to the new switch. A job with a day is in <Link to="/coming-up?kinds=todo">Coming up</Link>; one handed to somebody is in their <Link to="/my-work">My work</Link>.
|
|
</div>
|
|
<ErrorNotice error={error} />
|
|
|
|
{canEdit && (
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">Add a job</h3>
|
|
</div>
|
|
<div className="card-body">
|
|
<JobForm key={list?.length ?? 0} initial={{ title: "" }} people={people} places={places} busy={busy} submitLabel="Add" onSubmit={(v) => void create(v)} />
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{list === null && !error && <BlockSkeleton lines={3} />}
|
|
{list !== null && open.length === 0 && (
|
|
<div className="card mb-3">
|
|
<EmptyState title={mineOnly ? "Nothing is handed to you" : "Nothing to do"} hint={canEdit ? "Add a job above, and give it a day if it has one." : "Nothing has been written down."} />
|
|
</div>
|
|
)}
|
|
{open.length > 0 && (
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">To do ({open.length})</h3>
|
|
</div>
|
|
<ul className="list-group list-group-flush">{open.map(row)}</ul>
|
|
</div>
|
|
)}
|
|
{done.length > 0 && (
|
|
<div className="card">
|
|
<div className="card-header">
|
|
<h3 className="card-title">Done ({done.length})</h3>
|
|
<div className="card-actions">
|
|
<button type="button" className="btn btn-sm" onClick={() => setShowDone((v) => !v)}>
|
|
{showDone ? "Hide" : "Show"}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
{showDone && <ul className="list-group list-group-flush">{done.map(row)}</ul>}
|
|
</div>
|
|
)}
|
|
|
|
{editing && (
|
|
<DetailDrawer title="Edit the job" subtitle={editing.title} onClose={() => setEditing(null)}>
|
|
<JobForm
|
|
initial={{ title: editing.title, detail: editing.detail, dueDate: editing.dueDate, assigneeId: editing.assignee?.id ?? null, targetType: editing.target?.type ?? null, targetId: editing.target?.id ?? null }}
|
|
people={people}
|
|
places={places}
|
|
busy={busy}
|
|
submitLabel="Save"
|
|
onSubmit={(v) => void save(editing, v)}
|
|
onCancel={() => setEditing(null)}
|
|
/>
|
|
<hr />
|
|
<button type="button" className="btn btn-outline-danger" onClick={() => void remove(editing)}>
|
|
Remove it
|
|
</button>
|
|
</DetailDrawer>
|
|
)}
|
|
</>
|
|
);
|
|
}
|