"use client"; import React, { useEffect, useState } from "react"; import { Sparkles } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import { apiV2, type V2Result } from "@/lib/apiV2"; import { loadAiStatus, renewsText } from "@/lib/ai"; import { errorMessage } from "@/lib/utils"; type Usage = V2Result<"/ai/usage", "get">["data"]; type Helper = keyof Usage["settings"]["helpers"]; const HELPERS: { key: Helper; label: string; where: string }[] = [ { key: "draft_event", label: "Draft an event", where: "New event: describe it and the form is filled in" }, { key: "summarise_report", label: "Summarise this report", where: "Reports: a few plain-language insights" }, { key: "draft_message", label: "Draft a message", where: "Emails and WhatsApp messages to attendees, broadcasts" }, { key: "summarise_feedback", label: "Summarise feedback", where: "Post-event feedback results" }, { key: "improve_text", label: "Improve this text", where: "Event descriptions, the banner, message boxes" }, ]; // Fractions of a cent add up: show R0.0123 below a rand. const money = (cents: number) => (cents > 0 && cents < 100 ? `R${(cents / 100).toFixed(4)}` : `R${(cents / 100).toFixed(2)}`); /** Site settings → AI: the monthly allowance, who used it for what, and a switch per helper. */ export function AiTab({ active }: { active: boolean }) { const { token } = useAuth(); const [usage, setUsage] = useState(null); const [limit, setLimit] = useState("0"); const [msg, setMsg] = useState<{ ok: boolean; text: string } | null>(null); const [busy, setBusy] = useState(false); useEffect(() => { if (!active || !token) return; let alive = true; apiV2("get", "/ai/usage", { authToken: token }) .then(({ data }) => { if (alive) { setUsage(data); setLimit(String(data.settings.dailyLimitPerPerson)); } }) .catch(e => { if (alive) setMsg({ ok: false, text: errorMessage(e) || "Could not load AI use." }); }); return () => { alive = false; }; }, [active, token]); const save = async (helpers: Partial>, dailyLimitPerPerson?: number) => { if (!token) return; setBusy(true); setMsg(null); try { const { data } = await apiV2("put", "/ai/settings", { authToken: token, body: { helpers, ...(dailyLimitPerPerson != null ? { dailyLimitPerPerson } : {}) } }); setUsage(u => (u ? { ...u, settings: data, status: { ...u.status, helpers: data.helpers } } : u)); setMsg({ ok: true, text: "Saved." }); loadAiStatus(token, { fresh: true }).catch(() => {}); } catch (e) { setMsg({ ok: false, text: errorMessage(e) || "Could not save." }); } setBusy(false); }; if (!usage) return msg ?

{msg.text}

:

Loading…

; const s = usage.status; const maxDay = Math.max(0.0001, ...usage.days.map(d => d.chargedCents)); return (

AI helpers

One-click helpers for supervisors and admins: drafting events and messages, improving text, summarising reports and feedback. A person always checks the suggestion before anything is saved or sent.

{!s.enabled ? (
AI helpers aren't part of this site's licence, so their buttons are hidden. To add them, ask your provider for a monthly AI allowance.
) : (
This month {money(s.usedCents)} of {money(s.capCents)} · renews on {renewsText(s.renewsOn)}
= 80 ? "bg-amber-500" : "bg-violet-500"}`} style={{ width: `${s.usedPercent}%` }} />
{s.capReached &&

The allowance is used up: the AI buttons are disabled until {renewsText(s.renewsOn)}, or until your provider raises it.

} {!s.available &&

The provider's Hub can't be reached right now, so AI is unavailable. Everything else works as usual.

}

Admins get an email when 80% is used.

)}

Helpers

    {HELPERS.map(h => (
  • {h.where}

    save({ [h.key]: e.target.checked })} />
  • ))}
{ e.preventDefault(); save({}, Math.max(0, parseInt(limit, 10) || 0)); }}>
setLimit(e.target.value)} />

0 = no limit. Guards against one person using up the month.

{msg &&

{msg.text}

} {s.enabled && (

By person this month

{usage.people.length === 0 ?

Nobody has used AI yet this month.

: ( {usage.people.map(p => )}
PersonUsesCost
{p.name}{p.uses}{money(p.chargedCents)}
)}

By helper this month

{usage.helpers.length === 0 ?

—

: ( {usage.helpers.map(h => )}
HelperUsesCost
{HELPERS.find(x => x.key === h.helper)?.label || h.helper}{h.uses}{money(h.chargedCents)}
)}
{usage.days.length > 0 && (

Per day

{usage.days.map(d =>
)}
)}
)}

Requests go through your provider's Hub to Anthropic (Claude) in the United States. Only what a helper needs is sent — never names, contact details, medical or children's information — and nothing is kept by the provider. Add Anthropic to your operator register (Admin → Privacy) when you start using AI.

); }