feat(catalog): 首次扫描中的来源也出现在「来源与目录」,并显示实时进度

用户反馈:加了钉盘来源后「来源与目录」里看不到。根因不是丢数据,而是列表只列"已经有快照"
的来源 —— 首次扫描要几分钟到几十分钟(钉盘空间 2900+ 个文件夹),这段时间页面上什么都没有。

- 新 Store 方法 ListCatalogScanProgress:已启用 + 还没有 catalog head + 有在跑/排队的
  source.catalog.refresh(三条件同时成立)。进度直接取 worker 写在任务载荷里的同一份计数
  (progress/message/scanned_directories/discovered_directories/scanned_files),不另立口径。
- /api/v1/source-catalog/sources 在既有列表后追加这些行(snapshot_status="scanning" +
  scan_progress/scan_message)。走窄接口按需接线:未接线时安静跳过,不会把旧列表弄坏。
- 前端:左列显示「首次扫描中 44%」,选中后是进度面板(百分比 + 进度条 + 当前文案 +
  已发现文件数),并说明"扫完自动出现目录";有来源在扫描时每 3 秒自动刷新列表,
  扫完那一行自动变成「本地只读快照」。扫描中不拉目录节点、也不显示标注/格式入口。
- 解析器保持严格:snapshot_status 只接受 ready/scanning,scanning 必须带 scan_progress,
  否则整包拒绝 —— 不给"假装 0%"的机会。

测试:postgres 集成测试(真库)覆盖四种不该出现的来源(停用、已有快照、任务类型不符、
历史上已完成的那次任务);前端 parser + 渲染各一条;Go 全绿、前端 192 条 + tsc 干净。

面板实测:左列「公司级营销资料管理 · 首次扫描中 44%」,主区「首次扫描中 · 44% /
正在扫描目录:已完成 1540 个文件夹 / 已发现约 4934 份文件」。
This commit is contained in:
姚章浩
2026-10-10 17:04:59 +08:00
parent ed60b20790
commit 1283f65a95
9 changed files with 248 additions and 6 deletions
+10 -3
View File
@@ -56,11 +56,16 @@ export function SourceCatalogBrowserView({sources,selectedSource,page,loading,er
<div className="catalog-layout">
<aside className="catalog-sources" aria-label="内容来源">
<h2>内容来源</h2>
{sources.map(source=><button key={source.connection_id} className={selectedSource?.connection_id===source.connection_id?'catalog-source active':'catalog-source'} onClick={()=>onSelectSource(source)}><span><b>{source.name}</b><small>{sourceLabels[source.source_type]} · 本地只读快照</small></span><strong>{source.file_count}</strong></button>)}
{sources.map(source=>{const scanning=source.snapshot_status==='scanning';const percent=source.scan_progress??0;return <button key={source.connection_id} className={selectedSource?.connection_id===source.connection_id?'catalog-source active':scanning?'catalog-source scanning':'catalog-source'} onClick={()=>onSelectSource(source)}><span><b>{source.name}</b><small>{sourceLabels[source.source_type]} · {scanning?`首次扫描中 ${percent}%`:'本地只读快照'}</small></span><strong>{scanning?`${percent}%`:source.file_count}</strong></button>})}
{!loading&&!error&&!sources.length?<div className="empty-state">尚无来源目录。</div>:null}
</aside>
<section className="catalog-content">
{selectedSource&&page?<>
{selectedSource&&selectedSource.snapshot_status==='scanning'?<section className="catalog-scanning-state" role="status">
<div><strong>首次扫描中 · {selectedSource.scan_progress??0}%</strong><span>{selectedSource.name}</span></div>
<progress max="100" value={selectedSource.scan_progress??0}/>
<p>{selectedSource.scan_message||'正在从源头扫描目录…'}</p>
<small>已发现约 {selectedSource.file_count} 份文件(文件夹 {selectedSource.folder_count} 个)。扫完这一页会自动出现目录;现在可以先去做别的。</small>
</section>:selectedSource&&page?<>
<div className="catalog-pathbar" aria-label="当前目录路径">
<button type="button" className="catalog-back-button" aria-label="返回上一级" disabled={page.breadcrumbs.length<2} onClick={onBack}>←</button>
<nav aria-label="目录面包屑">{page.breadcrumbs.map((crumb,index)=><span key={crumb.node_id}>{index>0?<i aria-hidden="true">/</i>:null}<button type="button" className={index===page.breadcrumbs.length-1?'current':''} disabled={index===page.breadcrumbs.length-1} onClick={()=>onNavigateTo(crumb.node_id)}>{crumb.name}</button></span>)}</nav>
@@ -99,8 +104,10 @@ export function SourceCatalogBrowser(){
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)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])
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])
// 有来源正在首次扫描时自动刷新列表:百分比会自己往前走,扫完那一行自动变成正式来源。
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<string,unknown>).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])