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:
@@ -12,9 +12,16 @@ export const setSyncNotificationAdder = (adder: (title: string, message: string)
|
||||
notificationAdder = adder;
|
||||
};
|
||||
|
||||
// 동기화 실패 카운터 추가
|
||||
let syncFailureCount = 0;
|
||||
const MAX_SYNC_FAILURE_NOTIFICATIONS = 2; // 최대 알림 횟수
|
||||
|
||||
// 동기화 결과 처리 함수
|
||||
export const handleSyncResult = (result: SyncResult) => {
|
||||
if (result.success) {
|
||||
// 성공 시 실패 카운터 초기화
|
||||
syncFailureCount = 0;
|
||||
|
||||
let title = '';
|
||||
let description = '';
|
||||
|
||||
@@ -56,19 +63,26 @@ export const handleSyncResult = (result: SyncResult) => {
|
||||
// 동기화 실패
|
||||
console.error("동기화 실패 세부 결과:", result.details);
|
||||
|
||||
const title = "동기화 실패";
|
||||
const description = "데이터 동기화 중 문제가 발생했습니다. 다시 시도해주세요.";
|
||||
// 실패 카운터 증가 및 최대 알림 횟수 제한
|
||||
syncFailureCount++;
|
||||
|
||||
// 토스트 표시
|
||||
toast({
|
||||
title,
|
||||
description,
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
// 알림 추가 (설정된 경우)
|
||||
if (notificationAdder) {
|
||||
notificationAdder(title, description);
|
||||
if (syncFailureCount <= MAX_SYNC_FAILURE_NOTIFICATIONS) {
|
||||
const title = "동기화 실패";
|
||||
const description = "데이터 동기화 중 문제가 발생했습니다. 나중에 다시 시도합니다.";
|
||||
|
||||
// 토스트 표시
|
||||
toast({
|
||||
title,
|
||||
description,
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
// 알림 추가 (설정된 경우)
|
||||
if (notificationAdder) {
|
||||
notificationAdder(title, description);
|
||||
}
|
||||
} else {
|
||||
console.log(`동기화 실패 알림 제한 (${syncFailureCount}회 실패)`);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -85,3 +99,8 @@ export const useSyncNotifications = () => {
|
||||
return () => setSyncNotificationAdder(null);
|
||||
}, [addNotification]);
|
||||
};
|
||||
|
||||
// 동기화 실패 카운터 초기화 함수 (로그인/로그아웃 시 사용)
|
||||
export const resetSyncFailureCount = () => {
|
||||
syncFailureCount = 0;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user