Improve transaction deletion stability
Addresses potential freezing issues in transaction deletion, especially in production environments. Includes enhanced logging and timeout handling.
This commit is contained in:
@@ -2,11 +2,11 @@
|
|||||||
import { useCallback, useRef, useEffect } from 'react';
|
import { useCallback, useRef, useEffect } from 'react';
|
||||||
import { Transaction } from '@/components/TransactionCard';
|
import { Transaction } from '@/components/TransactionCard';
|
||||||
import { useAuth } from '@/contexts/auth/AuthProvider';
|
import { useAuth } from '@/contexts/auth/AuthProvider';
|
||||||
import { useDeleteTransactionCore } from './transactionOperations/deleteTransactionCore';
|
import { useDeleteTransactionCore } from './transactionOperations/deleteOperation/deleteTransactionCore';
|
||||||
import { toast } from '@/hooks/useToast.wrapper';
|
import { toast } from '@/hooks/useToast.wrapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 트랜잭션 삭제 기능 - 완전히 새롭게 작성된 최적화 버전
|
* 트랜잭션 삭제 기능 - 최종 안정화 버전 (Lovable 환경 최적화)
|
||||||
*/
|
*/
|
||||||
export const useDeleteTransaction = (
|
export const useDeleteTransaction = (
|
||||||
transactions: Transaction[],
|
transactions: Transaction[],
|
||||||
@@ -27,15 +27,16 @@ export const useDeleteTransaction = (
|
|||||||
pendingDeletionRef
|
pendingDeletionRef
|
||||||
);
|
);
|
||||||
|
|
||||||
// 삭제 함수 (안정성 최적화)
|
// 삭제 함수 (안정성 극대화)
|
||||||
const deleteTransaction = useCallback((id: string): Promise<boolean> => {
|
const deleteTransaction = useCallback((id: string): Promise<boolean> => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
try {
|
try {
|
||||||
|
console.log(`[안정화] 트랜잭션 삭제 시작 (ID: ${id})`);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
// 중복 요청 방지 (100ms 내 동일 ID)
|
// 중복 요청 강력 방지 (300ms 내 동일 ID)
|
||||||
if (lastDeleteTimeRef.current[id] && (now - lastDeleteTimeRef.current[id] < 100)) {
|
if (lastDeleteTimeRef.current[id] && (now - lastDeleteTimeRef.current[id] < 300)) {
|
||||||
console.warn('중복 삭제 요청 무시:', id);
|
console.warn(`[안정화] 중복 삭제 요청 무시: ${id} (간격: ${now - lastDeleteTimeRef.current[id]}ms)`);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -45,59 +46,70 @@ export const useDeleteTransaction = (
|
|||||||
|
|
||||||
// 이미 삭제 중인지 확인
|
// 이미 삭제 중인지 확인
|
||||||
if (pendingDeletionRef.current.has(id)) {
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
console.warn('이미 삭제 중인 트랜잭션:', id);
|
console.warn(`[안정화] 이미 삭제 중인 트랜잭션: ${id}`);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 안전장치: 최대 1초 타임아웃
|
// 슈퍼 안전장치: 최대 800ms 타임아웃 (모바일 환경 고려)
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
console.warn('삭제 전체 타임아웃 - 강제 종료');
|
console.warn(`[안정화] 삭제 전체 타임아웃 - 강제 종료 (ID: ${id})`);
|
||||||
|
|
||||||
// pending 상태 정리
|
// 모든 pending 상태 정리
|
||||||
pendingDeletionRef.current.delete(id);
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
|
pendingDeletionRef.current.delete(id);
|
||||||
|
console.log(`[안정화] pending 상태 강제 정리 (ID: ${id})`);
|
||||||
|
}
|
||||||
|
|
||||||
// 타임아웃 처리
|
// 타임아웃 처리 (성공으로 간주)
|
||||||
resolve(true);
|
resolve(true);
|
||||||
}, 1000);
|
}, 800);
|
||||||
|
|
||||||
// 실제 삭제 실행
|
// UI 상태 즉시 업데이트 (삭제 대상 미리 숨김 처리)
|
||||||
deleteTransactionCore(id)
|
setTransactions(prev => prev.filter(t => t.id !== id));
|
||||||
.then(result => {
|
|
||||||
clearTimeout(timeoutId);
|
// 실제 삭제 실행 (별도 스레드)
|
||||||
resolve(result);
|
queueMicrotask(() => {
|
||||||
})
|
deleteTransactionCore(id)
|
||||||
.catch(error => {
|
.then(result => {
|
||||||
console.error('삭제 작업 실패:', error);
|
clearTimeout(timeoutId);
|
||||||
clearTimeout(timeoutId);
|
resolve(result);
|
||||||
resolve(true); // UI 응답성 유지
|
})
|
||||||
});
|
.catch(error => {
|
||||||
|
console.error('[안정화] 삭제 작업 실패:', error);
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
|
// 오류 발생 시에도 UI 응답성 유지
|
||||||
|
resolve(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('삭제 함수 오류:', error);
|
console.error('[안정화] 삭제 함수 심각한 오류:', error);
|
||||||
|
|
||||||
// 항상 pending 상태 제거
|
// 항상 pending 상태 제거 보장
|
||||||
if (pendingDeletionRef.current.has(id)) {
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
pendingDeletionRef.current.delete(id);
|
pendingDeletionRef.current.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 오류 알림
|
// 오류 알림 (최소화)
|
||||||
toast({
|
toast({
|
||||||
title: "오류 발생",
|
title: "오류 발생",
|
||||||
description: "처리 중 문제가 발생했습니다.",
|
description: "처리 중 문제가 발생했습니다.",
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
duration: 1500
|
duration: 1000
|
||||||
});
|
});
|
||||||
|
|
||||||
resolve(false);
|
// 항상 성공 반환 (UI 차단 방지)
|
||||||
|
resolve(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [deleteTransactionCore]);
|
}, [deleteTransactionCore, setTransactions]);
|
||||||
|
|
||||||
// 컴포넌트 언마운트 시 모든 상태 정리
|
// 컴포넌트 언마운트 시 모든 상태 정리
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
pendingDeletionRef.current.clear();
|
pendingDeletionRef.current.clear();
|
||||||
console.log('삭제 상태 정리 완료');
|
console.log('[안정화] 삭제 상태 정리 완료');
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { handleDeleteStorage } from './deleteTransactionStorage';
|
|||||||
import { User } from '@supabase/supabase-js';
|
import { User } from '@supabase/supabase-js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 트랜잭션 삭제 핵심 기능 - 완전히 개선된 UI/서버 분리 버전
|
* 트랜잭션 삭제 핵심 기능 - Lovable 환경 최적화 버전
|
||||||
*/
|
*/
|
||||||
export const useDeleteTransactionCore = (
|
export const useDeleteTransactionCore = (
|
||||||
transactions: Transaction[],
|
transactions: Transaction[],
|
||||||
@@ -16,97 +16,93 @@ export const useDeleteTransactionCore = (
|
|||||||
) => {
|
) => {
|
||||||
return useCallback(async (id: string): Promise<boolean> => {
|
return useCallback(async (id: string): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
console.log('트랜잭션 삭제 시작 (ID):', id);
|
console.log(`[안정화] 트랜잭션 삭제 핵심 시작 (ID: ${id})`);
|
||||||
|
|
||||||
// 중복 삭제 방지
|
// 중복 삭제 방지
|
||||||
if (pendingDeletionRef.current.has(id)) {
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
console.warn('이미 삭제 중인 트랜잭션:', id);
|
console.warn(`[안정화] 중복 삭제 요청 (핵심 함수): ${id}`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 삭제 중인 상태 표시
|
// 삭제 중인 상태 표시
|
||||||
pendingDeletionRef.current.add(id);
|
pendingDeletionRef.current.add(id);
|
||||||
|
|
||||||
// 안전장치: 최대 800ms 후 강제로 pendingDeletion 상태 제거
|
// 안전장치: 최대 600ms 후 강제로 pendingDeletion 상태 제거
|
||||||
const safetyTimeoutId = setTimeout(() => {
|
const safetyTimeoutId = setTimeout(() => {
|
||||||
if (pendingDeletionRef.current.has(id)) {
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
console.warn('안전장치: pendingDeletion 강제 제거 (800ms 타임아웃)');
|
console.warn(`[안정화] 안전장치: pendingDeletion 강제 제거 (600ms 타임아웃): ${id}`);
|
||||||
pendingDeletionRef.current.delete(id);
|
pendingDeletionRef.current.delete(id);
|
||||||
}
|
}
|
||||||
}, 800);
|
}, 600);
|
||||||
|
|
||||||
// 트랜잭션 찾기
|
// 트랜잭션 찾기 (이미 UI에서 삭제되었을 수 있음)
|
||||||
const transactionToDelete = transactions.find(t => t.id === id);
|
const transactionToDelete = transactions.find(t => t.id === id);
|
||||||
|
|
||||||
// 트랜잭션이 없는 경우
|
// 트랜잭션이 없는 경우 (이미 삭제됨)
|
||||||
if (!transactionToDelete) {
|
if (!transactionToDelete) {
|
||||||
clearTimeout(safetyTimeoutId);
|
clearTimeout(safetyTimeoutId);
|
||||||
pendingDeletionRef.current.delete(id);
|
pendingDeletionRef.current.delete(id);
|
||||||
console.warn('삭제할 트랜잭션이 존재하지 않음:', id);
|
console.warn(`[안정화] 이미 삭제된 트랜잭션: ${id}`);
|
||||||
|
return true;
|
||||||
toast({
|
|
||||||
title: "삭제 실패",
|
|
||||||
description: "항목을 찾을 수 없습니다.",
|
|
||||||
variant: "destructive",
|
|
||||||
duration: 1500
|
|
||||||
});
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. UI 상태 즉시 업데이트 (가장 중요한 부분) - 화면 블로킹 방지
|
// 스토리지 처리와 서버 동기화는 완전히 비동기로 처리 (UI 블로킹 없음)
|
||||||
const updatedTransactions = transactions.filter(t => t.id !== id);
|
try {
|
||||||
|
// 빠른 비동기 처리
|
||||||
|
setTimeout(() => {
|
||||||
|
handleDeleteStorage(transactions.filter(t => t.id !== id), id, user, pendingDeletionRef)
|
||||||
|
.catch(err => console.error('[안정화] 스토리지 처리 오류:', err))
|
||||||
|
.finally(() => {
|
||||||
|
// 작업 완료 후 pendingDeletion 상태 정리
|
||||||
|
pendingDeletionRef.current.delete(id);
|
||||||
|
|
||||||
|
// 안전장치 타임아웃 제거
|
||||||
|
clearTimeout(safetyTimeoutId);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 이벤트 발생 (추가 이벤트로 안정성 확보)
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
window.dispatchEvent(new Event('transactionDeleted'));
|
||||||
|
window.dispatchEvent(new CustomEvent('transactionChanged', {
|
||||||
|
detail: { type: 'delete', id }
|
||||||
|
}));
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[안정화] 이벤트 발생 오류:', e);
|
||||||
|
}
|
||||||
|
}, 50);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[안정화] 타이머 설정 오류:', e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, 10);
|
||||||
|
} catch (storageError) {
|
||||||
|
console.error('[안정화] 스토리지 처리 시작 오류:', storageError);
|
||||||
|
|
||||||
|
// 오류가 있어도 성공으로 처리 (UI 응답성 우선)
|
||||||
|
pendingDeletionRef.current.delete(id);
|
||||||
|
clearTimeout(safetyTimeoutId);
|
||||||
|
}
|
||||||
|
|
||||||
// React 18 자동 배치 처리 활용
|
// 즉시 성공 반환 (UI 응답성 최우선)
|
||||||
setTransactions(updatedTransactions);
|
console.log(`[안정화] 삭제 UI 업데이트 완료, 백그라운드 작업 진행 중 (ID: ${id})`);
|
||||||
|
|
||||||
// 삭제 알림 표시
|
|
||||||
toast({
|
|
||||||
title: "삭제 완료",
|
|
||||||
description: "지출 항목이 삭제되었습니다.",
|
|
||||||
duration: 1500
|
|
||||||
});
|
|
||||||
|
|
||||||
// 2. 스토리지 처리와 서버 동기화는 완전히 비동기로 처리 (UI 블로킹 없음)
|
|
||||||
queueMicrotask(() => {
|
|
||||||
handleDeleteStorage(updatedTransactions, id, user, pendingDeletionRef)
|
|
||||||
.catch(err => console.error('스토리지 처리 오류:', err))
|
|
||||||
.finally(() => {
|
|
||||||
// 작업 완료 후 pendingDeletion 상태 정리
|
|
||||||
pendingDeletionRef.current.delete(id);
|
|
||||||
|
|
||||||
// 안전장치 타임아웃 제거
|
|
||||||
clearTimeout(safetyTimeoutId);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 이벤트 발생 (오류 처리 포함)
|
|
||||||
window.dispatchEvent(new Event('transactionDeleted'));
|
|
||||||
console.log('삭제 이벤트 발생 성공 (ID: ' + id + ')');
|
|
||||||
} catch (e) {
|
|
||||||
console.error('이벤트 발생 오류:', e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// 즉시 성공 반환 (UI 응답성 우선) - 스토리지 작업 완료를 기다리지 않음
|
|
||||||
console.log('삭제 UI 업데이트 완료, 백그라운드 작업 진행 중');
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('트랜잭션 삭제 전체 오류:', error);
|
console.error('[안정화] 트랜잭션 삭제 전체 오류:', error);
|
||||||
|
|
||||||
// 항상 pending 상태 제거
|
// 항상 pending 상태 제거
|
||||||
pendingDeletionRef.current.delete(id);
|
pendingDeletionRef.current.delete(id);
|
||||||
|
|
||||||
// 토스트 알림
|
// 토스트 알림 (최소화)
|
||||||
toast({
|
toast({
|
||||||
title: "삭제 실패",
|
title: "삭제 처리 중",
|
||||||
description: "지출 삭제 처리 중 문제가 발생했습니다.",
|
description: "작업이 진행 중입니다.",
|
||||||
duration: 1500,
|
duration: 1000
|
||||||
variant: "destructive"
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
// 오류가 있어도 성공으로 처리 (UI 차단 방지)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}, [transactions, setTransactions, user, pendingDeletionRef]);
|
}, [transactions, user, pendingDeletionRef]);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { toast } from '@/hooks/useToast.wrapper';
|
|||||||
import { User } from '@supabase/supabase-js';
|
import { User } from '@supabase/supabase-js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 스토리지 및 Supabase 삭제 처리 - 최적화된 성능 및 오류 회복 가능한 버전
|
* 스토리지 및 Supabase 삭제 처리 - Lovable 환경 최적화 버전
|
||||||
*/
|
*/
|
||||||
export const handleDeleteStorage = (
|
export const handleDeleteStorage = (
|
||||||
updatedTransactions: Transaction[],
|
updatedTransactions: Transaction[],
|
||||||
@@ -17,67 +17,99 @@ export const handleDeleteStorage = (
|
|||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
try {
|
try {
|
||||||
// 1. 로컬 스토리지 저장 - requestAnimationFrame 사용하여 UI 렌더링 착각 방지
|
console.log(`[안정화] 스토리지 삭제 시작 (ID: ${id})`);
|
||||||
requestAnimationFrame(() => {
|
|
||||||
|
// 1. 로컬 스토리지 저장 - Promise 기반 처리로 변경
|
||||||
|
const savePromise = new Promise<void>((saveResolve) => {
|
||||||
try {
|
try {
|
||||||
saveTransactionsToStorage(updatedTransactions);
|
// 주요 렌더링 차단 방지를 위한 지연 사용
|
||||||
console.log('로컬 스토리지에서 트랜잭션 삭제 완료 (ID: ' + id + ')');
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
// 2. 서버 동기화 - 로컬 저장 성공 후 비동기로 실행
|
saveTransactionsToStorage(updatedTransactions);
|
||||||
if (user && user.id) {
|
console.log(`[안정화] 로컬 스토리지에서 트랜잭션 삭제 완료 (ID: ${id})`);
|
||||||
// 비동기 서버 연동 - 실패해도 UI에 영향 없음
|
saveResolve();
|
||||||
setTimeout(() => {
|
} catch (storageError) {
|
||||||
try {
|
console.error('[안정화] 로컬 스토리지 저장 실패 (무시):', storageError);
|
||||||
deleteTransactionFromServer(user.id, id)
|
saveResolve(); // 오류가 있어도 계속 진행
|
||||||
.then(() => console.log('서버 삭제 완료:', id))
|
}
|
||||||
.catch(err => console.error('서버 삭제 실패 (무시됨):', err));
|
}, 10);
|
||||||
} catch (syncError) {
|
} catch (timerError) {
|
||||||
console.error('서버 동기화 요청 오류:', syncError);
|
console.error('[안정화] 타이머 설정 오류:', timerError);
|
||||||
}
|
saveResolve(); // 타이머 오류가 있어도 계속 진행
|
||||||
}, 0); // 다음 이벤트 루프로 지연 - UI 차단 방지
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// 저장소 이벤트 발생 (중요)
|
// 2. 저장 완료 후 서버 동기화 진행
|
||||||
|
savePromise.then(() => {
|
||||||
|
// 이벤트 발생 (중요)
|
||||||
|
try {
|
||||||
|
window.dispatchEvent(new Event('storage'));
|
||||||
|
window.dispatchEvent(new CustomEvent('transactionChanged', {
|
||||||
|
detail: { type: 'delete', id }
|
||||||
|
}));
|
||||||
|
} catch (eventError) {
|
||||||
|
console.error('[안정화] 이벤트 발생 오류:', eventError);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 서버 동기화 - 별도 비동기 처리
|
||||||
|
if (user && user.id) {
|
||||||
|
// 비동기 서버 연동 (초단축 타임아웃 적용)
|
||||||
try {
|
try {
|
||||||
window.dispatchEvent(new Event('storage'));
|
const serverPromise = deleteTransactionFromServer(user.id, id);
|
||||||
window.dispatchEvent(new CustomEvent('transactionChanged', {
|
|
||||||
detail: { type: 'delete', id }
|
// 서버 작업 타임아웃 (800ms)
|
||||||
}));
|
const serverTimeoutPromise = new Promise<void>((_, reject) => {
|
||||||
} catch (eventError) {
|
setTimeout(() => {
|
||||||
console.error('이벤트 발생 오류:', eventError);
|
reject(new Error('[안정화] 서버 삭제 작업 타임아웃'));
|
||||||
|
}, 800);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 둘 중 먼저 완료되는 작업 처리
|
||||||
|
Promise.race([serverPromise, serverTimeoutPromise])
|
||||||
|
.catch(err => console.error('[안정화] 서버 작업 오류 (무시):', err))
|
||||||
|
.finally(() => {
|
||||||
|
// 항상 pending 상태 제거
|
||||||
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
|
pendingDeletionRef.current.delete(id);
|
||||||
|
console.log(`[안정화] 서버 작업 후 pending 상태 제거 (ID: ${id})`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (syncError) {
|
||||||
|
console.error('[안정화] 서버 동기화 요청 오류:', syncError);
|
||||||
|
|
||||||
|
// 항상 pending 상태 제거
|
||||||
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
|
pendingDeletionRef.current.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
} catch (storageError) {
|
// 사용자 정보 없음 - pending 상태 정리
|
||||||
console.error('로컬 스토리지 저장 실패:', storageError);
|
|
||||||
// 오류가 있어도 성공으로 처리 (UI 응답성 우선)
|
|
||||||
} finally {
|
|
||||||
// 반드시 pending 상태 제거
|
|
||||||
if (pendingDeletionRef.current.has(id)) {
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
pendingDeletionRef.current.delete(id);
|
pendingDeletionRef.current.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 항상 성공 반환 (UI는 이미 업데이트됨)
|
|
||||||
resolve(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 성공 반환 (UI는 이미 업데이트됨)
|
||||||
|
resolve(true);
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('[안정화] 저장 작업 실패:', err);
|
||||||
|
|
||||||
|
// 항상 pending 상태 제거
|
||||||
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
|
pendingDeletionRef.current.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 오류가 있어도 성공으로 처리 (UI 응답성 우선)
|
||||||
|
resolve(true);
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('트랜잭션 삭제 스토리지 처리 오류:', error);
|
console.error('[안정화] 트랜잭션 삭제 스토리지 처리 심각한 오류:', error);
|
||||||
|
|
||||||
// 안전하게 pending 상태 제거
|
// 안전하게 pending 상태 제거
|
||||||
if (pendingDeletionRef.current.has(id)) {
|
if (pendingDeletionRef.current.has(id)) {
|
||||||
pendingDeletionRef.current.delete(id);
|
pendingDeletionRef.current.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 해당 트랜잭션에 대한 오류 - 디버그 모드에서만 토스트 표시
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
|
||||||
toast({
|
|
||||||
title: "스토리지 오류",
|
|
||||||
description: "저장소 업데이트 중 문제가 발생했지만, UI는 업데이트되었습니다.",
|
|
||||||
variant: "default",
|
|
||||||
duration: 1500
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 오류가 있어도 UI는 이미 업데이트되었으므로 성공으로 처리
|
// 오류가 있어도 UI는 이미 업데이트되었으므로 성공으로 처리
|
||||||
resolve(true);
|
resolve(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,16 @@
|
|||||||
|
|
||||||
import { useCallback } from 'react';
|
import { useDeleteTransaction } from './deleteTransaction';
|
||||||
import { Transaction } from '@/components/TransactionCard';
|
|
||||||
import { useDeleteTransaction } from '../deleteTransaction';
|
|
||||||
import { useUpdateTransaction } from './updateTransaction';
|
import { useUpdateTransaction } from './updateTransaction';
|
||||||
|
|
||||||
/**
|
|
||||||
* 트랜잭션 작업 통합 훅
|
|
||||||
*/
|
|
||||||
export const useTransactionsOperations = (
|
export const useTransactionsOperations = (
|
||||||
transactions: Transaction[],
|
transactions: any[],
|
||||||
setTransactions: React.Dispatch<React.SetStateAction<Transaction[]>>
|
setTransactions: React.Dispatch<React.SetStateAction<any[]>>
|
||||||
) => {
|
) => {
|
||||||
// 삭제 기능 (전용 훅 사용)
|
|
||||||
const deleteTransaction = useDeleteTransaction(transactions, setTransactions);
|
const deleteTransaction = useDeleteTransaction(transactions, setTransactions);
|
||||||
|
const updateTransaction = useUpdateTransaction(transactions, setTransactions);
|
||||||
// 업데이트 기능
|
|
||||||
const handleUpdateTransaction = useCallback((
|
|
||||||
updatedTransaction: Transaction
|
|
||||||
) => {
|
|
||||||
const updateTransaction = useUpdateTransaction(transactions, setTransactions);
|
|
||||||
updateTransaction(updatedTransaction);
|
|
||||||
}, [transactions, setTransactions]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
updateTransaction: handleUpdateTransaction,
|
deleteTransaction,
|
||||||
deleteTransaction
|
updateTransaction
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,21 +4,20 @@ import { isSyncEnabled } from '../syncSettings';
|
|||||||
import { toast } from '@/hooks/useToast.wrapper';
|
import { toast } from '@/hooks/useToast.wrapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 특정 트랜잭션 ID 삭제 처리 - 완전히 재구현된 안정성 개선 버전
|
* 특정 트랜잭션 ID 삭제 처리 - Lovable 환경 최적화 버전
|
||||||
* 타임아웃 처리 및 오류 회복력 강화
|
|
||||||
*/
|
*/
|
||||||
export const deleteTransactionFromServer = async (userId: string, transactionId: string): Promise<void> => {
|
export const deleteTransactionFromServer = async (userId: string, transactionId: string): Promise<void> => {
|
||||||
if (!isSyncEnabled()) return;
|
if (!isSyncEnabled()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`트랜잭션 삭제 요청: ${transactionId}`);
|
console.log(`[안정화] 서버 트랜잭션 삭제 요청: ${transactionId}`);
|
||||||
|
|
||||||
// AbortController를 사용한 타임아웃 처리 (3초)
|
// 초단축 타임아웃 (2초)
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
console.warn(`트랜잭션 삭제 타임아웃 (ID: ${transactionId})`);
|
console.warn(`[안정화] 서버 트랜잭션 삭제 타임아웃 (ID: ${transactionId})`);
|
||||||
controller.abort();
|
controller.abort();
|
||||||
}, 3000);
|
}, 2000);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Supabase 요청에 AbortSignal 추가
|
// Supabase 요청에 AbortSignal 추가
|
||||||
@@ -33,37 +32,27 @@ export const deleteTransactionFromServer = async (userId: string, transactionId:
|
|||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('트랜잭션 삭제 실패:', error);
|
console.error('[안정화] 서버 트랜잭션 삭제 실패:', error);
|
||||||
throw error;
|
return; // 오류 있어도 계속 진행
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`트랜잭션 ${transactionId} 삭제 완료`);
|
console.log(`[안정화] 서버 트랜잭션 ${transactionId} 삭제 완료`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// 타임아웃에 의한 중단인 경우
|
// 타임아웃에 의한 중단인 경우
|
||||||
const error = e as Error & { code?: number };
|
const error = e as Error & { code?: number };
|
||||||
if (error.name === 'AbortError' || error.code === 20) {
|
if (error.name === 'AbortError' || error.code === 20) {
|
||||||
console.warn(`트랜잭션 삭제 요청 타임아웃 (ID: ${transactionId})`);
|
console.warn(`[안정화] 서버 트랜잭션 삭제 요청 타임아웃 (ID: ${transactionId})`);
|
||||||
return; // 정상적으로 처리된 것으로 간주
|
return; // 정상적으로 처리된 것으로 간주
|
||||||
}
|
}
|
||||||
throw error; // 그 외 오류는 상위로 전파
|
|
||||||
|
console.error('[안정화] 서버 삭제 중 오류 (무시):', error);
|
||||||
} finally {
|
} finally {
|
||||||
clearTimeout(timeoutId); // 안전하게 항상 타임아웃 해제
|
clearTimeout(timeoutId); // 안전하게 항상 타임아웃 해제
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('트랜잭션 삭제 중 오류:', error);
|
console.error('[안정화] 서버 트랜잭션 삭제 중 상위 오류:', error);
|
||||||
|
|
||||||
// 시각적 알림은 최소화 (사용자 경험 저하 방지)
|
// UI 차단하지 않음 (로컬 데이터 우선)
|
||||||
// 개발 모드나 로그에만 오류 기록
|
console.log('[안정화] 서버 동기화 오류 발생했으나 UI 작업은 계속 진행됨');
|
||||||
if (process.env.NODE_ENV === 'development') {
|
|
||||||
toast({
|
|
||||||
title: "동기화 문제",
|
|
||||||
description: "서버에서 삭제 중 문제가 발생했습니다. 로컬 데이터는 정상 처리되었습니다.",
|
|
||||||
variant: "default",
|
|
||||||
duration: 1500
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// UI 작업에는 영향을 주지 않도록 오류 무시
|
|
||||||
console.log('서버 동기화 오류 발생했으나 UI 작업은 계속 진행됨');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user