import { useContext, useEffect, useRef, useState } from 'react' import { fetchCatalogNodes, fetchCatalogRoutingCapabilities, fetchCatalogSnapshotNodes, fetchCatalogSnapshots, fetchCatalogSources, routeCatalogFiles, } from './sourceCatalog' import type { CatalogNode, CatalogNodePage, CatalogPolicy, CatalogRoutingCapabilities, CatalogSnapshot, CatalogSource, } from './sourceCatalog' import { SnapshotHistoryView } from './SnapshotHistory' import { triggerCatalogRefresh } from './sourceCatalog' import { fetchJobs } from './jobs' import type { Job } from './jobs' import { WorkspaceContextTabsSetter } from './workspaceContext' import { FilenameFilterEditor } from './FilenameFilterEditor' import './sourceCatalogBrowser.css' const sourceLabels: Record = { dingtalk: '钉钉知识库', pp: 'PP资料下载中心', alist: 'AList文件源' } const duration = (milliseconds: number) => milliseconds <= 0 ? '未记录' : milliseconds < 1000 ? `${milliseconds}毫秒` : milliseconds < 60000 ? `${Math.round(milliseconds / 1000)}秒` : `${Math.floor(milliseconds / 60000)}分${Math.round((milliseconds % 60000) / 1000)}秒` const scope = (value: string) => value === 'internal' ? '对内' : value === 'external' ? '对外' : value === 'both' ? '对内 + 对外' : value === 'blocked' ? '禁止导入' : value === 'mixed' ? '混合' : '未选择' const progress = (node: CatalogNode) => node.imported_count ? `已解析 ${node.parsed_count} / 已导入 ${node.imported_count}` : '尚未导入' const time = (value?: string) => (value ? new Date(value).toLocaleString('zh-CN', { hour12: false }) : '未记录') // 失败原因可能带来源地址或凭据路径;界面只展示脱敏摘要,详细值留在作业表。 const sanitizeRefreshReason = (value?: string) => (value || '') .replace(/https?:\/\/\S+/g, '[地址已隐藏]') .replace(/\/run\/source-secrets\/\S+/g, '[凭据路径已隐藏]') .replace(/\b(?:token|api[-_]?key|password|authorization|cookie|secret)=\S+/gi, '[凭据已隐藏]') .slice(0, 160) interface ViewProps { sources: CatalogSource[] selectedSource: CatalogSource | null page: CatalogNodePage | null loading: boolean error: string | null routingCapabilities?: CatalogRoutingCapabilities routingMessage?: string routingLoading?: boolean refreshJob?: Job | null onSelectSource: (source: CatalogSource) => void onOpenFolder: (node: CatalogNode) => void onNavigateTo: (nodeID: string) => void onBack: () => void onPageChange: (offset: number) => void onRefresh: () => void onRoute?: (nodeIDs: string[], policy: CatalogPolicy) => Promise history?: { open: boolean snapshots: CatalogSnapshot[] active: CatalogSnapshot | null page: CatalogNodePage | null loading: boolean error: string | null onOpen: () => void onClose: () => void onOpenSnapshot: (snapshot: CatalogSnapshot) => void onOpenFolder: (snapshot: CatalogSnapshot, nodeID: string) => void } refreshHidden?: boolean onToggleRefreshHidden?: () => void onFiltersChanged?: () => void onViewProcessing?: (node: CatalogNode) => void } export function SourceCatalogBrowserView({ sources, selectedSource, page, loading, error, routingCapabilities = { internal_enabled: false, external_enabled: false }, routingMessage = '', routingLoading = false, refreshJob = null, onSelectSource, onOpenFolder, onNavigateTo, onBack, onPageChange, onRefresh, onRoute = async () => false, history, refreshHidden = false, onToggleRefreshHidden, onFiltersChanged, onViewProcessing, }: ViewProps) { const [selectedIDs, setSelectedIDs] = useState([]) const [appliedPolicies, setAppliedPolicies] = useState>({}) const atSourceRoot = Boolean(page && page.breadcrumbs.length === 1) const visibleItems = page?.items || [] useEffect(() => { setSelectedIDs([]) setAppliedPolicies({}) }, [selectedSource?.connection_id, page?.current.node_id]) useEffect(() => { if (routingMessage) setSelectedIDs([]) }, [routingMessage]) const selectableNodes = visibleItems const selectedNodeIDs = selectedIDs const selectedCount = selectedIDs.length const routeReady = !routingLoading const policyEnabled = (policy: CatalogPolicy) => policy === 'blocked' || policy === 'both' ? policy === 'blocked' || (routingCapabilities.internal_enabled && routingCapabilities.external_enabled) : policy === 'internal' ? routingCapabilities.internal_enabled : routingCapabilities.external_enabled const toggleNode = (id: string) => setSelectedIDs((current) => (current.includes(id) ? current.filter((value) => value !== id) : [...current, id])) const applyPolicy = async (policy: CatalogPolicy) => { if (!selectedNodeIDs.length) return const changed = await onRoute(selectedNodeIDs, policy) if (changed) setAppliedPolicies((current) => ({ ...current, ...Object.fromEntries(selectedNodeIDs.map((id) => [id, policy])), })) } const refreshPayload = refreshJob?.payload && typeof refreshJob.payload === 'object' ? (refreshJob.payload as Record) : {} const refreshProgress = typeof refreshPayload.progress === 'number' ? refreshPayload.progress : 0 const refreshMessage = typeof refreshPayload.message === 'string' ? refreshPayload.message : '等待后台刷新' const refreshDirectory = typeof refreshPayload.current_directory === 'string' && refreshPayload.current_directory ? refreshPayload.current_directory : '' const refreshFiles = typeof refreshPayload.scanned_files === 'number' && refreshPayload.scanned_files > 0 ? refreshPayload.scanned_files : 0 const refreshEstimate = refreshPayload.total_is_estimate === true const stageLabels: Record = { queued: '排队中', connecting: '连接来源', scanning: '扫描目录', building: '构建快照', activating: '写入快照', completed: '完成', failed: '失败', } const refreshStageLabel = typeof refreshPayload.stage === 'string' ? stageLabels[refreshPayload.stage] || refreshPayload.stage : '排队中' const pillSource = typeof refreshPayload.source_name === 'string' ? refreshPayload.source_name : '' const refreshPillSource = pillSource || selectedSource?.name || '' const refreshActive = refreshJob?.state === 'pending' || refreshJob?.state === 'running' || refreshJob?.state === 'retry' return (

