chore(release): snapshot 1.4.2
This commit is contained in:
57
front-end-dash.aggios.app/components/layout/ToastContext.tsx
Normal file
57
front-end-dash.aggios.app/components/layout/ToastContext.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useContext, useState, useCallback } from 'react';
|
||||
import ToastNotification, { Toast } from './ToastNotification';
|
||||
|
||||
interface ToastContextType {
|
||||
showToast: (type: Toast['type'], title: string, message?: string) => void;
|
||||
success: (title: string, message?: string) => void;
|
||||
error: (title: string, message?: string) => void;
|
||||
info: (title: string, message?: string) => void;
|
||||
}
|
||||
|
||||
const ToastContext = createContext<ToastContextType | undefined>(undefined);
|
||||
|
||||
export function ToastProvider({ children }: { children: React.ReactNode }) {
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
|
||||
const showToast = useCallback((type: Toast['type'], title: string, message?: string) => {
|
||||
const id = Date.now().toString();
|
||||
setToasts(prev => [...prev, { id, type, title, message }]);
|
||||
}, []);
|
||||
|
||||
const success = useCallback((title: string, message?: string) => {
|
||||
showToast('success', title, message);
|
||||
}, [showToast]);
|
||||
|
||||
const error = useCallback((title: string, message?: string) => {
|
||||
showToast('error', title, message);
|
||||
}, [showToast]);
|
||||
|
||||
const info = useCallback((title: string, message?: string) => {
|
||||
showToast('info', title, message);
|
||||
}, [showToast]);
|
||||
|
||||
const removeToast = useCallback((id: string) => {
|
||||
setToasts(prev => prev.filter(toast => toast.id !== id));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ showToast, success, error, info }}>
|
||||
{children}
|
||||
<div className="fixed top-4 right-4 z-[9999] flex flex-col gap-3 w-96">
|
||||
{toasts.map(toast => (
|
||||
<ToastNotification key={toast.id} toast={toast} onClose={removeToast} />
|
||||
))}
|
||||
</div>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useToast() {
|
||||
const context = useContext(ToastContext);
|
||||
if (!context) {
|
||||
throw new Error('useToast must be used within ToastProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user