"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, tokenText } 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" }, ]; /** "12K in · 3K out" for a row of use. */ const inOut = (u: { inputTokens: number; outputTokens: number }) => `${tokenText(u.inputTokens)} in · ${tokenText(u.outputTokens)} out`; /** 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 dayTokens = (d: { inputTokens: number; outputTokens: number }) => d.inputTokens + d.outputTokens; const maxDay = Math.max(1, ...usage.days.map(dayTokens)); const hasTokens = s.usedInputTokens != null && s.usedOutputTokens != null; const hasCap = s.capInputTokens != null && s.capOutputTokens != null && s.capInputTokens > 0; 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 {s.usedPercent}% used · renews on {renewsText(s.renewsOn)}
{hasTokens && (

{tokenText(s.usedInputTokens!)} input and {tokenText(s.usedOutputTokens!)} output tokens used {hasCap && <> of about {tokenText(s.capInputTokens!)} input and {tokenText(s.capOutputTokens!)} output}

)}
= 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. {hasCap && " The allowance in tokens is an estimate: it's what you'd get if half went on input (what's sent) and half on output (the answers). Repeated instructions are cached and cost less, so most sites get more input than this."}

)}

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 => )}
PersonUsesTokens
{p.name}{p.uses}{inOut(p)}
)}

By helper this month

{usage.helpers.length === 0 ?

—

: ( {usage.helpers.map(h => )}
HelperUsesTokens
{HELPERS.find(x => x.key === h.helper)?.label || h.helper}{h.uses}{inOut(h)}
)}
{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.

); }