fix: display defect trends without waiting for all product details

This commit is contained in:
2026-11-13 14:16:05 +08:00
parent 696c3d2f6a
commit f8f7335831
5 changed files with 63 additions and 27 deletions
+3 -2
View File
@@ -792,7 +792,7 @@ export default function QueryPage({ page, onUnauthorized }: {
<QueryTablePanel
heading={<SectionHeading title={tableProduct ? `产品${retainedTable?.granularity === 'quarter' ? '季度' : retainedTable?.granularity === 'year' ? '年度' : '月度'}质量数据` : page === 'overall' ? `${tablePeriod === 'year' ? '年度月度' : tablePeriod === 'quarter' ? '季度' : '月度'}汇总` : `${scopeName}质量汇总`} subtitle={tableProduct ? `${retainedTable?.year || year} 年 · ${tableProductLabel} · 重量 / kg 合格率 / %` : '重量 / kg 合格率 / %'} />} busy={queryBusy || !tableReady}
heading={<SectionHeading title={tableProduct ? `产品${retainedTable?.granularity === 'quarter' ? '季度' : retainedTable?.granularity === 'year' ? '年度' : '月度'}质量数据` : page === 'overall' ? `${tablePeriod === 'year' ? '年度月度' : tablePeriod === 'quarter' ? '季度' : '月度'}汇总` : `${scopeName}质量汇总`} subtitle={tableProduct ? `${retainedTable?.year || year} 年 · ${tableProductLabel} · 重量 / kg 合格率 / %` : '重量 / kg 合格率 / %'} />} busy={queryBusy || !tableReady || defectDrill.tablePending}
search={drilledProduct ? tableProductLabel : search} onSearchChange={setSearch} onSearch={() => applySearch()} productView={view === 'product'} searchPlaceholder={view === 'group' ? '搜索业务分组' : undefined} searchDisabled={Boolean(drilledProduct)}
fullFields={fullFields} onFullFields={setFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay}
returnControls={<>{!drilledProduct && (view !== baseView || view === 'group' || view === 'product' && (group.length > 0 || department.length > 0)) && <Button className="query-return-button" icon={<ArrowLeftOutlined />} onClick={() => { setSearch(''); setAppliedSearch(''); setTrendObject(''); if (view === 'product') { setProduct([]); setGroup([]); setViewOverride('group'); } else if (view === 'group') { setGroup([]); setDepartment([]); setViewOverride('department'); } else { setViewOverride(); } }}>返回上一级</Button>}
@@ -804,7 +804,8 @@ export default function QueryPage({ page, onUnauthorized }: {
catch (error) { message.error(`导出失败:${chooseError(error)}`); }
finally { setSummaryExporting(false); }
}}>
<SmoothTableFrame busy={queryBusy || !tableReady} blocked={queryBusy || summaryBusy || !tableReady}>
{defectDrill.tableControls}
<SmoothTableFrame busy={queryBusy || !tableReady || defectDrill.tablePending} blocked={queryBusy || summaryBusy || !tableReady || defectDrill.tablePending}>
<MetricTable
ref={summaryExport}
defectDrilldown={tableReady&&!queryBusy?defectDrill.table:undefined}
+3 -2
View File
@@ -151,11 +151,12 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
</div>
</QueryTrendPanel>
<QueryTablePanel heading={<SectionHeading title={table?.drilledProduct?`产品${table.granularity==='quarter'?'季度':table.granularity==='year'?'年度':'月度'}质量数据`:`${label}产品质量汇总`} subtitle={table?.drilledProduct?`${table.query.year} 年 · ${table.drilledProduct==='__UNASSIGNED__'?'未分类':table.drilledProduct} · 重量 / kg 合格率 / %`:'重量 / kg 合格率 / %'}/>}
busy={queryBusy||!tableReady} search={drilledLabel||search} onSearchChange={setSearch} onSearch={submit} searchDisabled={!!drilledProduct} searchUnavailable={!catalog||!!supportError}
busy={queryBusy||!tableReady||defectDrill.tablePending} search={drilledLabel||search} onSearchChange={setSearch} onSearch={submit} searchDisabled={!!drilledProduct} searchUnavailable={!catalog||!!supportError}
fullFields={fullFields} onFullFields={setFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay}
returnControls={drilledProduct&&<Button className="query-return-button" icon={<ArrowLeftOutlined/>} onClick={returnToProducts}>返回上一级</Button>}
exporting={exporting} exportDisabled={!!defectDrill.category&&(!defectDrill.table||defectDrill.pending||!!defectDrill.error)||!current||loading||!tableReady||!table?.rows.length} onExport={()=>void runExport()}>
<SmoothTableFrame busy={queryBusy||!tableReady} blocked={queryBusy||!tableReady}>
{defectDrill.tableControls}
<SmoothTableFrame busy={queryBusy||!tableReady||defectDrill.tablePending} blocked={queryBusy||!tableReady||defectDrill.tablePending}>
<MetricTable defectDrilldown={tableReady&&!queryBusy?defectDrill.table:undefined} ref={exporter} exportFilename={`${table?.drilledProduct||label}产品汇总-${table?.query.year||year}-${fullFields?'完整字段':'关键指标'}`}
paginationScope={JSON.stringify({query:table?.query,drilledProduct:table?.drilledProduct})}
rows={table?.rows||[]} view={table?.drilledProduct?'overall':'product'} layoutView="product" pageSizeOverride={table?.drilledProduct?12:undefined}
+31 -23
View File
@@ -11,14 +11,14 @@ export function useDefectDrilldown({active,mode,stage,query,trendQuery,tableQuer
const [selection,setSelection]=useState<{stage:Stage;category:string}|null>(null);
const [catalogFailure,setCatalogFailure]=useState('');
const [catalog,setCatalog]=useState<FineDefectCatalogResponse|null>(null);
const [loaded,setLoaded]=useState<{key:string;trend:DefectDrilldownResponse;table:DefectDrilldownResponse}|null>(null);
const [failure,setFailure]=useState<{key:string;message:string}|null>(null);
const [revision,setRevision]=useState(0);
const previousDisplay=useRef(display);
const enabled=active&&mode==='defects';
const category=mode==='defects'&&selection?.stage===stage?selection.category:null;
const catalogKey=JSON.stringify([enabled,stage,defectScope(query),revision,refreshKey]);
const key=JSON.stringify([category,stage,trendQuery,tableQuery,tableDimension,tableGranularity,revision,refreshKey]);
const common={stage,category,releaseId:query.releaseId};
const trend=useDefectPart(enabled&&!!category&&!!query.releaseId,{...defectScope(trendQuery),...common,dimension:'period',trendGranularity:trendQuery.trendGranularity||'month'},[revision,refreshKey],onUnauthorized);
const table=useDefectPart(enabled&&!!category&&!!query.releaseId,{...defectScope(tableQuery),...common,dimension:tableDimension,trendGranularity:tableGranularity},[revision,refreshKey],onUnauthorized);
useEffect(()=>{
if(!enabled||!query.releaseId)return;
const controller=new AbortController();setCatalog(null);setCatalogFailure('');
@@ -30,27 +30,35 @@ export function useDefectDrilldown({active,mode,stage,query,trendQuery,tableQuer
useEffect(()=>{
if(selection&&(selection.stage!==stage||mode!=='defects')){setSelection(null);onDisplay(previousDisplay.current);}
},[stage,mode]);
useEffect(()=>{
if(!enabled||!category||!query.releaseId)return;
const controller=new AbortController();setFailure(null);
const common={stage,category,releaseId:query.releaseId};
Promise.all([
api.defectDrilldown({...defectScope(trendQuery),...common,dimension:'period',trendGranularity:trendQuery.trendGranularity||'month'},controller.signal),
api.defectDrilldown({...defectScope(tableQuery),...common,dimension:tableDimension,trendGranularity:tableGranularity},controller.signal),
]).then(([trend,table])=>{
if(controller.signal.aborted)return;
if([trend,table].some(value=>value.releaseId!==query.releaseId||value.stage!==stage||value.category!==category))throw new Error('细分查询范围已变化,请重试。');
setLoaded({key,trend,table});
}).catch(error=>{if(!controller.signal.aborted&&!onUnauthorized(error))setFailure({key,message:error instanceof Error?error.message:'细分查询失败'});});
return()=>controller.abort();
},[enabled,key,onUnauthorized]);
const current=loaded?.key===key?loaded:null;
const error=failure?.key===key?failure.message:'';
const pending=!!category&&!current&&!error;
const error=trend.error||table.error;
const pending=trend.pending||table.pending;
const categories=useMemo(()=>(catalog?.categories||[]).filter(item=>item.stage===stage&&item.available&&catalog?.fields.some(field=>field.stage===stage&&field.category===item.category&&field.available!==false)).map(item=>item.category),[catalog,stage]);
const choose=(next:string)=>{if(!categories.includes(next))return;if(!category)previousDisplay.current=display;setSelection({stage,category:next});onDisplay('rate');};
const clear=()=>{setSelection(null);onDisplay(previousDisplay.current);};
const incomplete=!!current&&[current.trend,current.table].some(data=>data.groups.some(group=>group.reconciliationStatus!=='RECONCILED'));
const controls=category?<div className="defect-drilldown-controls"><Button type="link" onClick={clear}>全部分类</Button><span>› {category}</span>{pending&&<span><Spin size="small"/> 正在读取细分</span>}{error&&<Alert type="error" showIcon message={error} action={<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>}/>} {incomplete&&<span className="defect-drilldown-notice">细分来源未完全覆盖分类总量</span>}</div>:catalogFailure&&enabled?<Alert type="error" showIcon message="细分目录读取失败" description={catalogFailure} action={<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>}/>:null;
return {category,categories,choose,clear,controls,pending,error,trend:current?.trend,table:current?.table};
const incomplete=[trend.data,table.data].some(data=>data?.groups.some(group=>group.reconciliationStatus!=='RECONCILED'));
const retry=<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>;
const controls=category?<div className="defect-drilldown-controls"><Button type="link" onClick={clear}>全部分类</Button><span>› {category}</span>{trend.pending&&<span><Spin size="small"/> 正在读取细分</span>}{trend.error&&<Alert type="error" showIcon message={trend.error} action={retry}/>} {incomplete&&<span className="defect-drilldown-notice">细分来源未完全覆盖分类总量</span>}</div>:catalogFailure&&enabled?<Alert type="error" showIcon message="细分目录读取失败" description={catalogFailure} action={<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>}/>:null;
const tableControls=table.error?<Alert type="error" showIcon message={table.error} action={retry}/>:null;
return {category,categories,choose,clear,controls,tableControls,pending,error,trend:trend.data,table:table.data,trendPending:trend.pending,tablePending:table.pending,trendError:trend.error,tableError:table.error};
}
/** Keep the small trend independent from the potentially large object table. */
function useDefectPart(enabled:boolean,params:Record<string,unknown>,refresh:unknown[],onUnauthorized:(error:unknown)=>boolean) {
const key=JSON.stringify([params,refresh]);
const [result,setResult]=useState<{key:string;data?:DefectDrilldownResponse;error?:string}|null>(null);
useEffect(()=>{
if(!enabled){setResult(null);return;}
const controller=new AbortController();
api.defectDrilldown(params,controller.signal).then(data=>{
if(controller.signal.aborted)return;
if(data.releaseId!==params.releaseId||data.stage!==params.stage||data.category!==params.category)throw new Error('细分查询范围已变化,请重试。');
setResult({key,data});
}).catch(error=>{
if(!controller.signal.aborted&&!onUnauthorized(error))setResult({key,error:error instanceof Error?error.message:'细分查询失败'});
});
return()=>controller.abort();
},[enabled,key,onUnauthorized]);
const current=enabled&&result?.key===key?result:null;
return {data:current?.data,error:current?.error||'',pending:enabled&&!current};
}