Files
crosscode-platform/apps/website/frontend/src/app/admin/people/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

105 lines
6.1 KiB
TypeScript

"use client";
/**
* People (§10): who can use this website's admin, and as what. Adding someone by email gives them
* an account at the church's sign-in if they have none (they get an email to choose a password).
*/
import { useState } from "react";
import { api, ROLE_NAMES } from "@/admin/api";
import { Btn, Field, inputClass, Loading, Notice, Panel, Title, useAction, useAdmin, when } from "@/admin/ui";
type Person = { id: string; name: string; email: string; role: string; ministryId: string | null; ministry: string | null; lastSignInAt: string | null };
const ROLE_TEXT: Record<string, string> = {
owner: "Everything, including who is Owner",
admin: "Everything but Owners",
editor: "Edits everything and publishes",
ministry_leader: "Edits their own ministry; an Editor publishes it",
viewer: "Looks, changes nothing",
};
function RolePicker({ role, ministryId, onChange, ministries, canOwner, errors }: { role: string; ministryId: string | null; onChange: (r: string, m: string | null) => void; ministries: { id: string; name: string }[]; canOwner: boolean; errors: Record<string, string> }) {
return (
<div className="grid gap-3 sm:grid-cols-2">
<Field label="Role" hint={ROLE_TEXT[role]} error={errors.role}>
<select className={inputClass} value={role} onChange={(e) => onChange(e.target.value, ministryId)}>
{Object.keys(ROLE_TEXT).filter((r) => canOwner || r !== "owner").map((r) => <option key={r} value={r}>{ROLE_NAMES[r]}</option>)}
</select>
</Field>
{role === "ministry_leader" && (
<Field label="Their ministry" error={errors.ministryId}>
<select className={inputClass} value={ministryId ?? ""} onChange={(e) => onChange(role, e.target.value || null)}>
<option value="">Choose…</option>
{ministries.map((m) => <option key={m.id} value={m.id}>{m.name}</option>)}
</select>
</Field>
)}
</div>
);
}
export default function People() {
const me = useAdmin<{ user: { id: string; role: string } }>("/me");
const list = useAdmin<Person[]>("/people");
const ministries = useAdmin<{ id: string; name: string }[]>("/ministries");
const [add, setAdd] = useState({ name: "", email: "", role: "editor", ministryId: null as string | null });
const [editing, setEditing] = useState<{ id: string; role: string; ministryId: string | null } | null>(null);
const { busy, message, fields, run } = useAction();
if (!list.data || !me.data) return <Loading error={list.error ?? me.error} />;
const owner = me.data.user.role === "owner";
const mayTouch = (p: Person) => p.id !== me.data!.user.id && (owner || p.role !== "owner");
return (
<>
<Title sub="Who can use this website's admin. They sign in with their church account.">People</Title>
{message && <div className="mb-4"><Notice tone={message.tone}>{message.text}</Notice></div>}
<div className="grid gap-6 lg:grid-cols-[3fr_2fr]">
<Panel>
<ul className="divide-y divide-gray-200">
{list.data.map((p) => (
<li key={p.id} className="py-3">
{editing?.id === p.id ? (
<div className="space-y-3">
<p className="font-medium">{p.name}</p>
<RolePicker role={editing.role} ministryId={editing.ministryId} ministries={ministries.data ?? []} canOwner={owner} errors={fields} onChange={(role, ministryId) => setEditing({ ...editing, role, ministryId })} />
<div className="flex gap-2">
<Btn kind="primary" busy={busy} onClick={() => run(async () => { await api(`/people/${p.id}`, { method: "PUT", body: { role: editing.role, ministryId: editing.ministryId } }); setEditing(null); await list.reload(); }, "Saved. A lower role takes effect at once.")}>Save</Btn>
<Btn onClick={() => setEditing(null)}>Cancel</Btn>
</div>
</div>
) : (
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<p className="font-medium">{p.name}{p.id === me.data!.user.id && " (you)"}</p>
<p className="text-sm text-gray-600">{p.email} · {ROLE_NAMES[p.role]}{p.ministry && ` of ${p.ministry}`}</p>
<p className="text-xs text-gray-500">{p.lastSignInAt ? `Last signed in ${when(p.lastSignInAt)}` : "Hasn't signed in yet"}</p>
</div>
{mayTouch(p) && (
<div className="flex gap-2">
<Btn onClick={() => setEditing({ id: p.id, role: p.role, ministryId: p.ministryId })}>Change role</Btn>
<Btn kind="danger" onClick={() => run(async () => { await api(`/people/${p.id}`, { method: "DELETE" }); await list.reload(); }, `${p.name} no longer has access.`)}>Remove</Btn>
</div>
)}
</div>
)}
</li>
))}
</ul>
</Panel>
<Panel title="Add someone">
<div className="space-y-3">
<Field label="Name" error={fields.name}><input className={inputClass} value={add.name} maxLength={200} onChange={(e) => setAdd({ ...add, name: e.target.value })} /></Field>
<Field label="Email" hint="If they have no church account yet, they get an email to choose a password." error={fields.email}><input type="email" className={inputClass} value={add.email} onChange={(e) => setAdd({ ...add, email: e.target.value })} /></Field>
<RolePicker role={add.role} ministryId={add.ministryId} ministries={ministries.data ?? []} canOwner={owner} errors={fields} onChange={(role, ministryId) => setAdd({ ...add, role, ministryId })} />
<Btn kind="primary" busy={busy} disabled={!add.name || !add.email} onClick={() => run(async () => {
const r = await api<{ newAccount: boolean }>("/people", { body: add });
setAdd({ name: "", email: "", role: "editor", ministryId: null });
await list.reload();
return r;
}, "Added. They can sign in now.")}>Add</Btn>
</div>
</Panel>
</div>
</>
);
}