1- import type { RefinementMismatchError } from "../../../types/diagnostics" ;
1+ import type { RefinementMismatchError , TranslationTable } from "../../../types/diagnostics" ;
22import type { VCSimplificationResult } from "../../../types/vc-implications" ;
33import { renderHighlightedExpression } from "../../highlighting" ;
44import { renderCodicon } from "../../icons" ;
55import { escapeHtml } from "../../utils" ;
66import { renderImplication , renderImplicationChange } from "./vc-changes" ;
77
8- const stepIndexes = new Map < string , number > ( ) ; // errorId => step index, preserved across re-renders
9- const simplificationSteps = new Map < string , VCSimplificationResult [ ] > ( ) ; // errorId => simplification steps
8+ // state to preserve across re-renders
9+ type VCState = {
10+ steps : VCSimplificationResult [ ] ;
11+ translationTable : TranslationTable ;
12+ stepIndex : number ;
13+ } ;
14+
15+ const vcStates = new Map < string , VCState > ( ) ; // errorId => VCState
1016
1117function renderStepButton ( errorId : string , step : "previous" | "next" , disabled : boolean ) : string {
1218 const label = `${ step === "previous" ? "Previous" : "Next" } simplification` ;
@@ -40,45 +46,49 @@ function renderStepHeader(
4046 ` ;
4147}
4248
43- function getTargetStepIndex ( errorId : string , step : string | null ) : number | undefined {
44- const steps = simplificationSteps . get ( errorId ) ;
45- if ( ! steps ) return ;
46-
47- const index = stepIndexes . get ( errorId ) ?? 0 ;
48- const targetIndex = step === "previous" ? index + 1 : step === "next" ? index - 1 : - 1 ;
49- if ( targetIndex < 0 || targetIndex >= steps . length ) return ;
49+ function getTargetStepIndex ( state : VCState , step : string | null ) : number | undefined {
50+ const targetIndex = step === "previous"
51+ ? state . stepIndex + 1
52+ : step === "next"
53+ ? state . stepIndex - 1
54+ : - 1 ;
55+ if ( targetIndex < 0 || targetIndex >= state . steps . length ) return ;
5056 return targetIndex ;
5157}
5258
5359function renderSelectedStep ( errorId : string , previousIndex ?: number ) : string {
54- const steps = simplificationSteps . get ( errorId ) ;
55- if ( ! steps ) return "" ;
60+ const state = vcStates . get ( errorId ) ;
61+ if ( ! state ) return "" ;
5662
57- const index = Math . min ( stepIndexes . get ( errorId ) ?? 0 , steps . length - 1 ) ;
58- const current = steps [ index ] ;
63+ const { steps , translationTable , stepIndex } = state ;
64+ const current = steps [ stepIndex ] ;
5965 const previous = previousIndex === undefined ? undefined : steps [ previousIndex ] ;
6066 const implication = previous
61- ? `<div class="vc-chain">${ renderImplicationChange ( previous . implication , current . implication ) } </div>`
62- : `<div class="vc-chain">${ renderImplication ( current . implication ) } </div>` ;
67+ ? `<div class="vc-chain">${ renderImplicationChange ( previous . implication , current . implication , translationTable ) } </div>`
68+ : `<div class="vc-chain">${ renderImplication ( current . implication , translationTable ) } </div>` ;
6369
6470 return /*html*/ `
65- ${ steps . length > 1 ? renderStepHeader ( errorId , current , index , steps . length ) : "" }
71+ ${ steps . length > 1 ? renderStepHeader ( errorId , current , stepIndex , steps . length ) : "" }
6672 ${ implication }
6773 ` ;
6874}
6975
70- export function handleVCImplicationStepClick ( target : Element ) : boolean {
76+ export function handleVCImplicationStepClick ( target : Element , onStepChanged ?: ( ) => void ) : boolean {
7177 const errorId = target . getAttribute ( "data-error-id" ) ;
7278 const step = target . getAttribute ( "data-vc-step" ) ;
7379 if ( ! errorId || ( target as HTMLButtonElement ) . disabled ) return false ;
7480
75- const currentIndex = stepIndexes . get ( errorId ) ?? 0 ;
76- const targetIndex = getTargetStepIndex ( errorId , step ) ;
81+ const state = vcStates . get ( errorId ) ;
82+ if ( ! state ) return false ;
83+
84+ const currentIndex = state . stepIndex ;
85+ const targetIndex = getTargetStepIndex ( state , step ) ;
7786 const container = target . closest ?.( ".vc-container" ) ;
7887 if ( targetIndex === undefined ) return false ;
7988
80- stepIndexes . set ( errorId , targetIndex ) ;
89+ state . stepIndex = targetIndex ;
8190 if ( container ) container . innerHTML = renderSelectedStep ( errorId , currentIndex ) ;
91+ onStepChanged ?.( ) ;
8292 return true ;
8393}
8494
@@ -98,10 +108,8 @@ export function renderVCImplication(
98108 for ( let current : VCSimplificationResult | null = result ; current ; current = current . origin ) {
99109 steps . push ( current ) ;
100110 }
101- simplificationSteps . set ( errorId , steps ) ;
102-
103- const index = Math . min ( stepIndexes . get ( errorId ) ?? 0 , steps . length - 1 ) ;
104- stepIndexes . set ( errorId , index ) ;
111+ const stepIndex = Math . min ( vcStates . get ( errorId ) ?. stepIndex ?? 0 , steps . length - 1 ) ;
112+ vcStates . set ( errorId , { steps, translationTable : error . translationTable , stepIndex } ) ;
105113
106114 return /*html*/ `
107115 <div class="container vc-container" data-error-id="${ errorId } ">
0 commit comments