The budget was being tripled on the expense and analytics pages. This commit fixes the calculation logic to ensure the budget is displayed correctly.
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
|
|
import { useCallback } from 'react';
|
|
import { toast } from '@/hooks/useToast.wrapper';
|
|
import {
|
|
loadTransactionsFromStorage,
|
|
loadBudgetFromStorage
|
|
} from './storageUtils';
|
|
|
|
/**
|
|
* 트랜잭션 로딩 관련 훅
|
|
* 로컬 스토리지에서 트랜잭션 데이터를 로드합니다.
|
|
*/
|
|
export const useTransactionsLoader = (
|
|
setTransactions: (transactions: any[]) => void,
|
|
setTotalBudget: (budget: number) => void,
|
|
setIsLoading: (isLoading: boolean) => void,
|
|
setError: (error: string | null) => void
|
|
) => {
|
|
// 트랜잭션 로드
|
|
const loadTransactions = useCallback(() => {
|
|
setIsLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const localTransactions = loadTransactionsFromStorage();
|
|
setTransactions(localTransactions);
|
|
|
|
// 예산 가져오기 (월간 예산만 설정)
|
|
const budgetAmount = loadBudgetFromStorage();
|
|
setTotalBudget(budgetAmount);
|
|
|
|
console.log('로드된 예산 금액:', budgetAmount);
|
|
} catch (err) {
|
|
console.error('트랜잭션 로드 중 오류:', err);
|
|
setError('데이터를 불러오는 중 문제가 발생했습니다.');
|
|
toast({
|
|
title: "데이터 로드 실패",
|
|
description: "지출 내역을 불러오는데 실패했습니다.",
|
|
variant: "destructive",
|
|
duration: 4000
|
|
});
|
|
} finally {
|
|
// 로딩 상태를 약간 지연시켜 UI 업데이트가 원활하게 이루어지도록 함
|
|
setTimeout(() => setIsLoading(false), 300);
|
|
}
|
|
}, [setTransactions, setTotalBudget, setIsLoading, setError]);
|
|
|
|
return {
|
|
loadTransactions
|
|
};
|
|
};
|