import { createContext, useContext } from 'react'; import type { ToastContainerProps, ToastProps } from './types'; export type ToastControllerContextType = { show: (props: Omit) => string; hide: (id: string) => void; clearAll: () => void; }; export const fallbackToastControlContext: ToastControllerContextType = { show: (_: Omit) => '', hide: (_: string) => { // Fallback empty function }, clearAll: () => { // Fallback empty function }, }; export const ToastContext = createContext( fallbackToastControlContext ); type ToastConfigContextType = Pick< ToastContainerProps, 'position' | 'displayType' >; export const ToastConfigContext = createContext( {} as ToastConfigContextType ); export const useToastConfig = () => useContext(ToastConfigContext); export const useToast = () => { const context = useContext(ToastContext); if (!context) { // eslint-disable-next-line no-console console.warn('Toast was used without ToastProvider'); return fallbackToastControlContext; } return context; };