Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,531 Bytes
540437a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /**
* Reads the current user's Claude daily quota + plan tier from the backend.
*
* Fetches once when the user becomes authenticated, and exposes a `refresh()`
* that callers invoke after a successful session-create / model-switch so the
* chip reflects the new count without a full page reload.
*/
import { useCallback, useEffect, useState } from 'react';
import { useAgentStore } from '@/store/agentStore';
import { apiFetch } from '@/utils/api';
export type PlanTier = 'free' | 'pro' | 'org';
export interface UserQuota {
plan: PlanTier;
claudeUsedToday: number;
claudeDailyCap: number;
claudeRemaining: number;
}
export function useUserQuota() {
const user = useAgentStore((s) => s.user);
const [quota, setQuota] = useState<UserQuota | null>(null);
const [loading, setLoading] = useState(false);
const refresh = useCallback(async () => {
if (!user?.authenticated) return;
setLoading(true);
try {
const res = await apiFetch('/api/user/quota');
if (!res.ok) return;
const data = await res.json();
setQuota({
plan: (data.plan ?? 'free') as PlanTier,
claudeUsedToday: data.claude_used_today ?? 0,
claudeDailyCap: data.claude_daily_cap ?? 1,
claudeRemaining: data.claude_remaining ?? 0,
});
} catch {
/* backend unreachable — leave previous value */
} finally {
setLoading(false);
}
}, [user?.authenticated]);
useEffect(() => {
refresh();
}, [refresh]);
return { quota, loading, refresh };
}
|