Refactor toast duplicate prevention
Refactor the toast wrapper to improve the logic for preventing duplicate toast messages.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
import * as React from "react"
|
||||
|
||||
const TOAST_LIMIT = 5 // 최대 5개로 제한
|
||||
@@ -134,6 +135,14 @@ let memoryState: State = { toasts: [] }
|
||||
let lastAction: { type: string; id?: string; time: number } | null = null
|
||||
|
||||
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 &&
|
||||
@@ -141,7 +150,7 @@ function dispatch(action: Action) {
|
||||
((action.type === actionTypes.ADD_TOAST &&
|
||||
lastAction.time > now - 1000) || // ADD 액션은 1초 내 중복 방지
|
||||
(action.type !== actionTypes.ADD_TOAST &&
|
||||
action.toast?.id === lastAction.id &&
|
||||
actionId === lastAction.id &&
|
||||
lastAction.time > now - 300)); // 다른 액션은 300ms 내 중복 방지
|
||||
|
||||
if (isSameAction) {
|
||||
@@ -152,7 +161,7 @@ function dispatch(action: Action) {
|
||||
// 액션 추적 업데이트
|
||||
lastAction = {
|
||||
type: action.type,
|
||||
id: action.toast?.id,
|
||||
id: actionId,
|
||||
time: now
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user