Files
Homelab-manager/web/src/components/NotesCard.tsx
T
bobbanandClaude Sonnet 5.5 6e39430a1f Swedish interface: labels and short texts across every page
About 1,600 more headings, buttons, table columns, status words, placeholders and short messages are translated. Tables of words built when the app loads are now read when they are drawn, so they follow the language, and a test fails if text is translated at load time.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 20:16:39 +01:00

108 lines
3.8 KiB
TypeScript

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<NoteItem | null | undefined>(undefined);
const [draft, setDraft] = useState<string | null>(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 ? (
<div className="text-secondary">Couldn&rsquo;t load the note.</div>
) : note === undefined ? (
<BlockSkeleton lines={2} />
) : draft !== null ? (
<>
<textarea className="form-control mb-2" rows={8} maxLength={8000} value={draft} onChange={(e) => setDraft(e.target.value)} aria-label={t("The note")} placeholder={"What is it, where does it sit, what was done to it last time?\n\n- lists, **bold**, `code` and links work"} autoFocus />
<div className="d-flex gap-2">
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => void save()}>
{busy ? "Saving…" : t("Save")}
</button>
<button type="button" className="btn" disabled={busy} onClick={() => setDraft(null)}>
{t("Cancel")}
</button>
</div>
</>
) : note ? (
<>
<RunbookText text={note.body} />
<div className="text-secondary small mt-2">
{note.updatedBy ? `${note.updatedBy}, ` : ""}
<TimeAgo value={note.updatedAt} />
</div>
</>
) : (
<div className="text-secondary">Nothing written down yet.{canEdit ? " Add what is worth knowing about it." : ""}</div>
);
const edit =
canEdit && draft === null && note !== undefined && !failed ? (
<button type="button" className="btn btn-sm btn-primary" onClick={() => setDraft(note?.body ?? "")}>
{note ? "Edit" : t("Write a note")}
</button>
) : null;
const photos = !failed && note !== undefined && draft === null ? <NotePhotos targetType={targetType} targetId={targetId} canEdit={canEdit} /> : null;
if (bare) {
return (
<>
{body}
{photos}
{edit && <div className="mt-3">{edit}</div>}
</>
);
}
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">{t("Notes")}</h3>
{edit && <div className="card-actions">{edit}</div>}
</div>
<div className="card-body">
{body}
{photos}
</div>
</div>
);
}