import { subscription } from "@/lib/auth" import { ApiError } from "@/lib/types" import { useState } from "react" export const useUserBalance = () => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [balance, setBalance] = useState(0) const load = async (userId?: string) => { try { setLoading(true) const { data, error } = await subscription.list( userId ? { query: { referenceId: userId } } : undefined ) if (error) { setError(error) return } const meteredSubscriptions = data?.filter(sub => sub.type === 'metered') || [] const creditBalance = meteredSubscriptions[0]?.creditBalance?.remainingTokenBalance || 0 setBalance(creditBalance) } catch (e) { setError(e as ApiError) } finally { setLoading(false) } } return { balance, loading, error, load } }