import React, { useState, useEffect } from "react"; import { Button } from "antd-mobile"; import { updateChecker } from "@/utils/updateChecker"; import { ReloadOutlined } from "@ant-design/icons"; interface UpdateNotificationProps { position?: "top" | "bottom"; autoReload?: boolean; showToast?: boolean; forceShow?: boolean; onClose?: () => void; } const UpdateNotification: React.FC = ({ position = "top", autoReload = false, showToast = true, forceShow = false, onClose, }) => { const [hasUpdate, setHasUpdate] = useState(false); const [isVisible, setIsVisible] = useState(false); useEffect(() => { // 注册更新检测回调 const handleUpdate = (info: { hasUpdate: boolean }) => { if (info.hasUpdate) { setHasUpdate(true); setIsVisible(true); if (autoReload) { // 自动刷新 setTimeout(() => { updateChecker.forceReload(); }, 3000); } } }; updateChecker.onUpdate(handleUpdate); // 启动更新检测 updateChecker.start(); return () => { updateChecker.offUpdate(handleUpdate); updateChecker.stop(); }; }, [autoReload, showToast]); const handleReload = () => { updateChecker.forceReload(); }; const handleLater = () => { setIsVisible(false); onClose?.(); // 10分钟后再次检查 setTimeout( () => { updateChecker.start(); }, 10 * 60 * 1000, ); }; if ((!isVisible || !hasUpdate) && !forceShow) { return null; } return (
{/* 左侧内容 */}
{/* 更新图标 */}
{/* 文本信息 */}
发现新版本
建议立即更新获得更好体验
{/* 右侧按钮组 */}
{/* 动画样式 */}
); }; export default UpdateNotification;