Refactor useBudgetState into smaller files

Refactors the `useBudgetState.ts` file into smaller, more manageable files to improve code organization and maintainability. No functionality is changed.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 05:49:41 +00:00
parent f98db16d17
commit 650bbf2f6f
6 changed files with 286 additions and 159 deletions

View File

@@ -0,0 +1,37 @@
import { useState, useEffect, useCallback } from 'react';
import {
loadCategoryBudgetsFromStorage,
saveCategoryBudgetsToStorage,
clearAllCategoryBudgets
} from '../storageUtils';
// 카테고리 예산 상태 관리 훅
export const useCategoryBudgetState = () => {
const [categoryBudgets, setCategoryBudgets] = useState<Record<string, number>>(
loadCategoryBudgetsFromStorage()
);
// 카테고리 예산 업데이트 함수
const updateCategoryBudgets = useCallback((newCategoryBudgets: Record<string, number>) => {
setCategoryBudgets(newCategoryBudgets);
saveCategoryBudgetsToStorage(newCategoryBudgets);
// 로컬 이벤트 발생 (다른 컴포넌트에서 변경 감지하도록)
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
}, []);
// 카테고리 예산 초기화 함수
const resetCategoryBudgets = useCallback(() => {
clearAllCategoryBudgets();
setCategoryBudgets(loadCategoryBudgetsFromStorage());
console.log('카테고리 예산이 초기화되었습니다.');
}, []);
return {
categoryBudgets,
setCategoryBudgets,
updateCategoryBudgets,
resetCategoryBudgets
};
};