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,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