Ensure sync is disabled

The sync setting was not consistently being disabled in certain scenarios. This change ensures that the sync setting is properly disabled when expected.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-18 01:48:38 +00:00
parent dfb9d8ca69
commit 043db23836
5 changed files with 59 additions and 25 deletions

View File

@@ -36,8 +36,19 @@ export const useSyncToggle = () => {
// 인증 상태 변경 이벤트 리스너
window.addEventListener('auth-state-changed', updateSyncState);
// 스토리지 변경 이벤트에도 동기화 상태 확인 추가
const handleStorageChange = (event: StorageEvent) => {
if (event.key === 'syncEnabled' || event.key === null) {
setEnabled(isSyncEnabled());
console.log('스토리지 변경으로 동기화 상태 업데이트:', isSyncEnabled() ? '활성화' : '비활성화');
}
};
window.addEventListener('storage', handleStorageChange);
return () => {
window.removeEventListener('auth-state-changed', updateSyncState);
window.removeEventListener('storage', handleStorageChange);
};
}, [user]);
@@ -66,6 +77,9 @@ export const useSyncToggle = () => {
setEnabled(checked);
setSyncEnabled(checked);
// 이벤트 트리거
window.dispatchEvent(new Event('auth-state-changed'));
if (checked && user) {
try {
// 동기화 활성화 시 즉시 동기화 실행

View File

@@ -3,9 +3,9 @@ import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useToast } from '@/hooks/useToast.wrapper';
import { resetAllStorageData } from '@/utils/storageUtils';
import { clearCloudData } from '@/utils/syncUtils';
import { clearCloudData } from '@/utils/sync/clearCloudData';
import { useAuth } from '@/contexts/auth/AuthProvider';
import { isSyncEnabled } from '@/utils/sync/syncSettings';
import { isSyncEnabled, setSyncEnabled } from '@/utils/sync/syncSettings';
export interface DataResetResult {
isCloudResetSuccess: boolean | null;
@@ -36,8 +36,6 @@ export const useDataReset = () => {
if (cloudResetSuccess) {
console.log('클라우드 데이터 초기화 성공');
// 주석 처리 - 동기화 설정 유지
// setSyncEnabled(false);
} else {
console.warn('클라우드 데이터 초기화 실패 또는 부분 성공');
}
@@ -69,12 +67,6 @@ export const useDataReset = () => {
}
}
// 동기화 설정 백업
if (syncWasEnabled) {
authBackupItems['syncEnabled'] = 'true';
console.log('동기화 설정 백업: 활성화 상태');
}
// 데이터 초기화
resetAllStorageData();
@@ -95,11 +87,9 @@ export const useDataReset = () => {
}
});
// 동기화 설정 복원
if (syncWasEnabled) {
localStorage.setItem('syncEnabled', 'true');
console.log('동기화 설정 복원: 활성화 상태');
}
// 중요: 동기화 설정은 초기화 후 항상 비활성화
setSyncEnabled(false);
console.log('동기화 설정이 비활성화되었습니다.');
// 마지막 동기화 시간은 초기화
localStorage.removeItem('lastSync');
@@ -109,20 +99,19 @@ export const useDataReset = () => {
window.dispatchEvent(new Event('budgetDataUpdated'));
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
window.dispatchEvent(new StorageEvent('storage'));
window.dispatchEvent(new Event('auth-state-changed')); // 동기화 상태 변경 이벤트 추가
// 클라우드 초기화 상태에 따라 다른 메시지 표시
if (user) {
if (cloudResetSuccess) {
toast({
title: "모든 데이터가 초기화되었습니다.",
description: syncWasEnabled
? "로컬 및 클라우드의 모든 데이터가 초기화되었습니다. 동기화 설정은 유지됩니다."
: "로컬 및 클라우드의 모든 데이터가 초기화되었습니다.",
description: "로컬 및 클라우드의 모든 데이터가 초기화되었으며, 동기화 설정이 비활성화되었습니다.",
});
} else {
toast({
title: "로컬 데이터만 초기화됨",
description: "로컬 데이터는 초기화되었지만, 클라우드 데이터 초기화 중 문제가 발생했습니다.",
description: "로컬 데이터는 초기화되었지만, 클라우드 데이터 초기화 중 문제가 발생했습니다. 동기화 설정이 비활성화되었습니다.",
variant: "destructive"
});
}