Refactor useToast hook

The useToast hook was refactored into smaller, more manageable files to improve code organization and maintainability.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 09:47:29 +00:00
parent cd6c92d7de
commit 61acb461e0
11 changed files with 257 additions and 231 deletions

View File

@@ -0,0 +1,51 @@
import { Action, actionTypes } from './types'
import { TOAST_LIMIT } from './constants'
import { reducer } from './reducer'
import { listeners, memoryState, lastAction } from './store'
// ID 생성기
let count = 0
export function genId() {
count = (count + 1) % Number.MAX_SAFE_INTEGER
return count.toString()
}
export function dispatch(action: Action) {
// 마지막 액션 정보 추출
let actionId: string | undefined = undefined;
if ('toast' in action && action.toast) {
actionId = action.toast.id;
} else if ('toastId' in action) {
actionId = action.toastId;
}
// 동일한 토스트에 대한 중복 액션 방지
const now = Date.now();
const isSameAction = lastAction &&
lastAction.type === action.type &&
((action.type === actionTypes.ADD_TOAST &&
lastAction.time > now - 1000) || // ADD 액션은 1초 내 중복 방지
(action.type !== actionTypes.ADD_TOAST &&
actionId === lastAction.id &&
lastAction.time > now - 300)); // 다른 액션은 300ms 내 중복 방지
if (isSameAction) {
console.log('중복 토스트 액션 무시:', action.type);
return;
}
// 액션 추적 업데이트
lastAction = {
type: action.type,
id: actionId,
time: now
};
// 실제 상태 업데이트 및 리스너 호출
memoryState = reducer(memoryState, action)
listeners.forEach((listener) => {
listener(memoryState)
})
}