Fix transaction deletion issue
Addresses the issue where the transaction history page becomes unresponsive after deleting a transaction.
This commit is contained in:
@@ -25,7 +25,6 @@ export const useDeleteTransactionCore = (
|
||||
const promiseObj = new Promise<boolean>((resolve, reject) => {
|
||||
// 삭제 작업 취소 플래그 초기화
|
||||
let isCanceled = false;
|
||||
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
try {
|
||||
console.log('트랜잭션 삭제 작업 시작 - ID:', id);
|
||||
@@ -33,16 +32,14 @@ export const useDeleteTransactionCore = (
|
||||
// 이미 삭제 중인 트랜잭션인지 확인
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
console.warn('이미 삭제 중인 트랜잭션입니다:', id);
|
||||
reject(new Error('이미 삭제 중인 트랜잭션입니다'));
|
||||
return;
|
||||
return resolve(false);
|
||||
}
|
||||
|
||||
// 삭제할 트랜잭션이 존재하는지 확인 및 데이터 복사 보관
|
||||
const transactionToDelete = transactions.find(t => t.id === id);
|
||||
if (!transactionToDelete) {
|
||||
console.warn('삭제할 트랜잭션이 존재하지 않음:', id);
|
||||
reject(new Error('트랜잭션이 존재하지 않습니다'));
|
||||
return;
|
||||
return resolve(false);
|
||||
}
|
||||
|
||||
// 삭제 중인 상태로 표시
|
||||
@@ -56,7 +53,7 @@ export const useDeleteTransactionCore = (
|
||||
|
||||
// 사용자 인터페이스 응답성 감소 전에 이벤트 발생 처리
|
||||
try {
|
||||
// 상태 업데이트 바로 후 크로스 스레드 통신 방지
|
||||
// 상태 업데이트 바로 후 이벤트 발생
|
||||
setTimeout(() => {
|
||||
try {
|
||||
window.dispatchEvent(new Event('transactionUpdated'));
|
||||
@@ -68,48 +65,37 @@ export const useDeleteTransactionCore = (
|
||||
console.warn('이벤트 디스패치 설정 오류:', eventError);
|
||||
}
|
||||
|
||||
// UI 스레드 블록하지 않는 너비로 requestAnimationFrame 사용
|
||||
requestAnimationFrame(() => {
|
||||
// 백그라운드 작업 처리
|
||||
setTimeout(() => {
|
||||
if (isCanceled) {
|
||||
console.log('작업이 취소되었습니다.');
|
||||
return;
|
||||
}
|
||||
|
||||
// 백그라운드 작업은 너비로 처리
|
||||
timeoutId = setTimeout(() => {
|
||||
handleDeleteStorage(
|
||||
isCanceled,
|
||||
updatedTransactions,
|
||||
id,
|
||||
user,
|
||||
transactionToDelete,
|
||||
pendingDeletionRef
|
||||
);
|
||||
}, 0);
|
||||
});
|
||||
// 스토리지 처리
|
||||
handleDeleteStorage(
|
||||
isCanceled,
|
||||
updatedTransactions,
|
||||
id,
|
||||
user,
|
||||
transactionToDelete,
|
||||
pendingDeletionRef
|
||||
);
|
||||
|
||||
// 작업 완료 후 보류 중인 삭제 목록에서 제거
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
}, 50);
|
||||
|
||||
// 상태 업데이트가 이미 수행되었으므로 즉시 성공 반환
|
||||
console.log('트랜잭션 삭제 UI 업데이트 완료');
|
||||
resolve(true);
|
||||
|
||||
// 취소 기능을 가진 Promise 객체 생성
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(promiseObj as any).cancel = () => {
|
||||
isCanceled = true;
|
||||
|
||||
if (timeoutId !== null) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
console.log('트랜잭션 삭제 작업 취소 완료');
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 삭제 초기화 중 오류:', error);
|
||||
|
||||
// 오류 발생 시 토스트 표시
|
||||
toast({
|
||||
title: "시스템 오류",
|
||||
title: "삭제 실패",
|
||||
description: "지출 삭제 중 오류가 발생했습니다.",
|
||||
duration: 2000,
|
||||
variant: "destructive"
|
||||
@@ -121,6 +107,13 @@ export const useDeleteTransactionCore = (
|
||||
}
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(promiseObj as any).cancel = () => {
|
||||
isCanceled = true;
|
||||
pendingDeletionRef.current?.delete(id);
|
||||
console.log('트랜잭션 삭제 작업 취소 완료');
|
||||
};
|
||||
|
||||
return promiseObj;
|
||||
}, [transactions, setTransactions, user]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user