Fix persistent sync failure alerts

Addresses an issue where sync failures were repeatedly triggering notifications, even after the initial failure was acknowledged.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-22 13:07:18 +00:00
parent a7d7bba7ce
commit 3229913bde
4 changed files with 166 additions and 97 deletions

View File

@@ -9,6 +9,8 @@ import {
setLastSyncTime
} from '@/utils/syncUtils';
import useNotifications from '@/hooks/useNotifications';
import { resetSyncFailureCount } from './syncResultHandler';
import { checkNetworkStatus } from '@/utils/network/checker';
/**
* 동기화 토글 기능을 위한 커스텀 훅
@@ -17,6 +19,7 @@ export const useSyncToggle = () => {
const [enabled, setEnabled] = useState(isSyncEnabled());
const { user } = useAuth();
const { addNotification } = useNotifications();
const [isRetrying, setIsRetrying] = useState(false);
// 사용자 로그인 상태 변경 감지
useEffect(() => {
@@ -31,6 +34,9 @@ export const useSyncToggle = () => {
// 동기화 상태 업데이트
setEnabled(isSyncEnabled());
// 로그인/로그아웃 시 실패 카운터 초기화
resetSyncFailureCount();
};
// 초기 호출
@@ -71,6 +77,22 @@ export const useSyncToggle = () => {
return;
}
// 네트워크 상태 확인
const isOnline = await checkNetworkStatus();
if (checked && !isOnline) {
toast({
title: "네트워크 연결 필요",
description: "동기화를 위해 인터넷 연결이 필요합니다.",
variant: "destructive"
});
addNotification(
"네트워크 연결 필요",
"동기화를 위해 인터넷 연결이 필요합니다."
);
return;
}
// 현재 로컬 데이터 백업
const budgetDataBackup = localStorage.getItem('budgetData');
const categoryBudgetsBackup = localStorage.getItem('categoryBudgets');
@@ -82,9 +104,13 @@ export const useSyncToggle = () => {
transactions: transactionsBackup ? '있음' : '없음'
});
// 동기화 설정 변경
setEnabled(checked);
setSyncEnabled(checked);
// 실패 카운터 초기화
resetSyncFailureCount();
// 동기화 활성화/비활성화 알림 추가
addNotification(
checked ? "동기화 활성화" : "동기화 비활성화",
@@ -143,15 +169,37 @@ export const useSyncToggle = () => {
return { enabled, setEnabled, handleSyncToggle };
};
// 실제 동기화 수행 함수
// 실제 동기화 수행 함수 (최대 2회까지 자동 재시도)
const performSync = async (userId: string) => {
if (!userId) return;
try {
const result = await trySyncAllData(userId);
return result;
} catch (error) {
console.error('동기화 오류:', error);
throw error;
let attempts = 0;
const maxAttempts = 2;
while (attempts < maxAttempts) {
try {
attempts++;
console.log(`동기화 시도 ${attempts}/${maxAttempts}`);
// 네트워크 상태 확인
const isOnline = await checkNetworkStatus();
if (!isOnline) {
console.log('네트워크 연결 없음, 동기화 건너뜀');
throw new Error('네트워크 연결 필요');
}
const result = await trySyncAllData(userId);
return result;
} catch (error) {
console.error(`동기화 시도 ${attempts} 실패:`, error);
if (attempts < maxAttempts) {
// 재시도 전 잠시 대기
await new Promise(resolve => setTimeout(resolve, 2000));
console.log(`${attempts+1}번째 동기화 재시도 중...`);
} else {
throw error;
}
}
}
};