fix: 付费订阅bug
This commit is contained in:
38
hooks/use-balance.ts
Normal file
38
hooks/use-balance.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user