import React, { ReactNode, createContext, useCallback, useContext, useRef, } from 'react'; import { ToastConfig, ToastRef } from '../types'; import { Toast } from '../components/Toast'; const ToastContext = createContext< Pick >({ show: () => {}, hide: () => {}, isVisible: () => false, }); export const ToastProvider = ({ children, defaultConfig, }: { children: ReactNode; defaultConfig?: Partial; }) => { const toastRef = useRef(null); const hide = useCallback(() => { if (!toastRef.current) return; toastRef.current?.hide(); }, []); const show = useCallback((config: ToastConfig) => { if (!toastRef.current) return; toastRef.current.show(config); }, []); const isVisible = useCallback(() => { if (!toastRef.current) return false; return toastRef.current?.isVisible(); }, []); return ( {children} ); }; export const useToast = () => useContext(ToastContext);