Events 3.3.0, Accounts 1.3.0, contracts 1.11.0: Accounts looks like Events, backups; sign-out and upload fixes

- Accounts: account and admin pages in Cross Events' layout (top bar, sidebar,
  a page per section, dark footer); quick links to the church's sites
  (GET /me/apps); nightly pg_dump backups with Admin -> Backups (run, keep,
  download, audited; heartbeat lastBackupAt); a fuller built-in privacy notice
  with headings and bullets; a fixed favicon. /backups/ ignored (anchored, so
  src/backups is kept).
- Contracts 1.11.0: people.removeDemo (only @example.com, never an admin).
- Events: uploads served from the folder they're saved in (a backend started
  outside backend/ answered 404 for a logo it had just saved); Branding checks
  the file loads before saving; logout waits for the sign-out call so Accounts
  signs out too; set-demo removes the last demo's people from Accounts.
Tests: Accounts 62 + e2e 8, Events 782, contracts 225; npm run check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 11:19:57 +02:00
co-authored by Claude Opus 5.5
parent 8ee64bb9c9
commit 8856d6b35d
61 changed files with 1514 additions and 162 deletions
@@ -1,9 +1,13 @@
"use client";
import Link from "next/link";
import React, { useCallback, useState } from "react";
import QRCode from "qrcode";
import { Button, Card, Checkbox, Field, Heading, Notice, Shell } from "@crosscode/ui";
import { Activity as ActivityIcon, ArrowUpRight, Calendar, CheckCircle2, Globe, Lock, Mail, ShieldAlert, ShieldCheck, Smartphone, User, type LucideIcon } from "lucide-react";
import { Button, Checkbox, Field, Notice } from "@crosscode/ui";
import { useBranding } from "@/components/BrandingProvider";
import { AppShell, PageTitle, ShellLoading } from "@/components/AppShell";
import { Card, Heading } from "@/components/Panel";
import { call, selfSignIn } from "@/lib/api";
import { useExternalEffect } from "@/lib/hooks";
@@ -23,37 +27,124 @@ const EVENT: Record<string, string> = {
};
const when = (iso: string | null) => (iso ? new Date(iso).toLocaleString("en-ZA", { day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" }) : "—");
/** The account page (docs/ACCOUNTS-1.0-PLAN.md §8): everything about one's own account. */
export function Account() {
export type AccountSection = "overview" | "details" | "security" | "privacy" | "activity";
const SECTIONS: Record<AccountSection, { path: string; title: string; sub: (church: string) => string }> = {
overview: { path: "/account", title: "Your account", sub: (c) => `One account for all of ${c}'s sites and apps.` },
details: { path: "/account/details", title: "Your details", sub: (c) => `Your name and contact details, shared with ${c}'s sites when you sign in.` },
security: { path: "/account/security", title: "Signing in", sub: () => "Your password, two-step sign-in and recovery codes." },
privacy: { path: "/account/privacy", title: "Privacy", sub: () => "Your news choices, a copy of your data, and deleting your account." },
activity: { path: "/account/activity", title: "Activity", sub: () => "Recent sign-ins and changes to your account." },
};
/**
* The account page (docs/ACCOUNTS-1.0-PLAN.md §8): everything about one's own account. 1.3.0: in
* sections down the side (as Cross Events' dashboard), with quick links to the church's sites.
*/
export function Account({ section = "overview" }: { section?: AccountSection }) {
const branding = useBranding();
const [me, setMe] = useState<Me | null>(null);
const [msg, setMsg] = useState<{ tone: "success" | "error"; text: string } | null>(null);
const s = SECTIONS[section] ?? SECTIONS.overview;
const load = useCallback(async () => {
const r = await call<Me>("GET", "/me");
if (r.status === 401) return window.location.assign(selfSignIn("/account"));
if (r.status === 401) return window.location.assign(selfSignIn(s.path));
if (r.ok) setMe(r.data);
}, []);
}, [s.path]);
useExternalEffect(() => { void load(); }, [load]);
const done = (text: string) => { setMsg({ tone: "success", text }); void load(); };
const fail = (text: string) => setMsg({ tone: "error", text });
if (!me) return <Shell branding={branding}><p className="text-gray-600">Loading…</p></Shell>;
if (!me) return <ShellLoading />;
const signOut = <a href={`/api/oidc/session/end?client_id=accounts-self&post_logout_redirect_uri=${encodeURIComponent(`${window.location.origin}/signed-out`)}`} className="text-sm text-brand underline">Sign out</a>;
const first = me.name.split(" ")[0] || me.name;
return (
<Shell branding={branding} wide aside={<div className="flex gap-4">{me.role === "admin" && <a href="/admin" className="text-sm text-brand underline">Admin</a>}{signOut}</div>}>
<h1 className="mb-1 text-2xl font-semibold">Your account</h1>
<p className="mb-6 text-sm text-gray-600">One account for all of {branding.name}&apos;s sites and apps.</p>
<AppShell me={me} active={s.path}>
<PageTitle sub={s.sub(branding.name)}>{section === "overview" ? `Hi, ${first}` : s.title}</PageTitle>
{msg && <Notice tone={msg.tone}>{msg.text}</Notice>}
<div className="grid gap-6 md:grid-cols-2">
<Profile me={me} onDone={done} onFail={fail} />
<Security me={me} onDone={done} onFail={fail} />
<Privacy me={me} onDone={done} onFail={fail} />
<Activity onDone={done} />
{section === "overview" && <Overview me={me} />}
{section === "details" && <div className="max-w-2xl"><Profile me={me} onDone={done} onFail={fail} /></div>}
{section === "security" && <div className="max-w-2xl"><Security me={me} onDone={done} onFail={fail} /></div>}
{section === "privacy" && <div className="max-w-2xl"><Privacy me={me} onDone={done} onFail={fail} /></div>}
{section === "activity" && <div className="max-w-2xl"><Activity onDone={done} /></div>}
</AppShell>
);
}
// ─── Overview: the church's sites, and how the account stands ────────────────
type App = { id: string; product: string; name: string; description: string | null; url: string };
const APP_ICONS: Record<string, LucideIcon> = { events: Calendar, website: Globe, app: Smartphone };
function Overview({ me }: { me: Me }) {
const branding = useBranding();
const [apps, setApps] = useState<App[] | null>(null);
useExternalEffect(() => {
let alive = true;
call<{ items: App[] }>("GET", "/me/apps").then((r) => { if (alive) setApps(r.ok ? r.data.items : []); });
return () => { alive = false; };
}, []);
const checks: { ok: boolean; label: string; detail: string; href: string; icon: LucideIcon }[] = [
{ ok: !!me.twoFactor, label: me.twoFactor ? "Two-step sign-in is on" : "Two-step sign-in is off", detail: me.twoFactor ? `With ${METHOD[me.twoFactor]}${me.recoveryCodesLeft < 3 ? `, ${me.recoveryCodesLeft} recovery codes left` : ""}` : "Turn it on so a password alone can't open your account", href: "/account/security", icon: me.twoFactor ? ShieldCheck : ShieldAlert },
{ ok: me.emailVerified, label: me.emailVerified ? "Email address confirmed" : "Email address not confirmed", detail: me.email, href: "/account/details", icon: Mail },
{ ok: true, label: "Last sign-in", detail: when(me.lastSignInAt), href: "/account/activity", icon: ActivityIcon },
];
return (
<div className="space-y-6">
<section aria-labelledby="apps-title">
<h2 id="apps-title" className="mb-3 text-sm font-semibold uppercase tracking-wide text-gray-500">Go to</h2>
{apps === null && <p className="text-sm text-gray-500" role="status">Loading…</p>}
{apps && !apps.length && (
<Card><p className="text-sm text-gray-600">{branding.name}&apos;s sites and apps will show here once they sign people in with this account.</p></Card>
)}
{apps && apps.length > 0 && (
<div className="grid gap-4 sm:grid-cols-2 xl:grid-cols-3">
{apps.map((a) => {
const Icon = APP_ICONS[a.product] ?? Globe;
return (
<a key={a.id} href={a.url} className="group flex items-center gap-4 rounded-xl border bg-white p-5 shadow-sm transition hover:border-brand/40 hover:shadow-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand/50">
<span className="flex h-12 w-12 shrink-0 items-center justify-center rounded-xl bg-brand text-white" aria-hidden><Icon className="h-6 w-6" /></span>
<span className="min-w-0 flex-1">
<span className="block font-semibold text-gray-900">{a.name}</span>
{a.description && <span className="block text-sm text-gray-500">{a.description}</span>}
<span className="block truncate text-xs text-gray-400">{a.url.replace(/^https?:\/\//, "").replace(/\/.*$/, "")}</span>
</span>
<ArrowUpRight className="h-5 w-5 shrink-0 text-gray-300 transition group-hover:text-brand" aria-hidden />
</a>
);
})}
</div>
)}
</section>
<section aria-labelledby="status-title">
<h2 id="status-title" className="mb-3 text-sm font-semibold uppercase tracking-wide text-gray-500">Your account</h2>
<div className="grid gap-4 md:grid-cols-3">
{checks.map((c) => {
const Icon = c.icon;
return (
<a key={c.label} href={c.href} className="flex items-start gap-3 rounded-xl border bg-white p-4 shadow-sm hover:bg-gray-50">
<span className={`flex h-9 w-9 shrink-0 items-center justify-center rounded-lg ${c.ok ? "bg-brand/10 text-brand" : "bg-amber-50 text-amber-700"}`} aria-hidden><Icon className="h-4 w-4" /></span>
<span className="min-w-0">
<span className="block text-sm font-medium text-gray-900">{c.label}</span>
<span className="block truncate text-xs text-gray-500">{c.detail}</span>
</span>
</a>
);
})}
</div>
</section>
<div className="flex flex-wrap gap-3 text-sm">
<Link href="/account/details" className="inline-flex items-center gap-1.5 rounded-lg border bg-white px-3 py-2 text-gray-700 hover:bg-gray-50"><User className="h-4 w-4" aria-hidden /> Change your details</Link>
<Link href="/account/privacy" className="inline-flex items-center gap-1.5 rounded-lg border bg-white px-3 py-2 text-gray-700 hover:bg-gray-50"><Lock className="h-4 w-4" aria-hidden /> Download your data</Link>
{me.noticeVersionAccepted >= me.noticeVersion && <span className="inline-flex items-center gap-1.5 px-1 py-2 text-gray-500"><CheckCircle2 className="h-4 w-4 text-green-600" aria-hidden /> You&apos;ve read the latest <a href="/privacy" className="text-brand underline">privacy notice</a></span>}
</div>
</Shell>
</div>
);
}
@@ -64,7 +155,7 @@ function Profile({ me, onDone, onFail }: Props) {
const [changingEmail, setChangingEmail] = useState(false);
return (
<Card>
<Heading>Your details</Heading>
<Heading icon={<User className="h-4 w-4" />} sub="Change them here and the church's sites get them too.">Your details</Heading>
<form onSubmit={async (e) => {
e.preventDefault();
const f = new FormData(e.currentTarget);
@@ -111,7 +202,7 @@ function Security({ me, onDone, onFail }: Props) {
return (
<Card>
<Heading>Signing in</Heading>
<Heading icon={<ShieldCheck className="h-4 w-4" />}>Signing in</Heading>
<p className="mb-3 text-sm text-gray-700">
Two-step sign-in: {me.twoFactor ? <strong>on, with {METHOD[me.twoFactor]}</strong> : <strong>off</strong>}
{me.twoFactor && <span className="text-gray-500"> · {me.recoveryCodesLeft} recovery codes left</span>}
@@ -193,7 +284,7 @@ function Privacy({ me, onDone, onFail }: Props) {
return (
<Card>
<Heading>Privacy</Heading>
<Heading icon={<Lock className="h-4 w-4" />}>Privacy</Heading>
<form onChange={async (e) => {
const f = new FormData(e.currentTarget);
const r = await call("PUT", "/me/news", { email: f.get("email") === "on", whatsapp: f.get("whatsapp") === "on" });
@@ -249,7 +340,7 @@ function Activity({ onDone }: { onDone: (t: string) => void }) {
}, []);
return (
<Card>
<Heading sub="If you see something you don't recognise, change your password and sign out everywhere.">Recent activity</Heading>
<Heading icon={<ActivityIcon className="h-4 w-4" />} sub="If you see something you don't recognise, change your password and sign out everywhere.">Recent activity</Heading>
<ul className="mb-4 max-h-72 space-y-2 overflow-y-auto text-sm">
{items.map((e, i) => (
<li key={i} className="flex justify-between gap-3 border-b pb-2 last:border-0">