Add image uploads with a media library, and event card backgrounds
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>
This commit is contained in:
parent
cd393e4ab2
commit
eaff23839f
26 changed files with 1234 additions and 22 deletions
193
src/pages/admin/AdminMedia.tsx
Normal file
193
src/pages/admin/AdminMedia.tsx
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
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>
|
||||
);
|
||||
}
|
||||
|
|
@ -70,6 +70,13 @@ export const CMS_NAV = [
|
|||
label: "Front page",
|
||||
blurb: "The hero, its photos or livestream, and which bands the home page draws.",
|
||||
},
|
||||
// Not an entity: the uploaded-image library every image field
|
||||
// picks from. Its own page, AdminMedia.tsx.
|
||||
{
|
||||
to: "/admin/media",
|
||||
label: "Media",
|
||||
blurb: "Uploaded images: logos, photos and backgrounds, ready to reuse.",
|
||||
},
|
||||
];
|
||||
|
||||
/* Forms are submissions coming in rather than content going out, so
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { eventHref } from "../../lib/hrefs.ts";
|
|||
import { EVENT_TYPES, eventTypeLabel } from "../../lib/eventTypes.ts";
|
||||
import { seriesLabel } from "../../lib/eventSeries.ts";
|
||||
import PlatformIcon from "../../components/PlatformIcon.tsx";
|
||||
import { eventBackground, isAbsoluteMedia } from "../../lib/media.ts";
|
||||
|
||||
/* ═══════════════════════════════════════════════════════════════
|
||||
EVENT LIST — CARDS
|
||||
|
|
@ -35,7 +36,9 @@ const LOGOS = Object.fromEntries(
|
|||
Object.entries(LOGO_FILES).map(([path, src]) => [path.split("/").pop(), src])
|
||||
);
|
||||
|
||||
const logoSrc = file => (file && LOGOS[file]) || null;
|
||||
/* A bundled logo by filename, or an upload or URL as it is. */
|
||||
const logoSrc = file =>
|
||||
(file && LOGOS[file]) || (isAbsoluteMedia(file) ? file : null);
|
||||
|
||||
/* Last resort only. The API already falls back to the host
|
||||
organization's logo when an event doesn't name its own, so this
|
||||
|
|
@ -60,6 +63,25 @@ function Logo({ file, alt = "", className }) {
|
|||
const TEAL = "#138ba0"; // last-resort card color
|
||||
const CARD_BG = "#ffffff"; // sits under every card, gradient or not
|
||||
|
||||
/* Laid over a card's background image so black text reads on any
|
||||
photo. Heavier at the top, where the title and dates are, and
|
||||
lighter lower down so more of the picture shows through. */
|
||||
const CARD_IMAGE_VEIL =
|
||||
"linear-gradient(to bottom, rgba(255,255,255,0.9), rgba(255,255,255,0.78))";
|
||||
|
||||
/* The card's background layers, top first: its gradient if it has
|
||||
one, then the veil and photo if it has one, then white. */
|
||||
function cardBackground(ev) {
|
||||
const layers: string[] = [];
|
||||
if (ev.gradient) layers.push(ev.gradient);
|
||||
const image = eventBackground(ev.card_image);
|
||||
if (image) {
|
||||
layers.push(CARD_IMAGE_VEIL, `url(${JSON.stringify(image)}) center / cover no-repeat`);
|
||||
}
|
||||
layers.push(CARD_BG);
|
||||
return layers.join(", ");
|
||||
}
|
||||
|
||||
/* ── Grid view ────────────────────────────────────────────────
|
||||
The grid fits as many columns as the screen allows, with no
|
||||
breakpoints: each card is at least CARD_MIN wide, and the
|
||||
|
|
@ -185,7 +207,7 @@ export function Card({
|
|||
}`}
|
||||
style={{
|
||||
border: `1px solid ${color}`,
|
||||
background: ev.gradient ? `${ev.gradient}, ${CARD_BG}` : CARD_BG,
|
||||
background: cardBackground(ev),
|
||||
filter: past ? "saturate(0.75)" : "none",
|
||||
pointerEvents: interactive ? "auto" : "none",
|
||||
}}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useState } from "react";
|
||||
import ArrowLink from "../../components/ArrowLink.tsx";
|
||||
import { orgLogo } from "../../lib/media.ts";
|
||||
import {
|
||||
initialsFor,
|
||||
orgPath,
|
||||
|
|
@ -21,8 +22,6 @@ import {
|
|||
array — only the short card description.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
const LOGO_BASE = "/org-logos/";
|
||||
|
||||
const MUTED = "#7a9299";
|
||||
const INK = "#2c4a50";
|
||||
const FALLBACK_COLOR = "#4a6b72";
|
||||
|
|
@ -39,7 +38,7 @@ function OrgMark({ org, color }) {
|
|||
if (org.logo && !failed) {
|
||||
return (
|
||||
<img
|
||||
src={`${LOGO_BASE}${org.logo}`}
|
||||
src={orgLogo(org.logo) ?? undefined}
|
||||
alt=""
|
||||
onError={() => setFailed(true)}
|
||||
className="h-14 w-14 object-contain rounded-xl shrink-0"
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
|
|||
import { useCommunity } from "../../data/chapters.ts";
|
||||
import { Link } from "react-router-dom";
|
||||
import ArrowLink from "../../components/ArrowLink.tsx";
|
||||
import { orgLogo } from "../../lib/media.ts";
|
||||
import { initialsFor, orgPath } from "../../data/organizations.ts";
|
||||
import { AREAS, AREA_NAMES } from "../../data/mapGrid.ts";
|
||||
|
||||
|
|
@ -39,8 +40,6 @@ const ROWS = 7;
|
|||
// stays at max-w-6xl, so this deliberately breaks out past it.
|
||||
const CONTENT_MAX = "88rem";
|
||||
|
||||
const LOGO_BASE = "/org-logos/";
|
||||
|
||||
const MUTED = "#7a9299";
|
||||
const RULE = "#cfe3e7";
|
||||
const INK = "#2c4a50";
|
||||
|
|
@ -472,7 +471,7 @@ function OrgLogo({ org, color, size = "h-14 w-14" }) {
|
|||
if (org.logo && !failed) {
|
||||
return (
|
||||
<img
|
||||
src={`${LOGO_BASE}${org.logo}`}
|
||||
src={orgLogo(org.logo) ?? undefined}
|
||||
alt={org.name}
|
||||
onError={() => setFailed(true)}
|
||||
className={`${size} object-contain rounded-xl shrink-0`}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useId, useState } from "react";
|
||||
import ArrowLink from "../../components/ArrowLink.tsx";
|
||||
import { orgLogo } from "../../lib/media.ts";
|
||||
import PlatformIcon from "../../components/PlatformIcon.tsx";
|
||||
import {
|
||||
areasSentence,
|
||||
|
|
@ -36,8 +37,6 @@ import {
|
|||
a partner row simply doesn't have them.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
const LOGO_BASE = "/org-logos/";
|
||||
|
||||
const MUTED = "#7a9299";
|
||||
const RULE = "#cfe3e7";
|
||||
const INK = "#2c4a50";
|
||||
|
|
@ -66,7 +65,7 @@ function OrgMark({ org, color }) {
|
|||
if (org.logo && !failed) {
|
||||
return (
|
||||
<img
|
||||
src={`${LOGO_BASE}${org.logo}`}
|
||||
src={orgLogo(org.logo) ?? undefined}
|
||||
alt=""
|
||||
onError={() => setFailed(true)}
|
||||
className="h-8 w-8 object-contain shrink-0"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue