import React, { createContext, useContext, useState, useCallback } from 'react'; import { CheckCircle, DownloadSimple, Info, WarningCircle, X } from '@phosphor-icons/react'; export type ToastType = 'success' | 'error' | 'info' | 'download'; export interface Toast { id: string; message: string; type: ToastType; fileName?: string; fileSize?: string; } export interface ToastContextType { showToast: (message: string, type?: ToastType, fileName?: string, fileSize?: string) => void; showDownloadToast: (fileName: string, fileSize?: string) => void; } const ToastContext = createContext(undefined); const icons: Record = { success: , error: , info: , download: , }; export const ToastProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [toasts, setToasts] = useState([]); const push = useCallback((toast: Omit, duration: number) => { const id = Date.now().toString() + Math.random().toString(36).substring(2, 5); setToasts((prev) => [...prev, { ...toast, id }]); setTimeout(() => setToasts((prev) => prev.filter((t) => t.id !== id)), duration); }, []); const showToast = useCallback((message: string, type: ToastType = 'success', fileName?: string, fileSize?: string) => { push({ message, type, fileName, fileSize }, 4000); }, [push]); const showDownloadToast = useCallback((fileName: string, fileSize?: string) => { push({ message: `O material "${fileName}" foi baixado com sucesso.`, type: 'download', fileName, fileSize }, 4500); }, [push]); const removeToast = (id: string) => setToasts((prev) => prev.filter((t) => t.id !== id)); return ( {children}
{toasts.map((toast) => (
{icons[toast.type]} {toast.type === 'download' ? (

{toast.fileName}Baixado{toast.fileSize ? ` ยท ${toast.fileSize}` : ''}

) : (

{toast.message}

)}
))}
); }; export const useToast = () => { const context = useContext(ToastContext); if (!context) throw new Error('useToast must be used within ToastProvider'); return context; };