返回 DeepSeek-Reasonix
toast.tsx
根目录 / desktop / frontend / src / lib / toast.tsx
1 import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from "react";
2 import { ErrorMessage } from "../components/ErrorMessage";
3 import { useT } from "./i18n";
4
5 export interface Toast {
6 id: number;
7 text: string;
8 level: "info" | "warn" | "error";
9 summary?: string;
10 actionLabel?: string;
11 onAction?: () => void;
12 }
13
14 export interface ToastOptions {
15 summary?: string;
16 actionLabel?: string;
17 onAction?: () => void;
18 durationMs?: number;
19 }
20
21 export interface ToastContextValue {
22 toasts: Toast[];
23 showToast: (text: string, level?: Toast["level"], options?: ToastOptions) => void;
24 }
25
26 const ToastContext = createContext<ToastContextValue>({ toasts: [], showToast: () => {} });
27
28 export function useToast() {
29 return useContext(ToastContext);
30 }
31
32 let nextId = 1;
33
34 export function ToastProvider({ children }: { children: ReactNode }) {
35 const translate = useT();
36 const [toasts, setToasts] = useState<Toast[]>([]);
37 const timers = useRef(new Map<number, ReturnType<typeof setTimeout>>());
38 useEffect(() => {
39 const activeTimers = timers.current;
40 return () => { for (const timer of activeTimers.values()) clearTimeout(timer); activeTimers.clear(); };
41 }, []);
42
43 const showToast = useCallback((text: string, level: Toast["level"] = "info", options: ToastOptions = {}) => {
44 const id = nextId++;
45 setToasts((prev) => [...prev, { id, text, level, summary: options.summary, actionLabel: options.actionLabel, onAction: options.onAction }]);
46 const timer = setTimeout(() => {
47 setToasts((prev) => prev.filter((t) => t.id !== id));
48 timers.current.delete(id);
49 }, options.durationMs ?? (options.actionLabel || level !== "info" ? 8000 : 2500));
50 timers.current.set(id, timer);
51 }, []);
52
53 const dismissToast = useCallback((id: number) => {
54 const timer = timers.current.get(id);
55 if (timer) clearTimeout(timer);
56 timers.current.delete(id);
57 setToasts((prev) => prev.filter((t) => t.id !== id));
58 }, []);
59
60 return (
61 <ToastContext.Provider value={{ toasts, showToast }}>
62 {children}
63 <div className="toast-container" role="status" aria-live="polite">
64 {toasts.map((t) => (
65 <div key={t.id} className={`toast toast--${t.level}`} onClick={() => dismissToast(t.id)}>
66 {t.level === "warn" && <span className="toast__icon">⚠️</span>}
67 {t.level === "error" && <span className="toast__icon">❌</span>}
68 <span className="toast__text">{t.level === "info" ? t.text : <ErrorMessage error={t.text} summary={t.summary} onInspect={() => {
69 clearTimeout(timers.current.get(t.id));
70 timers.current.delete(t.id);
71 }} />}</span>
72 {t.actionLabel && t.onAction && (
73 <button
74 type="button"
75 className="toast__action"
76 onClick={(event) => {
77 event.stopPropagation();
78 dismissToast(t.id);
79 t.onAction?.();
80 }}
81 >
82 {t.actionLabel}
83 </button>
84 )}
85 <button type="button" className="toast__action" aria-label={translate("error.dismiss")}
86 onClick={(event) => { event.stopPropagation(); dismissToast(t.id); }}>×</button>
87 </div>
88 ))}
89 </div>
90 </ToastContext.Provider>
91 );
92 }
93
93 lines Plain Text