39 lines
835 B
TypeScript
39 lines
835 B
TypeScript
import { useState, useEffect } from 'react';
|
|
import { getUserBalance, type UserBalance } from '@/lib/api/balance';
|
|
|
|
export function useBalance() {
|
|
const [balance, setBalance] = useState<UserBalance>({
|
|
remainingTokenBalance: 0,
|
|
totalTokenBalance: 0,
|
|
usedTokenBalance: 0,
|
|
});
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const fetchBalance = async () => {
|
|
setIsLoading(true);
|
|
setError(null);
|
|
|
|
const response = await getUserBalance();
|
|
|
|
if (response.success) {
|
|
setBalance(response.data);
|
|
} else {
|
|
setError(response.message || '获取余额失败');
|
|
}
|
|
|
|
setIsLoading(false);
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchBalance();
|
|
}, []);
|
|
|
|
return {
|
|
balance,
|
|
isLoading,
|
|
error,
|
|
refresh: fetchBalance,
|
|
};
|
|
}
|