// @ts-nocheck import React, { createContext, useState, useContext } from "react"; import { Button, Flex, Card, Text, Box } from "@radix-ui/themes"; import { CheckCircledIcon, CrossCircledIcon, InfoCircledIcon, } from "@radix-ui/react-icons"; // 定义通知类型枚举 export enum NotificationType { SUCCESS = "success", ERROR = "error", INFO = "info", } // 通知类型定义 type Notification = { id: string; type: NotificationType; title: string; message?: string; }; // 通知配置类型定义 type NotificationConfig = { icon: React.ReactNode; bgColor: string; }; // 通知配置映射 const notificationConfigs: Record = { [NotificationType.SUCCESS]: { icon: , bgColor: "bg-[rgba(0,168,91,0.85)]", }, [NotificationType.ERROR]: { icon: , bgColor: "bg-[rgba(225,45,57,0.85)]", }, [NotificationType.INFO]: { icon: , bgColor: "bg-[rgba(38,131,255,0.85)]", }, }; // 修改通知上下文类型定义 type NotificationContextType = { show: (type: NotificationType, title: string, message?: string) => void; success: (title: string, message?: string) => void; error: (title: string, message?: string) => void; info: (title: string, message?: string) => void; }; const NotificationContext = createContext({ show: () => {}, success: () => {}, error: () => {}, info: () => {}, }); // 简化全局 toast 对象定义 export const toast: NotificationContextType = { show: () => {}, success: () => {}, error: () => {}, info: () => {}, }; export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const [notifications, setNotifications] = useState([]); // 统一参数顺序:title 在前,message 在后 const show = (type: NotificationType, title: string, message?: string) => { const id = Math.random().toString(36).substring(2, 9); const newNotification = { id, type, title, message }; setNotifications((prev) => [...prev, newNotification]); setTimeout(() => { setNotifications((prev) => prev.filter((notification) => notification.id !== id), ); }, 3000); }; // 简化快捷方法定义 const contextValue = { show, success: (title: string, message?: string) => show(NotificationType.SUCCESS, title, message), error: (title: string, message?: string) => show(NotificationType.ERROR, title, message), info: (title: string, message?: string) => show(NotificationType.INFO, title, message), }; // 初始化全局方法 React.useEffect(() => { Object.assign(toast, contextValue); }, []); const closeNotification = (id: string) => { setNotifications((prev) => prev.filter((notification) => notification.id !== id), ); }; return ( {notifications.length > 0 && ( {notifications.map((notification) => ( {notificationConfigs[notification.type].icon} {notification.title && ( {notification.title} )} {notification.message}
))} )} {children} ); }; // 导出hook export const useNotification = () => { const context = useContext(NotificationContext); if (!context) { throw new Error( "useNotification must be used within a NotificationProvider", ); } return context; };