Files
zellyy-finance/src/hooks/sync/useManualSync.ts
gpt-engineer-app[bot] 8e6eb9d8aa Review and refine sync logic
This commit reviews and refines the synchronization logic to ensure proper functionality and data integrity.
2025-03-21 11:24:40 +00:00

68 lines
1.7 KiB
TypeScript

import { useState } from 'react';
import { toast } from '@/hooks/useToast.wrapper';
import { trySyncAllData, setLastSyncTime } from '@/utils/syncUtils';
import { handleSyncResult } from './syncResultHandler';
/**
* 수동 동기화 기능을 위한 커스텀 훅
*/
export const useManualSync = (user: any) => {
const [syncing, setSyncing] = useState(false);
// 수동 동기화 핸들러
const handleManualSync = async () => {
if (!user) {
toast({
title: "로그인 필요",
description: "데이터 동기화를 위해 로그인이 필요합니다.",
variant: "destructive"
});
return;
}
// 이미 동기화 중이면 중복 실행 방지
if (syncing) {
console.log('이미 동기화가 진행 중입니다.');
return;
}
await performSync(user.id);
};
// 실제 동기화 수행 함수
const performSync = async (userId: string) => {
if (!userId) return;
try {
setSyncing(true);
console.log('수동 동기화 시작');
// 동기화 실행
const result = await trySyncAllData(userId);
// 동기화 결과 처리
handleSyncResult(result);
// 동기화 시간 업데이트
if (result.success) {
setLastSyncTime(new Date().toISOString());
}
return result;
} catch (error) {
console.error('동기화 오류:', error);
toast({
title: "동기화 오류",
description: "동기화 중 문제가 발생했습니다. 다시 시도해주세요.",
variant: "destructive"
});
} finally {
setSyncing(false);
console.log('수동 동기화 종료');
}
};
return { syncing, handleManualSync };
};