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 (
{ 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 }); }} >
setTitle(e.target.value)} placeholder="e.g. Replace the UPS battery" autoFocus />
setDue(e.target.value)} />