);
}
/**
* 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 (