Admin API and screens: dashboard and setup checklist, template and style, pages and blocks, ministries, leadership, media library with the storage limit, publish and history. Pending changes with who and when, discard one, one Publish, restore any version (each version keeps the working copy's rows, never sent to visitors). Preview on the site's own addresses with a bar (widths, back, publish); shared preview links for 7 days or until revoked. Roles checked, same-origin changes only. Publish refreshes the public pages at once (WEBSITE_CONTENT_CACHE_SECONDS). E2E on a production build: the admin's flows and axe on every screen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
5.2 KiB
TypeScript
94 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
/** The media library: pick an image, or upload one (with its alt text) and pick it. */
|
|
import { useRef, useState } from "react";
|
|
import { api } from "./api";
|
|
import { Btn, cx, Field, inputClass, Notice, useAction, useAdmin } from "./ui";
|
|
|
|
export type MediaItem = { id: string; url: string; alt: string; width: number | null; height: number | null; bytes: number };
|
|
export type Storage = { usedBytes: number; limitBytes: number; full: boolean; percent: number };
|
|
|
|
export const gb = (n: number) => `${(n / 1024 ** 3).toFixed(n < 1024 ** 3 ? 2 : 1)} GB`;
|
|
|
|
export function StorageMeter({ s }: { s: Storage }) {
|
|
return (
|
|
<div>
|
|
<div className="h-2 w-full overflow-hidden rounded bg-gray-200" role="progressbar" aria-valuenow={s.percent} aria-valuemin={0} aria-valuemax={100} aria-label="Storage used">
|
|
<div className={cx("h-full", s.percent >= 100 ? "bg-red-600" : s.percent >= 80 ? "bg-amber-500" : "bg-brand")} style={{ width: `${s.percent}%` }} />
|
|
</div>
|
|
<p className="mt-1 text-xs text-gray-600">{gb(s.usedBytes)} of {gb(s.limitBytes)} used</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Upload form: a photo and what it shows (alt text is required, §3 "Every image needs alt text"). */
|
|
export function Uploader({ onUploaded, disabled }: { onUploaded: (m: MediaItem) => void; disabled?: boolean }) {
|
|
const file = useRef<HTMLInputElement>(null);
|
|
const [alt, setAlt] = useState("");
|
|
const { busy, message, run } = useAction();
|
|
const go = () => run(async () => {
|
|
const f = file.current?.files?.[0];
|
|
if (!f) throw new Error("Choose a photo first.");
|
|
if (!alt.trim()) throw new Error("Say what the photo shows (for people who can't see it).");
|
|
const m = await api<MediaItem & { filename: string }>(`/media?alt=${encodeURIComponent(alt.trim())}`, { raw: f });
|
|
setAlt("");
|
|
if (file.current) file.current.value = "";
|
|
onUploaded(m);
|
|
}, "Uploaded.");
|
|
return (
|
|
<div className="space-y-3">
|
|
<Field label="Photo" hint="JPEG, PNG or WebP, up to 15 MB. Landscape photos of people work best for the top of a page.">
|
|
<input ref={file} type="file" accept="image/jpeg,image/png,image/webp" className="block text-sm" disabled={disabled} />
|
|
</Field>
|
|
<Field label="What the photo shows" hint="Read out to people who can't see it, e.g. “Families at the church picnic”.">
|
|
<input className={inputClass} value={alt} onChange={(e) => setAlt(e.target.value)} maxLength={500} disabled={disabled} />
|
|
</Field>
|
|
<Btn kind="primary" onClick={go} busy={busy} disabled={disabled}>Upload</Btn>
|
|
{message && <Notice tone={message.tone}>{message.text}</Notice>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** One image field: shows the chosen image, with Choose and Remove. */
|
|
export function ImagePicker({ value, onChange, label }: { value: string | null; onChange: (id: string | null) => void; label: string }) {
|
|
const [open, setOpen] = useState(false);
|
|
const { data, reload } = useAdmin<{ storage: Storage; items: MediaItem[] }>(open || value ? "/media" : null);
|
|
const chosen = data?.items.find((m) => m.id === value);
|
|
return (
|
|
<div>
|
|
<span className="block text-sm font-medium text-gray-800">{label}</span>
|
|
<div className="mt-1 flex items-center gap-3">
|
|
<div className="h-16 w-24 shrink-0 overflow-hidden rounded border border-gray-200 bg-gray-50">
|
|
{chosen ? <img src={chosen.url} alt={chosen.alt} className="h-full w-full object-cover" /> : <span className="flex h-full items-center justify-center text-xs text-gray-400">No image</span>}
|
|
</div>
|
|
<Btn onClick={() => setOpen(true)}>{value ? "Change" : "Choose"}</Btn>
|
|
{value && <Btn kind="link" onClick={() => onChange(null)}>Remove</Btn>}
|
|
</div>
|
|
{open && (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4" role="dialog" aria-modal="true" aria-label="Choose an image">
|
|
<div className="max-h-[90vh] w-full max-w-3xl overflow-y-auto rounded-lg bg-white p-6 shadow-xl">
|
|
<div className="mb-4 flex items-center justify-between">
|
|
<h2 className="text-lg font-semibold">Choose an image</h2>
|
|
<Btn onClick={() => setOpen(false)}>Close</Btn>
|
|
</div>
|
|
<div className="grid grid-cols-3 gap-3 sm:grid-cols-4">
|
|
{data?.items.map((m) => (
|
|
<button key={m.id} type="button" onClick={() => { onChange(m.id); setOpen(false); }}
|
|
className={cx("overflow-hidden rounded border-2 text-left", m.id === value ? "border-brand" : "border-transparent hover:border-gray-300")}>
|
|
<img src={m.url} alt="" className="aspect-[4/3] w-full object-cover" />
|
|
<span className="block truncate p-1 text-xs text-gray-600">{m.alt || "No description"}</span>
|
|
</button>
|
|
))}
|
|
</div>
|
|
<div className="mt-6 border-t border-gray-200 pt-4">
|
|
<h3 className="mb-2 font-semibold">Upload a new one</h3>
|
|
{data?.storage.full && <Notice tone="warning">Your storage is full, so uploads have stopped. To get more (5 GB at a time), contact Cross Code.</Notice>}
|
|
<Uploader disabled={data?.storage.full} onUploaded={(m) => { reload(); onChange(m.id); setOpen(false); }} />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|