아이폰 앱 오류 수정

This commit is contained in:
hansoo
2025-03-24 00:11:30 +09:00
parent 79a943e9dd
commit 9f9c49c588

View File

@@ -1,4 +1,3 @@
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import NavBar from '@/components/NavBar'; import NavBar from '@/components/NavBar';
import AddTransactionButton from '@/components/AddTransactionButton'; import AddTransactionButton from '@/components/AddTransactionButton';
@@ -66,60 +65,75 @@ const Index = () => {
console.log('트랜잭션:', transactions.length); console.log('트랜잭션:', transactions.length);
console.log('예산 데이터:', budgetData); console.log('예산 데이터:', budgetData);
// 페이지 마운트 시 데이터 동기화 이벤트 수동 발생 // 페이지 마운트 시에만 실행되는 로직으로 수정
try { const isFirstMount = sessionStorage.getItem('initialDataLoaded') !== 'true';
window.dispatchEvent(new Event('transactionUpdated'));
window.dispatchEvent(new Event('budgetDataUpdated')); if (isFirstMount) {
window.dispatchEvent(new Event('categoryBudgetsUpdated')); try {
} catch (e) { // 백업된 데이터 복구 확인 (메인 데이터가 없는 경우만)
console.error('이벤트 발생 오류:', e); if (!localStorage.getItem('budgetData')) {
const budgetBackup = localStorage.getItem('budgetData_backup');
if (budgetBackup) {
console.log('예산 데이터 백업에서 복구');
localStorage.setItem('budgetData', budgetBackup);
}
}
if (!localStorage.getItem('categoryBudgets')) {
const categoryBackup = localStorage.getItem('categoryBudgets_backup');
if (categoryBackup) {
console.log('카테고리 예산 백업에서 복구');
localStorage.setItem('categoryBudgets', categoryBackup);
}
}
if (!localStorage.getItem('transactions')) {
const transactionBackup = localStorage.getItem('transactions_backup');
if (transactionBackup) {
console.log('트랜잭션 백업에서 복구');
localStorage.setItem('transactions', transactionBackup);
}
}
// 한 번만 이벤트 발생
window.dispatchEvent(new Event('transactionUpdated'));
window.dispatchEvent(new Event('budgetDataUpdated'));
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
// 초기 로드 완료 표시
sessionStorage.setItem('initialDataLoaded', 'true');
} catch (error) {
console.error('백업 복구 시도 중 오류:', error);
}
} }
}, []); // 의존성 배열 비움 - 컴포넌트 마운트 시 한 번만 실행
// 백업된 데이터 복구 확인 (메인 데이터가 없는 경우만)
try {
if (!localStorage.getItem('budgetData')) {
const budgetBackup = localStorage.getItem('budgetData_backup');
if (budgetBackup) {
console.log('예산 데이터 백업에서 복구');
localStorage.setItem('budgetData', budgetBackup);
window.dispatchEvent(new Event('budgetDataUpdated'));
}
}
if (!localStorage.getItem('categoryBudgets')) {
const categoryBackup = localStorage.getItem('categoryBudgets_backup');
if (categoryBackup) {
console.log('카테고리 예산 백업에서 복구');
localStorage.setItem('categoryBudgets', categoryBackup);
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
}
}
if (!localStorage.getItem('transactions')) {
const transactionBackup = localStorage.getItem('transactions_backup');
if (transactionBackup) {
console.log('트랜잭션 백업에서 복구');
localStorage.setItem('transactions', transactionBackup);
window.dispatchEvent(new Event('transactionUpdated'));
}
}
} catch (error) {
console.error('백업 복구 시도 중 오류:', error);
}
}, [transactions.length, budgetData]);
// 앱이 포커스를 얻었을 때 데이터를 새로고침 // 앱이 포커스를 얻었을 때 데이터를 새로고침
useEffect(() => { useEffect(() => {
const handleFocus = () => { const handleFocus = () => {
console.log('창이 포커스를 얻음 - 데이터 새로고침'); console.log('창이 포커스를 얻음 - 데이터 새로고침');
// 이벤트 발생시켜 데이터 새로고침 // 이미 리프레시 중인지 확인하는 플래그
if (sessionStorage.getItem('isRefreshing') === 'true') {
console.log('이미 리프레시 진행 중, 중복 실행 방지');
return;
}
try { try {
sessionStorage.setItem('isRefreshing', 'true');
// 이벤트 발생시켜 데이터 새로고침
window.dispatchEvent(new Event('storage')); window.dispatchEvent(new Event('storage'));
window.dispatchEvent(new Event('transactionUpdated')); window.dispatchEvent(new Event('transactionUpdated'));
window.dispatchEvent(new Event('budgetDataUpdated')); window.dispatchEvent(new Event('budgetDataUpdated'));
window.dispatchEvent(new Event('categoryBudgetsUpdated')); window.dispatchEvent(new Event('categoryBudgetsUpdated'));
// 리프레시 완료 표시 (300ms 후에 플래그 해제)
setTimeout(() => {
sessionStorage.setItem('isRefreshing', 'false');
}, 300);
} catch (e) { } catch (e) {
console.error('이벤트 발생 오류:', e); console.error('이벤트 발생 오류:', e);
sessionStorage.setItem('isRefreshing', 'false');
} }
}; };
@@ -127,24 +141,27 @@ const Index = () => {
window.addEventListener('focus', handleFocus); window.addEventListener('focus', handleFocus);
// 가시성 변경 이벤트 (백그라운드에서 전경으로 돌아올 때) // 가시성 변경 이벤트 (백그라운드에서 전경으로 돌아올 때)
document.addEventListener('visibilitychange', () => { const handleVisibilityChange = () => {
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
console.log('페이지가 다시 보임 - 데이터 새로고침'); console.log('페이지가 다시 보임 - 데이터 새로고침');
handleFocus(); handleFocus();
} }
}); };
// 정기적인 데이터 새로고침 (10초마다) document.addEventListener('visibilitychange', handleVisibilityChange);
// 정기적인 데이터 새로고침 (60초마다로 변경 - 너무 빈번한 리프레시 방지)
const refreshInterval = setInterval(() => { const refreshInterval = setInterval(() => {
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible' &&
sessionStorage.getItem('isRefreshing') !== 'true') {
console.log('정기 새로고침 - 데이터 업데이트'); console.log('정기 새로고침 - 데이터 업데이트');
handleFocus(); handleFocus();
} }
}, 10000); }, 60000); // 10초에서 60초로 변경
return () => { return () => {
window.removeEventListener('focus', handleFocus); window.removeEventListener('focus', handleFocus);
document.removeEventListener('visibilitychange', () => {}); document.removeEventListener('visibilitychange', handleVisibilityChange);
clearInterval(refreshInterval); clearInterval(refreshInterval);
}; };
}, []); }, []);