From a4533aea70147e6e308f287a1b4d3a4ad44061f0 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Tue, 18 Mar 2025 01:21:54 +0000 Subject: [PATCH] Retain sync setting on reset After data reset, the sync setting should remain in its previous state (On or Off) instead of always defaulting to Off. --- src/components/security/DataResetDialog.tsx | 9 +++++- src/components/security/DataResetSection.tsx | 9 +++++- src/hooks/useDataReset.ts | 31 +++++++++++++++----- src/utils/storageUtils.ts | 12 ++++++-- src/utils/sync/clearCloudData.ts | 3 +- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/components/security/DataResetDialog.tsx b/src/components/security/DataResetDialog.tsx index 994061f..644089b 100644 --- a/src/components/security/DataResetDialog.tsx +++ b/src/components/security/DataResetDialog.tsx @@ -18,6 +18,7 @@ interface DataResetDialogProps { onConfirm: () => Promise; isResetting: boolean; isLoggedIn: boolean; + syncEnabled: boolean; } const DataResetDialog: React.FC = ({ @@ -25,7 +26,8 @@ const DataResetDialog: React.FC = ({ onOpenChange, onConfirm, isResetting, - isLoggedIn + isLoggedIn, + syncEnabled }) => { return ( @@ -40,6 +42,11 @@ const DataResetDialog: React.FC = ({ 클라우드 데이터도 함께 삭제됩니다. + {syncEnabled && ( +
+ 동기화 설정은 계속 활성화 상태로 유지됩니다. +
+ )} ) : ( "이 작업은 되돌릴 수 없으며, 모든 예산, 지출 내역, 설정이 영구적으로 삭제됩니다." diff --git a/src/components/security/DataResetSection.tsx b/src/components/security/DataResetSection.tsx index a3afce0..adad37d 100644 --- a/src/components/security/DataResetSection.tsx +++ b/src/components/security/DataResetSection.tsx @@ -5,11 +5,13 @@ import { Button } from '@/components/ui/button'; import { useAuth } from '@/contexts/auth/AuthProvider'; import { useDataReset } from '@/hooks/useDataReset'; import DataResetDialog from './DataResetDialog'; +import { isSyncEnabled } from '@/utils/sync/syncSettings'; const DataResetSection = () => { const [isResetDialogOpen, setIsResetDialogOpen] = useState(false); const { user } = useAuth(); const { isResetting, resetAllData } = useDataReset(); + const syncEnabled = isSyncEnabled(); const handleResetAllData = async () => { await resetAllData(); @@ -26,7 +28,11 @@ const DataResetSection = () => {

데이터 초기화

- {user ? "로컬 및 클라우드의 모든 예산, 지출 내역이 초기화됩니다." : "모든 예산, 지출 내역, 설정이 초기화됩니다."} + {user + ? syncEnabled + ? "로컬 및 클라우드의 모든 예산, 지출 내역이 초기화됩니다. 동기화 설정은 유지됩니다." + : "로컬 및 클라우드의 모든 예산, 지출 내역이 초기화됩니다." + : "모든 예산, 지출 내역, 설정이 초기화됩니다."}

@@ -46,6 +52,7 @@ const DataResetSection = () => { onConfirm={handleResetAllData} isResetting={isResetting} isLoggedIn={!!user} + syncEnabled={syncEnabled} /> ); diff --git a/src/hooks/useDataReset.ts b/src/hooks/useDataReset.ts index c41c9d6..7d98fc3 100644 --- a/src/hooks/useDataReset.ts +++ b/src/hooks/useDataReset.ts @@ -5,7 +5,7 @@ import { useToast } from '@/hooks/useToast.wrapper'; import { resetAllStorageData } from '@/utils/storageUtils'; import { clearCloudData } from '@/utils/syncUtils'; import { useAuth } from '@/contexts/auth/AuthProvider'; -import { setSyncEnabled } from '@/utils/sync/syncSettings'; +import { isSyncEnabled } from '@/utils/sync/syncSettings'; export interface DataResetResult { isCloudResetSuccess: boolean | null; @@ -23,6 +23,10 @@ export const useDataReset = () => { setIsResetting(true); console.log('모든 데이터 초기화 시작'); + // 현재 동기화 설정 저장 + const syncWasEnabled = isSyncEnabled(); + console.log('데이터 초기화 전 동기화 상태:', syncWasEnabled ? '활성화' : '비활성화'); + // 클라우드 데이터 초기화 먼저 시도 (로그인 상태인 경우) let cloudResetSuccess = false; if (user) { @@ -32,8 +36,8 @@ export const useDataReset = () => { if (cloudResetSuccess) { console.log('클라우드 데이터 초기화 성공'); - // 동기화 비활성화 (중요: 초기화 후 자동 동기화 방지) - setSyncEnabled(false); + // 주석 처리 - 동기화 설정 유지 + // setSyncEnabled(false); } else { console.warn('클라우드 데이터 초기화 실패 또는 부분 성공'); } @@ -65,6 +69,12 @@ export const useDataReset = () => { } } + // 동기화 설정 백업 + if (syncWasEnabled) { + authBackupItems['syncEnabled'] = 'true'; + console.log('동기화 설정 백업: 활성화 상태'); + } + // 데이터 초기화 resetAllStorageData(); @@ -85,12 +95,15 @@ export const useDataReset = () => { } }); - // 동기화 설정 초기화 - if (user) { - localStorage.removeItem('lastSync'); - localStorage.setItem('syncEnabled', 'false'); + // 동기화 설정 복원 + if (syncWasEnabled) { + localStorage.setItem('syncEnabled', 'true'); + console.log('동기화 설정 복원: 활성화 상태'); } + // 마지막 동기화 시간은 초기화 + localStorage.removeItem('lastSync'); + // 스토리지 이벤트 트리거하여 다른 컴포넌트에 변경 알림 window.dispatchEvent(new Event('transactionUpdated')); window.dispatchEvent(new Event('budgetDataUpdated')); @@ -102,7 +115,9 @@ export const useDataReset = () => { if (cloudResetSuccess) { toast({ title: "모든 데이터가 초기화되었습니다.", - description: "로컬 및 클라우드의 모든 데이터가 초기화되었습니다. 동기화 기능이 꺼졌습니다.", + description: syncWasEnabled + ? "로컬 및 클라우드의 모든 데이터가 초기화되었습니다. 동기화 설정은 유지됩니다." + : "로컬 및 클라우드의 모든 데이터가 초기화되었습니다.", }); } else { toast({ diff --git a/src/utils/storageUtils.ts b/src/utils/storageUtils.ts index f8ad441..b236316 100644 --- a/src/utils/storageUtils.ts +++ b/src/utils/storageUtils.ts @@ -77,6 +77,8 @@ export const resetAllStorageData = (): void => { const authSession = localStorage.getItem('authSession'); const sbAuth = localStorage.getItem('sb-auth-token'); const supabase = localStorage.getItem('supabase.auth.token'); + // 동기화 설정 백업 (변경된 부분) + const syncEnabled = localStorage.getItem('syncEnabled'); // 모든 Storage 키 목록 (로그인 관련 항목 제외) const keysToRemove = [ @@ -100,7 +102,7 @@ export const resetAllStorageData = (): void => { 'budgetHistory', 'transactionHistory', 'lastSync', - 'syncEnabled' + // 'syncEnabled' 제거됨 - 동기화 설정은 보존 ]; // 키 삭제 @@ -154,13 +156,19 @@ export const resetAllStorageData = (): void => { localStorage.setItem('supabase.auth.token', supabase); } + // 동기화 설정 복원 (변경된 부분) + if (syncEnabled) { + localStorage.setItem('syncEnabled', syncEnabled); + console.log('동기화 설정 복원:', syncEnabled); + } + // 이벤트 발생 window.dispatchEvent(new Event('transactionUpdated')); window.dispatchEvent(new Event('budgetDataUpdated')); window.dispatchEvent(new Event('categoryBudgetsUpdated')); window.dispatchEvent(new StorageEvent('storage')); - console.log('모든 저장소 데이터가 완전히 초기화되었습니다. (로그인 상태는 유지)'); + console.log('모든 저장소 데이터가 완전히 초기화되었습니다. (로그인 상태와 동기화 설정 유지)'); } catch (error) { console.error('데이터 초기화 중 오류:', error); } diff --git a/src/utils/sync/clearCloudData.ts b/src/utils/sync/clearCloudData.ts index 50d718a..92ed4ef 100644 --- a/src/utils/sync/clearCloudData.ts +++ b/src/utils/sync/clearCloudData.ts @@ -54,8 +54,9 @@ export const clearCloudData = async (userId: string): Promise => { } // 동기화 설정 초기화 및 마지막 동기화 시간 초기화 + // 변경: 동기화 설정을 비활성화하지 않고 마지막 동기화 시간만 초기화 localStorage.removeItem('lastSync'); - localStorage.setItem('syncEnabled', 'false'); + // localStorage.setItem('syncEnabled', 'false'); 이 줄 제거 console.log('클라우드 데이터 초기화 완료'); return true;