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
+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};
}