31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
|
import { Toast, ToastContainer, useToast as useToastHook } from '../components/Toast';
|
|
|
|
interface ToastContextType {
|
|
success: (message: string, duration?: number) => void;
|
|
error: (message: string, duration?: number) => void;
|
|
warning: (message: string, duration?: number) => void;
|
|
info: (message: string, duration?: number) => void;
|
|
}
|
|
|
|
const ToastContext = createContext<ToastContextType | undefined>(undefined);
|
|
|
|
export const ToastProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
|
const { toasts, closeToast, success, error, warning, info } = useToastHook();
|
|
|
|
return (
|
|
<ToastContext.Provider value={{ success, error, warning, info }}>
|
|
{children}
|
|
<ToastContainer toasts={toasts} onClose={closeToast} />
|
|
</ToastContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useToast = () => {
|
|
const context = useContext(ToastContext);
|
|
if (!context) {
|
|
throw new Error('useToast must be used within ToastProvider');
|
|
}
|
|
return context;
|
|
};
|