Uploads (migration 028, server/src/media.js, routes/media.js): editors and up upload JPEG, PNG, GIF, WebP or SVG. Photos are resized to 2400px and saved as WebP by default, or kept as uploaded on request. Files live in UPLOADS_DIR, served by nginx in production and by the API in dev. Admins and up delete, after seeing what still uses the file. Every image field in the editor is now an image widget: preview, the path as text, Upload and Choose from library. A new Media page lists the library. Org lists and event cards now resolve logos through media.ts, so uploaded paths work there. Events get a card_image (migration 029): a photo behind the card under a light veil so the text stays readable. The card's size is unchanged. Setup for production is in docs/media-uploads.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
6 KiB
TypeScript
193 lines
6 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
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<Use[] | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(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 (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={ask}
|
|
disabled={busy}
|
|
className="text-xs text-[#4a6b72] hover:text-[#b3261e]"
|
|
>
|
|
Delete
|
|
</button>
|
|
{error && <p className="mt-1 text-xs text-[#b3261e]">{error}</p>}
|
|
</>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="rounded-lg bg-[#fdf3f2] p-2 text-xs text-[#26454c]">
|
|
{uses.length > 0 ? (
|
|
<p>
|
|
Still used by{" "}
|
|
{uses.map((use) => `${use.count} in ${use.table.replace(/_/g, " ")}`).join(", ")}. Those
|
|
will show no image.
|
|
</p>
|
|
) : (
|
|
<p>Nothing uses this file.</p>
|
|
)}
|
|
<div className="mt-2 flex gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={remove}
|
|
disabled={busy}
|
|
className="font-medium text-[#b3261e] hover:underline"
|
|
>
|
|
{busy ? "Deleting…" : "Delete for good"}
|
|
</button>
|
|
<button type="button" onClick={() => setUses(null)} className="text-[#4a6b72] hover:underline">
|
|
Cancel
|
|
</button>
|
|
</div>
|
|
{error && <p className="mt-1 text-[#b3261e]">{error}</p>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function CopyPath({ url }: { url: string }) {
|
|
const [copied, setCopied] = useState(false);
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
navigator.clipboard?.writeText(url).then(() => {
|
|
setCopied(true);
|
|
setTimeout(() => setCopied(false), 1500);
|
|
});
|
|
}}
|
|
className="text-xs text-[#138ba0] hover:underline"
|
|
>
|
|
{copied ? "Copied" : "Copy path"}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
export default function AdminMedia() {
|
|
const { user } = useAuth();
|
|
const [query, setQuery] = useState("");
|
|
const { items, error, setItems } = useMediaLibrary(query);
|
|
|
|
return (
|
|
<div>
|
|
<div className="flex flex-wrap items-baseline gap-3">
|
|
<h1 className="text-2xl font-bold text-[#138ba0]">Media</h1>
|
|
<p className="text-sm text-[#4a6b72]">
|
|
Uploaded images. Image fields in every editor pick from here.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="mt-6 flex flex-wrap items-start gap-6">
|
|
{roleCanWrite(user) && (
|
|
<div className="max-w-md">
|
|
<UploadButton
|
|
multiple
|
|
label="Upload images"
|
|
onUploaded={(item) => setItems((current) => [item, ...(current ?? [])])}
|
|
/>
|
|
<p className="mt-1 text-xs text-[#4a6b72]">{UPLOAD_HELP}</p>
|
|
</div>
|
|
)}
|
|
<input
|
|
type="search"
|
|
value={query}
|
|
onChange={(e) => 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]"
|
|
/>
|
|
</div>
|
|
|
|
<div className="mt-6">
|
|
{error ? (
|
|
<p className="text-sm text-[#b3261e]">{error}</p>
|
|
) : items === null ? (
|
|
<p className="text-sm text-[#4a6b72]">Loading…</p>
|
|
) : (
|
|
<MediaGrid
|
|
items={items}
|
|
renderActions={(item) => (
|
|
<div className="flex flex-wrap items-start gap-x-3 gap-y-1">
|
|
<CopyPath url={item.url} />
|
|
<a
|
|
href={item.url}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="text-xs text-[#138ba0] hover:underline"
|
|
>
|
|
Open
|
|
</a>
|
|
{canDelete(user) && (
|
|
<div className="w-full">
|
|
<DeleteControl
|
|
item={item}
|
|
onDeleted={() =>
|
|
setItems((current) => (current ?? []).filter((m) => m.id !== item.id))
|
|
}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|