- A section's photo (hero, welcome, invitation, image-text) shows in the
shape of its frame for the current template and layout, with a Move and
zoom dialog: drag or arrow keys to choose what stays in view, zoom to
300%. Saved with the section as data.framing ({ x, y, zoom } by media
id), a draft until published. The site draws it with object-position
and scale() around that point; Bold split/collage frames now clip, and
a zoomed photo stays inside Heritage's mat. Framing for photos a section
no longer shows is dropped on save.
- Media library: each photo says where it's used; one used nowhere can be
deleted with its files (DELETE /admin/media/:id, 409 IN_USE otherwise,
audited as media_deleted).
Checked: Website backend tests (135) and typecheck, frontend typecheck,
lint and production build, and an e2e test of both flows in Chromium.
Lighthouse not run.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
5.0 KiB
TypeScript
80 lines
5.0 KiB
TypeScript
"use client";
|
||
|
||
import { useState } from "react";
|
||
import { api, canEdit } from "@/admin/api";
|
||
import { gb, StorageMeter, Uploader, type MediaItem, type Storage } from "@/admin/media";
|
||
import { Btn, inputClass, Loading, Notice, Panel, Title, useAction, useAdmin, when } from "@/admin/ui";
|
||
import { Image as ImageIcon, Trash2 } from "lucide-react";
|
||
|
||
/** The media library (§10): uploads, what each photo shows (alt text), and storage used. */
|
||
export default function Media() {
|
||
const { data, error, reload } = useAdmin<{ storage: Storage; request: { at: string; steps: number } | null; items: MediaItem[] }>("/media");
|
||
const me = useAdmin<{ user: { role: string } }>("/me");
|
||
const [alts, setAlts] = useState<Record<string, string>>({});
|
||
const [steps, setSteps] = useState(1);
|
||
const { busy, message, run } = useAction();
|
||
if (!data) return <Loading error={error} />;
|
||
const manager = ["owner", "admin"].includes(me.data?.user.role ?? "");
|
||
const editor = canEdit(me.data?.user.role ?? "");
|
||
const s = data.storage;
|
||
return (
|
||
<>
|
||
<Title icon={ImageIcon} sub="Photos for your pages. Sermon audio and video are linked (YouTube, Spotify), never uploaded.">Media library</Title>
|
||
<div className="grid gap-6 lg:grid-cols-[2fr_1fr]">
|
||
<Panel>
|
||
{message && <div className="mb-3"><Notice tone={message.tone}>{message.text}</Notice></div>}
|
||
{data.items.length === 0 && <p className="text-gray-600">No photos yet.</p>}
|
||
<ul className="grid gap-4 sm:grid-cols-2">
|
||
{data.items.map((m) => (
|
||
<li key={m.id} className="space-y-2">
|
||
<img src={m.url} alt={m.alt} className="aspect-[4/3] w-full rounded object-cover" />
|
||
<label className="block text-xs text-gray-600">What it shows
|
||
<input className={inputClass} value={alts[m.id] ?? m.alt} maxLength={500} onChange={(e) => setAlts({ ...alts, [m.id]: e.target.value })} />
|
||
</label>
|
||
{alts[m.id] !== undefined && alts[m.id] !== m.alt && (
|
||
<Btn busy={busy} onClick={() => run(async () => { await api(`/media/${m.id}`, { method: "PUT", body: { alt: alts[m.id] } }); await reload(); }, "Saved.")}>Save</Btn>
|
||
)}
|
||
{!m.alt && <p className="text-xs font-medium text-amber-800">Add what it shows, for people who can't see it.</p>}
|
||
<p className="text-xs text-gray-500">{m.width && m.height ? `${m.width} × ${m.height}` : ""} · {gb(m.bytes)}</p>
|
||
{m.usedIn?.length ? (
|
||
<p className="text-xs text-gray-600">Used on: {m.usedIn.join(", ")}</p>
|
||
) : editor && (
|
||
<Btn kind="danger" busy={busy} onClick={() => {
|
||
if (window.confirm(`Delete this photo${m.alt ? ` (“${m.alt}”)` : ""}? It isn't used anywhere. This can't be undone.`)) {
|
||
void run(async () => { await api(`/media/${m.id}`, { method: "DELETE" }); await reload(); }, "Deleted.");
|
||
}
|
||
}}><Trash2 className="h-4 w-4" aria-hidden />Delete</Btn>
|
||
)}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</Panel>
|
||
<div className="space-y-6">
|
||
<Panel title="Storage">
|
||
<StorageMeter s={s} />
|
||
{s.percent >= 80 && <div className="mt-3"><Notice tone={s.full ? "error" : "warning"}>{s.full ? "Storage is full: uploads have stopped until more storage is added." : "Storage is nearly full."} Everything already uploaded keeps showing.</Notice></div>}
|
||
<div className="mt-4 border-t border-gray-200 pt-4">
|
||
<h3 className="font-semibold">Get more storage</h3>
|
||
<p className="mt-1 text-sm text-gray-600">In steps of 5 GB, R20 a month each, added to your church's account.</p>
|
||
{data.request ? (
|
||
<div className="mt-3"><Notice tone="info">You asked for {data.request.steps * 5} GB more on {when(data.request.at)}. Cross Code adds it to your account; uploads work again within a minute of that.</Notice></div>
|
||
) : manager ? (
|
||
<div className="mt-3 flex flex-wrap items-center gap-2">
|
||
<label className="text-sm" htmlFor="steps">Add</label>
|
||
<select id="steps" className={`${inputClass} w-28`} value={steps} onChange={(e) => setSteps(Number(e.target.value))}>
|
||
{[1, 2, 3, 4].map((n) => <option key={n} value={n}>{n * 5} GB</option>)}
|
||
</select>
|
||
<Btn kind="primary" busy={busy} onClick={() => run(async () => { await api("/storage-request", { body: { steps } }); await reload(); }, "Asked: Cross Code will add it.")}>Ask for it</Btn>
|
||
</div>
|
||
) : <p className="mt-2 text-sm text-gray-600">Your website's Owner or an Admin can ask for it.</p>}
|
||
</div>
|
||
</Panel>
|
||
<Panel title="Upload a photo">
|
||
{s.full ? <Notice tone="warning">Uploads have stopped: your storage is full.</Notice> : <Uploader onUploaded={() => reload()} />}
|
||
</Panel>
|
||
</div>
|
||
</div>
|
||
</>
|
||
);
|
||
}
|