Reduce token usage
The prompt indicated that the previous implementation was consuming too many tokens. This commit aims to reduce token usage.
This commit is contained in:
@@ -5,7 +5,7 @@ import { toast } from '@/hooks/useToast.wrapper';
|
||||
import { handleDeleteStorage } from './deleteTransactionStorage';
|
||||
|
||||
/**
|
||||
* 트랜잭션 삭제 핵심 기능 - 안정성 대폭 개선
|
||||
* 트랜잭션 삭제 핵심 기능 - 심각한 버그 수정
|
||||
*/
|
||||
export const useDeleteTransactionCore = (
|
||||
transactions: Transaction[],
|
||||
@@ -13,19 +13,31 @@ export const useDeleteTransactionCore = (
|
||||
user: any,
|
||||
pendingDeletionRef: MutableRefObject<Set<string>>
|
||||
) => {
|
||||
// 트랜잭션 삭제 - 안정성 대폭 개선 버전
|
||||
return useCallback((id: string): Promise<boolean> => {
|
||||
// 반드시 Promise 반환
|
||||
return new Promise<boolean>((resolve) => {
|
||||
// 트랜잭션 삭제 - 안정성 개선 버전
|
||||
return useCallback(async (id: string): Promise<boolean> => {
|
||||
return new Promise<boolean>(async (resolve) => {
|
||||
try {
|
||||
console.log('트랜잭션 삭제 작업 시작 - ID:', id);
|
||||
|
||||
// 이미 삭제 중인지 확인
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
console.warn('이미 삭제 중인 트랜잭션:', id);
|
||||
// 이미 진행 중이면 true 반환하고 종료
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// pendingDeletion에 추가
|
||||
pendingDeletionRef.current.add(id);
|
||||
|
||||
// 전달된 ID로 트랜잭션 찾기
|
||||
const transactionToDelete = transactions.find(t => t.id === id);
|
||||
|
||||
// 트랜잭션이 존재하는지 확인
|
||||
if (!transactionToDelete) {
|
||||
console.warn('삭제할 트랜잭션이 없음:', id);
|
||||
|
||||
// 토스트 메시지
|
||||
toast({
|
||||
title: "삭제 실패",
|
||||
description: "해당 항목을 찾을 수 없습니다.",
|
||||
@@ -33,56 +45,52 @@ export const useDeleteTransactionCore = (
|
||||
duration: 1500
|
||||
});
|
||||
|
||||
// ID 정리
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
|
||||
// 삭제 중 표시 제거
|
||||
pendingDeletionRef.current.delete(id);
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. UI 업데이트 단계
|
||||
// 1. UI 업데이트 단계 (트랜잭션 목록에서 제거)
|
||||
try {
|
||||
// 상태 업데이트 (렌더링 트리거)
|
||||
const updatedTransactions = transactions.filter(t => t.id !== id);
|
||||
setTransactions(updatedTransactions);
|
||||
|
||||
// 삭제 성공 토스트 (UI 피드백)
|
||||
// 삭제 성공 토스트
|
||||
toast({
|
||||
title: "삭제 완료",
|
||||
description: "지출 항목이 삭제되었습니다.",
|
||||
duration: 1500
|
||||
});
|
||||
|
||||
// 2. 스토리지 업데이트 단계 (백그라운드 작업)
|
||||
// 2. 스토리지 업데이트 단계
|
||||
try {
|
||||
// 스토리지 처리 (로컬 스토리지 및 Supabase 업데이트)
|
||||
const storageResult = handleDeleteStorage(
|
||||
updatedTransactions,
|
||||
id,
|
||||
user,
|
||||
// 스토리지 처리 (Promise 반환)
|
||||
const storageResult = await handleDeleteStorage(
|
||||
updatedTransactions,
|
||||
id,
|
||||
user,
|
||||
pendingDeletionRef
|
||||
);
|
||||
|
||||
// 이벤트 발생
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new Event('transactionDeleted'));
|
||||
}, 100);
|
||||
window.dispatchEvent(new Event('transactionDeleted'));
|
||||
|
||||
console.log('삭제 작업 UI 단계 완료:', id);
|
||||
|
||||
// 스토리지 결과와 관계없이 UI는 이미 업데이트되었으므로 true 반환
|
||||
console.log('삭제 작업 완료:', id);
|
||||
resolve(true);
|
||||
} catch (storageError) {
|
||||
console.error('스토리지 작업 오류:', storageError);
|
||||
// 스토리지 오류가 발생해도 UI는 이미 업데이트되었으므로 true 반환
|
||||
|
||||
// 스토리지 오류가 있어도 UI는 이미 업데이트됨
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
resolve(true);
|
||||
}
|
||||
} catch (uiError) {
|
||||
console.error('UI 업데이트 단계 오류:', uiError);
|
||||
|
||||
// 중요: 실패해도 pendingDeletion에서 삭제
|
||||
// 삭제 중 표시 제거
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
@@ -99,7 +107,7 @@ export const useDeleteTransactionCore = (
|
||||
} catch (error) {
|
||||
console.error('트랜잭션 삭제 전체 오류:', error);
|
||||
|
||||
// 중요: 모든 상황에서 pendingDeletion에서 제거
|
||||
// 삭제 중 표시 제거
|
||||
if (pendingDeletionRef.current.has(id)) {
|
||||
pendingDeletionRef.current.delete(id);
|
||||
}
|
||||
@@ -114,5 +122,5 @@ export const useDeleteTransactionCore = (
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
}, [transactions, setTransactions, user]);
|
||||
}, [transactions, setTransactions, user, pendingDeletionRef]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user