Files
knowledge-governance/web/src/DocumentWorkbench.tsx
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

589 lines
24 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useContext, useEffect, useMemo, useState } from 'react'
import type { SourceAsset } from './assets'
import type { SourceConnectionPage } from './sourceConnections'
import { retryPublication } from './testPublications'
import { clearScopeRule, fetchScopeRules, setScopeRule } from './scopeRules'
import type { ClearScopeRuleDecision, ScopeRule, ScopeRuleDecision } from './scopeRules'
import { ScopeRuleEditor } from './ScopeRuleEditor'
import { WorkspaceContextTabsSetter } from './workspaceContext'
import {
connectionKey,
directoryGroups,
documentPipeline,
documentProblem,
documentStage,
fetchDocumentCatalog,
filterDocuments,
operationalDocuments,
sourceProgress,
stageLabels,
visibilityOrigin,
} from './documentWorkbench'
import type { DocumentStage } from './documentWorkbench'
import './documentWorkbench.css'
type Mode = 'directory' | 'monitor' | 'attention'
const sourceNames: Record<string, string> = {
pp: 'PP公开文档',
alist: 'AList文件源',
dingtalk: '钉钉知识库',
manual: '手工验收文档',
}
const activeStages: DocumentStage[] = ['downloading', 'uploading', 'parsing', 'indexing']
interface ViewProps {
items: SourceAsset[]
connections: SourceConnectionPage['items']
mode: Mode
loading: boolean
error: string | null
onRefresh: () => void
onRetry: (id: string) => void
retryingId: string | null
}
const time = (value: string) => new Date(value).toLocaleString('zh-CN', { hour12: false })
const bytes = (value: number) =>
value >= 1048576 ? `${(value / 1048576).toFixed(1)} MB` : `${(value / 1024).toFixed(1)} KB`
export function DocumentWorkbenchView({
items,
connections,
mode,
loading,
error,
onRefresh,
onRetry,
retryingId,
}: ViewProps) {
const [connection, setConnection] = useState('')
const [directory, setDirectory] = useState<string | null>(null)
const [query, setQuery] = useState('')
const [visibility, setVisibility] = useState('')
const [stage, setStage] = useState('')
const [page, setPage] = useState(0)
const [selectedID, setSelectedID] = useState<string | null>(null)
const [showTestDocuments, setShowTestDocuments] = useState(false)
const [scopeRules, setScopeRules] = useState<ScopeRule[]>([])
const [scopeLoading, setScopeLoading] = useState(false)
const [scopeError, setScopeError] = useState<string | null>(null)
const setWorkspaceContextTabs = useContext(WorkspaceContextTabsSetter)
const visibleItems = useMemo(() => operationalDocuments(items, showTestDocuments), [items, showTestDocuments])
const groups = useMemo(() => directoryGroups(visibleItems), [visibleItems])
const sources = useMemo(() => {
const map = new Map(
connections
.filter((c) => showTestDocuments || c.source_type !== 'manual')
.map((c) => [c.id, { id: c.id, type: c.source_type, name: c.name }])
)
for (const item of visibleItems)
if (!map.has(connectionKey(item)))
map.set(connectionKey(item), { id: connectionKey(item), type: item.source_type, name: item.connection_name })
return [...map.values()]
}, [visibleItems, connections, showTestDocuments])
const scoped = filterDocuments(visibleItems, { connection, directory, query: '', visibility: '', stage: '' })
const filtered = filterDocuments(visibleItems, { connection, directory, query, visibility, stage }).filter(
(item) => mode !== 'attention' || ['blocked', 'failed'].includes(documentStage(item))
)
const shownPage = Math.min(page, Math.max(0, Math.ceil(filtered.length / 40) - 1))
const selected = filtered.find((item) => item.id === selectedID)
const reset = () => {
setPage(0)
setSelectedID(null)
}
const selectFolder = (id: string, path: string | null) => {
setConnection(id)
setDirectory(path)
reset()
}
const title = mode === 'monitor' ? '解析监控' : mode === 'attention' ? '待处理文件' : '来源与目录'
const unavailable = items.length === 0 && Boolean(loading || error)
const progress = sourceProgress(visibleItems)
const manualCount = items.filter((item) => item.source_type === 'manual').length
const directDirectoryRule = scopeRules.find(
(rule) =>
rule.target_type === 'directory' && rule.connection_id === connection && rule.directory_path === (directory ?? '')
)
const selectedItemRule = scopeRules.find((rule) => rule.target_type === 'item' && rule.source_item_id === selectedID)
const selectedSourceLabel = sourceNames[sources.find((item) => item.id === connection)?.type || ''] || '当前来源'
useEffect(() => {
if (!connection) {
setScopeRules([])
setScopeError(null)
return
}
const controller = new AbortController()
setScopeLoading(true)
void fetchScopeRules(connection, controller.signal)
.then(setScopeRules)
.catch((cause) => {
if (!controller.signal.aborted) setScopeError(cause instanceof Error ? cause.message : '加载范围规则失败')
})
.finally(() => {
if (!controller.signal.aborted) setScopeLoading(false)
})
return () => controller.abort()
}, [connection])
useEffect(() => {
if (!connection) {
setWorkspaceContextTabs([])
return
}
const tabs = [
{
key: 'source',
label: selectedSourceLabel,
onActivate: () => {
setDirectory(null)
setSelectedID(null)
setPage(0)
},
},
]
if (directory !== null)
tabs.push({ key: 'directory', label: directory || '分类待补齐', onActivate: () => setSelectedID(null) })
if (selected)
tabs.push({ key: 'file', label: selected.title || '当前文件', onActivate: () => setSelectedID(selected.id) })
setWorkspaceContextTabs(tabs)
return () => setWorkspaceContextTabs([])
}, [connection, directory, selected, selectedSourceLabel, setWorkspaceContextTabs])
const mutateScope = async (decision: ScopeRuleDecision | ClearScopeRuleDecision, clear: boolean) => {
if (scopeLoading) return
setScopeLoading(true)
setScopeError(null)
try {
if (clear) await clearScopeRule(decision as ClearScopeRuleDecision)
else await setScopeRule(decision as ScopeRuleDecision)
setScopeRules(await fetchScopeRules(connection))
onRefresh()
} catch (cause) {
setScopeError(cause instanceof Error ? cause.message : '保存范围规则失败')
} finally {
setScopeLoading(false)
}
}
return (
<main className="page-shell document-workbench">
<header className="hero compact">
<div>
<p className="eyebrow">文档工作台</p>
<h1>{title}</h1>
<p className="subtitle">
{mode === 'monitor'
? '看每份文件处理到哪一步。解析完成不代表已上线。'
: mode === 'attention'
? '只列出超限、失败或来源不可访问的文件。内容审核请进入发布管理。'
: '先选来源和目录,再看文件、对内/对外和处理状态。'}
</p>
</div>
<button onClick={onRefresh} disabled={loading}>
{loading ? '刷新中…' : '刷新状态'}
</button>
</header>
{error ? (
<div role="alert" className="error-banner">
{error}(旧数据仅供参考)
</div>
) : null}
{mode === 'monitor' && !unavailable ? (
<section className="source-progress" aria-label="来源处理概览">
<div className="section-title">
<strong>来源处理概览</strong>
<span>直接看各来源完成、处理中和异常数量</span>
</div>
<div className="source-progress-grid">
{progress.map((item) => {
const source = sources.find((value) => value.id === item.connection)
return (
<button
key={item.connection}
className={connection === item.connection ? 'source-progress-card active' : 'source-progress-card'}
onClick={() => selectFolder(item.connection, null)}
>
<strong>{sourceNames[source?.type || ''] || source?.name || '当前来源'}</strong>
<span>
已就绪 {item.completed + item.reused} / {item.total}
</span>
<small>
独立解析 {item.completed} · 内容复用 {item.reused}
</small>
<small>
处理中 {item.active} · 需处理 {item.attention} · 等待 {item.waiting}
</small>
</button>
)
})}
</div>
</section>
) : null}
<div className="document-layout">
<aside className="directory-panel" aria-label="来源目录">
<button className={!connection ? 'folder active' : 'folder'} onClick={() => selectFolder('', null)}>
全部来源 <b>{unavailable ? '—' : visibleItems.length}</b>
</button>
{sources.map((source) => (
<section key={source.id}>
<button
className={connection === source.id && directory === null ? 'folder active' : 'folder'}
onClick={() => selectFolder(source.id, null)}
title={source.name}
>
<span>{sourceNames[source.type] || source.name}</span>
<b>{visibleItems.filter((x) => connectionKey(x) === source.id).length}</b>
</button>
<small className="directory-note">
{source.type === 'pp'
? 'PP分类 · 不代表完整原始目录'
: source.type === 'manual'
? '隔离验收用文档'
: '已收录目录'}
</small>
{groups
.filter((g) => g.connection === source.id)
.map((group) => (
<button
key={group.key}
className={
connection === source.id && directory === group.path ? 'folder child active' : 'folder child'
}
onClick={() => selectFolder(source.id, group.path)}
>
<span>▸ {group.path || (source.type === 'pp' ? '分类待补齐' : '目录待补齐')}</span>
<b>{group.count}</b>
</button>
))}
</section>
))}
{(['dingtalk', 'alist'] as const)
.filter((type) => !sources.some((s) => s.type === type))
.map((type) => (
<div className="source-unconnected" key={type}>
<strong>{sourceNames[type]}</strong>
<small>未接通目录</small>
</div>
))}
{manualCount ? (
<label className="test-document-toggle">
<input
type="checkbox"
checked={showTestDocuments}
onChange={(event) => {
setShowTestDocuments(event.target.checked)
selectFolder('', null)
}}
/>
显示 {manualCount} 份验收文档
</label>
) : null}
</aside>
<section className="document-content">
{!unavailable ? (
<div className="document-counts" aria-label="当前目录统计">
<span>
文件 <b>{scoped.length}</b>
</span>
{mode === 'monitor' ? (
<>
<span>
独立解析 <b>{scoped.filter((x) => documentStage(x) === 'completed').length}</b>
</span>
<span>
内容复用 <b>{scoped.filter((x) => documentStage(x) === 'reused').length}</b>
</span>
<span>
处理中 <b>{scoped.filter((x) => activeStages.includes(documentStage(x))).length}</b>
</span>
<span>
等待 <b>{scoped.filter((x) => documentStage(x) === 'waiting').length}</b>
</span>
<span>
需处理(含超限){' '}
<b>{scoped.filter((x) => ['blocked', 'failed'].includes(documentStage(x))).length}</b>
</span>
</>
) : (
<>
<span>
对内 <b>{scoped.filter((x) => x.visibility === 'internal').length}</b>
</span>
<span>
对外 <b>{scoped.filter((x) => x.visibility === 'external').length}</b>
</span>
<span>
处理中 <b>{scoped.filter((x) => activeStages.includes(documentStage(x))).length}</b>
</span>
<span>
已解析 <b>{scoped.filter((x) => documentStage(x) === 'completed').length}</b>
</span>
<span>
需处理 <b>{scoped.filter((x) => ['blocked', 'failed'].includes(documentStage(x))).length}</b>
</span>
</>
)}
</div>
) : (
<p className="directory-note">文件数量尚未核实</p>
)}
{mode === 'directory' && connection && directory !== '' ? (
<ScopeRuleEditor
target={{
target_type: 'directory',
connection_id: connection,
directory_path: directory ?? '',
source_item_id: '',
}}
currentRule={directDirectoryRule}
saving={scopeLoading}
error={scopeError}
onSave={(decision) => void mutateScope(decision, false)}
onClear={(decision) => void mutateScope(decision, true)}
/>
) : mode === 'directory' && connection && directory === '' ? (
<p className="scope-unavailable">
“分类待补齐”不是可靠目录,不能单独设置范围。请先补齐来源目录,或返回来源级设置。
</p>
) : null}
<div className="document-filters">
<label>
找文件
<input
type="search"
placeholder="输入文件名"
value={query}
onChange={(e) => {
setQuery(e.target.value)
reset()
}}
/>
</label>
<label>
对内/对外
<select
value={visibility}
onChange={(e) => {
setVisibility(e.target.value)
reset()
}}
>
<option value="">全部范围</option>
<option value="internal">对内</option>
<option value="external">对外</option>
</select>
</label>
<label>
处理状态
<select
value={stage}
onChange={(e) => {
setStage(e.target.value)
reset()
}}
>
<option value="">全部状态</option>
{Object.entries(stageLabels).map(([value, label]) => (
<option value={value} key={value}>
{label}
</option>
))}
</select>
</label>
</div>
<div className="document-location">
{!connection ? '全部来源' : sourceNames[sources.find((s) => s.id === connection)?.type || ''] || '当前来源'}{' '}
/ {directory === null ? '全部文件' : directory || '目录待补齐'}{' '}
{!unavailable ? <span>筛选结果 {filtered.length} 份</span> : null}
</div>
{loading && !items.length ? <div className="empty-state">正在核对完整文档清单…</div> : null}
{!loading && !error && !filtered.length ? <div className="empty-state">没有符合条件的文件。</div> : null}
{filtered.length ? (
<div className="table-wrap">
<table className="document-table">
<thead>
<tr>
<th>文件名</th>
<th>对内/对外</th>
<th>处理状态</th>
<th>大小</th>
<th>状态更新时间</th>
</tr>
</thead>
<tbody>
{filtered.slice(shownPage * 40, (shownPage + 1) * 40).map((item) => (
<tr key={item.id} className={item.id === selectedID ? 'selected-row' : ''}>
<td>
<button className="file-link" onClick={() => setSelectedID(item.id)}>
{item.title || '文件名待补齐'}
</button>
<small>
{sourceNames[item.source_type]} / {item.directory_path || '目录待补齐'}
</small>
</td>
<td>
<span className={`scope-badge ${item.visibility}`}>
{item.visibility === 'external' ? '对外' : '对内'}
</span>
<small>{visibilityOrigin(item)}</small>
</td>
<td>
<span className={`document-stage ${documentStage(item)}`}>
{stageLabels[documentStage(item)]}
</span>
</td>
<td>{bytes(item.size_bytes)}</td>
<td>
<time>{time(item.updated_at)}</time>
</td>
</tr>
))}
</tbody>
</table>
</div>
) : null}
<div className="pagination">
<span>
第 {shownPage + 1} / {Math.max(1, Math.ceil(filtered.length / 40))} 页
</span>
<div>
<button disabled={shownPage === 0} onClick={() => setPage(shownPage - 1)}>
上一页
</button>
<button disabled={(shownPage + 1) * 40 >= filtered.length} onClick={() => setPage(shownPage + 1)}>
下一页
</button>
</div>
</div>
{mode === 'monitor' ? (
<p className="directory-note">
每15秒自动刷新。当前接口未提供页数或逐页百分比,因此只显示真实阶段。时间为状态更新时间,不是源文件修改时间。
</p>
) : null}
</section>
</div>
{selected ? (
<section className="document-inspector panel" aria-label="文件详情">
<div className="panel-heading">
<h2>{selected.title || '文件名待补齐'}</h2>
<button onClick={() => setSelectedID(null)}>关闭详情</button>
</div>
<div className="inspector-body">
<dl>
<dt>来源位置</dt>
<dd>
{sourceNames[selected.source_type]} / {selected.directory_path || '目录待补齐'}
</dd>
<dt>对内/对外</dt>
<dd>
{selected.visibility === 'external' ? '对外' : '对内'} · {visibilityOrigin(selected)}
</dd>
<dt>当前进度</dt>
<dd>{stageLabels[documentStage(selected)]}</dd>
</dl>
{documentProblem(selected) ? <p role="status">{documentProblem(selected)}</p> : null}
<div className="file-facts">
<span>
文件大小 <b>{bytes(selected.size_bytes)}</b>
</span>
<span>
状态更新时间 <b>{time(selected.updated_at)}</b>
</span>
<span>
来源变化 <b>{selected.source_change === 'unchanged' ? '无变化' : '需要核对'}</b>
</span>
<span>
检索状态 <b>{selected.retrieval_state === 'enabled' ? '测试环境已启用' : '未证明已启用'}</b>
</span>
</div>
<div className="document-pipeline" aria-label="文件处理链路">
{documentPipeline(selected).map((step) => (
<div className={`pipeline-step ${step.state}`} key={step.label}>
<span aria-hidden="true" />
<div>
<strong>{step.label}</strong>
<small>{step.detail}</small>
</div>
</div>
))}
</div>
<p>原文与版本差异尚未接通,请在源系统核对内容;不要仅凭“解析完成”批准发布。</p>
{mode === 'directory' ? (
<ScopeRuleEditor
target={{ target_type: 'item', connection_id: '', directory_path: '', source_item_id: selected.id }}
currentRule={selectedItemRule}
saving={scopeLoading}
error={scopeError}
onSave={(decision) => void mutateScope(decision, false)}
onClear={(decision) => void mutateScope(decision, true)}
/>
) : null}
{selected.can_retry && selected.publication_id ? (
<button disabled={!!retryingId} onClick={() => onRetry(selected.publication_id!)}>
{retryingId === selected.publication_id ? '提交中…' : '重试此文件'}
</button>
) : null}
{selected.last_error ? (
<details>
<summary>错误详情</summary>
<code>{selected.last_error}</code>
</details>
) : null}
</div>
</section>
) : null}
</main>
)
}
export function DocumentWorkbench({ mode, connections }: { mode: Mode; connections: SourceConnectionPage['items'] }) {
const [items, setItems] = useState<SourceAsset[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [revision, setRevision] = useState(0)
const [retryingId, setRetryingId] = useState<string | null>(null)
useEffect(() => {
const controller = new AbortController()
setLoading(true)
void fetchDocumentCatalog(controller.signal)
.then((data) => {
if (!controller.signal.aborted) {
setItems(data)
setError(null)
}
})
.catch((cause) => {
if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载文件失败')
})
.finally(() => {
if (!controller.signal.aborted) setLoading(false)
})
return () => controller.abort()
}, [revision])
useEffect(() => {
if (mode !== 'monitor' || loading) return
const timer = window.setTimeout(() => setRevision((r) => r + 1), 15000)
return () => window.clearTimeout(timer)
}, [mode, loading, revision])
const retry = async (id: string) => {
if (retryingId) return
if (!window.confirm('仅重试这份失败文件,不影响已成功文件或生产。继续吗?')) return
setRetryingId(id)
try {
await retryPublication(id)
setRevision((r) => r + 1)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '重试失败')
} finally {
setRetryingId(null)
}
}
return (
<DocumentWorkbenchView
items={items}
connections={connections}
mode={mode}
loading={loading}
error={error}
onRefresh={() => setRevision((r) => r + 1)}
onRetry={(id) => void retry(id)}
retryingId={retryingId}
/>
)
}