50 lines
6.1 KiB
TypeScript
50 lines
6.1 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { RotateCw } from 'lucide-react';
|
|
import { api } from './api';
|
|
import './InstallationStatus.css';
|
|
|
|
type Operation = {
|
|
id: string; sequence: number; state: string; stage: string; label: string; updatedAt: string; elapsedMs: number; scope?: string;
|
|
counts?: { completedFiles: number; totalFiles: number; completedBytes: number | null; totalBytes: number | null; currentPath?: string } | null;
|
|
candidate?: { version: string; manifestSha256: string } | null;
|
|
detail?: { error?: { code: string; message: string }; receiptFile?: string; originalError?: string };
|
|
progressFile: string; interrupted?: boolean;
|
|
};
|
|
type Status = { installed: boolean; runningVersion: string; activeVersion?: string; previousVersion?: string | null; installRoot?: string; operations: Operation[] };
|
|
const size = (bytes: number) => { const unit = bytes < 1024 ? 0 : bytes < 1024 ** 2 ? 1 : bytes < 1024 ** 3 ? 2 : 3; return new Intl.NumberFormat('zh-CN', { maximumFractionDigits: 1 }).format(bytes / 1024 ** unit) + ' ' + ['B', 'KB', 'MB', 'GB'][unit]; };
|
|
const scopeLabel = (scope?: string) => scope === 'full-manifest' ? '全部发行文件' : scope === 'directory-inventory' ? '实际目录范围' : scope === 'loaded-inputs' ? '宿主与客户端入口' : '';
|
|
|
|
export function InstallationStatus() {
|
|
const [status, setStatus] = useState<Status | null>(null), [error, setError] = useState(''), [busy, setBusy] = useState(false);
|
|
const [refresh, setRefresh] = useState(0);
|
|
const [selected, setSelected] = useState('');
|
|
useEffect(() => {
|
|
let alive = true, timer: ReturnType<typeof setTimeout>;
|
|
const load = async () => {
|
|
setBusy(true);
|
|
try {
|
|
const value = await api<Status>('/installation/status');
|
|
if (alive) { setStatus(value); setError(''); }
|
|
} catch (value) { if (alive) setError(value instanceof Error ? value.message : '无法读取原安装进度。'); }
|
|
finally { if (alive) { setBusy(false); timer = setTimeout(() => void load(), 2500); } }
|
|
};
|
|
void load(); return () => { alive = false; clearTimeout(timer); };
|
|
}, [refresh]);
|
|
const operation = status?.operations.find(value => value.id === selected) ?? status?.operations[0];
|
|
return <section className="installation-status" aria-label="版本与更新状态">
|
|
<header><div><h3>当前版本</h3><p>{status ? <>正在运行 <strong>v{status.runningVersion}</strong>{status.installed && <> · 已登记 v{status.activeVersion}</>}</> : '正在读取本机安装登记…'}</p></div><button className="button secondary" disabled={busy} onClick={() => setRefresh(value => value + 1)}><RotateCw size={14} />刷新状态</button></header>
|
|
{error && <p role="alert" className="installation-error">{error}{status && ' 以下保留上次读取结果,请刷新后再判断当前状态。'}</p>}
|
|
{status && !status.installed && <p>当前使用源码或便携目录。安装版会在这里显示所属更新阶段和原始失败记录。</p>}
|
|
{status?.installed && <dl className="installation-facts"><dt>程序目录</dt><dd>{status.installRoot}</dd><dt>前一版本</dt><dd>{status.previousVersion ? 'v' + status.previousVersion : '首次安装'}</dd></dl>}
|
|
{status?.installed && !operation && <p className="installation-empty">还没有新版安装进度记录。此前版本的原操作收据保存在程序目录的 .installation 中。</p>}
|
|
{status && status.operations.length > 1 && <label className="installation-history">安装操作<select aria-label="查看安装操作" value={selected} onChange={event => setSelected(event.target.value)}><option value="">最近一次操作</option>{status.operations.map(value => <option key={value.id} value={value.id}>{new Date(value.updatedAt).toLocaleString('zh-CN')} · {value.interrupted ? '结果待核对' : value.label}</option>)}</select></label>}
|
|
{operation && <article className={'installation-operation installation-' + operation.state}>
|
|
<div className="installation-stage" role="status"><span className="installation-state-dot" /><strong>{operation.interrupted ? '原安装进程已结束,结果待核对' : operation.label}</strong><span>{Math.round(operation.elapsedMs / 1000)} 秒</span></div>
|
|
{operation.counts && <div className="installation-counts"><span>{scopeLabel(operation.scope)}</span><strong>{operation.counts.completedFiles.toLocaleString()} / {operation.counts.totalFiles.toLocaleString()} 文件</strong>{operation.counts.completedBytes !== null && operation.counts.totalBytes !== null && <span>{size(operation.counts.completedBytes)} / {size(operation.counts.totalBytes)}</span>}<progress aria-label="当前核对范围" max={Math.max(1, operation.counts.totalFiles)} value={operation.counts.completedFiles} />{operation.counts.currentPath && <code>{operation.counts.currentPath}</code>}</div>}
|
|
{operation.detail?.error && <div className="installation-error" role="alert"><strong>{operation.detail.error.code}</strong><p>{operation.detail.error.message}</p></div>}
|
|
<dl className="installation-facts"><dt>目标版本</dt><dd>{operation.candidate ? 'v' + operation.candidate.version : '准备时尚未读取候选'}</dd><dt>操作编号</dt><dd>{operation.id} · 序号 {operation.sequence}</dd><dt>原始记录</dt><dd>{operation.progressFile}</dd>{operation.detail?.receiptFile && <><dt>结果收据</dt><dd>{operation.detail.receiptFile}</dd></>}</dl>
|
|
<p className="installation-note">{operation.state === 'ready-to-copy' ? '预检已通过,写入进度由安装器显示;新版本尚未激活。' : operation.interrupted ? '请保留原安装日志与收据,核对已登记版本。不要根据窗口关闭推断安装成功,也不要重复执行原任务。' : operation.state === 'failed' ? '原失败记录已保留。请按具体原因处理,核对已登记版本及回退收据后再开始新的安装。' : operation.state === 'staged' ? '文件已核对并暂存,当前活动版本保留。' : operation.state === 'running' ? '当前阶段核对实际文件。发布版本和回退期间需要保持安装器打开;各阶段单独计数。' : '原会话与工件保留。这里的安装结果不代表模型工程任务已经通过。'}</p>
|
|
</article>}
|
|
</section>;
|
|
}
|