import type { ComponentChildren, JSX as JSXInternal } from 'preact'; import { useCallback, useEffect } from 'preact/hooks'; import { useTranslation } from 'react-i18next'; import styles from './styles.scss'; import { createClassName } from '../../helpers/createClassName'; import CloseIcon from '../../icons/close.svg'; export type AlertProps = { id?: string; onDismiss?: (id?: string) => void; success?: boolean; warning?: boolean; error?: boolean; color?: string; hideCloseButton?: boolean; className?: string; style?: JSXInternal.CSSProperties; children?: ComponentChildren; timeout?: number; }; const Alert = ({ id, onDismiss, success, warning, error, color, hideCloseButton = false, className, style = {}, children, timeout = 3000, }: AlertProps) => { const { t } = useTranslation(); const handleDismiss = useCallback(() => { onDismiss?.(id); }, [id, onDismiss]); useEffect(() => { let dismissTimeout: ReturnType | undefined; if (Number.isFinite(timeout) && timeout > 0) { dismissTimeout = setTimeout(handleDismiss, timeout); } return () => clearTimeout(dismissTimeout); }, [handleDismiss, timeout]); return (
{children}
{!hideCloseButton && ( )}
); }; export default Alert;