111 lines
2.2 KiB
TypeScript
111 lines
2.2 KiB
TypeScript
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,
|
||
};
|
||
}
|