GrantForge Bot
Deploy sha-565ad85979610064f6d1c18ab3b6404357d61073 — source build (no GHCR)
ce8f04a
Raw
History Blame Contribute Delete
1.06 kB
import { useQuery } from '@tanstack/react-query';
import { useAuth } from '@clerk/clerk-react';
import { getSubscriptionStatus } from '../api/client';
import { ME_QUERY_KEY } from '../api/queryKeys';
export interface MeData {
clerk_id?: string;
tier: 'free' | 'pro' | 'business' | string;
wizard_iterations_today: number;
tokens_used_month: number;
limits: {
max_wizard_iterations: number;
max_tokens_monthly: number;
};
}
/** Fetches /api/me only when Clerk session is ready — avoids 401 redirect storms. */
export function useMe() {
const { isLoaded, isSignedIn } = useAuth();
return useQuery<MeData>({
queryKey: ME_QUERY_KEY,
queryFn: getSubscriptionStatus,
enabled: Boolean(isLoaded && isSignedIn),
staleTime: 30_000,
// One retry after token may have arrived (do not hammer 401)
retry: (count, err: unknown) => {
const status = (err as { response?: { status?: number } })?.response?.status;
if (status === 401) return count < 1;
return count < 1;
},
retryDelay: 800,
});
}