Fix welcome message and sync
- Prevent duplicate welcome messages. - Remove sync notifications. - Ensure automatic sync updates last sync time.
This commit is contained in:
@@ -1,28 +1,45 @@
|
||||
|
||||
import { SyncResult } from '@/utils/sync/data';
|
||||
import { toast } from '@/hooks/useToast.wrapper';
|
||||
import useNotifications from '@/hooks/useNotifications';
|
||||
|
||||
// 알림 인스턴스 얻기 위한 전역 변수
|
||||
let notificationAdder: ((title: string, message: string) => void) | null = null;
|
||||
|
||||
// 알림 함수 설정
|
||||
export const setSyncNotificationAdder = (adder: (title: string, message: string) => void) => {
|
||||
notificationAdder = adder;
|
||||
};
|
||||
|
||||
// 동기화 결과 처리 함수
|
||||
export const handleSyncResult = (result: SyncResult) => {
|
||||
if (result.success) {
|
||||
let title = '';
|
||||
let description = '';
|
||||
|
||||
if (result.uploadSuccess && result.downloadSuccess) {
|
||||
// 양방향 동기화 성공
|
||||
toast({
|
||||
title: "동기화 완료",
|
||||
description: "모든 데이터가 성공적으로 동기화되었습니다.",
|
||||
});
|
||||
title = "동기화 완료";
|
||||
description = "모든 데이터가 성공적으로 동기화되었습니다.";
|
||||
} else if (result.uploadSuccess) {
|
||||
// 업로드만 성공
|
||||
toast({
|
||||
title: "동기화 완료",
|
||||
description: "로컬 데이터가 클라우드에 업로드되었습니다.",
|
||||
});
|
||||
title = "동기화 완료";
|
||||
description = "로컬 데이터가 클라우드에 업로드되었습니다.";
|
||||
} else if (result.downloadSuccess) {
|
||||
// 다운로드만 성공
|
||||
toast({
|
||||
title: "동기화 완료",
|
||||
description: "클라우드 데이터가 기기에 다운로드되었습니다.",
|
||||
});
|
||||
title = "동기화 완료";
|
||||
description = "클라우드 데이터가 기기에 다운로드되었습니다.";
|
||||
}
|
||||
|
||||
// 토스트 표시
|
||||
toast({
|
||||
title,
|
||||
description,
|
||||
});
|
||||
|
||||
// 알림 추가 (설정된 경우)
|
||||
if (notificationAdder) {
|
||||
notificationAdder(title, description);
|
||||
}
|
||||
|
||||
// 상세 결과 로깅
|
||||
@@ -38,12 +55,32 @@ export const handleSyncResult = (result: SyncResult) => {
|
||||
// 동기화 실패
|
||||
console.error("동기화 실패 세부 결과:", result.details);
|
||||
|
||||
const title = "동기화 실패";
|
||||
const description = "데이터 동기화 중 문제가 발생했습니다. 다시 시도해주세요.";
|
||||
|
||||
// 토스트 표시
|
||||
toast({
|
||||
title: "동기화 실패",
|
||||
description: "데이터 동기화 중 문제가 발생했습니다. 다시 시도해주세요.",
|
||||
title,
|
||||
description,
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
// 알림 추가 (설정된 경우)
|
||||
if (notificationAdder) {
|
||||
notificationAdder(title, description);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// 커스텀 훅: 동기화 알림 관리
|
||||
export const useSyncNotifications = () => {
|
||||
const { addNotification } = useNotifications();
|
||||
|
||||
// 컴포넌트 마운트 시 알림 함수 설정
|
||||
React.useEffect(() => {
|
||||
setSyncNotificationAdder(addNotification);
|
||||
return () => setSyncNotificationAdder(null);
|
||||
}, [addNotification]);
|
||||
};
|
||||
|
||||
@@ -3,12 +3,14 @@ import { useState } from 'react';
|
||||
import { toast } from '@/hooks/useToast.wrapper';
|
||||
import { trySyncAllData, setLastSyncTime } from '@/utils/syncUtils';
|
||||
import { handleSyncResult } from './syncResultHandler';
|
||||
import useNotifications from '@/hooks/useNotifications';
|
||||
|
||||
/**
|
||||
* 수동 동기화 기능을 위한 커스텀 훅
|
||||
*/
|
||||
export const useManualSync = (user: any) => {
|
||||
const [syncing, setSyncing] = useState(false);
|
||||
const { addNotification } = useNotifications();
|
||||
|
||||
// 수동 동기화 핸들러
|
||||
const handleManualSync = async () => {
|
||||
@@ -18,6 +20,11 @@ export const useManualSync = (user: any) => {
|
||||
description: "데이터 동기화를 위해 로그인이 필요합니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
addNotification(
|
||||
"로그인 필요",
|
||||
"데이터 동기화를 위해 로그인이 필요합니다."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -38,6 +45,11 @@ export const useManualSync = (user: any) => {
|
||||
setSyncing(true);
|
||||
console.log('수동 동기화 시작');
|
||||
|
||||
addNotification(
|
||||
"동기화 시작",
|
||||
"데이터 동기화가 시작되었습니다."
|
||||
);
|
||||
|
||||
// 동기화 실행
|
||||
const result = await trySyncAllData(userId);
|
||||
|
||||
@@ -59,6 +71,11 @@ export const useManualSync = (user: any) => {
|
||||
description: "동기화 중 문제가 발생했습니다. 다시 시도해주세요.",
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
addNotification(
|
||||
"동기화 오류",
|
||||
"동기화 중 문제가 발생했습니다. 다시 시도해주세요."
|
||||
);
|
||||
} finally {
|
||||
setSyncing(false);
|
||||
console.log('수동 동기화 종료');
|
||||
|
||||
@@ -58,7 +58,8 @@ export const useSyncStatus = () => {
|
||||
|
||||
const updateLastSyncTime = (event?: Event | CustomEvent) => {
|
||||
const newTime = getLastSyncTime();
|
||||
console.log('마지막 동기화 시간 업데이트됨:', newTime, event instanceof CustomEvent ? `(이벤트 상세: ${JSON.stringify(event.detail)})` : '');
|
||||
const eventDetails = event instanceof CustomEvent ? ` (이벤트 상세: ${JSON.stringify(event.detail)})` : '';
|
||||
console.log(`마지막 동기화 시간 업데이트됨: ${newTime} ${eventDetails}`);
|
||||
setLastSync(newTime);
|
||||
};
|
||||
|
||||
@@ -75,12 +76,26 @@ export const useSyncStatus = () => {
|
||||
|
||||
window.addEventListener('storage', handleStorageChange);
|
||||
|
||||
// 동기화 완료 이벤트도 모니터링 (새로 추가)
|
||||
window.addEventListener('syncComplete', updateLastSyncTime);
|
||||
|
||||
// 초기 상태 업데이트
|
||||
updateLastSyncTime();
|
||||
|
||||
// 1초마다 업데이트 상태 확인 (문제 해결을 위한 임시 방안)
|
||||
const checkInterval = setInterval(() => {
|
||||
const currentTime = getLastSyncTime();
|
||||
if (currentTime !== lastSync) {
|
||||
console.log('주기적 확인에서 동기화 시간 변경 감지:', currentTime);
|
||||
setLastSync(currentTime);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('syncTimeUpdated', updateLastSyncTime);
|
||||
window.removeEventListener('storage', handleStorageChange);
|
||||
window.removeEventListener('syncComplete', updateLastSyncTime);
|
||||
clearInterval(checkInterval);
|
||||
};
|
||||
}, [lastSync]);
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@ import { toast } from '@/hooks/useToast.wrapper';
|
||||
import {
|
||||
isSyncEnabled,
|
||||
setSyncEnabled,
|
||||
trySyncAllData
|
||||
trySyncAllData,
|
||||
setLastSyncTime
|
||||
} from '@/utils/syncUtils';
|
||||
import useNotifications from '@/hooks/useNotifications';
|
||||
|
||||
/**
|
||||
* 동기화 토글 기능을 위한 커스텀 훅
|
||||
@@ -14,6 +16,7 @@ import {
|
||||
export const useSyncToggle = () => {
|
||||
const [enabled, setEnabled] = useState(isSyncEnabled());
|
||||
const { user } = useAuth();
|
||||
const { addNotification } = useNotifications();
|
||||
|
||||
// 사용자 로그인 상태 변경 감지
|
||||
useEffect(() => {
|
||||
@@ -60,6 +63,11 @@ export const useSyncToggle = () => {
|
||||
description: "데이터 동기화를 위해 로그인이 필요합니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
addNotification(
|
||||
"로그인 필요",
|
||||
"데이터 동기화를 위해 로그인이 필요합니다."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -77,13 +85,28 @@ export const useSyncToggle = () => {
|
||||
setEnabled(checked);
|
||||
setSyncEnabled(checked);
|
||||
|
||||
// 동기화 활성화/비활성화 알림 추가
|
||||
addNotification(
|
||||
checked ? "동기화 활성화" : "동기화 비활성화",
|
||||
checked
|
||||
? "데이터가 클라우드와 동기화됩니다."
|
||||
: "클라우드 동기화가 중지되었습니다."
|
||||
);
|
||||
|
||||
// 이벤트 트리거
|
||||
window.dispatchEvent(new Event('auth-state-changed'));
|
||||
|
||||
if (checked && user) {
|
||||
try {
|
||||
// 동기화 수행
|
||||
await performSync(user.id);
|
||||
const result = await performSync(user.id);
|
||||
|
||||
// 동기화 성공 시 마지막 동기화 시간 업데이트
|
||||
if (result && result.success) {
|
||||
const currentTime = new Date().toISOString();
|
||||
console.log('동기화 활성화 후 첫 동기화 성공, 시간 업데이트:', currentTime);
|
||||
setLastSyncTime(currentTime);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('동기화 중 오류, 로컬 데이터 복원 시도:', error);
|
||||
|
||||
@@ -108,6 +131,11 @@ export const useSyncToggle = () => {
|
||||
description: "동기화 중 문제가 발생하여 로컬 데이터가 복원되었습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
addNotification(
|
||||
"동기화 오류",
|
||||
"동기화 중 문제가 발생하여 로컬 데이터가 복원되었습니다."
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user