26 lines
1 KiB
JavaScript
26 lines
1 KiB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
|
|
import { createContext, useContext, useState, useCallback } from "react";
|
|
const ToastContext = createContext(null);
|
|
export function useToast() {
|
|
const context = useContext(ToastContext);
|
|
if (!context) {
|
|
throw new Error("useToast must be used within a ToastProvider");
|
|
}
|
|
return context;
|
|
}
|
|
export function ToastProvider({ children }) {
|
|
const [toasts, setToasts] = useState([]);
|
|
const addToast = useCallback((message, type = "info") => {
|
|
const id = Math.random().toString(36).substring(2, 9);
|
|
setToasts(prev => [...prev, { id, message, type }]);
|
|
// Auto-remove after 5 seconds
|
|
setTimeout(() => {
|
|
setToasts(prev => prev.filter(t => t.id !== id));
|
|
}, 5000);
|
|
return id;
|
|
}, []);
|
|
const removeToast = useCallback((id) => {
|
|
setToasts(prev => prev.filter(t => t.id !== id));
|
|
}, []);
|
|
return (_jsx(ToastContext.Provider, { value: { toasts, addToast, removeToast }, children: children }));
|
|
}
|