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초로 단축
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,25 +1,46 @@
|
||||
|
||||
import { useToast as useOriginalToast, toast as originalToast, ToasterToast } from '@/hooks/use-toast';
|
||||
|
||||
// 토스트 중복 호출 방지를 위한 디바운스 구현
|
||||
let lastToastTime = 0;
|
||||
let lastToastMessage = '';
|
||||
const DEBOUNCE_TIME = 500; // 0.5초 내에 동일 메시지 방지
|
||||
// 토스트 이벤트 추적을 위한 히스토리 및 설정
|
||||
let toastHistory: { message: string; timestamp: number }[] = [];
|
||||
const DEBOUNCE_TIME = 1000; // 1초 내에 동일 메시지 방지
|
||||
const HISTORY_LIMIT = 10; // 히스토리에 유지할 최대 토스트 개수
|
||||
const CLEAR_INTERVAL = 30000; // 30초마다 오래된 히스토리 정리
|
||||
|
||||
// 히스토리 정리 함수
|
||||
const cleanupHistory = () => {
|
||||
const now = Date.now();
|
||||
toastHistory = toastHistory.filter(item => (now - item.timestamp) < 10000); // 10초 이상 지난 항목 제거
|
||||
};
|
||||
|
||||
// 주기적 히스토리 정리
|
||||
setInterval(cleanupHistory, CLEAR_INTERVAL);
|
||||
|
||||
// 중복 토스트 방지 래퍼 함수
|
||||
const debouncedToast = (params: Omit<ToasterToast, "id">) => {
|
||||
const currentMessage = params.description?.toString() || params.title?.toString() || '';
|
||||
const now = Date.now();
|
||||
const currentMessage = params.description?.toString() || '';
|
||||
|
||||
// 동일 메시지가 짧은 시간 내에 반복되는 경우 무시
|
||||
if (now - lastToastTime < DEBOUNCE_TIME && currentMessage === lastToastMessage) {
|
||||
// 유사한 메시지가 최근에 표시되었는지 확인
|
||||
const isDuplicate = toastHistory.some(item =>
|
||||
item.message === currentMessage && (now - item.timestamp) < DEBOUNCE_TIME
|
||||
);
|
||||
|
||||
// 중복이면 무시
|
||||
if (isDuplicate) {
|
||||
console.log('중복 토스트 감지로 무시됨:', currentMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
// 정상적인 토스트 호출
|
||||
lastToastTime = now;
|
||||
lastToastMessage = currentMessage;
|
||||
// 히스토리에 추가
|
||||
toastHistory.push({ message: currentMessage, timestamp: now });
|
||||
|
||||
// 히스토리 크기 제한
|
||||
if (toastHistory.length > HISTORY_LIMIT) {
|
||||
toastHistory.shift(); // 가장 오래된 항목 제거
|
||||
}
|
||||
|
||||
// 실제 토스트 표시
|
||||
originalToast({
|
||||
...params,
|
||||
duration: params.duration || 3000, // 기본 3초로 설정
|
||||
|
||||
Reference in New Issue
Block a user