diff --git a/frontend/src/components/ZhsQueuePanel.tsx b/frontend/src/components/ZhsQueuePanel.tsx index 6f5c75c1..b1bbac5e 100644 --- a/frontend/src/components/ZhsQueuePanel.tsx +++ b/frontend/src/components/ZhsQueuePanel.tsx @@ -17,12 +17,16 @@ export function ZhsQueuePanel({status,unavailable=false,onRefresh}:{ const [detailMessage,setDetailMessage]=useState(""); const [serverComparison,setServerComparison]=useState(""); const currentRun=useRef(status.run_id); + const detailRequestId=useRef(0); + const currentDetailScope=useRef(status.run_id+":"+status.course_id); currentRun.current=status.run_id; + currentDetailScope.current=status.run_id+":"+status.course_id; useEffect(()=>{ + detailRequestId.current++; setRequested(new Set());setMessage(""); setDetail(null);setDetailMessage("");setDetailBusy(false); setServerComparison(""); - },[status.run_id]); + },[status.run_id,status.course_id]); const queue=status.zhs_queue; if(!queue)return null; const withinChapter=queue.parallel_mode==="resources"; @@ -54,13 +58,16 @@ export function ZhsQueuePanel({status,unavailable=false,onRefresh}:{ const courseKey=status.course_id; const run=status.run_id; if(!courseKey||!/^[1-9][0-9]{0,18}$/.test(knowledgeId))return; + const requestId=++detailRequestId.current; + const scope=run+":"+courseKey; setDetailBusy(true);setDetailMessage(""); const previous=detail?.knowledge_id===knowledgeId?detail:null; try{ const result=await request({ operation:"zhidao.zhs.knowledge.detail",course_key:courseKey,knowledge_id:knowledgeId }); - if(run!==currentRun.current)return; + if(run!==currentRun.current||scope!==currentDetailScope.current|| + requestId!==detailRequestId.current)return; if(result.course_key!==courseKey||result.knowledge_id!==knowledgeId|| result.verification!=="read_only"){ throw new Error("ZHS_DETAIL_UNAVAILABLE"); @@ -86,13 +93,20 @@ export function ZhsQueuePanel({status,unavailable=false,onRefresh}:{ setServerComparison("已建立 ZHS 服务器进度基线。目前无资源知识点尚未有经验证的执行心跳;重新核验只会读取真实服务器状态。"); } }catch(error){ - if(run!==currentRun.current)return; + if(run!==currentRun.current||scope!==currentDetailScope.current|| + requestId!==detailRequestId.current)return; setDetail(null); setDetailMessage("知识点详情读取失败:"+friendlyBridgeError(error)); }finally{ - if(run===currentRun.current)setDetailBusy(false); + if(run===currentRun.current&&scope===currentDetailScope.current&& + requestId===detailRequestId.current)setDetailBusy(false); } } + function closeDetail(){ + // Dismissing a detail also invalidates an outstanding read-only response. + detailRequestId.current++; + setDetail(null);setDetailBusy(false);setDetailMessage("");setServerComparison(""); + } type Row=NonNullable["tasks"][number]; function row(task:Row){ const inProgress=task.state==="queued"||task.state==="running"; @@ -178,7 +192,7 @@ export function ZhsQueuePanel({status,unavailable=false,onRefresh}:{ {detail&&
{detail.name} · 服务器只读 - +

ZHS 原会话知识点进度:{detail.knowledge_progress_percent}% · 资源目录来源:{detail.catalog_source==="mobile"?"移动版":detail.catalog_source==="desktop"?"桌面版":"未确认"} · diff --git a/frontend/tests/zhsDetailReadbackLatestSelection.test.cjs b/frontend/tests/zhsDetailReadbackLatestSelection.test.cjs new file mode 100644 index 00000000..8e2b9ac7 --- /dev/null +++ b/frontend/tests/zhsDetailReadbackLatestSelection.test.cjs @@ -0,0 +1,24 @@ +const test=require("node:test"); +const assert=require("node:assert/strict"); +const fs=require("node:fs"); +const path=require("node:path"); + +const source=fs.readFileSync(path.join(__dirname,"../src/components/ZhsQueuePanel.tsx"),"utf8"); + +test("late knowledge readback cannot overwrite a newer selection",()=>{ + assert.match(source,/const detailRequestId=useRef\(0\)/); + assert.match(source,/const requestId=\+\+detailRequestId\.current/); + const inspect=source.split("async function inspectDetail(",2)[1]?.split("function closeDetail()",1)[0]||""; + const guards=inspect.match(/requestId!==detailRequestId\.current/g)||[]; + assert.ok(guards.length>=2,"success and error both need latest-request checks"); + assert.match(inspect,/requestId===detailRequestId\.current\)setDetailBusy\(false\)/); + assert.match(inspect,/scope!==currentDetailScope\.current/); +}); + +test("dismissing details cancels older readback updates",()=>{ + const close=source.split("function closeDetail()",2)[1]?.split("type Row=",1)[0]||""; + assert.match(close,/detailRequestId\.current\+\+/); + assert.match(close,/setDetail\(null\)/); + assert.match(source,/onClick=\{closeDetail\}>收起详情/); + assert.match(source,/\[status\.run_id,status\.course_id\]/); +});