Files
edr-platform/apps/edr-freight-web/backoffice/src/hooks/use-toast.ts

28 lines
785 B
TypeScript

import { useCallback } from 'react';
import toast from 'react-hot-toast';
interface ToastOptions {
title?: string;
description?: string;
variant?: 'default' | 'destructive';
duration?: number;
}
export function useToast() {
// Stable identity so callers can safely list `toast` in effect/callback deps
// without re-firing on every render.
const showToast = useCallback((options: ToastOptions) => {
const { title, description, variant = 'default', duration = 3000 } = options;
const message = title ? `${title}${description ? ': ' + description : ''}` : description || '';
if (variant === 'destructive') {
toast.error(message, { duration });
} else {
toast.success(message, { duration });
}
}, []);
return { toast: showToast };
}