Files
zellyy-finance/src/hooks/transactions/useTransactionsLoader.ts
gpt-engineer-app[bot] 0bc53208b5 Fix budget calculation and display
Ensure daily and weekly budgets are calculated correctly based on the monthly budget, and that the monthly budget is consistent across the app.
2025-03-22 12:02:41 +00:00

68 lines
2.3 KiB
TypeScript

import { useCallback } from 'react';
import { toast } from '@/hooks/useToast.wrapper';
import {
loadTransactionsFromStorage
} 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);
// 예산 데이터에서 직접 월간 예산 값을 가져옴
try {
const budgetDataStr = localStorage.getItem('budgetData');
if (budgetDataStr) {
const budgetData = JSON.parse(budgetDataStr);
// 월간 예산 값만 사용
if (budgetData && budgetData.monthly && typeof budgetData.monthly.targetAmount === 'number') {
const monthlyBudget = budgetData.monthly.targetAmount;
setTotalBudget(monthlyBudget);
console.log('월간 예산 설정:', monthlyBudget);
} else {
console.log('유효한 월간 예산 데이터가 없습니다. 기본값 0 사용');
setTotalBudget(0);
}
} else {
console.log('예산 데이터가 없습니다. 기본값 0 사용');
setTotalBudget(0);
}
} catch (budgetErr) {
console.error('예산 데이터 파싱 오류:', budgetErr);
setTotalBudget(0);
}
} 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
};
};