diff --git a/apps/www/registry/default/ui/use-toast.ts b/apps/www/registry/default/ui/use-toast.ts index b38cacbd749..3ef59b0eb93 100644 --- a/apps/www/registry/default/ui/use-toast.ts +++ b/apps/www/registry/default/ui/use-toast.ts @@ -132,6 +132,12 @@ const listeners: Array<(state: State) => void> = [] let memoryState: State = { toasts: [] } function dispatch(action: Action) { + if (action.type === "ADD_TOAST") { + const toastExists = memoryState.toasts.some((t) => t.id === action.toast.id) + if (toastExists) { + return + } + } memoryState = reducer(memoryState, action) listeners.forEach((listener) => { listener(memoryState) @@ -140,10 +146,10 @@ function dispatch(action: Action) { type Toast = Omit -function toast({ ...props }: Toast) { - const id = genId() +function toast({ ...props }: Toast & { id?: string }) { + const id = props?.id || genId() - const update = (props: ToasterToast) => + const update = (props: Toast) => dispatch({ type: "UPDATE_TOAST", toast: { ...props, id }, diff --git a/apps/www/registry/new-york/ui/use-toast.ts b/apps/www/registry/new-york/ui/use-toast.ts index b38cacbd749..3ef59b0eb93 100644 --- a/apps/www/registry/new-york/ui/use-toast.ts +++ b/apps/www/registry/new-york/ui/use-toast.ts @@ -132,6 +132,12 @@ const listeners: Array<(state: State) => void> = [] let memoryState: State = { toasts: [] } function dispatch(action: Action) { + if (action.type === "ADD_TOAST") { + const toastExists = memoryState.toasts.some((t) => t.id === action.toast.id) + if (toastExists) { + return + } + } memoryState = reducer(memoryState, action) listeners.forEach((listener) => { listener(memoryState) @@ -140,10 +146,10 @@ function dispatch(action: Action) { type Toast = Omit -function toast({ ...props }: Toast) { - const id = genId() +function toast({ ...props }: Toast & { id?: string }) { + const id = props?.id || genId() - const update = (props: ToasterToast) => + const update = (props: Toast) => dispatch({ type: "UPDATE_TOAST", toast: { ...props, id },