"use client";
/** The admin's own building blocks: plain, calm, the church's colour for the main action (as Cross Events' dashboard). */
import Link from "next/link";
import { createContext, useCallback, useContext, useEffect, useState } from "react";
import { ArrowRight, type LucideIcon } from "lucide-react";
import { api, AdminError } from "./api";
export const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
export function Title({ children, sub, actions, icon: Icon }: { children: React.ReactNode; sub?: React.ReactNode; actions?: React.ReactNode; icon?: LucideIcon }) {
return (
{Icon &&
}
{children}
{sub &&
{sub}
}
{actions &&
{actions}
}
);
}
/** A white card; with a title, an icon in a tile of the church's colour and a line under it. */
export function Panel({ children, className, title, icon: Icon, sub, actions }: { children: React.ReactNode; className?: string; title?: string; icon?: LucideIcon; sub?: React.ReactNode; actions?: React.ReactNode }) {
return (
{title && (
)}
{children}
);
}
/** Icon tabs (as Cross Events' Site Settings): one part of a page at a time. */
export function Tabs({ tabs, value, onChange, label }: { tabs: { id: T; label: string; icon: LucideIcon; badge?: React.ReactNode }[]; value: T; onChange: (id: T) => void; label: string }) {
return (
{tabs.map((t) => {
const Icon = t.icon;
const on = t.id === value;
return (
onChange(t.id)}
className={cx("inline-flex items-center gap-1.5 rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors", on ? "border-brand bg-brand text-brand-on" : "border-gray-200 bg-white text-gray-700 hover:bg-gray-50")}>
{t.label}
{t.badge}
);
})}
);
}
const linkClass = "mt-3 inline-flex items-center gap-1 text-xs font-medium text-brand-text hover:underline";
const TONES = { brand: "bg-brand/10 text-brand-text", amber: "bg-amber-50 text-amber-700", green: "bg-green-50 text-green-700", gray: "bg-gray-100 text-gray-600" };
/** A number on the dashboard: an icon tile, a label, the value, and where to go for more. */
export function StatCard({ icon: Icon, label, value, detail, href, linkLabel, tone = "brand" }: { icon: LucideIcon; label: string; value: React.ReactNode; detail?: React.ReactNode; href?: string; linkLabel?: string; tone?: keyof typeof TONES }) {
return (
{detail &&
{detail}
}
{href && (href.startsWith("/admin")
?
{linkLabel ?? "Open"}
:
{linkLabel ?? "Open"} )}
);
}
/** A quick-action tile: icon, title and a line about it; the whole tile is the link. */
export function ActionTile({ icon: Icon, title, description, href }: { icon: LucideIcon; title: string; description: string; href: string }) {
return (
{title}
{description}
);
}
type BtnProps = React.ButtonHTMLAttributes & { kind?: "primary" | "secondary" | "danger" | "link" | "icon"; busy?: boolean };
export function Btn({ kind = "secondary", busy, className, children, disabled, ...rest }: BtnProps) {
return (
{busy && kind !== "icon" ? "Working…" : children}
);
}
export function Field({ label, hint, error, children }: { label: string; hint?: React.ReactNode; error?: string; children: React.ReactNode }) {
return (
{label}
{children}
{hint && !error && {hint} }
{error && {error} }
);
}
export const inputClass = "w-full rounded-lg border border-gray-300 px-3 py-2 text-sm text-gray-900 shadow-sm focus:border-brand focus:outline-none focus:ring-2 focus:ring-brand/30";
export function Notice({ tone = "info", children }: { tone?: "info" | "error" | "success" | "warning"; children: React.ReactNode }) {
const tones = { info: "border-blue-200 bg-blue-50 text-blue-900", error: "border-red-200 bg-red-50 text-red-900", success: "border-green-200 bg-green-50 text-green-900", warning: "border-amber-200 bg-amber-50 text-amber-900" };
return {children}
;
}
/** Loads `path` from the admin API; `reload` fetches it again. */
export function useAdmin(path: string | null) {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const load = useCallback(async () => {
if (!path) return;
try {
setData(await api(path));
setError(null);
} catch (e) {
setError((e as Error).message);
}
}, [path]);
useEffect(() => {
// Fetching on mount is this hook's job.
// eslint-disable-next-line react-hooks/set-state-in-effect
load();
}, [load]);
return { data, error, reload: load, setData };
}
/** The dashboard's numbers, shared by the frame (the change count, the setup steps) and the pages. */
export type Overview = {
pending: number;
lastPublished: { number: number; at: string } | null;
checklist: { key: string; label: string; done: boolean; href: string | null }[];
};
export const OverviewContext = createContext<{ overview: Overview | null; refresh: () => void }>({ overview: null, refresh: () => {} });
export const useOverview = () => useContext(OverviewContext);
/** Runs an action, keeping its busy state, message and field errors. Afterwards the frame's change count and steps catch up. */
export function useAction() {
const { refresh } = useOverview();
const [busy, setBusy] = useState(false);
const [message, setMessage] = useState<{ tone: "error" | "success"; text: string } | null>(null);
const [fields, setFields] = useState>({});
const run = useCallback(async (fn: () => Promise, success?: string): Promise => {
setBusy(true);
setMessage(null);
setFields({});
try {
const out = await fn();
if (success) setMessage({ tone: "success", text: success });
return out;
} catch (e) {
setMessage({ tone: "error", text: (e as Error).message });
if (e instanceof AdminError) setFields(e.fields);
return null;
} finally {
setBusy(false);
refresh();
}
}, [refresh]);
return { busy, message, fields, run, setMessage };
}
export function Loading({ error }: { error?: string | null }) {
return error ? {error} : Loading…
;
}
export const when = (iso: string | null) => (iso ? new Intl.DateTimeFormat("en-ZA", { day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }).format(new Date(iso)) : "");