完善:同月多文件导入与当前用户离线迁移更新
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { Alert, Button, Spin, Tag } from 'antd';
|
||||
import { ReloadOutlined } from '@ant-design/icons';
|
||||
import { api, type SystemUpdateStatus } from './api';
|
||||
import { configurationDateTime } from './configurationName';
|
||||
import { usePageCache } from './pageCache';
|
||||
import { useVisiblePolling } from './useVisiblePolling';
|
||||
|
||||
const runningStates = new Set(['queued', 'downloading', 'backingUp', 'installing', 'restarting', 'rollingBack']);
|
||||
|
||||
function errorStatus(error: unknown): number | undefined {
|
||||
return error && typeof error === 'object' && 'status' in error && typeof error.status === 'number' ? error.status : undefined;
|
||||
}
|
||||
|
||||
function reconnectable(error: unknown): boolean {
|
||||
const status = errorStatus(error);
|
||||
return status === 0 || status === 408 || (status !== undefined && status >= 500 && status <= 599);
|
||||
}
|
||||
|
||||
export function SystemUpdatePanel({ onUnauthorized }: { onUnauthorized: (error: unknown) => boolean }) {
|
||||
const { active } = usePageCache();
|
||||
const [status, setStatus] = useState<SystemUpdateStatus>();
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [reconnecting, setReconnecting] = useState(false);
|
||||
const [permissionDenied, setPermissionDenied] = useState(false);
|
||||
const inFlight = useRef(false);
|
||||
const restarting = useRef(false);
|
||||
const running = !!status && runningStates.has(status.state);
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const next = await api.systemUpdateStatus();
|
||||
setStatus(next);
|
||||
setError('');
|
||||
setReconnecting(false);
|
||||
setPermissionDenied(false);
|
||||
restarting.current = runningStates.has(next.state);
|
||||
} catch (failure) {
|
||||
const denied = [401, 403].includes(errorStatus(failure) ?? -1);
|
||||
const handled = onUnauthorized(failure);
|
||||
if (handled || denied) {
|
||||
restarting.current = false;
|
||||
setReconnecting(false);
|
||||
setPermissionDenied(true);
|
||||
if (!handled) setError(failure instanceof Error ? failure.message : '无权查看更新状态');
|
||||
} else if (restarting.current && reconnectable(failure)) {
|
||||
setReconnecting(true);
|
||||
} else {
|
||||
restarting.current = false;
|
||||
setReconnecting(false);
|
||||
setError(failure instanceof Error ? failure.message : '更新状态读取失败');
|
||||
}
|
||||
}
|
||||
}, [onUnauthorized]);
|
||||
useEffect(() => { if (active) void load(); }, [active, load]);
|
||||
useVisiblePolling(load, running || reconnecting ? 4_000 : 30_000, active && !permissionDenied);
|
||||
|
||||
const run = async (apply: boolean) => {
|
||||
if (inFlight.current) return;
|
||||
inFlight.current = true;
|
||||
setBusy(true);
|
||||
setError('');
|
||||
try {
|
||||
if (apply) restarting.current = true;
|
||||
const next = await (apply ? api.applySystemUpdate() : api.checkSystemUpdate());
|
||||
setStatus(next);
|
||||
restarting.current = runningStates.has(next.state);
|
||||
} catch (failure) {
|
||||
// A successful updater intentionally takes the server offline. A lost
|
||||
// response is resolved by polling the persisted task after restart.
|
||||
if (apply && reconnectable(failure)) {
|
||||
setReconnecting(true);
|
||||
} else {
|
||||
restarting.current = false;
|
||||
const denied = [401, 403].includes(errorStatus(failure) ?? -1);
|
||||
setPermissionDenied(denied);
|
||||
if (!onUnauthorized(failure)) setError(failure instanceof Error ? failure.message : '更新操作失败');
|
||||
if (!denied) void load();
|
||||
}
|
||||
} finally {
|
||||
setBusy(false);
|
||||
inFlight.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
return <section className="panel section-panel">
|
||||
<div className="completion-panel-heading">
|
||||
<strong>系统更新</strong>
|
||||
<div className="completion-actions">
|
||||
<Button icon={<ReloadOutlined />} loading={busy && !restarting.current} disabled={!status?.enabled || running || reconnecting || busy} onClick={() => void run(false)}>检查更新</Button>
|
||||
{status?.available && !running && !reconnecting && <Button type="primary" loading={busy && restarting.current} disabled={busy} onClick={() => void run(true)}>更新并重启</Button>}
|
||||
</div>
|
||||
</div>
|
||||
{!status && !error && <Spin />}
|
||||
{error && <Alert showIcon type="error" message={error} action={<Button size="small" onClick={() => void load()}>重试</Button>} />}
|
||||
{reconnecting ? <Alert showIcon type="info" icon={<Spin size="small" />} message="正在重新启动,完成后将自动连接。" />
|
||||
: status && <>
|
||||
{status.enabled && <div className="governance-monitor-grid">
|
||||
<div><small>当前版本</small><strong>{status.currentVersion || '—'}</strong></div>
|
||||
{status.version && <div><small>{status.available ? '可用版本' : '最近检查版本'}</small><strong>{status.version}</strong></div>}
|
||||
{status.checkedAt && <div><small>检查时间</small><strong>{configurationDateTime(status.checkedAt)}</strong></div>}
|
||||
</div>}
|
||||
<Alert showIcon type={status.state === 'failed' ? 'error' : status.state === 'completed' ? 'success' : 'info'}
|
||||
message={<>{running && <Tag color="processing">更新中</Tag>}{status.message || '点击检查更新获取最新版本。'}</>} />
|
||||
</>}
|
||||
</section>;
|
||||
}
|
||||
Reference in New Issue
Block a user