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:
gpt-engineer-app[bot]
2025-03-18 05:42:34 +00:00
parent 915967a9ac
commit 82ab8e3504
4 changed files with 174 additions and 161 deletions

View File

@@ -6,80 +6,94 @@ import { deleteTransactionFromSupabase } from '../../supabaseUtils';
import { toast } from '@/hooks/useToast.wrapper';
/**
* 스토리지 및 Supabase 삭제 처리 - 안정성 대폭 개선
* 스토리지 및 Supabase 삭제 처리 - 안정성 개선 버전
*/
export const handleDeleteStorage = (
updatedTransactions: Transaction[],
id: string,
user: any,
pendingDeletionRef: MutableRefObject<Set<string>>
): boolean => {
try {
// 로컬 스토리지 업데이트 (동기 처리)
): Promise<boolean> => {
return new Promise((resolve) => {
try {
saveTransactionsToStorage(updatedTransactions);
console.log('로컬 스토리지 저장 완료');
} catch (storageError) {
console.error('로컬 스토리지 저장 실패:', storageError);
// 스토리지 실패해도 계속 진행 (Supabase 업데이트 시도)
}
// Supabase 업데이트 (비동기 처리)
if (user) {
// 네트워크 작업은 비동기로 진행 - 실패해도 UI에 영향 없음
// 타임아웃 설정 (10초)
const timeoutId = setTimeout(() => {
console.warn('Supabase 삭제 작업 시간 초과:', id);
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
}, 10000);
// 로컬 스토리지 업데이트 (동기 처리)
try {
deleteTransactionFromSupabase(user, id)
.then(() => {
console.log('Supabase 삭제 완료:', id);
})
.catch(error => {
console.error('Supabase 삭제 오류:', error);
})
.finally(() => {
// 타임아웃 취소
clearTimeout(timeoutId);
// 작업 완료 후 반드시 pendingDeletion에서 제거
saveTransactionsToStorage(updatedTransactions);
console.log('로컬 스토리지 저장 완료 (ID: ' + id + ')');
} catch (storageError) {
console.error('로컬 스토리지 저장 실패:', storageError);
// 오류가 있어도 계속 진행
}
// Supabase 업데이트 (비동기 처리)
if (user) {
let isCompleted = false;
// 10초 타임아웃 설정
const timeoutId = setTimeout(() => {
if (!isCompleted) {
console.warn('Supabase 삭제 작업 타임아웃:', id);
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
});
} catch (e) {
console.error('Supabase 작업 오류:', e);
// 타임아웃 취소
clearTimeout(timeoutId);
if (!isCompleted) {
isCompleted = true;
resolve(true); // UI 업데이트는 이미 완료되었으므로 성공으로 처리
}
}
}, 5000);
// 작업 완료 표시
try {
// Supabase 호출 (삭제)
deleteTransactionFromSupabase(user, id)
.then(() => {
console.log('Supabase 삭제 완료:', id);
})
.catch(error => {
console.error('Supabase 삭제 오류:', error);
})
.finally(() => {
clearTimeout(timeoutId);
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
if (!isCompleted) {
isCompleted = true;
resolve(true);
}
});
} catch (e) {
console.error('Supabase 작업 오류:', e);
clearTimeout(timeoutId);
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
if (!isCompleted) {
isCompleted = true;
resolve(true);
}
}
} else {
// 로그인 안한 사용자는 바로 완료 처리
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
resolve(true);
}
} else {
// 로그인 안한 사용자는 바로 완료 처리
} catch (storageError) {
console.error('스토리지 작업 중 오류:', storageError);
// 작업 완료 표시 (오류 발생해도 필수)
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
// 실패해도 UI는 업데이트 완료된 상태로 간주
resolve(false);
}
// 추가 확인: 성공적으로 로컬 업데이트 완료
return true;
} catch (storageError) {
console.error('스토리지 작업 중 오류:', storageError);
// 작업 완료 표시 (오류 발생해도 필수)
if (pendingDeletionRef.current) {
pendingDeletionRef.current.delete(id);
}
// 스토리지 작업 실패 시 false 반환
return false;
}
});
};