the-tip-top-frontend/hooks/useToast.ts
2025-11-17 23:38:02 +01:00

111 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import toast, { Toast, ToastOptions } from 'react-hot-toast';
/**
* Hook personnalisé pour gérer les notifications toast
* Wrapper autour de react-hot-toast avec des méthodes simplifiées
*/
export function useToast() {
/**
* Affiche une notification de succès
*/
const success = (message: string, options?: ToastOptions) => {
return toast.success(message, {
duration: 4000,
position: 'top-right',
...options,
});
};
/**
* Affiche une notification d'erreur
*/
const error = (message: string, options?: ToastOptions) => {
return toast.error(message, {
duration: 5000,
position: 'top-right',
...options,
});
};
/**
* Affiche une notification d'information
*/
const info = (message: string, options?: ToastOptions) => {
return toast(message, {
duration: 4000,
position: 'top-right',
icon: '',
...options,
});
};
/**
* Affiche une notification d'avertissement
*/
const warning = (message: string, options?: ToastOptions) => {
return toast(message, {
duration: 4000,
position: 'top-right',
icon: '⚠️',
...options,
});
};
/**
* Affiche une notification de chargement
*/
const loading = (message: string, options?: ToastOptions) => {
return toast.loading(message, {
position: 'top-right',
...options,
});
};
/**
* Affiche une notification promise (loading -> success/error)
*/
const promise = <T,>(
promise: Promise<T>,
messages: {
loading: string;
success: string | ((data: T) => string);
error: string | ((error: any) => string);
},
options?: ToastOptions
) => {
return toast.promise(
promise,
messages,
{
position: 'top-right',
...options,
}
);
};
/**
* Ferme une notification spécifique
*/
const dismiss = (toastId?: string) => {
toast.dismiss(toastId);
};
/**
* Ferme toutes les notifications
*/
const dismissAll = () => {
toast.dismiss();
};
return {
success,
error,
info,
warning,
loading,
promise,
dismiss,
dismissAll,
};
}