ToastContext.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { createContext, useContext, useState, useCallback, useRef, useEffect, type ReactNode } from 'react';
  2. import { CheckCircle, XCircle, AlertCircle, Info, X, Loader2 } from 'lucide-react';
  3. type ToastType = 'success' | 'error' | 'warning' | 'info' | 'loading';
  4. interface Toast {
  5. id: string;
  6. message: string;
  7. type: ToastType;
  8. persistent?: boolean;
  9. }
  10. interface ToastContextType {
  11. showToast: (message: string, type?: ToastType) => void;
  12. showPersistentToast: (id: string, message: string, type?: ToastType) => void;
  13. dismissToast: (id: string) => void;
  14. }
  15. const ToastContext = createContext<ToastContextType | undefined>(undefined);
  16. export function useToast() {
  17. const context = useContext(ToastContext);
  18. if (!context) {
  19. throw new Error('useToast must be used within a ToastProvider');
  20. }
  21. return context;
  22. }
  23. const icons = {
  24. success: <CheckCircle className="w-5 h-5 text-green-400" />,
  25. error: <XCircle className="w-5 h-5 text-red-400" />,
  26. warning: <AlertCircle className="w-5 h-5 text-yellow-400" />,
  27. info: <Info className="w-5 h-5 text-blue-400" />,
  28. loading: <Loader2 className="w-5 h-5 text-bambu-green animate-spin" />,
  29. };
  30. const bgColors = {
  31. success: 'bg-green-500/10 border-green-500/30',
  32. error: 'bg-red-500/10 border-red-500/30',
  33. warning: 'bg-yellow-500/10 border-yellow-500/30',
  34. info: 'bg-blue-500/10 border-blue-500/30',
  35. loading: 'bg-bambu-green/10 border-bambu-green/30',
  36. };
  37. export function ToastProvider({ children }: { children: ReactNode }) {
  38. const [toasts, setToasts] = useState<Toast[]>([]);
  39. const timeoutRefs = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
  40. // Clean up all timeouts on unmount
  41. useEffect(() => {
  42. const timeouts = timeoutRefs.current;
  43. return () => {
  44. timeouts.forEach((timeout) => clearTimeout(timeout));
  45. timeouts.clear();
  46. };
  47. }, []);
  48. const showToast = useCallback((message: string, type: ToastType = 'success') => {
  49. const id = Math.random().toString(36).substr(2, 9);
  50. setToasts((prev) => [...prev, { id, message, type }]);
  51. // Auto-dismiss after 3 seconds
  52. const timeout = setTimeout(() => {
  53. setToasts((prev) => prev.filter((t) => t.id !== id));
  54. timeoutRefs.current.delete(id);
  55. }, 3000);
  56. timeoutRefs.current.set(id, timeout);
  57. }, []);
  58. const showPersistentToast = useCallback((id: string, message: string, type: ToastType = 'info') => {
  59. setToasts((prev) => {
  60. // Update existing toast if same id, otherwise add new one
  61. const exists = prev.find((t) => t.id === id);
  62. if (exists) {
  63. return prev.map((t) => (t.id === id ? { ...t, message, type, persistent: true } : t));
  64. }
  65. return [...prev, { id, message, type, persistent: true }];
  66. });
  67. }, []);
  68. const dismissToast = useCallback((id: string) => {
  69. // Clear any pending auto-dismiss timeout
  70. const timeout = timeoutRefs.current.get(id);
  71. if (timeout) {
  72. clearTimeout(timeout);
  73. timeoutRefs.current.delete(id);
  74. }
  75. setToasts((prev) => prev.filter((t) => t.id !== id));
  76. }, []);
  77. return (
  78. <ToastContext.Provider value={{ showToast, showPersistentToast, dismissToast }}>
  79. {children}
  80. {/* Toast Container */}
  81. <div className="fixed bottom-4 right-4 z-50 flex flex-col gap-2">
  82. {toasts.map((toast) => (
  83. <div
  84. key={toast.id}
  85. className={`flex items-center gap-3 px-4 py-3 rounded-lg border shadow-lg backdrop-blur-sm animate-slide-in ${bgColors[toast.type]}`}
  86. >
  87. {icons[toast.type]}
  88. <span className="text-white text-sm">{toast.message}</span>
  89. <button
  90. onClick={() => dismissToast(toast.id)}
  91. className="ml-2 text-bambu-gray hover:text-white transition-colors"
  92. >
  93. <X className="w-4 h-4" />
  94. </button>
  95. </div>
  96. ))}
  97. </div>
  98. </ToastContext.Provider>
  99. );
  100. }