Prevent duplicate toast notifications
The application was displaying duplicate toast notifications due to events being triggered multiple times. This commit prevents duplicate notifications.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
|
||||
import * as React from "react"
|
||||
|
||||
const TOAST_LIMIT = 20
|
||||
export const TOAST_REMOVE_DELAY = 1000000
|
||||
const TOAST_LIMIT = 5 // 최대 5개로 제한
|
||||
export const TOAST_REMOVE_DELAY = 5000 // 5초 후 DOM에서 제거
|
||||
|
||||
export type ToasterToast = {
|
||||
id: string
|
||||
@@ -55,6 +54,7 @@ interface State {
|
||||
|
||||
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
|
||||
|
||||
// 토스트 자동 제거 함수
|
||||
const addToRemoveQueue = (toastId: string) => {
|
||||
if (toastTimeouts.has(toastId)) {
|
||||
return
|
||||
@@ -126,11 +126,37 @@ export const reducer = (state: State, action: Action): State => {
|
||||
}
|
||||
}
|
||||
|
||||
// 전역 상태 및 리스너
|
||||
const listeners: Array<(state: State) => void> = []
|
||||
|
||||
let memoryState: State = { toasts: [] }
|
||||
|
||||
// 마지막 액션 추적 (중복 방지용)
|
||||
let lastAction: { type: string; id?: string; time: number } | null = null
|
||||
|
||||
function dispatch(action: Action) {
|
||||
// 동일한 토스트에 대한 중복 액션 방지
|
||||
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 &&
|
||||
action.toast?.id === lastAction.id &&
|
||||
lastAction.time > now - 300)); // 다른 액션은 300ms 내 중복 방지
|
||||
|
||||
if (isSameAction) {
|
||||
console.log('중복 토스트 액션 무시:', action.type);
|
||||
return;
|
||||
}
|
||||
|
||||
// 액션 추적 업데이트
|
||||
lastAction = {
|
||||
type: action.type,
|
||||
id: action.toast?.id,
|
||||
time: now
|
||||
};
|
||||
|
||||
// 실제 상태 업데이트 및 리스너 호출
|
||||
memoryState = reducer(memoryState, action)
|
||||
listeners.forEach((listener) => {
|
||||
listener(memoryState)
|
||||
@@ -158,7 +184,7 @@ function toast({ ...props }: Toast) {
|
||||
onOpenChange: (open) => {
|
||||
if (!open) dismiss()
|
||||
},
|
||||
duration: props.duration || 5000, // 기본 지속 시간 설정
|
||||
duration: props.duration || 3000, // 기본 지속 시간 3초로 단축
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user