Fix: Resolve toast notification issue
The toast notifications were not displaying. This commit addresses the problem.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
import { useToast } from "@/hooks/use-toast"
|
import { useToast } from "@/hooks/use-toast"
|
||||||
import {
|
import {
|
||||||
Toast,
|
Toast,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
// useToast.wrapper.ts에서 수정된 toast 함수를 사용하도록 리디렉션
|
// Shadcn UI의 최신 버전에서는 use-toast가 hooks 폴더로 이동했습니다.
|
||||||
import { useToast, toast } from "@/hooks/useToast.wrapper";
|
// 이 파일은 기존 import 경로가 작동하도록 리디렉션합니다.
|
||||||
|
import { useToast, toast } from "@/hooks/use-toast";
|
||||||
|
|
||||||
export { useToast, toast };
|
export { useToast, toast };
|
||||||
|
export type { ToasterToast } from "@/hooks/use-toast";
|
||||||
|
|||||||
@@ -1,88 +1,190 @@
|
|||||||
|
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { ToastProps } from "@/components/ui/toast"
|
|
||||||
|
|
||||||
// useToastStore 대신 toast를 임포트합니다.
|
const TOAST_LIMIT = 20
|
||||||
import { toast as originalToast } from "@/components/ui/use-toast"
|
export const TOAST_REMOVE_DELAY = 1000000
|
||||||
import { ToastActionElement } from "@/components/ui/toast"
|
|
||||||
|
|
||||||
// 기본 지속 시간 늘리기 (기존 3000ms → 3500ms)
|
export type ToasterToast = {
|
||||||
export const DEFAULT_TOAST_DURATION = 3500;
|
id: string
|
||||||
|
title?: React.ReactNode
|
||||||
|
description?: React.ReactNode
|
||||||
|
action?: React.ReactNode
|
||||||
|
variant?: "default" | "destructive"
|
||||||
|
duration?: number
|
||||||
|
}
|
||||||
|
|
||||||
export const TOAST_REMOVE_DELAY = 1000000;
|
const actionTypes = {
|
||||||
|
ADD_TOAST: "ADD_TOAST",
|
||||||
export type ToasterToast = ToastProps & {
|
UPDATE_TOAST: "UPDATE_TOAST",
|
||||||
id: string;
|
DISMISS_TOAST: "DISMISS_TOAST",
|
||||||
title?: React.ReactNode;
|
REMOVE_TOAST: "REMOVE_TOAST",
|
||||||
description?: React.ReactNode;
|
} as const
|
||||||
action?: React.ReactNode;
|
|
||||||
duration?: number; // 지속 시간 추가
|
|
||||||
variant?: "default" | "destructive"; // variant 속성 추가
|
|
||||||
};
|
|
||||||
|
|
||||||
const actionTypes = ['default', 'cancel'] as const
|
|
||||||
|
|
||||||
export type ToastActionType = typeof actionTypes[number]
|
|
||||||
|
|
||||||
let count = 0
|
let count = 0
|
||||||
|
|
||||||
function genId() {
|
function genId() {
|
||||||
return String(count++)
|
count = (count + 1) % Number.MAX_SAFE_INTEGER
|
||||||
|
return count.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useToast = () => {
|
type ActionType = typeof actionTypes
|
||||||
const [toasts, setToasts] = React.useState<ToasterToast[]>([]);
|
|
||||||
|
|
||||||
// 토스트 추가 함수
|
type Action =
|
||||||
const addToast = React.useCallback(
|
| {
|
||||||
(props: Omit<ToasterToast, "id">) => {
|
type: ActionType["ADD_TOAST"]
|
||||||
// 토스트에 기본 지속 시간 추가
|
toast: ToasterToast
|
||||||
const toastWithDefaults = {
|
}
|
||||||
...props,
|
| {
|
||||||
duration: props.duration || DEFAULT_TOAST_DURATION,
|
type: ActionType["UPDATE_TOAST"]
|
||||||
};
|
toast: Partial<ToasterToast> & { id: string }
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: ActionType["DISMISS_TOAST"]
|
||||||
|
toastId?: string
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: ActionType["REMOVE_TOAST"]
|
||||||
|
toastId?: string
|
||||||
|
}
|
||||||
|
|
||||||
const id = genId()
|
interface State {
|
||||||
|
toasts: ToasterToast[]
|
||||||
|
}
|
||||||
|
|
||||||
setToasts((prev) => [...prev, { id, ...toastWithDefaults }])
|
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
|
||||||
return id
|
|
||||||
},
|
|
||||||
[toasts]
|
|
||||||
);
|
|
||||||
|
|
||||||
const updateToast = React.useCallback(
|
const addToRemoveQueue = (toastId: string) => {
|
||||||
(id: string, props: Partial<ToasterToast>) => {
|
if (toastTimeouts.has(toastId)) {
|
||||||
setToasts((prev) =>
|
return
|
||||||
prev.map((toast) => (toast.id === id ? { ...toast, ...props } : toast))
|
}
|
||||||
)
|
|
||||||
},
|
|
||||||
[toasts]
|
|
||||||
)
|
|
||||||
|
|
||||||
const dismissToast = React.useCallback(
|
const timeout = setTimeout(() => {
|
||||||
(id: string) => {
|
toastTimeouts.delete(toastId)
|
||||||
setToasts((prev) => prev.filter((toast) => toast.id !== id))
|
dispatch({
|
||||||
},
|
type: actionTypes.REMOVE_TOAST,
|
||||||
[toasts]
|
toastId: toastId,
|
||||||
)
|
})
|
||||||
|
}, TOAST_REMOVE_DELAY)
|
||||||
|
|
||||||
|
toastTimeouts.set(toastId, timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const reducer = (state: State, action: Action): State => {
|
||||||
|
switch (action.type) {
|
||||||
|
case actionTypes.ADD_TOAST:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
|
||||||
|
}
|
||||||
|
|
||||||
|
case actionTypes.UPDATE_TOAST:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
toasts: state.toasts.map((t) =>
|
||||||
|
t.id === action.toast.id ? { ...t, ...action.toast } : t
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
case actionTypes.DISMISS_TOAST: {
|
||||||
|
const { toastId } = action
|
||||||
|
|
||||||
|
if (toastId) {
|
||||||
|
addToRemoveQueue(toastId)
|
||||||
|
} else {
|
||||||
|
state.toasts.forEach((toast) => {
|
||||||
|
addToRemoveQueue(toast.id)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toasts,
|
...state,
|
||||||
addToast,
|
toasts: state.toasts.map((t) =>
|
||||||
updateToast,
|
t.id === toastId || toastId === undefined
|
||||||
dismissToast,
|
? {
|
||||||
toast: (props: Omit<ToasterToast, "id">) => addToast(props),
|
...t,
|
||||||
};
|
open: false,
|
||||||
};
|
}
|
||||||
|
: t
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case actionTypes.REMOVE_TOAST:
|
||||||
|
if (action.toastId === undefined) {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
toasts: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
toasts: state.toasts.filter((t) => t.id !== action.toastId),
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 전역 토스트 함수에도 기본 지속 시간 적용
|
const listeners: Array<(state: State) => void> = []
|
||||||
export const toast = (props: Omit<ToasterToast, "id">) => {
|
|
||||||
// 기본 지속 시간을 적용
|
let memoryState: State = { toasts: [] }
|
||||||
const toastWithDefaults = {
|
|
||||||
|
function dispatch(action: Action) {
|
||||||
|
memoryState = reducer(memoryState, action)
|
||||||
|
listeners.forEach((listener) => {
|
||||||
|
listener(memoryState)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Toast = Omit<ToasterToast, "id">
|
||||||
|
|
||||||
|
function toast({ ...props }: Toast) {
|
||||||
|
const id = genId()
|
||||||
|
|
||||||
|
const update = (props: ToasterToast) =>
|
||||||
|
dispatch({
|
||||||
|
type: actionTypes.UPDATE_TOAST,
|
||||||
|
toast: { ...props, id },
|
||||||
|
})
|
||||||
|
const dismiss = () => dispatch({ type: actionTypes.DISMISS_TOAST, toastId: id })
|
||||||
|
|
||||||
|
dispatch({
|
||||||
|
type: actionTypes.ADD_TOAST,
|
||||||
|
toast: {
|
||||||
...props,
|
...props,
|
||||||
duration: props.duration || DEFAULT_TOAST_DURATION,
|
id,
|
||||||
};
|
open: true,
|
||||||
|
onOpenChange: (open) => {
|
||||||
|
if (!open) dismiss()
|
||||||
|
},
|
||||||
|
duration: props.duration || 5000, // 기본 지속 시간 설정
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
// 원래 toast 함수 호출
|
return {
|
||||||
return originalToast(toastWithDefaults);
|
id,
|
||||||
};
|
dismiss,
|
||||||
|
update,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function useToast() {
|
||||||
|
const [state, setState] = React.useState<State>(memoryState)
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
listeners.push(setState)
|
||||||
|
return () => {
|
||||||
|
const index = listeners.indexOf(setState)
|
||||||
|
if (index > -1) {
|
||||||
|
listeners.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [state])
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
toast,
|
||||||
|
dismiss: (toastId?: string) => dispatch({ type: actionTypes.DISMISS_TOAST, toastId }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { useToast, toast }
|
||||||
|
|||||||
Reference in New Issue
Block a user