video-accessibility-old/frontend/src/hooks/useToast.ts
2025-08-24 16:28:33 -05:00

58 lines
No EOL
1.4 KiB
TypeScript

import { useState, useCallback } from 'react';
import type { ToastMessage } from '../components/Toast/Toast';
export function useToast() {
const [toasts, setToasts] = useState<ToastMessage[]>([]);
const addToast = useCallback((
message: string,
type: ToastMessage['type'] = 'info',
duration?: number
) => {
const id = Math.random().toString(36).substr(2, 9);
const toast: ToastMessage = {
id,
message,
type,
duration,
};
setToasts(prev => [...prev, toast]);
return id;
}, []);
const removeToast = useCallback((id: string) => {
setToasts(prev => prev.filter(toast => toast.id !== id));
}, []);
const success = useCallback((message: string, duration?: number) => {
return addToast(message, 'success', duration);
}, [addToast]);
const error = useCallback((message: string, duration?: number) => {
return addToast(message, 'error', duration || 6000); // Longer for errors
}, [addToast]);
const warning = useCallback((message: string, duration?: number) => {
return addToast(message, 'warning', duration);
}, [addToast]);
const info = useCallback((message: string, duration?: number) => {
return addToast(message, 'info', duration);
}, [addToast]);
const clearAll = useCallback(() => {
setToasts([]);
}, []);
return {
toasts,
addToast,
removeToast,
success,
error,
warning,
info,
clearAll,
};
}