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 = { 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(null) const [query, setQuery] = useState('') const [visibility, setVisibility] = useState('') const [stage, setStage] = useState('') const [page, setPage] = useState(0) const [selectedID, setSelectedID] = useState(null) const [showTestDocuments, setShowTestDocuments] = useState(false) const [scopeRules, setScopeRules] = useState([]) const [scopeLoading, setScopeLoading] = useState(false) const [scopeError, setScopeError] = useState(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 (

文档工作台

{title}

{mode === 'monitor' ? '看每份文件处理到哪一步。解析完成不代表已上线。' : mode === 'attention' ? '只列出超限、失败或来源不可访问的文件。内容审核请进入发布管理。' : '先选来源和目录,再看文件、对内/对外和处理状态。'}

{error ? (
{error}(旧数据仅供参考)
) : null} {mode === 'monitor' && !unavailable ? (
来源处理概览 直接看各来源完成、处理中和异常数量
{progress.map((item) => { const source = sources.find((value) => value.id === item.connection) return ( ) })}
) : null}
{!unavailable ? (
文件 {scoped.length} {mode === 'monitor' ? ( <> 独立解析 {scoped.filter((x) => documentStage(x) === 'completed').length} 内容复用 {scoped.filter((x) => documentStage(x) === 'reused').length} 处理中 {scoped.filter((x) => activeStages.includes(documentStage(x))).length} 等待 {scoped.filter((x) => documentStage(x) === 'waiting').length} 需处理(含超限){' '} {scoped.filter((x) => ['blocked', 'failed'].includes(documentStage(x))).length} ) : ( <> 对内 {scoped.filter((x) => x.visibility === 'internal').length} 对外 {scoped.filter((x) => x.visibility === 'external').length} 处理中 {scoped.filter((x) => activeStages.includes(documentStage(x))).length} 已解析 {scoped.filter((x) => documentStage(x) === 'completed').length} 需处理 {scoped.filter((x) => ['blocked', 'failed'].includes(documentStage(x))).length} )}
) : (

文件数量尚未核实

)} {mode === 'directory' && connection && directory !== '' ? ( void mutateScope(decision, false)} onClear={(decision) => void mutateScope(decision, true)} /> ) : mode === 'directory' && connection && directory === '' ? (

“分类待补齐”不是可靠目录,不能单独设置范围。请先补齐来源目录,或返回来源级设置。

) : null}
{!connection ? '全部来源' : sourceNames[sources.find((s) => s.id === connection)?.type || ''] || '当前来源'}{' '} / {directory === null ? '全部文件' : directory || '目录待补齐'}{' '} {!unavailable ? 筛选结果 {filtered.length} 份 : null}
{loading && !items.length ?
正在核对完整文档清单…
: null} {!loading && !error && !filtered.length ?
没有符合条件的文件。
: null} {filtered.length ? (
{filtered.slice(shownPage * 40, (shownPage + 1) * 40).map((item) => ( ))}
文件名 对内/对外 处理状态 大小 状态更新时间
{sourceNames[item.source_type]} / {item.directory_path || '目录待补齐'} {item.visibility === 'external' ? '对外' : '对内'} {visibilityOrigin(item)} {stageLabels[documentStage(item)]} {bytes(item.size_bytes)}
) : null}
第 {shownPage + 1} / {Math.max(1, Math.ceil(filtered.length / 40))} 页
{mode === 'monitor' ? (

每15秒自动刷新。当前接口未提供页数或逐页百分比,因此只显示真实阶段。时间为状态更新时间,不是源文件修改时间。

) : null}
{selected ? (

{selected.title || '文件名待补齐'}

来源位置
{sourceNames[selected.source_type]} / {selected.directory_path || '目录待补齐'}
对内/对外
{selected.visibility === 'external' ? '对外' : '对内'} · {visibilityOrigin(selected)}
当前进度
{stageLabels[documentStage(selected)]}
{documentProblem(selected) ?

{documentProblem(selected)}

: null}
文件大小 {bytes(selected.size_bytes)} 状态更新时间 {time(selected.updated_at)} 来源变化 {selected.source_change === 'unchanged' ? '无变化' : '需要核对'} 检索状态 {selected.retrieval_state === 'enabled' ? '测试环境已启用' : '未证明已启用'}
{documentPipeline(selected).map((step) => (
))}

原文与版本差异尚未接通,请在源系统核对内容;不要仅凭“解析完成”批准发布。

{mode === 'directory' ? ( void mutateScope(decision, false)} onClear={(decision) => void mutateScope(decision, true)} /> ) : null} {selected.can_retry && selected.publication_id ? ( ) : null} {selected.last_error ? (
错误详情 {selected.last_error}
) : null}
) : null}
) } export function DocumentWorkbench({ mode, connections }: { mode: Mode; connections: SourceConnectionPage['items'] }) { const [items, setItems] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [revision, setRevision] = useState(0) const [retryingId, setRetryingId] = useState(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 ( setRevision((r) => r + 1)} onRetry={(id) => void retry(id)} retryingId={retryingId} /> ) }