Clarify data synchronization process

Clarify the data synchronization process and address a scenario where data is lost after logging out, initializing data, and logging back in.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 09:58:20 +00:00
parent 3f22e6c484
commit bdf1584095
4 changed files with 303 additions and 71 deletions

View File

@@ -2,12 +2,13 @@
import React, { useState, useEffect } from 'react';
import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
import { CloudUpload, RefreshCw } from "lucide-react";
import { CloudUpload, RefreshCw, AlertCircle } from "lucide-react";
import { isSyncEnabled, setSyncEnabled, syncAllData, getLastSyncTime } from "@/utils/syncUtils";
import { toast } from "@/hooks/useToast.wrapper";
import { useAuth } from "@/contexts/auth";
import { useNavigate } from "react-router-dom";
import { Button } from "@/components/ui/button";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
const SyncSettings = () => {
const [enabled, setEnabled] = useState(isSyncEnabled());
@@ -19,6 +20,18 @@ const SyncSettings = () => {
useEffect(() => {
// 마지막 동기화 시간 업데이트
setLastSync(getLastSyncTime());
// 세션 상태 변경 감지
const handleAuthChange = () => {
setEnabled(isSyncEnabled());
setLastSync(getLastSyncTime());
};
window.addEventListener('auth-state-changed', handleAuthChange);
return () => {
window.removeEventListener('auth-state-changed', handleAuthChange);
};
}, []);
const handleSyncToggle = async (checked: boolean) => {
@@ -50,6 +63,9 @@ const SyncSettings = () => {
description: "동기화 중 문제가 발생했습니다. 다시 시도해주세요.",
variant: "destructive"
});
// 실패 시 동기화 비활성화
setEnabled(false);
setSyncEnabled(false);
} finally {
setSyncing(false);
}
@@ -116,18 +132,32 @@ const SyncSettings = () => {
{enabled && (
<div className="space-y-3">
<div className="flex justify-between items-center text-sm">
<span className="text-muted-foreground"> : {formatLastSyncTime()}</span>
{user ? (
<button
onClick={handleManualSync}
disabled={syncing}
className="neuro-button py-1 px-3 flex items-center gap-1 bg-neuro-income text-white hover:bg-neuro-income/90"
>
<RefreshCw className={`h-4 w-4 ${syncing ? 'animate-spin' : ''}`} />
<span>{syncing ? '동기화 중...' : '지금 동기화'}</span>
</button>
) : (
{user ? (
<>
<div className="flex justify-between items-center text-sm">
<span className="text-muted-foreground"> : {formatLastSyncTime()}</span>
<button
onClick={handleManualSync}
disabled={syncing}
className="neuro-button py-1 px-3 flex items-center gap-1 bg-neuro-income text-white hover:bg-neuro-income/90"
>
<RefreshCw className={`h-4 w-4 ${syncing ? 'animate-spin' : ''}`} />
<span>{syncing ? '동기화 중...' : '지금 동기화'}</span>
</button>
</div>
<Alert className="bg-amber-50 text-amber-800 border-amber-200">
<AlertCircle className="h-4 w-4" />
<AlertTitle> </AlertTitle>
<AlertDescription className="text-sm">
. .
.
</AlertDescription>
</Alert>
</>
) : (
<div className="flex justify-between items-center">
<span className="text-sm text-muted-foreground"> </span>
<Button
onClick={() => navigate('/login')}
size="sm"
@@ -135,8 +165,8 @@ const SyncSettings = () => {
>
</Button>
)}
</div>
</div>
)}
</div>
)}
</div>