app: uploaded images for announcements, campaigns and events (APP-1.0-PLAN §10)

Photos (JPEG, PNG, WebP, checked by their bytes) uploaded in the admin are
turned upright, stripped of their metadata and kept at most 2000 px wide in
backend/uploads (UPLOAD_DIR), served at /api/uploads (never a listing, a
sandboxing CSP). Content keeps the image's full address, so an upload and an
image from elsewhere work alike. 5 GB included or the licence's storage
limit; an image still in use can't be deleted. The announcement, campaign
and event forms get Upload a photo next to the address. Migration media.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 14:47:33 +02:00
co-authored by Claude Opus 5.5
parent 13eaaffb95
commit c934f47b28
15 changed files with 337 additions and 4 deletions
@@ -1,6 +1,7 @@
"use client";
import { useEffect, useState } from "react";
import { ImageField } from "./ImageField";
import { adminCall, Field, fromLocalInput, input, Notice, Panel, toLocalInput, type Msg } from "./kit";
type Row = {
@@ -73,7 +74,7 @@ export function AdminAnnouncements({ timezone, campuses }: { timezone: string; c
<Field label="Title" error={errors.title}><input required maxLength={160} value={form.title} onChange={(e) => set("title", e.target.value)} className={input} /></Field>
<Field label="Text" error={errors.body} hint="Markdown: **bold**, *italics*, lists and [links](https://…). No HTML."><textarea required rows={8} maxLength={20000} value={form.body} onChange={(e) => set("body", e.target.value)} className={input} /></Field>
<div className="grid gap-4 sm:grid-cols-3">
<Field label="Image address (optional)" error={errors.imageUrl}><input type="url" value={form.imageUrl ?? ""} onChange={(e) => set("imageUrl", e.target.value)} className={input} /></Field>
<ImageField value={form.imageUrl} onChange={(url) => set("imageUrl", url)} error={errors.imageUrl} alt={form.title} />
<Field label="Button words (optional)" error={errors.buttonLabel}><input maxLength={40} value={form.buttonLabel ?? ""} onChange={(e) => set("buttonLabel", e.target.value)} className={input} placeholder="Sign up" /></Field>
<Field label="Button link" error={errors.buttonUrl}><input type="url" value={form.buttonUrl ?? ""} onChange={(e) => set("buttonUrl", e.target.value)} className={input} /></Field>
</div>
@@ -1,6 +1,7 @@
"use client";
import { useEffect, useState } from "react";
import { ImageField } from "./ImageField";
import { adminCall, Field, input, Notice, Panel, type Msg } from "./kit";
type Row = {
@@ -82,7 +83,7 @@ export function AdminEvents({ today }: { today: string }) {
<Field label="Where" error={errors.location}><input maxLength={200} value={form.location ?? ""} onChange={(e) => set("location", e.target.value)} className={input} /></Field>
<Field label="Description" error={errors.description}><textarea rows={5} maxLength={10000} value={form.description ?? ""} onChange={(e) => set("description", e.target.value)} className={input} /></Field>
<div className="grid gap-4 sm:grid-cols-2">
<Field label="Image address (optional)" error={errors.imageUrl}><input type="url" value={form.imageUrl ?? ""} onChange={(e) => set("imageUrl", e.target.value)} className={input} /></Field>
<ImageField value={form.imageUrl} onChange={(url) => set("imageUrl", url)} error={errors.imageUrl} alt={form.title} />
<Field label="More info link (optional)" error={errors.link}><input type="url" value={form.link ?? ""} onChange={(e) => set("link", e.target.value)} className={input} /></Field>
</div>
<div className="flex flex-wrap gap-6 text-sm font-medium">
@@ -0,0 +1,54 @@
"use client";
import { useId, useRef, useState } from "react";
import { errorMessage } from "@/lib/prayer";
import { input } from "./kit";
/**
* An image for an announcement, campaign or event (APP-1.0-PLAN §10): upload a photo (JPEG, PNG or
* WebP; kept without its metadata, at most 2000 px wide) or give an image's address. Shows a
* preview and Remove.
*/
export function ImageField({ value, onChange, error, label = "Image (optional)", alt = "" }: { value: string | null; onChange: (url: string | null) => void; error?: string; label?: string; alt?: string }) {
const id = useId();
const file = useRef<HTMLInputElement>(null);
const [busy, setBusy] = useState(false);
const [problem, setProblem] = useState<string | null>(null);
async function upload(f: File) {
setBusy(true);
setProblem(null);
try {
const res = await fetch(`/api/v1/admin/media?alt=${encodeURIComponent(alt)}`, { method: "POST", headers: { "Content-Type": f.type || "application/octet-stream" }, body: f });
if (!res.ok) {
setProblem(await errorMessage(res, "The photo wasn't uploaded. Please try again."));
return;
}
onChange(((await res.json()) as { url: string }).url);
} catch {
setProblem("The App's server can't be reached. Please try again.");
} finally {
setBusy(false);
if (file.current) file.current.value = "";
}
}
return (
<div className="space-y-2 text-sm">
<label htmlFor={id} className="block font-medium">{label}</label>
{value && (
<div className="flex items-start gap-3">
<img src={value} alt="" className="h-20 w-32 rounded-md border border-gray-200 object-cover" />
<button type="button" className="text-sm font-semibold underline" onClick={() => onChange(null)}>Remove</button>
</div>
)}
<div className="flex flex-wrap items-center gap-2">
<button type="button" className="cca-btn-quiet" disabled={busy} onClick={() => file.current?.click()}>{busy ? "Uploading…" : value ? "Upload another photo" : "Upload a photo"}</button>
<input ref={file} type="file" accept="image/jpeg,image/png,image/webp" className="sr-only" tabIndex={-1} aria-hidden onChange={(e) => e.target.files?.[0] && upload(e.target.files[0])} />
<span className="text-gray-500">or its address:</span>
</div>
<input id={id} type="url" value={value ?? ""} onChange={(e) => onChange(e.target.value || null)} className={input} placeholder="https://…" />
{(problem || error) && <p role="alert" className="text-xs text-red-700">{problem ?? error}</p>}
</div>
);
}
@@ -2,6 +2,7 @@
import { useCallback, useEffect, useState } from "react";
import { addDays, dayName, duration, errorMessage, hhmm, span, type Recurrence } from "@/lib/prayer";
import { ImageField } from "@/components/admin/ImageField";
const TABS = [
{ key: "week", label: "This week" },
@@ -325,7 +326,7 @@ function Campaigns({ today }: { today: string }) {
<label className="text-sm font-medium">Aim<input maxLength={200} value={editing.goal ?? ""} onChange={(e) => setEditing({ ...editing, goal: e.target.value })} className={input} /></label>
<label className="text-sm font-medium">People per slot<input type="number" min={1} max={50} value={editing.targetPerSlot ?? ""} placeholder="As the wall" onChange={(e) => setEditing({ ...editing, targetPerSlot: e.target.value ? Number(e.target.value) : null })} className={input} /></label>
</div>
<label className="block text-sm font-medium">Image address (optional)<input type="url" value={editing.imageUrl ?? ""} onChange={(e) => setEditing({ ...editing, imageUrl: e.target.value })} className={input} /></label>
<ImageField value={editing.imageUrl} onChange={(url) => setEditing({ ...editing, imageUrl: url })} alt={editing.title} />
<p className="text-xs text-gray-600">During a campaign, Home and the Prayer tab lead with it: days left, coverage so far and hours prayed (self-reported). Never rankings or anyone&apos;s own total.</p>
<div className="flex gap-2"><button className="cca-btn">Save</button><button type="button" className="cca-btn-quiet" onClick={() => setEditing(null)}>Cancel</button></div>
</form>