The toast function was updated to correctly pass the 'id' property, resolving the TypeScript error.
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
|
|
import * as React from "react"
|
|
import { ToastProps } from "@/components/ui/toast"
|
|
|
|
import { useToastStore } from "@/components/ui/use-toast"
|
|
import { ToastActionElement } from "@/components/ui/toast"
|
|
|
|
// 기본 지속 시간 늘리기 (기존 3000ms → 3500ms)
|
|
export const DEFAULT_TOAST_DURATION = 3500;
|
|
|
|
export const TOAST_REMOVE_DELAY = 1000000;
|
|
|
|
export type ToasterToast = ToastProps & {
|
|
id: string;
|
|
title?: React.ReactNode;
|
|
description?: React.ReactNode;
|
|
action?: React.ReactNode;
|
|
duration?: number; // 지속 시간 추가
|
|
variant?: "default" | "destructive"; // variant 속성 추가
|
|
};
|
|
|
|
const actionTypes = ['default', 'cancel'] as const
|
|
|
|
export type ToastActionType = typeof actionTypes[number]
|
|
|
|
let count = 0
|
|
|
|
function genId() {
|
|
return String(count++)
|
|
}
|
|
|
|
export const useToast = () => {
|
|
const [toasts, setToasts] = React.useState<ToasterToast[]>([]);
|
|
|
|
// 토스트 추가 함수
|
|
const addToast = React.useCallback(
|
|
(props: Omit<ToasterToast, "id">) => {
|
|
// 토스트에 기본 지속 시간 추가
|
|
const toastWithDefaults = {
|
|
...props,
|
|
duration: props.duration || DEFAULT_TOAST_DURATION,
|
|
};
|
|
|
|
const id = genId()
|
|
|
|
setToasts((prev) => [...prev, { id, ...toastWithDefaults }])
|
|
return id
|
|
},
|
|
[toasts]
|
|
);
|
|
|
|
const updateToast = React.useCallback(
|
|
(id: string, props: Partial<ToasterToast>) => {
|
|
setToasts((prev) =>
|
|
prev.map((toast) => (toast.id === id ? { ...toast, ...props } : toast))
|
|
)
|
|
},
|
|
[toasts]
|
|
)
|
|
|
|
const dismissToast = React.useCallback(
|
|
(id: string) => {
|
|
setToasts((prev) => prev.filter((toast) => toast.id !== id))
|
|
},
|
|
[toasts]
|
|
)
|
|
|
|
return {
|
|
toasts,
|
|
addToast,
|
|
updateToast,
|
|
dismissToast,
|
|
toast: (props: Omit<ToasterToast, "id">) => addToast(props),
|
|
};
|
|
};
|
|
|
|
// 전역 토스트 함수에도 기본 지속 시간 적용
|
|
export const toast = (props: Omit<ToasterToast, "id">) => {
|
|
// 기본 지속 시간을 적용
|
|
const toastWithDefaults = {
|
|
...props,
|
|
duration: props.duration || DEFAULT_TOAST_DURATION,
|
|
};
|
|
|
|
return useToastStore().addToast(toastWithDefaults);
|
|
};
|