import * as T from '@radix-ui/react-toast' import * as React from 'react' import { TLUiToast, useToasts } from '../hooks/useToastsProvider' import { useTranslation } from '../hooks/useTranslation/useTranslation' import { TLUiIconType } from '../icon-types' import { Button } from './primitives/Button' import { Icon } from './primitives/Icon' function Toast({ toast }: { toast: TLUiToast }) { const { removeToast } = useToasts() const msg = useTranslation() const onOpenChange = (isOpen: boolean) => { if (!isOpen) { removeToast(toast.id) } } const hasActions = toast.actions && toast.actions.length > 0 return ( {toast.icon && (
)}
{toast.title && {toast.title}} {toast.description && ( {toast.description} )}
{toast.actions && (
{toast.actions.map((action, i) => ( ))}
)}
{!hasActions && ( )}
) } function _Toasts() { const { toasts } = useToasts() return ( <> {toasts.map((toast) => ( ))} ) } export const Toasts = React.memo(_Toasts) export function ToastViewport() { const { toasts } = useToasts() const [hasToasts, setHasToasts] = React.useState(false) React.useEffect(() => { let cancelled = false if (toasts.length) { setHasToasts(true) } else { setTimeout(() => { if (!cancelled) { setHasToasts(false) } }, 1000) } return () => { cancelled = true } }, [toasts.length, setHasToasts]) if (!hasToasts) return null return }