Fix transaction deletion issue

Addresses the issue where deleting a transaction in the transaction history would cause the application to freeze.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-18 03:51:17 +00:00
parent 7f30d08466
commit 27b4e3274e
4 changed files with 126 additions and 99 deletions

View File

@@ -6,7 +6,7 @@ import { handleDeleteStorage } from './deleteTransactionStorage';
import { sortTransactionsByDate } from './deleteTransactionUtils';
/**
* 트랜잭션 삭제 핵심 기능
* 트랜잭션 삭제 핵심 기능 - 성능 및 안정성 개선
*/
export const useDeleteTransactionCore = (
transactions: Transaction[],
@@ -14,37 +14,31 @@ export const useDeleteTransactionCore = (
user: any,
pendingDeletionRef: MutableRefObject<Set<string>>
) => {
// 트랜잭션 삭제 - 안정성과 성능 개선 버전
// 트랜잭션 삭제 - 성능 및 안정성 개선 버전
return useCallback((id: string): Promise<boolean> => {
// pendingDeletionRef 초기화 확인
if (!pendingDeletionRef.current) {
pendingDeletionRef.current = new Set<string>();
}
// 프로미스 객체와 취소 함수 참조를 위한 변수 선언
let promiseObj: Promise<boolean> & { cancel?: () => void };
// 기존 promise를 변수로 저장해서 참조 가능하게 함
promiseObj = new Promise<boolean>((resolve, reject) => {
// 삭제 작업 취소 플래그 초기화 - 프로미스 내부에서 선언
let isCanceled = false;
// 삭제 작업 중복 방지
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);
// 이미 삭제 중인 트랜잭션인지 확인
if (pendingDeletionRef.current.has(id)) {
console.warn('이미 삭제 중인 트랜잭션입니다:', id);
return resolve(false);
}
// 삭제할 트랜잭션이 존재하는지 확인 및 데이터 복사 보관
const transactionToDelete = transactions.find(t => t.id === id);
if (!transactionToDelete) {
console.warn('삭제할 트랜잭션이 존재하지 않음:', id);
return resolve(false);
}
// 삭제 중인 상태로 표시
pendingDeletionRef.current.add(id);
@@ -54,45 +48,36 @@ export const useDeleteTransactionCore = (
// UI 업데이트 - 동기식 처리
setTransactions(updatedTransactions);
// 사용자 인터페이스 응답성 감소 전에 이벤트 발생 처리
try {
// 상태 업데이트 바로 후 이벤트 발생
setTimeout(() => {
try {
window.dispatchEvent(new Event('transactionUpdated'));
} catch (innerError) {
console.warn('이벤트 발생 중 비치명적 오류:', innerError);
}
}, 0);
} catch (eventError) {
console.warn('이벤트 디스패치 설정 오류:', eventError);
}
// 백그라운드 작업 처리
setTimeout(() => {
if (isCanceled) {
console.log('작업이 취소되었습니다.');
return;
}
// 스토리지 처리
handleDeleteStorage(
isCanceled,
updatedTransactions,
id,
user,
transactionToDelete,
pendingDeletionRef
);
// 작업 완료 후 보류 중인 삭제 목록에서 제거
pendingDeletionRef.current?.delete(id);
}, 50);
// 상태 업데이트가 이미 수행되었으므로 즉시 성공 반환
console.log('트랜잭션 삭제 UI 업데이트 완료');
// UI 업데이트 완료 후 즉시 성공 반환 (사용자 경험 개선)
resolve(true);
// 백그라운드에서 스토리지 작업 처리
setTimeout(() => {
try {
// 스토리지 처리
handleDeleteStorage(
false, // 취소 상태 없음
updatedTransactions,
id,
user,
transactionToDelete,
pendingDeletionRef
);
} catch (storageError) {
console.error('스토리지 작업 오류:', storageError);
} finally {
// 작업 완료 후 보류 중인 삭제 목록에서 제거
pendingDeletionRef.current?.delete(id);
// 트랜잭션 삭제 완료 이벤트 발생
try {
window.dispatchEvent(new Event('transactionDeleted'));
} catch (e) {
console.warn('이벤트 발생 오류:', e);
}
}
}, 50);
} catch (error) {
console.error('트랜잭션 삭제 초기화 중 오류:', error);
@@ -106,17 +91,8 @@ export const useDeleteTransactionCore = (
// 캣치된 모든 오류에서 보류 삭제 표시 제거
pendingDeletionRef.current?.delete(id);
reject(error);
resolve(false);
}
// cancel 함수를 프로미스 객체에 연결 (프로미스 내부에서)
promiseObj.cancel = () => {
isCanceled = true;
pendingDeletionRef.current?.delete(id);
console.log('트랜잭션 삭제 작업 취소 완료');
};
});
return promiseObj;
}, [transactions, setTransactions, user]);
};