import { useEffect, useRef } from 'react'; import { faXmark } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; type NotificationType = 'success' | 'error' | 'info'; export type TNotification = { text: string; type?: NotificationType; timeoutMs?: number; }; interface NotificationProps { notification: Required; clearNotification: (text: string) => void; } const Notification = ({ notification, clearNotification }: NotificationProps) => { const timeout = useRef(); const handleClearNotification = () => clearNotification(notification.text); const setNotifTimeout = () => { timeout.current = window.setTimeout(handleClearNotification, notification.timeoutMs); }; const cancelNotifTimeout = () => { clearTimeout(timeout.current); }; useEffect(() => { setNotifTimeout(); return cancelNotifTimeout; }, []); return (
{notification.type === 'error' && (
)} {notification.type === 'success' && (
)} {notification.type === 'info' && (
)}

{notification.text}

); }; export default Notification;