import { useQuery } from '@tanstack/react-query'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { PLATFORM_OWNER_PUBKEYS } from '@/hooks/useListingRestrictions'; import { sasquatchAiStatusUrl } from '@/lib/sasquatchAi'; /** Report shape from the proxy's owner-only `/api/ai/status`. */ export interface SasquatchAiStatus { configured: boolean; balanceSats: number | null; low: boolean; provider?: string; } const AI_STATUS_URL = sasquatchAiStatusUrl(); /** * Owner-only: the Sasquatch AI (upstream) account balance, so the operator can be * nudged to top up before AI silently degrades to the keyless fallback. Polled * every few minutes. Returns `undefined`/skips for non-owners. */ export function useSasquatchAiStatus(options: { enabled?: boolean } = {}) { const { user } = useCurrentUser(); const isOwner = !!user?.pubkey && PLATFORM_OWNER_PUBKEYS.includes(user.pubkey); return useQuery({ queryKey: ['sasquatch-ai-status', user?.pubkey], enabled: (options.enabled ?? true) && isOwner, refetchInterval: 5 * 60 * 1000, staleTime: 60 * 1000, queryFn: async () => { // No signature required — see handleStatus(); avoids an Amber prompt just // to show a balance. const res = await fetch(AI_STATUS_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}', signal: AbortSignal.timeout(10000), }); if (!res.ok) throw new Error(`AI status ${res.status}`); return (await res.json()) as SasquatchAiStatus; }, }); }