"use client";
/** The admin's own building blocks: plain, calm, the church's colour for the main action. */
import { useCallback, useEffect, useState } from "react";
import { api, AdminError } from "./api";
export const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
export function Title({ children, sub, actions }: { children: React.ReactNode; sub?: React.ReactNode; actions?: React.ReactNode }) {
return (
{children}
{sub &&
{sub}
}
{actions &&
{actions}
}
);
}
export function Panel({ children, className, title }: { children: React.ReactNode; className?: string; title?: string }) {
return (
{title && {title}
}
{children}
);
}
type BtnProps = React.ButtonHTMLAttributes & { kind?: "primary" | "secondary" | "danger" | "link"; busy?: boolean };
export function Btn({ kind = "secondary", busy, className, children, disabled, ...rest }: BtnProps) {
return (
);
}
export function Field({ label, hint, error, children }: { label: string; hint?: React.ReactNode; error?: string; children: React.ReactNode }) {
return (
);
}
export const inputClass = "w-full rounded-md 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 };
}
/** Runs an action, keeping its busy state, message and field errors. */
export function useAction() {
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);
}
}, []);
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)) : "");