Review and refine sync logic
This commit reviews and refines the synchronization logic to ensure proper functionality and data integrity.
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
import { toast } from '@/hooks/useToast.wrapper';
|
||||
import { trySyncAllData } from '@/utils/syncUtils';
|
||||
import { getLastSyncTime, setLastSyncTime } from '@/utils/syncUtils';
|
||||
import { trySyncAllData, setLastSyncTime } from '@/utils/syncUtils';
|
||||
import { handleSyncResult } from './syncResultHandler';
|
||||
import type { SyncResult } from '@/utils/syncUtils';
|
||||
|
||||
/**
|
||||
* 수동 동기화 기능을 위한 커스텀 훅
|
||||
@@ -23,6 +21,12 @@ export const useManualSync = (user: any) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 이미 동기화 중이면 중복 실행 방지
|
||||
if (syncing) {
|
||||
console.log('이미 동기화가 진행 중입니다.');
|
||||
return;
|
||||
}
|
||||
|
||||
await performSync(user.id);
|
||||
};
|
||||
|
||||
@@ -32,11 +36,20 @@ export const useManualSync = (user: any) => {
|
||||
|
||||
try {
|
||||
setSyncing(true);
|
||||
// 인자 수정: userId만 전달
|
||||
console.log('수동 동기화 시작');
|
||||
|
||||
// 동기화 실행
|
||||
const result = await trySyncAllData(userId);
|
||||
|
||||
// 동기화 결과 처리
|
||||
handleSyncResult(result);
|
||||
setLastSyncTime(new Date().toISOString());
|
||||
|
||||
// 동기화 시간 업데이트
|
||||
if (result.success) {
|
||||
setLastSyncTime(new Date().toISOString());
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('동기화 오류:', error);
|
||||
toast({
|
||||
@@ -46,6 +59,7 @@ export const useManualSync = (user: any) => {
|
||||
});
|
||||
} finally {
|
||||
setSyncing(false);
|
||||
console.log('수동 동기화 종료');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,29 +3,71 @@ import { useState, useEffect } from 'react';
|
||||
import { getLastSyncTime } from '@/utils/syncUtils';
|
||||
|
||||
/**
|
||||
* 동기화 상태와 마지막 동기화 시간을 관리하는 커스텀 훅
|
||||
* 동기화 상태 관리를 위한 커스텀 훅
|
||||
*/
|
||||
export const useSyncStatus = () => {
|
||||
const [lastSync, setLastSync] = useState<string | null>(getLastSyncTime());
|
||||
|
||||
// 마지막 동기화 시간 정기적으로 업데이트
|
||||
useEffect(() => {
|
||||
const intervalId = setInterval(() => {
|
||||
setLastSync(getLastSyncTime());
|
||||
}, 10000); // 10초마다 업데이트
|
||||
|
||||
return () => clearInterval(intervalId);
|
||||
}, []);
|
||||
|
||||
|
||||
// 마지막 동기화 시간 포맷팅
|
||||
const formatLastSyncTime = () => {
|
||||
if (!lastSync) return "아직 동기화된 적 없음";
|
||||
|
||||
if (lastSync === '부분-다운로드') return "부분 동기화 (다운로드만)";
|
||||
|
||||
const date = new Date(lastSync);
|
||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
|
||||
};
|
||||
const formatLastSyncTime = (): string => {
|
||||
if (!lastSync) {
|
||||
return '없음';
|
||||
}
|
||||
|
||||
return { lastSync, formatLastSyncTime };
|
||||
try {
|
||||
const date = new Date(lastSync);
|
||||
|
||||
// 유효한 날짜인지 확인
|
||||
if (isNaN(date.getTime())) {
|
||||
return '없음';
|
||||
}
|
||||
|
||||
// 오늘 날짜인 경우
|
||||
const today = new Date();
|
||||
const isToday = date.getDate() === today.getDate() &&
|
||||
date.getMonth() === today.getMonth() &&
|
||||
date.getFullYear() === today.getFullYear();
|
||||
|
||||
if (isToday) {
|
||||
// 시간만 표시
|
||||
return `오늘 ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// 어제 날짜인 경우
|
||||
const yesterday = new Date(today);
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
const isYesterday = date.getDate() === yesterday.getDate() &&
|
||||
date.getMonth() === yesterday.getMonth() &&
|
||||
date.getFullYear() === yesterday.getFullYear();
|
||||
|
||||
if (isYesterday) {
|
||||
return `어제 ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// 그 외 날짜
|
||||
return `${date.getFullYear()}/${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`;
|
||||
} catch (error) {
|
||||
console.error('날짜 포맷 오류:', error);
|
||||
return '없음';
|
||||
}
|
||||
};
|
||||
|
||||
// 동기화 시간이 변경될 때 상태 업데이트
|
||||
useEffect(() => {
|
||||
const updateLastSyncTime = () => {
|
||||
setLastSync(getLastSyncTime());
|
||||
};
|
||||
|
||||
// 이벤트 리스너 등록
|
||||
window.addEventListener('syncTimeUpdated', updateLastSyncTime);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('syncTimeUpdated', updateLastSyncTime);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
lastSync,
|
||||
formatLastSyncTime
|
||||
};
|
||||
};
|
||||
|
||||
@@ -82,7 +82,7 @@ export const useSyncToggle = () => {
|
||||
|
||||
if (checked && user) {
|
||||
try {
|
||||
// 인자 수정: userId만 전달
|
||||
// 동기화 수행
|
||||
await performSync(user.id);
|
||||
} catch (error) {
|
||||
console.error('동기화 중 오류, 로컬 데이터 복원 시도:', error);
|
||||
@@ -120,7 +120,6 @@ const performSync = async (userId: string) => {
|
||||
if (!userId) return;
|
||||
|
||||
try {
|
||||
// 인자 수정: userId만 전달
|
||||
const result = await trySyncAllData(userId);
|
||||
return result;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user