All files / src/hooks useToast.ts

100% Statements 109/109
100% Branches 11/11
100% Functions 5/5
100% Lines 109/109

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 2x 2x 43x 43x 43x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x  
/**
 * useToast Hook
 *
 * Centralized toast notification hook for displaying user feedback messages.
 * Must be used within ToastProvider context.
 *
 * @example
 * ```tsx
 * import { useToast } from '@/hooks/useToast';
 *
 * function MyComponent() {
 *   const { addToast } = useToast();
 *
 *   const handleClick = () => {
 *     addToast({
 *       title: 'Success',
 *       message: 'Item added to cart',
 *       variant: 'success',
 *       duration: 5000,
 *     });
 *   };
 *
 *   return <button onClick={handleClick}>Add Item</button>;
 * }
 * ```
 */
 
import { useContext } from 'react';
import { ToastContext } from '@/components/ui/Toast';
 
/**
 * Toast notification interface
 */
export interface Toast {
  id: string;
  title?: string;
  message: string;
  variant?: 'info' | 'success' | 'warning' | 'error';
  duration?: number;
}
 
/**
 * Toast context value interface
 */
export interface ToastContextValue {
  toasts: Toast[];
  addToast: (toast: Omit<Toast, 'id'>) => void;
  removeToast: (id: string) => void;
}
 
/**
 * Hook to use toast notifications
 *
 * @throws {Error} If used outside of ToastProvider
 * @returns {ToastContextValue} Toast context methods
 */
export function useToast(): ToastContextValue {
  const context = useContext(ToastContext);
 
  if (!context) {
    throw new Error('useToast must be used within ToastProvider');
  }
 
  return context;
}
 
/**
 * Helper function to show success toast
 */
export function useSuccessToast() {
  const { addToast } = useToast();
 
  return (message: string, title = 'Success') => {
    addToast({ title, message, variant: 'success' });
  };
}
 
/**
 * Helper function to show error toast
 */
export function useErrorToast() {
  const { addToast } = useToast();
 
  return (message: string, title = 'Error') => {
    addToast({ title, message, variant: 'error' });
  };
}
 
/**
 * Helper function to show warning toast
 */
export function useWarningToast() {
  const { addToast } = useToast();
 
  return (message: string, title = 'Warning') => {
    addToast({ title, message, variant: 'warning' });
  };
}
 
/**
 * Helper function to show info toast
 */
export function useInfoToast() {
  const { addToast } = useToast();
 
  return (message: string, title?: string) => {
    addToast({ title, message, variant: 'info' });
  };
}