feat(library-web): 恢复入口按真实条件启用并支持签发恢复授权

原来"恢复至该版本"写死 disabled,标题还写"生产恢复未接线"——而 POST /library/restores
与授权接口早已可用,等于把整库还原的唯一入口藏起来。

现在页面按 GET /library/restore-availability 启用:缺授权时管理员可签发 30 分钟
有效期的 restore 授权(固定期限,没有"永久授权"),条件满足才可点恢复;工程师只看到
条件与"仅管理员可发起"。封存不可发起时也不再写死"通道未接线",而是显示接口原因、
执行器身份与观测时间。
This commit is contained in:
姚章浩
2026-10-29 13:01:46 +08:00
parent 47eb23cf68
commit c40696145b
4 changed files with 433 additions and 14 deletions
+163 -7
View File
@@ -1,10 +1,13 @@
import { useCallback, useEffect, useState } from 'react'
import {
createLibraryRestoreAuthorization,
enqueueLibraryCapture,
enqueueLibraryRecovery,
enqueueLibraryRestore,
fetchLibraryCaptures,
fetchLibraryMaintenance,
fetchLibraryOverview,
fetchLibraryRestoreAvailability,
fetchLibrarySnapshots,
formatBytes,
scopeLabel,
@@ -12,6 +15,7 @@ import {
type LibraryJobRecord,
type LibraryMaintenanceView,
type LibraryOverview,
type LibraryRestoreAvailability,
type LibrarySnapshot,
type LibrarySnapshotList,
type LibraryStageRow,
@@ -85,6 +89,14 @@ interface ViewProps {
pendingCapture: boolean
canCapture: boolean
canWrite: boolean
// 按版本恢复是管理员动作:工程师看得到条件,但拿不到恢复入口。
canRestore: boolean
restore: LibraryRestoreAvailability | null
restoreNotice: string | null
pendingRestore: boolean
pendingGrant: boolean
onGrantRestore: () => void
onRestore: () => void
overview: LibraryOverview | null
maintenance: LibraryMaintenanceView | null
now: number
@@ -118,6 +130,20 @@ function scopeView(
const scopeName = (scope: Scope) => (scope === 'internal' ? '对内知识库' : '对外知识库')
// 不可发起时必须说清是哪一条不满足:读的是接口给出的真实原因(执行器不可达、
// 指向别的实例、降级、观测过期),不是一句写死的"通道未接线"。
function captureHint(view: ScopeView, canWrite: boolean): string {
if (!canWrite) return '封存需要知识工程师及以上权限'
const executor = view.overview?.executor
const reason = view.overview?.capture_reason || executor?.reason
const observed = executor?.observed_at ? `(观测于 ${stamp(executor.observed_at)})` : ''
const identity = executor?.project
? `执行器 ${executor.project}${executor.source_app ? ` / ${executor.source_app}` : ''} · `
: ''
if (reason) return `${identity}封存通道不可用:${reason}${observed}`
return '封存通道状态未知:还没有读到执行器观测'
}
function statusOf(view: ScopeView) {
const job = view.job
if (job && (job.state === 'running' || job.state === 'pending')) {
@@ -629,7 +655,7 @@ function ReadyBody({
{pendingCapture ? '投递中…' : '封存测试版本'}
</button>
) : (
<span className="rel-hint">{canWrite ? '封存通道未接线' : '封存需要知识工程师及以上权限'}</span>
<span className="rel-hint">{captureHint(view, canWrite)}</span>
)}
</div>
</>
@@ -822,8 +848,16 @@ interface DrawerProps {
onExpand: (id: string | null) => void
onClose: () => void
canCapture: boolean
canRestore: boolean
pendingCapture: boolean
onCapture: () => void
// 恢复条件来自容器(真实接口判定):双闸 / 该快照的授权 / 目标是否在维护。
restore: LibraryRestoreAvailability | null
restoreNotice: string | null
pendingRestore: boolean
pendingGrant: boolean
onGrantRestore: () => void
onRestore: () => void
}
function Drawer({
@@ -835,8 +869,15 @@ function Drawer({
onExpand,
onClose,
canCapture,
canRestore,
pendingCapture,
onCapture,
restore,
restoreNotice,
pendingRestore,
pendingGrant,
onGrantRestore,
onRestore,
}: DrawerProps) {
if (!open) return null
const listing = view.history
@@ -919,8 +960,12 @@ function Drawer({
>
{isSelected ? '收起快照' : '查看快照'}
</button>
<button type="button" className="rel-link" disabled title="生产恢复未接线,也没有恢复演练记录">
不可直接恢复
<button
type="button"
className="rel-link"
onClick={() => onExpand(isSelected ? null : snapshot.snapshot_id)}
>
{isSelected ? '恢复条件见下方' : '查看恢复条件'}
</button>
</td>
</tr>
@@ -991,13 +1036,48 @@ function Drawer({
以上是同一恢复点的配套内容,不支持只选择 Wiki 或某张数据表恢复。恢复速度需实测;“有备份文件”不等于可恢复。
</p>
<div className="rel-drawer-actions">
<button type="button" disabled title="生产恢复验证记录未接入">
<button type="button" disabled title="逐项校验证据在同步/恢复执行时产生,作业详情里可见">
查看校验记录
</button>
<button type="button" className="rel-danger" disabled title="生产恢复未接线,也没有经过恢复演练">
恢复至该版本
</button>
{canRestore && restore && restore.authorization.state !== 'active' ? (
<button type="button" className="rel-secondary" onClick={onGrantRestore} disabled={pendingGrant}>
{pendingGrant ? '签发中…' : '签发恢复授权(30 分钟)'}
</button>
) : null}
{canRestore ? (
<button
type="button"
className="rel-danger"
onClick={onRestore}
disabled={pendingRestore || restore?.allowed !== true}
title={
restore?.allowed
? '把整套生产知识状态恢复到这个快照'
: (restore?.reasons ?? []).join(';') || '恢复条件尚未读取到'
}
>
{pendingRestore ? '投递中…' : '恢复至该版本'}
</button>
) : (
<span className="rel-hint">按版本恢复仅管理员可发起</span>
)}
</div>
{restore && !restore.allowed ? (
<ul className="rel-gate" aria-label="恢复条件">
{restore.reasons.map((reason) => (
<li key={reason}>{reason}</li>
))}
</ul>
) : null}
{restore?.allowed ? (
<p className="rel-hint">
条件已满足:生产写入闸口打开、这份快照有恢复授权
{restore.authorization.expires_at ? `(有效至 ${stamp(restore.authorization.expires_at)})` : ''}
、目标实例未在维护。
</p>
) : null}
{restore === null ? <p className="rel-hint">正在读取恢复条件…</p> : null}
{restoreNotice ? <p className="rel-notice">{restoreNotice}</p> : null}
</section>
) : null}
@@ -1047,6 +1127,13 @@ export function ReleaseTargetsView(props: ViewProps) {
pendingCapture,
canCapture,
canWrite,
canRestore,
restore,
restoreNotice,
pendingRestore,
pendingGrant,
onGrantRestore,
onRestore,
now,
maintenance,
onRefresh,
@@ -1241,8 +1328,15 @@ export function ReleaseTargetsView(props: ViewProps) {
onExpand={onExpandSnapshot}
onClose={() => onDrawer(false)}
canCapture={canCapture}
canRestore={canRestore}
pendingCapture={pendingCapture}
onCapture={onCapture}
restore={restore}
restoreNotice={restoreNotice}
pendingRestore={pendingRestore}
pendingGrant={pendingGrant}
onGrantRestore={onGrantRestore}
onRestore={onRestore}
/>
</section>
)
@@ -1313,8 +1407,63 @@ export function ReleaseTargets() {
const role = auth?.identity?.role
const canWrite = role === 'engineer' || role === 'admin'
// 恢复写的是整套生产知识状态,接口只认管理员;页面必须与它一致。
const canRestore = role === 'admin'
const canCapture = canWrite && page?.capture_available === true
// 恢复条件随"展开的那份快照"变化:闸口、该快照的授权、实例是否在维护都由接口判定。
const [restore, setRestore] = useState<LibraryRestoreAvailability | null>(null)
const [restoreNotice, setRestoreNotice] = useState<string | null>(null)
const [pendingRestore, setPendingRestore] = useState(false)
const [pendingGrant, setPendingGrant] = useState(false)
const [restoreRound, setRestoreRound] = useState(0)
const restoreSnapshot = expandedSnapshot ?? ''
useEffect(() => {
if (!drawerOpen || !restoreSnapshot) {
setRestore(null)
return
}
const controller = new AbortController()
void fetchLibraryRestoreAvailability(scope, restoreSnapshot, controller.signal)
.then((value) => setRestore(value))
.catch(() => setRestore(null))
return () => controller.abort()
}, [drawerOpen, restoreSnapshot, scope, restoreRound])
const grantRestore = async () => {
if (!restoreSnapshot) return
setPendingGrant(true)
setRestoreNotice(null)
try {
const grant = await createLibraryRestoreAuthorization(scope, restoreSnapshot)
setRestoreNotice(
`已签发恢复授权${grant.authorized_by ? `(${grant.authorized_by})` : ''},有效至 ${stamp(grant.expires_at)}`
)
setRestoreRound((round) => round + 1)
await load()
} catch (grantError) {
setRestoreNotice(grantError instanceof Error ? grantError.message : '签发恢复授权失败')
} finally {
setPendingGrant(false)
}
}
const startRestore = async () => {
if (!restoreSnapshot) return
setPendingRestore(true)
setRestoreNotice(null)
try {
const accepted = await enqueueLibraryRestore(scope, restoreSnapshot)
setRestoreNotice(`已投递恢复:${accepted.job_id}(整套生产知识状态回到该快照)`)
setRestoreRound((round) => round + 1)
await load()
} catch (restoreError) {
setRestoreNotice(restoreError instanceof Error ? restoreError.message : '发起恢复失败')
} finally {
setPendingRestore(false)
}
}
const startCapture = async () => {
setPendingCapture(true)
setNotice(null)
@@ -1364,6 +1513,13 @@ export function ReleaseTargets() {
pendingCapture={pendingCapture}
canCapture={canCapture}
canWrite={canWrite}
canRestore={canRestore}
restore={restore}
restoreNotice={restoreNotice}
pendingRestore={pendingRestore}
pendingGrant={pendingGrant}
onGrantRestore={() => void grantRestore()}
onRestore={() => void startRestore()}
maintenance={maintenances[scope]}
now={now}
onRefresh={() => void load()}