1- import { useState , useEffect , useMemo , useCallback } from 'react' ;
1+ import { useState , useEffect , useMemo , useCallback , useRef , useImperativeHandle , forwardRef } from 'react' ;
22import { ComposedChart , Bar , Line , XAxis , YAxis , CartesianGrid , Tooltip , ErrorBar , ResponsiveContainer , Legend , Cell , ReferenceLine , LabelList } from 'recharts' ;
33import * as api from '../api/cdm' ;
44import { timeWork } from '../debugLog' ;
@@ -28,6 +28,8 @@ function formatYTick(value) {
2828// Compact value for bar labels — max 4 significant digits
2929function formatBarLabel ( v ) {
3030 if ( v == null ) return '' ;
31+ v = Number ( v ) ;
32+ if ( isNaN ( v ) ) return '' ;
3133 var abs = Math . abs ( v ) ;
3234 if ( abs === 0 ) return '0' ;
3335 if ( abs >= 1000000 ) return ( v / 1000000 ) . toPrecision ( 3 ) + 'M' ;
@@ -39,6 +41,8 @@ function formatBarLabel(v) {
3941
4042function formatValue ( v ) {
4143 if ( v == null ) return '' ;
44+ v = Number ( v ) ;
45+ if ( isNaN ( v ) ) return '' ;
4246 if ( Math . abs ( v ) >= 1000 ) return v . toFixed ( 0 ) ;
4347 if ( Math . abs ( v ) >= 1 ) return v . toFixed ( 2 ) ;
4448 return v . toPrecision ( 3 ) ;
@@ -488,7 +492,7 @@ function buildDimOptions(iterations) {
488492 return opts ;
489493}
490494
491- export default function CompareView ( { selected, groupByList, setGroupByList, hiddenFields, setHiddenFields } ) {
495+ const CompareView = forwardRef ( function CompareView ( { selected, groupByList, setGroupByList, hiddenFields, setHiddenFields, restoredMetrics } , ref ) {
492496 var [ metricValues , setMetricValues ] = useState ( { } ) ;
493497 var [ loading , setLoading ] = useState ( false ) ;
494498 var [ supplementalMetrics , setSupplementalMetrics ] = useState ( [ ] ) ; // [{ source, type, values: {iterId: {mean,...}} }]
@@ -505,6 +509,12 @@ export default function CompareView({ selected, groupByList, setGroupByList, hid
505509 return Array . from ( selected . values ( ) ) ;
506510 } , [ selected ] ) ;
507511
512+ useImperativeHandle ( ref , function ( ) {
513+ return {
514+ getSupplementalMetrics : function ( ) { return supplementalMetrics ; } ,
515+ } ;
516+ } , [ supplementalMetrics ] ) ;
517+
508518 // Helper to get run IDs and date range from iterations
509519 function getRunContext ( ) {
510520 var runIdSet = new Set ( ) ;
@@ -574,6 +584,44 @@ export default function CompareView({ selected, groupByList, setGroupByList, hid
574584 }
575585 } , [ iterations . length > 0 && dimOptions . length > 1 ] ) ;
576586
587+ // Restore supplemental metrics from URL state
588+ var restoredMetricsApplied = useRef ( false ) ;
589+ useEffect ( function ( ) {
590+ if ( restoredMetricsApplied . current ) return ;
591+ if ( ! restoredMetrics || restoredMetrics . length === 0 ) return ;
592+ if ( iterations . length === 0 ) return ;
593+ restoredMetricsApplied . current = true ;
594+ var ctx = getRunContext ( ) ;
595+ restoredMetrics . forEach ( function ( rm ) {
596+ var bestIdx = computeBestSampleIndex ( ) ;
597+ var sIdx = rm . sampleIndex != null ? rm . sampleIndex : bestIdx ;
598+ timeWork ( 'Restore ' + rm . source + '::' + rm . type , function ( ) {
599+ return api . getSupplementalMetric ( {
600+ iterations : ctx . iterations , start : ctx . start , end : ctx . end ,
601+ source : rm . source , type : rm . type ,
602+ breakout : rm . breakouts || [ ] ,
603+ filter : rm . filter || null ,
604+ sampleIndex : sIdx ,
605+ } ) ;
606+ } ) . then ( function ( res ) {
607+ setSupplementalMetrics ( function ( prev ) {
608+ return prev . concat ( [ {
609+ source : rm . source ,
610+ type : rm . type ,
611+ values : res . values || { } ,
612+ display : rm . display || 'panel' ,
613+ chartType : rm . chartType || 'bar' ,
614+ filter : rm . filter || '' ,
615+ sampleIndex : sIdx ,
616+ breakouts : rm . breakouts || [ ] ,
617+ remainingBreakouts : res . remainingBreakouts || [ ] ,
618+ loading : false ,
619+ } ] ) ;
620+ } ) ;
621+ } ) ;
622+ } ) ;
623+ } , [ iterations . length > 0 ] ) ;
624+
577625 var handleShowAddMetric = useCallback ( function ( ) {
578626 setShowAddMetric ( true ) ;
579627 setAddMetricSource ( '' ) ;
@@ -1936,4 +1984,6 @@ export default function CompareView({ selected, groupByList, setGroupByList, hid
19361984
19371985 </ div >
19381986 ) ;
1939- }
1987+ } ) ;
1988+
1989+ export default CompareView ;
0 commit comments