Files
crosscode-platform/apps/website/frontend/src/app/admin/messages/page.tsx
T
joshuaandClaude Opus 5.5 37a1f6acf1 feat(website): people and roles, messages with the YouTube feed, simple events
People: Owners and Admins add someone by email (people.ensure through
Accounts), change roles, remove access; only Owners touch Owners,
nobody changes themselves, a lower role ends sessions at once.
Messages: from a YouTube channel or playlist every hour (public feed,
no key) or by hand with a YouTube or Spotify link; own publish, hide.
Simple events: own publish, cancel, remove. Tests and e2e with axe on
the new screens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:05:33 +02:00

111 lines
8.6 KiB
TypeScript

"use client";
/**
* Messages (§7): from the church's YouTube channel or playlist automatically, or added by hand with
* a YouTube or Spotify link. Each publishes on its own (decision 9); ones from YouTube go live by
* themselves. Add the speaker, series and passage afterwards, or hide one.
*/
import { useState } from "react";
import { api, canEdit } from "@/admin/api";
import { Btn, Field, inputClass, Loading, Notice, Panel, Title, useAction, useAdmin, when } from "@/admin/ui";
type Message = { id: string; title: string; date: string; mediaUrl: string; source: string; speaker: string | null; passage: string | null; notes: string | null; series: string | null; hidden: boolean; published: boolean; thumbnail: string | null };
type Data = { youtube: { source: { kind: string; id: string } | null; lastSync: string | null; lastError: string | null }; items: Message[] };
const blank = { title: "", date: new Date().toISOString().slice(0, 10), mediaUrl: "", speaker: "", passage: "", series: "", notes: "", hidden: false };
function MessageForm({ initial, fromFeed, onSave, onCancel }: { initial: typeof blank; fromFeed: boolean; onSave: (v: typeof blank) => Promise<unknown>; onCancel: () => void }) {
const [v, setV] = useState(initial);
const { busy, message, fields, run } = useAction();
const set = (k: keyof typeof blank, x: string | boolean) => setV({ ...v, [k]: x });
return (
<div className="space-y-3">
<div className="grid gap-3 sm:grid-cols-2">
<Field label="Title" error={fields.title}><input className={inputClass} value={v.title} maxLength={500} onChange={(e) => set("title", e.target.value)} /></Field>
<Field label="Date" error={fields.date}><input type="date" className={inputClass} value={v.date.slice(0, 10)} onChange={(e) => set("date", e.target.value)} /></Field>
{!fromFeed && <Field label="YouTube or Spotify link" hint="A video (youtube.com/watch?v=…, youtu.be/…) or a Spotify episode." error={fields.mediaUrl}><input className={inputClass} value={v.mediaUrl} onChange={(e) => set("mediaUrl", e.target.value)} /></Field>}
<Field label="Speaker" error={fields.speaker}><input className={inputClass} value={v.speaker} maxLength={300} onChange={(e) => set("speaker", e.target.value)} /></Field>
<Field label="Series" hint="Messages with the same series name are grouped." error={fields.series}><input className={inputClass} value={v.series} maxLength={300} onChange={(e) => set("series", e.target.value)} /></Field>
<Field label="Bible passage" error={fields.passage}><input className={inputClass} value={v.passage} maxLength={300} onChange={(e) => set("passage", e.target.value)} /></Field>
</div>
<Field label="Notes" error={fields.notes}><textarea className={inputClass} rows={3} value={v.notes} maxLength={8000} onChange={(e) => set("notes", e.target.value)} /></Field>
<label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={v.hidden} onChange={(e) => set("hidden", e.target.checked)} />Hide it from the site</label>
<div className="flex gap-2"><Btn kind="primary" busy={busy} onClick={() => run(() => onSave(v))}>Save</Btn><Btn onClick={onCancel}>Cancel</Btn></div>
{message && <Notice tone={message.tone}>{message.text}</Notice>}
</div>
);
}
export default function Messages() {
const me = useAdmin<{ user: { role: string } }>("/me");
const { data, error, reload } = useAdmin<Data>("/messages");
const [editing, setEditing] = useState<string | null>(null);
const [youtube, setYoutube] = useState<string | null>(null);
const { busy, message, fields, run } = useAction();
if (!data || !me.data) return <Loading error={error ?? me.error} />;
const editor = canEdit(me.data.user.role);
const body = (v: typeof blank) => ({ ...v, date: v.date, speaker: v.speaker || null, passage: v.passage || null, series: v.series || null, notes: v.notes || null });
const yt = data.youtube;
return (
<>
<Title sub="Sermons. Audio and video stay on YouTube or Spotify: nothing is uploaded.">Messages</Title>
{message && <div className="mb-4"><Notice tone={message.tone}>{message.text}</Notice></div>}
<div className="space-y-6">
<Panel title="From YouTube, automatically">
<p className="mb-3 text-sm text-gray-600">Each new video becomes a message within the hour. A playlist is better when your channel has other videos too.</p>
<div className="flex flex-wrap items-end gap-3">
<div className="min-w-[18rem] flex-1">
<Field label="Channel id or playlist link" hint="Your channel id starts with UC: on YouTube, open Settings → Advanced settings. Or paste a playlist's address." error={fields.youtube}>
<input className={inputClass} value={youtube ?? (yt.source ? (yt.source.kind === "playlist" ? `https://www.youtube.com/playlist?list=${yt.source.id}` : yt.source.id) : "")} disabled={!editor} onChange={(e) => setYoutube(e.target.value)} />
</Field>
</div>
{editor && <Btn kind="primary" busy={busy} onClick={() => run(async () => { const r = await api<{ sync: { added: number } | null }>("/messages-youtube", { method: "PUT", body: { youtube: youtube ?? "" } }); setYoutube(null); await reload(); return r; }, "Saved. New videos come in every hour.")}>Save</Btn>}
{editor && yt.source && <Btn busy={busy} onClick={() => run(async () => { await api("/messages-youtube/sync", { body: {} }); await reload(); }, "Checked YouTube.")}>Check now</Btn>}
</div>
{yt.lastSync && <p className="mt-2 text-xs text-gray-500">Last checked {when(yt.lastSync)}</p>}
{yt.lastError && <div className="mt-2"><Notice tone="warning">YouTube couldn&apos;t be reached last time: {yt.lastError.replace(/^\S+\s/, "")}</Notice></div>}
</Panel>
{editor && (
<Panel title="Add one by hand">
{editing === "new"
? <MessageForm initial={blank} fromFeed={false} onCancel={() => setEditing(null)} onSave={async (v) => { await api("/messages", { body: body(v) }); setEditing(null); await reload(); }} />
: <Btn kind="primary" onClick={() => setEditing("new")}>Add a message</Btn>}
</Panel>
)}
<Panel title="All messages">
{data.items.length === 0 && <p className="text-gray-600">None yet.</p>}
<ul className="divide-y divide-gray-200">
{data.items.map((m) => (
<li key={m.id} className="py-3">
{editing === m.id ? (
<MessageForm fromFeed={m.source === "youtube_feed"} onCancel={() => setEditing(null)}
initial={{ title: m.title, date: m.date.slice(0, 10), mediaUrl: m.mediaUrl, speaker: m.speaker ?? "", passage: m.passage ?? "", series: m.series ?? "", notes: m.notes ?? "", hidden: m.hidden }}
onSave={async (v) => { await api(`/messages/${m.id}`, { method: "PUT", body: body(v) }); setEditing(null); await reload(); }} />
) : (
<div className="flex flex-wrap items-center gap-4">
{m.thumbnail ? <img src={m.thumbnail} alt="" className="h-14 w-24 rounded object-cover" /> : <div className="h-14 w-24 rounded bg-gray-100" />}
<div className="min-w-0 flex-1">
<p className="font-medium">{m.title}</p>
<p className="text-sm text-gray-600">{[when(m.date).split(",")[0], m.speaker, m.series, m.source === "youtube_feed" ? "from YouTube" : m.source === "spotify" ? "Spotify" : "YouTube"].filter(Boolean).join(" · ")}</p>
<p className="text-xs">{m.hidden ? <span className="text-amber-800">Hidden</span> : m.published ? <span className="text-green-800">On the site</span> : <span className="text-gray-500">Not published</span>}</p>
</div>
{editor && (
<div className="flex flex-wrap gap-2">
<Btn onClick={() => setEditing(m.id)}>Edit</Btn>
<Btn busy={busy} onClick={() => run(async () => { await api(`/messages/${m.id}/publish`, { body: { live: !m.published } }); await reload(); }, m.published ? "Taken off the site." : "On the site now.")}>{m.published ? "Unpublish" : "Publish"}</Btn>
{m.source !== "youtube_feed" && <Btn kind="danger" busy={busy} onClick={() => run(async () => { await api(`/messages/${m.id}`, { method: "DELETE" }); await reload(); }, "Removed.")}>Remove</Btn>}
</div>
)}
</div>
)}
</li>
))}
</ul>
</Panel>
</div>
</>
);
}