"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(() => (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; }