|
| 1 | +import React, { createContext, useContext, useState, useCallback } from "react"; |
| 2 | + |
| 3 | +type ToastType = "success" | "error" | "info"; |
| 4 | + |
| 5 | +interface ToastItem { |
| 6 | + id: number; |
| 7 | + type: ToastType; |
| 8 | + message: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface ToastContextValue { |
| 12 | + addToast: (type: ToastType, message: string) => void; |
| 13 | +} |
| 14 | + |
| 15 | +const ToastContext = createContext<ToastContextValue | null>(null); |
| 16 | + |
| 17 | +export function useToast(): ToastContextValue { |
| 18 | + const ctx = useContext(ToastContext); |
| 19 | + if (!ctx) throw new Error("useToast must be used within <ToastProvider>"); |
| 20 | + return ctx; |
| 21 | +} |
| 22 | + |
| 23 | +let nextId = 0; |
| 24 | + |
| 25 | +const typeStyles: Record<ToastType, { bg: string; border: string; icon: string }> = { |
| 26 | + success: { bg: "rgba(52, 211, 153, 0.12)", border: "rgba(52, 211, 153, 0.3)", icon: "✓" }, |
| 27 | + error: { bg: "rgba(244, 63, 94, 0.12)", border: "rgba(244, 63, 94, 0.3)", icon: "✕" }, |
| 28 | + info: { bg: "rgba(56, 189, 248, 0.12)", border: "rgba(56, 189, 248, 0.3)", icon: "ℹ" }, |
| 29 | +}; |
| 30 | + |
| 31 | +const typeColor: Record<ToastType, string> = { |
| 32 | + success: "#34d399", |
| 33 | + error: "#f43f5e", |
| 34 | + info: "#38bdf8", |
| 35 | +}; |
| 36 | + |
| 37 | +export function ToastProvider({ children }: { children: React.ReactNode }) { |
| 38 | + const [toasts, setToasts] = useState<ToastItem[]>([]); |
| 39 | + |
| 40 | + const addToast = useCallback((type: ToastType, message: string) => { |
| 41 | + const id = ++nextId; |
| 42 | + setToasts((prev) => [...prev, { id, type, message }]); |
| 43 | + setTimeout(() => { |
| 44 | + setToasts((prev) => prev.filter((t) => t.id !== id)); |
| 45 | + }, 4000); |
| 46 | + }, []); |
| 47 | + |
| 48 | + return ( |
| 49 | + <ToastContext.Provider value={{ addToast }}> |
| 50 | + {children} |
| 51 | + <div |
| 52 | + style={{ |
| 53 | + position: "fixed", |
| 54 | + bottom: "24px", |
| 55 | + right: "24px", |
| 56 | + display: "flex", |
| 57 | + flexDirection: "column", |
| 58 | + gap: "10px", |
| 59 | + zIndex: 9999, |
| 60 | + pointerEvents: "none", |
| 61 | + }} |
| 62 | + > |
| 63 | + {toasts.map((toast) => { |
| 64 | + const s = typeStyles[toast.type]; |
| 65 | + return ( |
| 66 | + <div |
| 67 | + key={toast.id} |
| 68 | + style={{ |
| 69 | + display: "flex", |
| 70 | + alignItems: "center", |
| 71 | + gap: "10px", |
| 72 | + padding: "12px 18px", |
| 73 | + background: "#16161f", |
| 74 | + border: `1px solid ${s.border}`, |
| 75 | + borderRadius: "12px", |
| 76 | + boxShadow: "0 4px 24px rgba(0,0,0,0.4)", |
| 77 | + fontSize: "14px", |
| 78 | + fontFamily: "'Inter', system-ui, sans-serif", |
| 79 | + color: "#f0f0f5", |
| 80 | + animation: "toast-in 300ms ease-out", |
| 81 | + pointerEvents: "auto", |
| 82 | + maxWidth: "380px", |
| 83 | + }} |
| 84 | + > |
| 85 | + <span |
| 86 | + style={{ |
| 87 | + width: "24px", |
| 88 | + height: "24px", |
| 89 | + display: "flex", |
| 90 | + alignItems: "center", |
| 91 | + justifyContent: "center", |
| 92 | + borderRadius: "50%", |
| 93 | + background: s.bg, |
| 94 | + color: typeColor[toast.type], |
| 95 | + fontSize: "12px", |
| 96 | + fontWeight: 700, |
| 97 | + flexShrink: 0, |
| 98 | + }} |
| 99 | + > |
| 100 | + {s.icon} |
| 101 | + </span> |
| 102 | + {toast.message} |
| 103 | + </div> |
| 104 | + ); |
| 105 | + })} |
| 106 | + </div> |
| 107 | + </ToastContext.Provider> |
| 108 | + ); |
| 109 | +} |
0 commit comments