import { useEffect, useRef, useState } from "react"; import { api, type NotePhoto } from "../api/client"; import { confirmDialog, promptDialog } from "../utils/dialogs"; import { readableError } from "../utils/errors"; import { shrinkPhoto } from "../utils/shrinkPhoto"; import { toast } from "../utils/toast"; import TimeAgo from "./TimeAgo"; /** Most photos on one note; the server enforces the same. */ const MAX_PHOTOS = 12; /** The picture, large, over the page. Esc or a click outside closes it; the arrow keys move between pictures. */ function Viewer({ photos, index, canEdit, onMove, onClose, onChanged }: { photos: NotePhoto[]; index: number; canEdit: boolean; onMove: (to: number) => void; onClose: () => void; onChanged: () => void }) { const photo = photos[index]; useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); else if (e.key === "ArrowLeft" && index > 0) onMove(index - 1); else if (e.key === "ArrowRight" && index < photos.length - 1) onMove(index + 1); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [index, photos.length, onMove, onClose]); if (!photo) return null; async function caption() { const value = await promptDialog({ title: "Caption", message: "What is in the picture?", defaultValue: photo.caption ?? "", confirmLabel: "Save" }); if (value === null) return; try { await api.notePhotos.setCaption(photo.id, value.trim() === "" ? null : value); onChanged(); } catch (err) { toast.error(readableError(err)); } } async function remove() { if (!(await confirmDialog({ title: "Remove the photo", message: "Remove this photo from the note?", confirmLabel: "Remove", danger: true }))) return; try { await api.notePhotos.remove(photo.id); onChanged(); if (photos.length <= 1) onClose(); else onMove(Math.min(index, photos.length - 2)); } catch (err) { toast.error(readableError(err)); } } return (
e.stopPropagation()}>
{photo.caption
{photo.caption &&
{photo.caption}
}
{photo.createdBy ? `${photo.createdBy}, ` : ""} · {Math.max(1, Math.round(photo.size / 1024))} kB · {index + 1} of {photos.length}
{canEdit && ( <> )}
); } /** * The photos on a note — the rack as it is, a cable's label, the back of the NAS. Everyone signed in sees them; operators add (from the camera or the files), caption and remove. * Each is made small in the browser before it is sent. Shown only when there is one, or when the person can add one. */ export default function NotePhotos({ targetType, targetId, canEdit }: { targetType: "server" | "device"; targetId: number; canEdit: boolean }) { const [photos, setPhotos] = useState(null); const [open, setOpen] = useState(null); const [busy, setBusy] = useState(false); const input = useRef(null); function load() { return api.notePhotos .list(targetType, targetId) .then((r) => setPhotos(r.photos)) .catch(() => setPhotos([])); } useEffect(() => { setPhotos(null); setOpen(null); void load(); }, [targetType, targetId]); async function add(files: FileList | null) { if (!files || files.length === 0) return; setBusy(true); let added = 0; try { for (const file of Array.from(files)) { if ((photos?.length ?? 0) + added >= MAX_PHOTOS) { toast.error(`There can be at most ${MAX_PHOTOS} photos on a note.`); break; } try { await api.notePhotos.add(targetType, targetId, await shrinkPhoto(file)); added += 1; } catch (err) { toast.error(readableError(err)); } } if (added > 0) toast.success(added === 1 ? "Photo added." : `${added} photos added.`); } finally { if (input.current) input.current.value = ""; setBusy(false); await load(); } } if (photos === null) return null; if (photos.length === 0 && !canEdit) return null; return (
Photos{photos.length > 0 ? ` (${photos.length})` : ""}
{canEdit && ( <> void add(e.target.files)} aria-label="Choose photos to add" /> )}
{photos.length === 0 ? (
None yet — add a picture of the rack or of a label.
) : (
{photos.map((p, i) => ( ))}
)} {open !== null && photos[open] && setOpen(null)} onChanged={() => void load()} />}
); }