Investigate transaction deletion issue
Further investigate the issue where deleting transactions causes the application to freeze.
This commit is contained in:
@@ -20,83 +20,62 @@ export const useDeleteTransactionCore = (
|
||||
try {
|
||||
console.log('트랜잭션 삭제 작업 시작 - ID:', id);
|
||||
|
||||
// pendingDeletionRef 초기화 확인
|
||||
if (!pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current = new Set<string>();
|
||||
}
|
||||
|
||||
// 이미 삭제 중인지 확인
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
console.log('이미 삭제 중인 트랜잭션:', id);
|
||||
// 전달된 ID로 트랜잭션 찾기
|
||||
const transactionToDelete = transactions.find(t => t.id === id);
|
||||
|
||||
// 트랜잭션이 존재하는지 확인
|
||||
if (!transactionToDelete) {
|
||||
console.warn('삭제할 트랜잭션이 없음:', id);
|
||||
toast({
|
||||
title: "처리 중",
|
||||
description: "이전 삭제 작업이 진행 중입니다.",
|
||||
duration: 1500
|
||||
title: "삭제 실패",
|
||||
description: "해당 항목을 찾을 수 없습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 트랜잭션이 존재하는지 확인
|
||||
const transactionToDelete = transactions.find(t => t.id === id);
|
||||
if (!transactionToDelete) {
|
||||
console.warn('삭제할 트랜잭션이 존재하지 않음:', id);
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 삭제 중인 상태로 표시
|
||||
pendingDeletionRef.current.add(id);
|
||||
|
||||
// 즉시 상태 업데이트 (현재 상태 복사를 통한 안전한 처리)
|
||||
const updatedTransactions = transactions.filter(transaction => transaction.id !== id);
|
||||
|
||||
// UI 업데이트 - 동기식 처리
|
||||
// 즉시 상태 업데이트 (현재 상태에서 삭제할 항목 필터링)
|
||||
const updatedTransactions = transactions.filter(t => t.id !== id);
|
||||
setTransactions(updatedTransactions);
|
||||
|
||||
// 삭제 성공 토스트
|
||||
toast({
|
||||
title: "삭제 완료",
|
||||
description: "지출 항목이 삭제되었습니다.",
|
||||
duration: 2000
|
||||
duration: 1500
|
||||
});
|
||||
|
||||
// UI 업데이트 완료 후 즉시 성공 반환 (사용자 경험 개선)
|
||||
resolve(true);
|
||||
|
||||
// 백그라운드에서 스토리지 작업 처리
|
||||
setTimeout(() => {
|
||||
try {
|
||||
// 스토리지 처리 - 간소화된 함수 호출
|
||||
handleDeleteStorage(
|
||||
updatedTransactions,
|
||||
id,
|
||||
user,
|
||||
pendingDeletionRef
|
||||
);
|
||||
} catch (storageError) {
|
||||
console.error('스토리지 작업 오류:', storageError);
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
}
|
||||
}, 50);
|
||||
|
||||
// 스토리지 업데이트 작업 시작 (백그라운드)
|
||||
try {
|
||||
// 스토리지 처리 (스토리지 및 Supabase 업데이트)
|
||||
handleDeleteStorage(
|
||||
updatedTransactions,
|
||||
id,
|
||||
user,
|
||||
pendingDeletionRef
|
||||
);
|
||||
|
||||
// 이벤트 발생
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('transactionDeleted'));
|
||||
}, 100);
|
||||
|
||||
console.log('삭제 작업 완료:', id);
|
||||
resolve(true);
|
||||
} catch (storageError) {
|
||||
console.error('스토리지 작업 오류:', storageError);
|
||||
// 스토리지 오류가 발생해도 UI는 이미 업데이트되었으므로 true 반환
|
||||
resolve(true);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 삭제 초기화 중 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 표시
|
||||
console.error('트랜잭션 삭제 오류:', error);
|
||||
toast({
|
||||
title: "삭제 실패",
|
||||
description: "지출 삭제 중 오류가 발생했습니다.",
|
||||
duration: 2000,
|
||||
duration: 1500,
|
||||
variant: "destructive"
|
||||
});
|
||||
|
||||
// 캣치된 모든 오류에서 보류 삭제 표시 제거
|
||||
if (pendingDeletionRef.current) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user