11import type { WebObservation , WebPlanProposal , WebCommandResult , WebPlanTask , WebPlanGroup } from '../collaboration' ;
2+ import type { WebRun } from '../shared' ;
23import { render } from 'preact' ;
34import { commandId , createElement , errorText , jsonText , editStatusLabel , type WebApi } from './collaborationUi' ;
45import { Code , renderOptions , renderSection } from './ui/Section' ;
@@ -8,12 +9,21 @@ type PlanDraft = { text: string; receipt?: WebCommandResult; previewText?: strin
89 busy ?: boolean ; observation ?: WebObservation ; comparison ?: WebObservation [ 'plan' ] ; pending ?: { proposal : WebPlanProposal ; missing ?: boolean } } ;
910const responseStatus = ( error : unknown ) => error && typeof error === 'object' && 'status' in error ? error . status : undefined ;
1011const scheduling = ( ) => < > < option value = "active" > { t ( '继续' ) } </ option > < option value = "paused" > { t ( '暂停' ) } </ option > < option value = "stopped" > { t ( '停止' ) } </ option > </ > ;
12+ type PlanEditorAccess = { runId ?: string ; status ?: WebRun [ 'status' ] ; connected : boolean ; readOnly : boolean ; edit ?: boolean } ;
13+ export function planEditUnavailableReason ( access : PlanEditorAccess ) : string | undefined {
14+ if ( ! access . runId ) return t ( '当前未打开此运行。草稿已保留。' ) ;
15+ if ( access . readOnly ) return t ( '当前仅可查看计划。草稿已保留。' ) ;
16+ if ( access . edit === false ) return t ( '当前没有此运行的计划编辑权限。草稿已保留。' ) ;
17+ if ( ! access . connected ) return t ( '连接中断,暂时无法预览或提交计划。草稿已保留。' ) ;
18+ if ( access . status === 'queued' ) return t ( '运行尚未开始,暂时无法修改计划。草稿已保留。' ) ;
19+ if ( access . status !== 'running' ) return t ( '运行已结束,无法再修改计划。草稿已保留。' ) ;
20+ }
1121
1222/** The dialog skeleton; form fields stay uncontrolled because partially typed lists must survive re-parsing the plan. */
1323function PlanDialog ( ) {
1424 return < >
1525 < header class = "collab-dialog-heading" > < div > < span class = "collab-eyebrow" > { t ( '研究计划' ) } </ span > < h2 > { t ( '调整下一步工作' ) } </ h2 > </ div > < button type = "button" class = "icon-button" data-plan-close aria-label = { t ( '关闭计划编辑器' ) } > ×</ button > </ header >
16- < div class = "collab-plan-content" > < p class = "collab-help" > { t ( '计划包含全部任务与路线。提交应用后,受影响证明的重新检查会单独报告。' ) } </ p > < div data-plan-versions > </ div >
26+ < div class = "collab-plan-content" > < p class = "collab-help" > { t ( '计划包含全部任务与路线。提交应用后,受影响证明的重新检查会单独报告。' ) } </ p > < div data-plan-availability role = "status" aria-live = "polite" > </ div > < div data-plan- versions> </ div >
1727 < div class = "collab-plan-common" >
1828 < label class = "collab-span-all" > { t ( '任务' ) } < select data-plan-task aria-label = { t ( '编辑的任务' ) } > </ select > </ label >
1929 < label > { t ( '任务类型' ) } < select data-plan-kind aria-label = { t ( '任务类型' ) } > < option value = "research" > { t ( '研究' ) } </ option > < option value = "sketch" > { t ( '证明草图' ) } </ option > < option value = "lemma" > { t ( '引理' ) } </ option > < option value = "goal" disabled > { t ( '原始目标' ) } </ option > </ select > </ label >
@@ -69,6 +79,8 @@ export class TaskPlanEditor {
6979 private taskId = '' ;
7080 private groupId = '' ;
7181 private routeId = '' ;
82+ private access = new Map < string , { reason ?: string } > ( ) ;
83+ private shownRunId ?: string ;
7284 private advanced : HTMLTextAreaElement ;
7385 constructor ( private api : WebApi , private observed : ( observation : WebObservation ) => void ) {
7486 this . dialog . setAttribute ( 'aria-label' , t ( '编辑完整研究计划' ) ) ;
@@ -108,6 +120,31 @@ export class TaskPlanEditor {
108120 if ( this . runId === runId ) { this . runId = '' ; this . observation = undefined ; this . dialog . close ( ) ; }
109121 }
110122 private owns ( runId : string , draft : PlanDraft ) { return ! this . disposed && this . drafts . get ( runId ) === draft ; }
123+ /** Polling can revoke operations while this dialog and its local draft remain open. */
124+ updateAccess ( access : PlanEditorAccess ) {
125+ if ( this . disposed ) return ;
126+ let changed = this . shownRunId !== access . runId ;
127+ this . shownRunId = access . runId ;
128+ if ( access . runId ) {
129+ const reason = planEditUnavailableReason ( access ) , previous = this . access . get ( access . runId ) ;
130+ if ( ! previous || previous . reason !== reason ) {
131+ this . access . set ( access . runId , { reason } ) ; changed = true ;
132+ const draft = this . drafts . get ( access . runId ) ;
133+ if ( draft ) {
134+ draft . previewText = undefined ;
135+ if ( ! reason && previous ?. reason ) draft . notice = t ( '现在可以继续调整计划。请重新预览后提交。' ) ;
136+ }
137+ }
138+ }
139+ if ( changed && this . dialog . open && this . drafts . has ( this . runId ) ) this . renderStatus ( ) ;
140+ }
141+ private editUnavailableReason ( runId = this . runId ) {
142+ const access = this . access . get ( runId ) ;
143+ return access ? access . reason : t ( '当前未打开此运行。草稿已保留。' ) ;
144+ }
145+ private currentEditUnavailableReason ( ) {
146+ return this . shownRunId !== this . runId ? t ( '当前未打开此运行。草稿已保留。' ) : this . editUnavailableReason ( ) ;
147+ }
111148 update ( observation : WebObservation ) {
112149 if ( this . disposed ) return ;
113150 const draft = this . drafts . get ( observation . webRunId ) ;
@@ -337,7 +374,8 @@ export class TaskPlanEditor {
337374 { latest && latest !== base && < strong > { t ( '版本已变化 · 本地修改已保留' ) } </ strong > } </ div > ) ;
338375 }
339376 private renderStatus ( ) {
340- const draft = this . draft ( ) , receipt = draft . receipt ;
377+ const draft = this . draft ( ) , receipt = draft . receipt , unavailable = this . currentEditUnavailableReason ( ) ;
378+ renderSection ( this . dialog . querySelector ( '[data-plan-availability]' ) ! , unavailable ? < p class = "collab-notice" > { unavailable } </ p > : null ) ;
341379 renderSection ( this . dialog . querySelector ( '[data-plan-receipt]' ) ! , < >
342380 { draft . error && < p class = "collab-error" > { draft . error } </ p > }
343381 { draft . notice && < p class = "collab-help" > { draft . notice } </ p > }
@@ -365,8 +403,10 @@ export class TaskPlanEditor {
365403 for ( const control of this . dialog . querySelectorAll < HTMLInputElement | HTMLSelectElement > ( '[data-plan-route-fields] input,[data-plan-route-fields] select' ) ) control . disabled ||= ! hasRoute ;
366404 this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-remove-route]' ) ! . disabled ||= ! hasRoute ;
367405 const invalidField = Boolean ( this . dialog . querySelector ( 'input:invalid' ) ) ;
368- this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-preview]' ) ! . disabled ||= ! plan || invalidField ;
369- this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-apply]' ) ! . disabled = Boolean ( draft . busy || draft . pending || ! plan || invalidField ) || ! receipt || draft . previewText !== draft . text || ! [ 'validated' , 'previewed' , 'ready' ] . includes ( receipt . status ) ;
406+ this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-preview]' ) ! . disabled ||= Boolean ( unavailable ) || ! plan || invalidField ;
407+ this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-rebase]' ) ! . disabled ||= Boolean ( unavailable ) ;
408+ this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-retry]' ) ! . disabled ||= Boolean ( unavailable ) ;
409+ this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-apply]' ) ! . disabled = Boolean ( unavailable || draft . busy || draft . pending || ! plan || invalidField ) || ! receipt || draft . previewText !== draft . text || ! [ 'validated' , 'previewed' , 'ready' ] . includes ( receipt . status ) ;
370410 this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-receipt-refresh]' ) ! . hidden = ! draft . pending ;
371411 this . dialog . querySelector < HTMLButtonElement > ( '[data-plan-retry]' ) ! . hidden = ! draft . pending ?. missing ;
372412 this . renderVersions ( ) ;
@@ -377,7 +417,7 @@ export class TaskPlanEditor {
377417 this . observed ( observation ) ;
378418 }
379419 private async readLatest ( rebase : boolean ) {
380- const runId = this . runId , draft = this . draft ( ) ; if ( draft . busy ) return ;
420+ const runId = this . runId , draft = this . draft ( ) , access = this . access . get ( runId ) ; if ( draft . busy || rebase && this . currentEditUnavailableReason ( ) ) return ;
381421 let proposal : WebPlanProposal | undefined ; draft . busy = true ; this . renderStatus ( ) ;
382422 try {
383423 if ( rebase ) proposal = this . parse ( ) ;
@@ -387,17 +427,17 @@ export class TaskPlanEditor {
387427 this . receive ( runId , observation ) ;
388428 const latest = draft . observation ! ;
389429 draft . comparison = latest . plan ;
390- if ( proposal ) {
430+ if ( proposal && access === this . access . get ( runId ) ) {
391431 proposal . baseVersion = latest . plan ! . baseVersion ; proposal . id = commandId ( ) ;
392432 draft . text = jsonText ( proposal ) ; draft . previewText = undefined ; draft . receipt = undefined ; draft . error = undefined ; draft . submitted = false ;
393433 if ( this . runId === runId ) { this . advanced . value = draft . text ; this . renderCommon ( ) ; }
394- }
434+ } else proposal = undefined ;
395435 } catch ( error ) { if ( ! this . owns ( runId , draft ) ) return ; draft . error = errorText ( error ) ; proposal = undefined ; }
396436 finally { draft . busy = false ; if ( this . owns ( runId , draft ) && this . runId === runId ) this . renderStatus ( ) ; }
397437 if ( proposal ) await this . requestPlan ( runId , draft , proposal , false ) ;
398438 }
399439 private async submit ( apply : boolean ) {
400- const runId = this . runId , draft = this . draft ( ) ; if ( draft . busy || apply && draft . pending ) return ;
440+ const runId = this . runId , draft = this . draft ( ) ; if ( draft . busy || this . currentEditUnavailableReason ( ) || apply && draft . pending ) return ;
401441 try {
402442 const proposal = this . parse ( ) ;
403443 await this . requestPlan ( this . runId , draft , proposal , apply ) ;
@@ -420,7 +460,8 @@ export class TaskPlanEditor {
420460 affectedTaskIds : edit . affectedTasks ?? receipt . affectedTaskIds } : receipt ;
421461 }
422462 private async requestPlan ( runId : string , draft : PlanDraft , proposal : WebPlanProposal , apply : boolean ) {
423- if ( ! this . owns ( runId , draft ) ) return ;
463+ if ( ! this . owns ( runId , draft ) || this . editUnavailableReason ( runId ) ) return ;
464+ const access = this . access . get ( runId ) ;
424465 const submittedText = draft . text ; draft . busy = true ; draft . error = undefined ; draft . notice = undefined ;
425466 const pending = apply ? { proposal : structuredClone ( proposal ) } : undefined ;
426467 if ( apply ) { draft . submitted = true ; draft . pending = pending ; }
@@ -431,7 +472,7 @@ export class TaskPlanEditor {
431472 if ( receipt . id !== proposal . id ) throw new Error ( t ( '命令回执身份不匹配' ) ) ;
432473 if ( apply ) this . recordReceipt ( runId , draft , receipt ) ;
433474 else { if ( receipt . observation ) this . receive ( runId , receipt . observation ) ; draft . receipt = this . observedReceipt ( draft , receipt ) ; }
434- draft . previewText = apply ? undefined : submittedText ;
475+ draft . previewText = apply || access !== this . access . get ( runId ) ? undefined : submittedText ;
435476 if ( apply && draft . pending ) await this . readReceipt ( runId , draft ) ;
436477 } catch ( error ) {
437478 if ( ! this . owns ( runId , draft ) ) return ;
@@ -455,7 +496,7 @@ export class TaskPlanEditor {
455496 }
456497 }
457498 private async recover ( retryMissing : boolean ) {
458- const runId = this . runId , draft = this . draft ( ) ; if ( draft . busy || ! draft . pending ) return ;
499+ const runId = this . runId , draft = this . draft ( ) ; if ( draft . busy || ! draft . pending || retryMissing && this . currentEditUnavailableReason ( ) ) return ;
459500 const proposal = draft . pending . proposal ; draft . busy = true ; this . renderStatus ( ) ;
460501 let retry = false ;
461502 try { const found = await this . readReceipt ( runId , draft ) ; retry = retryMissing && ! found && Boolean ( draft . pending ?. missing ) ; }
0 commit comments