Refactor useTransactions hook

Refactor the useTransactions hook into smaller, more manageable files to improve code organization and maintainability. All existing functionality is preserved.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 09:39:56 +00:00
parent 662cacbc99
commit d45e1bbf78
7 changed files with 410 additions and 215 deletions

View File

@@ -0,0 +1,59 @@
import { useEffect } from 'react';
/**
* 트랜잭션 이벤트 관련 훅
* 각종 이벤트 리스너를 설정합니다.
*/
export const useTransactionsEvents = (
loadTransactions: () => void,
refreshKey: number
) => {
// 이벤트 리스너 설정
useEffect(() => {
console.log('useTransactions - 이벤트 리스너 설정');
// 트랜잭션 업데이트 이벤트 리스너
const handleTransactionUpdated = () => {
console.log('트랜잭션 업데이트 이벤트 감지됨');
loadTransactions();
};
// 스토리지 변경 이벤트 리스너
const handleStorageChange = (e: StorageEvent) => {
if (e.key === 'transactions' || e.key === null) {
console.log('로컬 스토리지 변경 감지됨:', e.key);
loadTransactions();
}
};
// 페이지 포커스/가시성 이벤트 리스너
const handleFocus = () => {
console.log('창 포커스 - 트랜잭션 새로고침');
loadTransactions();
};
const handleVisibilityChange = () => {
if (document.visibilityState === 'visible') {
console.log('페이지 가시성 변경 - 트랜잭션 새로고침');
loadTransactions();
}
};
// 이벤트 리스너 등록
window.addEventListener('transactionUpdated', handleTransactionUpdated);
window.addEventListener('storage', handleStorageChange);
window.addEventListener('focus', handleFocus);
document.addEventListener('visibilitychange', handleVisibilityChange);
// 컴포넌트 마운트시에만 수동으로 트랜잭션 업데이트 이벤트 발생
window.dispatchEvent(new Event('transactionUpdated'));
return () => {
window.removeEventListener('transactionUpdated', handleTransactionUpdated);
window.removeEventListener('storage', handleStorageChange);
window.removeEventListener('focus', handleFocus);
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}, [loadTransactions, refreshKey]);
};