import { useValue } from '@tldraw/editor' import { Toast as _Toast } from 'radix-ui' import { memo } from 'react' import { AlertSeverity, TLUiToast, useToasts } from '../context/toasts' import { useTranslation } from '../hooks/useTranslation/useTranslation' import { TLUiIconType } from '../icon-types' import { TldrawUiButton } from './primitives/Button/TldrawUiButton' import { TldrawUiButtonLabel } from './primitives/Button/TldrawUiButtonLabel' import { TldrawUiIcon } from './primitives/TldrawUiIcon' const DEFAULT_TOAST_DURATION = 4000 const SEVERITY_TO_ICON: { [msg in AlertSeverity]: TLUiIconType } = { success: 'check-circle', warning: 'warning-triangle', error: 'cross-circle', info: 'info-circle', } /** @internal */ function TldrawUiToast({ 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 const icon = toast.icon || (toast.severity && SEVERITY_TO_ICON[toast.severity]) const iconLabel = toast.iconLabel || (toast.severity ? msg(`toast.${toast.severity}`) : '') return ( <_Toast.Root onOpenChange={onOpenChange} className="tlui-toast__container" duration={toast.keepOpen ? Infinity : DEFAULT_TOAST_DURATION} data-severity={toast.severity} > {icon && (