Fix transaction deletion issue

Addresses the issue where deleting transactions would sometimes cause the application to freeze.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-18 04:05:23 +00:00
parent a63c8f1b16
commit 1fc2ee8a15
4 changed files with 70 additions and 48 deletions

View File

@@ -3,7 +3,6 @@ import { useCallback, MutableRefObject } from 'react';
import { Transaction } from '@/components/TransactionCard';
import { toast } from '@/hooks/useToast.wrapper';
import { handleDeleteStorage } from './deleteTransactionStorage';
import { sortTransactionsByDate } from './deleteTransactionUtils';
/**
* 트랜잭션 삭제 핵심 기능 - 성능 및 안정성 개선
@@ -16,29 +15,24 @@ export const useDeleteTransactionCore = (
) => {
// 트랜잭션 삭제 - 성능 및 안정성 개선 버전
return useCallback((id: string): Promise<boolean> => {
// pendingDeletionRef 초기화 확인
if (!pendingDeletionRef.current) {
pendingDeletionRef.current = new Set<string>();
}
// 삭제 작업 중복 방지
if (pendingDeletionRef.current.has(id)) {
console.log('이미 삭제 중인 트랜잭션:', id);
return Promise.resolve(false);
}
// 트랜잭션이 존재하는지 확인
const transactionToDelete = transactions.find(t => t.id === id);
if (!transactionToDelete) {
console.warn('삭제할 트랜잭션이 존재하지 않음:', id);
return Promise.resolve(false);
}
// 프로미스 생성
return new Promise<boolean>((resolve) => {
try {
console.log('트랜잭션 삭제 작업 시작 - ID:', id);
// pendingDeletionRef 초기화 확인
if (!pendingDeletionRef.current) {
pendingDeletionRef.current = new Set<string>();
}
// 트랜잭션이 존재하는지 확인
const transactionToDelete = transactions.find(t => t.id === id);
if (!transactionToDelete) {
console.warn('삭제할 트랜잭션이 존재하지 않음:', id);
resolve(false);
return;
}
// 삭제 중인 상태로 표시
pendingDeletionRef.current.add(id);
@@ -51,7 +45,7 @@ export const useDeleteTransactionCore = (
// UI 업데이트 완료 후 즉시 성공 반환 (사용자 경험 개선)
resolve(true);
// 백그라운드에서 스토리지 작업 처리
// 백그라운드에서 스토리지 작업 처리 (setTimeout 사용해 메인 스레드 차단 방지)
setTimeout(() => {
try {
// 스토리지 처리
@@ -67,7 +61,9 @@ export const useDeleteTransactionCore = (
console.error('스토리지 작업 오류:', storageError);
} finally {
// 작업 완료 후 보류 중인 삭제 목록에서 제거
pendingDeletionRef.current?.delete(id);
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
// 트랜잭션 삭제 완료 이벤트 발생
try {
@@ -90,7 +86,9 @@ export const useDeleteTransactionCore = (
});
// 캣치된 모든 오류에서 보류 삭제 표시 제거
pendingDeletionRef.current?.delete(id);
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
resolve(false);
}
});