Files
Homelab-manager/web/src/components/NotePhotos.tsx
T
bobbanandClaude Sonnet 5.5 a16996d975 Bulk snooze, alert rules judged on report, photos on notes
Bulk snooze: Snooze... and Wake on the ticked alerts, with undo that puts back
each alert's earlier snooze exactly.

Alert rules are judged for a server right after its agent reports, instead of at
the next 15-minute pass; only that server's rule findings change, and the check
is serialized with the pass so neither overwrites the other's state.

Photos on server and device notes: up to 12 each, shrunk in the browser,
accepted by their own bytes (JPEG/PNG/GIF/WebP, 4 MB), kept in the database and
removed with the server or device.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 22:53:09 +02:00

171 lines
7.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 (
<div className="modal modal-blur d-block" role="dialog" aria-modal="true" aria-label="Photo" style={{ background: "rgba(0,0,0,0.78)" }} onClick={onClose}>
<div className="modal-dialog modal-xl modal-dialog-centered" onClick={(e) => e.stopPropagation()}>
<div className="modal-content">
<div className="modal-body text-center">
<img src={api.notePhotos.imageUrl(photo.id)} alt={photo.caption ?? "A photo on the note"} style={{ maxWidth: "100%", maxHeight: "70vh", objectFit: "contain" }} />
<div className="mt-2">
{photo.caption && <div className="fw-bold">{photo.caption}</div>}
<div className="text-secondary small">
{photo.createdBy ? `${photo.createdBy}, ` : ""}
<TimeAgo value={photo.createdAt} /> · {Math.max(1, Math.round(photo.size / 1024))} kB · {index + 1} of {photos.length}
</div>
</div>
</div>
<div className="modal-footer">
<button type="button" className="btn" disabled={index === 0} onClick={() => onMove(index - 1)} aria-label="Previous photo">
‹
</button>
<button type="button" className="btn" disabled={index >= photos.length - 1} onClick={() => onMove(index + 1)} aria-label="Next photo">
›
</button>
{canEdit && (
<>
<button type="button" className="btn" onClick={() => void caption()}>
Caption…
</button>
<button type="button" className="btn btn-outline-danger" onClick={() => void remove()}>
Remove
</button>
</>
)}
<button type="button" className="btn btn-primary ms-auto" onClick={onClose} autoFocus>
Close
</button>
</div>
</div>
</div>
</div>
);
}
/**
* 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<NotePhoto[] | null>(null);
const [open, setOpen] = useState<number | null>(null);
const [busy, setBusy] = useState(false);
const input = useRef<HTMLInputElement>(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 (
<div className="mt-3">
<div className="d-flex align-items-center mb-2">
<div className="text-secondary small fw-bold">Photos{photos.length > 0 ? ` (${photos.length})` : ""}</div>
{canEdit && (
<>
<input ref={input} type="file" accept="image/*" multiple className="d-none" onChange={(e) => void add(e.target.files)} aria-label="Choose photos to add" />
<button type="button" className="btn btn-sm ms-auto" disabled={busy || photos.length >= MAX_PHOTOS} onClick={() => input.current?.click()}>
{busy ? "Adding…" : "Add photo"}
</button>
</>
)}
</div>
{photos.length === 0 ? (
<div className="text-secondary small">None yet — add a picture of the rack or of a label.</div>
) : (
<div className="d-flex flex-wrap gap-2">
{photos.map((p, i) => (
<button key={p.id} type="button" className="btn p-0 border-0 text-start" style={{ width: 112 }} onClick={() => setOpen(i)} aria-label={p.caption ? `Open the photo: ${p.caption}` : "Open the photo"}>
<img src={api.notePhotos.imageUrl(p.id)} alt={p.caption ?? ""} loading="lazy" width={112} height={112} style={{ width: 112, height: 112, objectFit: "cover", borderRadius: 4, display: "block" }} />
{p.caption && (
<div className="text-secondary small text-truncate" style={{ maxWidth: 112 }}>
{p.caption}
</div>
)}
</button>
))}
</div>
)}
{open !== null && photos[open] && <Viewer photos={photos} index={open} canEdit={canEdit} onMove={setOpen} onClose={() => setOpen(null)} onChanged={() => void load()} />}
</div>
);
}