Fix recent transactions deletion
Fixes an issue where deleting transactions from the recent expenses section of the homepage resulted in a malfunction.
This commit is contained in:
@@ -6,7 +6,7 @@ import { deleteTransactionFromSupabase } from '../../supabaseUtils';
|
||||
import { toast } from '@/hooks/useToast.wrapper';
|
||||
|
||||
/**
|
||||
* 스토리지 및 Supabase 삭제 처리 - 안정성 개선 버전
|
||||
* 스토리지 및 Supabase 삭제 처리 - 완전히 개선된 버전
|
||||
*/
|
||||
export const handleDeleteStorage = (
|
||||
updatedTransactions: Transaction[],
|
||||
@@ -15,84 +15,60 @@ export const handleDeleteStorage = (
|
||||
pendingDeletionRef: MutableRefObject<Set<string>>
|
||||
): Promise<boolean> => {
|
||||
return new Promise((resolve) => {
|
||||
// 연속 호출 방지 (이미 완료된 요청일 경우 처리하지 않음)
|
||||
if (!pendingDeletionRef.current.has(id)) {
|
||||
console.warn('삭제 요청이 이미 완료되었습니다. ID:', id);
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 로컬 스토리지 업데이트 (동기 처리)
|
||||
// 로컬 스토리지 동기 처리 (즉시 처리)
|
||||
try {
|
||||
saveTransactionsToStorage(updatedTransactions);
|
||||
console.log('로컬 스토리지 저장 완료 (ID: ' + id + ')');
|
||||
} catch (storageError) {
|
||||
console.error('로컬 스토리지 저장 실패:', storageError);
|
||||
// 오류가 있어도 계속 진행
|
||||
}
|
||||
|
||||
// Supabase 업데이트 (비동기 처리)
|
||||
// Supabase 처리 (비동기)
|
||||
if (user) {
|
||||
let isCompleted = false;
|
||||
|
||||
// 10초 타임아웃 설정
|
||||
// 최대 3초 타임아웃 (UI 블로킹 방지)
|
||||
const timeoutId = setTimeout(() => {
|
||||
if (!isCompleted) {
|
||||
console.warn('Supabase 삭제 작업 타임아웃:', id);
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
if (!isCompleted) {
|
||||
isCompleted = true;
|
||||
resolve(true); // UI 업데이트는 이미 완료되었으므로 성공으로 처리
|
||||
}
|
||||
}
|
||||
}, 5000);
|
||||
console.log('Supabase 삭제 타임아웃 - UI 정상화 처리');
|
||||
pendingDeletionRef.current.delete(id);
|
||||
resolve(true);
|
||||
}, 3000);
|
||||
|
||||
// Supabase 호출 시도
|
||||
try {
|
||||
// Supabase 호출 (삭제)
|
||||
deleteTransactionFromSupabase(user, id)
|
||||
.then(() => {
|
||||
console.log('Supabase 삭제 완료:', id);
|
||||
clearTimeout(timeoutId); // 타임아웃 제거
|
||||
pendingDeletionRef.current.delete(id);
|
||||
resolve(true);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Supabase 삭제 오류:', error);
|
||||
})
|
||||
.finally(() => {
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
|
||||
if (!isCompleted) {
|
||||
isCompleted = true;
|
||||
resolve(true);
|
||||
}
|
||||
clearTimeout(timeoutId); // 타임아웃 제거
|
||||
pendingDeletionRef.current.delete(id);
|
||||
resolve(true); // UI는 이미 업데이트되어 있으므로 true 반환
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Supabase 작업 오류:', e);
|
||||
console.error('Supabase 작업 시작 실패:', e);
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
|
||||
if (!isCompleted) {
|
||||
isCompleted = true;
|
||||
resolve(true);
|
||||
}
|
||||
pendingDeletionRef.current.delete(id);
|
||||
resolve(true);
|
||||
}
|
||||
} else {
|
||||
// 로그인 안한 사용자는 바로 완료 처리
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
// 로그인하지 않은 경우 바로 완료 처리
|
||||
pendingDeletionRef.current.delete(id);
|
||||
resolve(true);
|
||||
}
|
||||
} catch (storageError) {
|
||||
console.error('스토리지 작업 중 오류:', storageError);
|
||||
|
||||
// 작업 완료 표시 (오류 발생해도 필수)
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
|
||||
// 실패해도 UI는 업데이트 완료된 상태로 간주
|
||||
} catch (error) {
|
||||
console.error('스토리지 작업 중 일반 오류:', error);
|
||||
pendingDeletionRef.current.delete(id); // 항상 pending 상태 제거
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user