/* ═══════════════════════════════════════════════════════════════ ADMIN: MEDIA The uploaded-image library: search it, add to it, copy a file's path, delete what nothing needs. Image fields in the editors upload and pick from the same library (MediaLibrary.tsx), so this page is for housekeeping rather than the way images get in. Two ranks, as on the server: editors and up upload, admins and up delete. A delete first asks the server which records still name the file, since nothing stops one being removed from under a page (an image field stores the path, not a foreign key). ═══════════════════════════════════════════════════════════════ */ import { useState } from "react"; import { ApiError, del, get } from "../../lib/api.ts"; import { useAuth } from "../../lib/auth.tsx"; import { canDelete, canWrite as roleCanWrite } from "../../lib/roles.ts"; import { MediaGrid, UPLOAD_HELP, UploadButton, useMediaLibrary, type MediaItem, } from "../../components/admin/MediaLibrary.tsx"; type Use = { table: string; count: number }; function DeleteControl({ item, onDeleted }: { item: MediaItem; onDeleted: () => void }) { const [uses, setUses] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function ask() { setBusy(true); setError(null); try { const data = await get(`/admin/media/${item.id}/uses`, { ttl: 0 }); setUses(data.uses); } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't check where it's used."); } finally { setBusy(false); } } async function remove() { setBusy(true); setError(null); try { await del(`/admin/media/${item.id}`); onDeleted(); } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't delete that."); setBusy(false); } } if (uses === null) { return ( <> {error &&

{error}

} ); } return (
{uses.length > 0 ? (

Still used by{" "} {uses.map((use) => `${use.count} in ${use.table.replace(/_/g, " ")}`).join(", ")}. Those will show no image.

) : (

Nothing uses this file.

)}
{error &&

{error}

}
); } function CopyPath({ url }: { url: string }) { const [copied, setCopied] = useState(false); return ( ); } export default function AdminMedia() { const { user } = useAuth(); const [query, setQuery] = useState(""); const { items, error, setItems } = useMediaLibrary(query); return (

Media

Uploaded images. Image fields in every editor pick from here.

{roleCanWrite(user) && (
setItems((current) => [item, ...(current ?? [])])} />

{UPLOAD_HELP}

)} setQuery(e.target.value)} placeholder="Search by file name" className="ml-auto w-64 rounded-lg border border-[#4a6b72]/25 px-3 py-2 text-sm outline-none focus:border-[#138ba0]" />
{error ? (

{error}

) : items === null ? (

Loading…

) : ( (
Open {canDelete(user) && (
setItems((current) => (current ?? []).filter((m) => m.id !== item.id)) } />
)}
)} /> )}
); }