// ============================================================ // UpdateSetting — 版本更新区块 // ============================================================ import { useState } from 'react'; import { Card, Button, Typography, Progress, Tag, Modal, Divider } from 'antd'; import { SyncOutlined, CheckCircleOutlined, ExclamationCircleOutlined, RocketOutlined, DownloadOutlined, FileTextOutlined, } from '@ant-design/icons'; import { useUpdater } from '@/hooks/use-updater'; import { isDev } from '@/utils/platform'; const { Text, Title } = Typography; /** 简单渲染 changelog(Markdown → JSX),支持 ## / - / --- */ function renderChangelog(markdown: string) { const lines = markdown.split('\n'); const elements: React.ReactNode[] = []; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const key = `cl-${i}`; if (line.startsWith('## ')) { elements.push( {line.slice(3)} , ); continue; } if (/^-{3,}$/.test(line.trim())) { elements.push(); continue; } if (line.trim().startsWith('- ')) { elements.push( • {line.trim().slice(2)} , ); continue; } if (line.trim() === '') { elements.push(
); continue; } elements.push( {line} , ); } return elements.length > 0 ? elements : 暂无更新详情; } export function UpdateSetting() { const { status, info, progress, error, checkForUpdates, downloadUpdate, installUpdate } = useUpdater(); const [detailOpen, setDetailOpen] = useState(false); return (
{/* ---- idle ---- */} {status === 'idle' && ( 点击下方按钮检查是否有新版本 )} {/* ---- checking ---- */} {status === 'checking' && (
正在检查更新...
)} {/* ---- no-update ---- */} {status === 'no-update' && (
已是最新版本
)} {/* ---- error ---- */} {status === 'error' && (
{error?.message || '检查更新失败'}
)} {/* ---- 发现新版本(等待用户决定)---- */} {status === 'available' && ( <>
发现新版本:{info?.version} {info?.forceUpdate && 强制更新}
{info?.releaseNotes && (
{info.releaseNotes .replace(/^## .+?\n\n?/, '') .replace(/\n---\n?[\s\S]*$/, '') .trim()}
)} )} {/* ---- 下载中 ---- */} {status === 'downloading' && ( <>
正在下载 v{info?.version}...
)} {/* ---- 下载完成 ---- */} {status === 'downloaded' && ( <>
更新已下载完毕,点击安装并重启
)} {/* ---- 操作按钮 ---- */}
{isDev() && ( 生产环境启动 5 秒后自动检查 )}
{/* ---- 更新详情弹窗 ---- */} setDetailOpen(false)} footer={} width={520} >
{info?.releaseNotes ? ( renderChangelog(info.releaseNotes) ) : ( 暂无更新详情 )}
); }