Files
crosscode-platform/apps/website/frontend/src/app/admin/media/page.tsx
T
joshuaandClaude Opus 5.5 f5f778f0d7 website 1.11.0: layouts and backgrounds for the blocks; a video for the top of the page
- Blocks get layouts (BLOCK_VARIANTS: text standard/centred; image and text photo left,
  right or above; quote centred/with a line; call to action centred/banner), stored on
  Section.variant and checked against the type; an unknown one draws as the default, as
  every block looked before. The demo's image-text block id is now image-left.
- Each block has its own background (Section.background, migration
  20261201000000_block_background), in the published source only when set, put back by
  Discard and Restore, drawn as the homepage's (data-bg).
- MP4 uploads up to 20 MB for the hero's Video layout: QuickTime refused, metadata boxes
  (udta, meta, uuid) blanked into free boxes of the same size so where it was filmed is
  gone and the frame offsets hold; size read from tkhd. GET /media lists mime.
- Admin: a block's Layout and Background when open; the hero's Video field (only videos;
  photo fields only photos); the media library shows and takes videos. An upload over the
  limit says so instead of "That request isn't valid."
- Tests: backend (152, three new); e2e for block layouts and a video upload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:57:02 +02:00

80 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useState } from "react";
import { api, canEdit } from "@/admin/api";
import { gb, MediaThumb, 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, and a short video for the top of your home page. 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 or videos yet.</p>}
<ul className="grid gap-4 sm:grid-cols-2">
{data.items.map((m) => (
<li key={m.id} className="space-y-2">
<MediaThumb m={m} 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&apos;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&apos;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&apos;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 kind="any" onUploaded={() => reload()} />}
</Panel>
</div>
</div>
</>
);
}