import { useEffect, useState } from "react"; import { api, type NoteItem } from "../api/client"; import { BlockSkeleton } from "./Skeleton"; import NotePhotos from "./NotePhotos"; import RunbookText from "./RunbookText"; import TimeAgo from "./TimeAgo"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; import { t } from "../i18n"; /** * Free-form notes about a server or a network device — what it is, where it sits, what was done to it last time. One text, written with the same small formatting * as a runbook (lists, **bold**, `code`, links). Everyone signed in reads it; operators write it. Emptying it takes the note away. */ export default function NotesCard({ targetType, targetId, canEdit, bare = false }: { targetType: "server" | "device"; targetId: number; canEdit: boolean; /** Without the card round it (inside a drawer). */ bare?: boolean }) { const [note, setNote] = useState(undefined); const [draft, setDraft] = useState(null); const [busy, setBusy] = useState(false); const [failed, setFailed] = useState(false); useEffect(() => { let alive = true; setNote(undefined); setDraft(null); api.notes .get(targetType, targetId) .then((res) => alive && setNote(res.note)) .catch(() => alive && setFailed(true)); return () => { alive = false; }; }, [targetType, targetId]); async function save() { if (draft === null) return; setBusy(true); try { const res = await api.notes.save(targetType, targetId, draft); setNote(res.note); setDraft(null); toast.success(res.note ? t("Saved.") : t("Note removed.")); } catch (err) { toast.error(readableError(err)); } finally { setBusy(false); } } const body = failed ? (
Couldn’t load the note.
) : note === undefined ? ( ) : draft !== null ? ( <>