Align admin, community, search, library, learning and course management with the mockup. Add saved courses, download history, public community feed, instructor directory, backend search, course views, admin content tools and community moderation. Harden API and Nginx for production.
72 lines
2.7 KiB
TypeScript
72 lines
2.7 KiB
TypeScript
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<ToastContextType | undefined>(undefined);
|
|
|
|
const icons: Record<ToastType, React.ReactNode> = {
|
|
success: <CheckCircle className="ph" />,
|
|
error: <WarningCircle className="ph" />,
|
|
info: <Info className="ph" />,
|
|
download: <DownloadSimple className="ph" />,
|
|
};
|
|
|
|
export const ToastProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
const [toasts, setToasts] = useState<Toast[]>([]);
|
|
|
|
const push = useCallback((toast: Omit<Toast, 'id'>, 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 (
|
|
<ToastContext.Provider value={{ showToast, showDownloadToast }}>
|
|
{children}
|
|
<div className="toasts" role="status" aria-live="polite">
|
|
{toasts.map((toast) => (
|
|
<div key={toast.id} className={`toast ${toast.type}`}>
|
|
{icons[toast.type]}
|
|
{toast.type === 'download' ? (
|
|
<p>{toast.fileName}<small>Baixado{toast.fileSize ? ` · ${toast.fileSize}` : ''}</small></p>
|
|
) : (
|
|
<p>{toast.message}</p>
|
|
)}
|
|
<button type="button" className="iconbtn" onClick={() => removeToast(toast.id)} aria-label="Fechar aviso"><X className="ph" /></button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</ToastContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useToast = () => {
|
|
const context = useContext(ToastContext);
|
|
if (!context) throw new Error('useToast must be used within ToastProvider');
|
|
return context;
|
|
};
|