Archived
The site calls AI through the Hub (protocol 3b, Hub 1.6.0): src/ai works out whether the AI buttons show (licence token first, then the Hub, cached a minute), runs a helper call with per-helper switches and a per-person daily limit, logs AiCall rows, and emails admins at 80%. v2 /ai/status, /ai/usage, /ai/settings; useAiStatus, AiButton, AiTab. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
25 lines
903 B
TypeScript
25 lines
903 B
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { currentAiStatus, loadAiStatus, subscribeAiStatus, type AiStatus } from "@/lib/ai";
|
|
|
|
/**
|
|
* Whether to show the AI buttons, shared across the page (one request per page load). Null while
|
|
* loading, and for anyone who isn't a supervisor or admin — AI helpers are theirs only.
|
|
*/
|
|
export function useAiStatus(): AiStatus | null {
|
|
const { user, token } = useAuth();
|
|
const allowed = !!token && (user?.role === "admin" || user?.role === "supervisor");
|
|
const [status, setStatus] = useState<AiStatus | null>(() => (allowed ? currentAiStatus() : null));
|
|
|
|
useEffect(() => {
|
|
if (!allowed || !token) return;
|
|
const off = subscribeAiStatus(setStatus);
|
|
loadAiStatus(token).then(setStatus).catch(() => {});
|
|
return off;
|
|
}, [allowed, token]);
|
|
|
|
return allowed ? status : null;
|
|
}
|