来源与目录

各个知识源头的资料总览与标注

{error ? (
{error}
) : null} {refreshJob && refreshActive && refreshHidden ? ( ) : null} {refreshJob && !(refreshActive && refreshHidden) ? ( <>
{refreshActive ? '实时刷新中' : refreshJob.state === 'completed' ? '实时快照已完成' : '实时刷新失败'} {selectedSource ? selectedSource.name + ' · ' : ''} {refreshStageLabel} {refreshMessage}
{refreshActive ? `${refreshProgress}%${refreshEstimate ? '(估算)' : ''} · 后台运行,可以切换页面` : refreshJob.state === 'completed' ? `完成于 ${time(refreshJob.updated_at)}` : `失败于 ${time(refreshJob.updated_at)} · 旧快照 ${time(selectedSource?.observed_at)}`} {refreshActive && onToggleRefreshHidden ? ( ) : null} {refreshJob.state !== 'completed' ? ( ) : null}
{refreshActive && (refreshDirectory || refreshFiles) ? (
{refreshDirectory ? 当前目录:{refreshDirectory} : null} {refreshFiles ? 已发现 {refreshFiles} 份文件 : null}
) : null} {refreshJob.state === 'failed' ? (

刷新失败;旧快照仍然保留,目录浏览与标注不受影响。原因: {sanitizeRefreshReason(refreshJob.last_error) || '未提供'} · 作业 {refreshJob.id}

) : null} ) : null}
{selectedSource && selectedSource.snapshot_status === 'scanning' ? (
首次扫描中 · {selectedSource.scan_progress ?? 0}% {selectedSource.name}

{selectedSource.scan_message || '正在从源头扫描目录…'}

已发现约 {selectedSource.file_count} 份文件(文件夹 {selectedSource.folder_count}{' '} 个)。扫完这一页会自动出现目录;现在可以先去做别的。
) : selectedSource && page ? ( <>
{page.current.name} {selectedSource.snapshot_only ? '本地只读快照,不代表实时连接' : '实时目录'}
{atSourceRoot ? `第一层目录 ${page.items.filter((node) => node.node_type === 'folder').length}` : `当前子项 ${page.total}`} 含所有子目录文件 {page.current.subtree_file_count}
文件格式筛选 item.name) .join('/')} label={atSourceRoot ? '整个来源' : '当前目录'} onChanged={onFiltersChanged} />
{visibleItems.length ? (
已选 {selectedCount} 项
{routingMessage ?

{routingMessage}

: null}
) : null} {visibleItems.length ? (
{visibleItems.map((node) => { const visibility = appliedPolicies[node.node_id] || node.visibility return ( ) })}
名称 类型 处理策略 文件数量 导入与解析 更新时间
{node.node_type === 'folder' ? (
toggleNode(node.node_id)} />
) : ( )}
{node.node_type === 'folder' ? '文件夹' : '文件'}
{scope(visibility)}
{node.node_type === 'folder' ? `${node.subtree_file_count} 份` : '—'} {progress(node)} {node.active_count ? 处理中 {node.active_count} : null} {node.failed_count ? ( 失败 {node.failed_count} ) : null} {onViewProcessing && node.node_type === 'file' && node.imported_count > 0 ? ( ) : null} {node.modified_at ? new Date(node.modified_at).toLocaleString('zh-CN', { hour12: false }) : '—'}
) : (
当前目录为空。
)} {page.total > page.limit ? (
第 {Math.floor(page.offset / page.limit) + 1} / {Math.max(1, Math.ceil(page.total / page.limit))} 页
) : null}
快照就绪 快照时间:{new Date(selectedSource.observed_at).toLocaleString('zh-CN', { hour12: false })} 上次刷新耗时 {duration(selectedSource.last_refresh_duration_ms)} {history ? ( ) : null}
{history && history.open ? ( ) : null} ) : loading ? (
正在读取第一层目录…
) : !error ? (
请选择一个内容来源。
) : null}
) } export function SourceCatalogBrowser({ focus, onViewProcessing, onFocusApplied, }: { focus?: { connectionID: string; nodeID: string } | null onViewProcessing?: (source: CatalogSource, node: CatalogNode) => void onFocusApplied?: () => void } = {}) { const [sources, setSources] = useState([]) const [selected, setSelected] = useState(null) const [parentID, setParentID] = useState('') const [page, setPage] = useState(null) const [offset, setOffset] = useState(0) const [revision, setRevision] = useState(0) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const setContextTabs = useContext(WorkspaceContextTabsSetter) const [routingCapabilities, setRoutingCapabilities] = useState({ internal_enabled: false, external_enabled: false, }) const [routingMessage, setRoutingMessage] = useState('') const [routingLoading, setRoutingLoading] = useState(false) const [refreshJob, setRefreshJob] = useState(null) const [historySnapshots, setHistorySnapshots] = useState([]) const [historyActive, setHistoryActive] = useState(null) const [historyPage, setHistoryPage] = useState(null) const [historyLoading, setHistoryLoading] = useState(false) const [historyError, setHistoryError] = useState(null) const [historyOpen, setHistoryOpen] = useState(false) const [refreshHidden, setRefreshHidden] = useState(false) const openHistory = async () => { if (!selected || historyLoading) return if (historyOpen) { closeHistory() return } setHistoryOpen(true) setHistoryLoading(true) setHistoryError(null) try { const result = await fetchCatalogSnapshots(selected.connection_id, 20, 0) setHistorySnapshots(result.items) const head = result.items.find((item) => item.is_head) || result.items[0] || null setHistoryActive(head) if (head) setHistoryPage(await fetchCatalogSnapshotNodes(selected.connection_id, head.snapshot_id, head.root_node_id)) else setHistoryPage(null) } catch (cause) { setHistoryError(cause instanceof Error ? cause.message : '加载快照历史失败') } finally { setHistoryLoading(false) } } const openHistorySnapshot = async (snapshot: CatalogSnapshot) => { if (!selected || historyLoading) return setHistoryLoading(true) setHistoryError(null) try { setHistoryActive(snapshot) setHistoryPage( await fetchCatalogSnapshotNodes(selected.connection_id, snapshot.snapshot_id, snapshot.root_node_id) ) } catch (cause) { setHistoryError(cause instanceof Error ? cause.message : '加载历史快照目录失败') } finally { setHistoryLoading(false) } } const openHistoryFolder = async (snapshot: CatalogSnapshot, nodeID: string) => { if (!selected || historyLoading) return setHistoryLoading(true) setHistoryError(null) try { setHistoryPage(await fetchCatalogSnapshotNodes(selected.connection_id, snapshot.snapshot_id, nodeID)) } catch (cause) { setHistoryError(cause instanceof Error ? cause.message : '加载历史快照目录失败') } finally { setHistoryLoading(false) } } const closeHistory = () => { setHistoryOpen(false) setHistoryActive(null) setHistoryPage(null) setHistoryError(null) } useEffect(() => { const controller = new AbortController() setLoading(true) void fetchCatalogSources(controller.signal) .then((result) => { if (controller.signal.aborted) return setSources(result) setSelected( (current) => (current && result.find((source) => source.connection_id === current.connection_id)) || result[0] || null ) setParentID((current) => current || result[0]?.root_node_id || '') 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 (!selected || !parentID || selected.snapshot_status === 'scanning') return const controller = new AbortController() setLoading(true) void fetchCatalogNodes(selected.connection_id, parentID, 50, offset, controller.signal) .then((result) => { if (!controller.signal.aborted) { setPage(result) setError(null) } }) .catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载目录失败') }) .finally(() => { if (!controller.signal.aborted) setLoading(false) }) return () => controller.abort() }, [selected, parentID, offset, revision]) // 从解析监控“在来源与目录定位”进入:这是一次性导航请求。应用后由父消费; // 同时用 ref 防同一请求在 sources 刷新/revision 重载时重放,手工换目录不会被拽回。 const appliedFocus = useRef(null) useEffect(() => { if (!focus || !sources.length || appliedFocus.current === focus) return const target = sources.find((source) => source.connection_id === focus.connectionID) if (!target) return appliedFocus.current = focus setSelected(target) setParentID(focus.nodeID || target.root_node_id) setOffset(0) setPage(null) closeHistory() onFocusApplied?.() }, [focus, sources]) // 有来源正在首次扫描时自动刷新列表:百分比会自己往前走,扫完那一行自动变成正式来源。 useEffect(() => { if (!sources.some((source) => source.snapshot_status === 'scanning')) return const timer = window.setTimeout(() => setRevision((value) => value + 1), 3000) return () => window.clearTimeout(timer) }, [sources]) useEffect(() => { const controller = new AbortController() void fetchCatalogRoutingCapabilities(controller.signal) .then(setRoutingCapabilities) .catch(() => setRoutingCapabilities({ internal_enabled: false, external_enabled: false })) return () => controller.abort() }, [revision]) useEffect(() => { if (!selected) return const controller = new AbortController() void fetchJobs(controller.signal) .then((items) => { if (controller.signal.aborted) return const match = items.find( (job) => job.type === 'source.catalog.refresh' && job.payload && typeof job.payload === 'object' && (job.payload as Record).connection_id === selected.connection_id ) setRefreshJob(match || null) }) .catch(() => undefined) return () => controller.abort() }, [selected]) useEffect(() => { if (!refreshJob || !['pending', 'running', 'retry'].includes(refreshJob.state)) return const timer = window.setTimeout( () => void fetchJobs() .then((items) => { const current = items.find((job) => job.id === refreshJob.id) if (!current) return setRefreshJob((previous) => { if (previous?.state !== 'completed' && current.state === 'completed') setRevision((value) => value + 1) return current }) }) .catch(() => undefined), 750 ) return () => window.clearTimeout(timer) }, [refreshJob]) useEffect(() => { if (!selected || !page) { setContextTabs([]) return } setContextTabs( page.breadcrumbs.map((item) => ({ key: item.node_id, label: item.name, onActivate: () => { setParentID(item.node_id) setOffset(0) }, })) ) return () => setContextTabs([]) }, [selected, page, setContextTabs]) const selectSource = (source: CatalogSource) => { if (selected?.connection_id === source.connection_id && parentID === source.root_node_id && offset === 0) return setSelected(source) setParentID(source.root_node_id) setOffset(0) setPage(null) setRefreshJob(null) closeHistory() } const back = () => { if (!page || page.breadcrumbs.length < 2) return setParentID(page.breadcrumbs[page.breadcrumbs.length - 2].node_id) setOffset(0) } const route = async (nodeIDs: string[], policy: CatalogPolicy): Promise => { if (!selected || routingLoading) return false setRoutingLoading(true) setRoutingMessage('') try { const result = await routeCatalogFiles({ connection_id: selected.connection_id, node_ids: nodeIDs, policy, reason: `目录页策略:${scope(policy)}`, }) setRoutingMessage( `策略已保存 ${result.selected_files} 份;已记入意图 ${result.intents_recorded} 份;排队从源头取件 ${result.fetch_queued} 份;列入处理 ${result.planned_publications} 份;标记禁止 ${result.blocked_items} 份;目录外无文件可标注 ${result.unbound_files} 份。取件校验完成后会自动上传解析,可在“解析监控”跟踪进度。` ) setRevision((value) => value + 1) return true } catch (cause) { setRoutingMessage(cause instanceof Error ? cause.message : '文件处理失败') return false } finally { setRoutingLoading(false) } } const refresh = async () => { if (!selected || (refreshJob && ['pending', 'running', 'retry'].includes(refreshJob.state))) return setError(null) try { setRefreshJob(await triggerCatalogRefresh(selected.connection_id)) } catch (cause) { setError(cause instanceof Error ? cause.message : '启动实时刷新失败') } } return ( { setRoutingMessage('') selectSource(source) }} onOpenFolder={(node) => { setRoutingMessage('') setParentID(node.node_id) setOffset(0) }} onNavigateTo={(nodeID) => { setRoutingMessage('') setParentID(nodeID) setOffset(0) }} onBack={back} onPageChange={setOffset} onRefresh={() => void refresh()} onRoute={(nodeIDs, visibility) => route(nodeIDs, visibility)} history={{ open: historyOpen, snapshots: historySnapshots, active: historyActive, page: historyPage, loading: historyLoading, error: historyError, onOpen: () => void openHistory(), onClose: closeHistory, onOpenSnapshot: (snapshot) => void openHistorySnapshot(snapshot), onOpenFolder: (snapshot, nodeID) => void openHistoryFolder(snapshot, nodeID), }} refreshHidden={refreshHidden} onToggleRefreshHidden={() => setRefreshHidden((value) => !value)} onFiltersChanged={() => setRevision((value) => value + 1)} onViewProcessing={onViewProcessing && selected ? (node) => onViewProcessing(selected, node) : undefined} /> ) }