Fix budget data persistence issue

Addresses the problem where budget data was not persisting across page transitions, causing budget and expense information to disappear from the expense page and only expense data to appear on the analytics page.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 07:29:25 +00:00
parent 84553d4fee
commit 23ba0f7e90
5 changed files with 158 additions and 113 deletions

View File

@@ -12,10 +12,29 @@ export const loadBudgetDataFromStorage = (): BudgetData => {
if (storedBudgetData) {
const parsed = JSON.parse(storedBudgetData);
console.log('예산 데이터 로드 완료', parsed);
// 데이터 유효성 검사 추가
if (!parsed || !parsed.monthly || !parsed.daily || !parsed.weekly) {
throw new Error('잘못된 형식의 예산 데이터');
}
return parsed;
}
} catch (error) {
console.error('예산 데이터 파싱 오류:', error);
// 백업에서 복구 시도
try {
const backupData = localStorage.getItem('budgetData_backup');
if (backupData) {
const parsed = JSON.parse(backupData);
console.log('백업에서 예산 데이터 복구 완료', parsed);
localStorage.setItem('budgetData', backupData); // 메인 스토리지에 저장
return parsed;
}
} catch (backupError) {
console.error('백업 데이터 복구 실패:', backupError);
}
}
// 새 사용자를 위한 기본 예산 데이터 저장
@@ -39,6 +58,7 @@ export const saveBudgetDataToStorage = (budgetData: BudgetData): void => {
// 중요: 즉시 자동 백업 (데이터 손실 방지)
localStorage.setItem('budgetData_backup', dataString);
localStorage.setItem('lastBudgetSaveTime', new Date().toISOString());
// 스토리지 이벤트 수동 트리거 (동일 창에서도 감지하기 위함)
try {