@@ -47,6 +47,7 @@ import {
4747 AlertState ,
4848 BuilderChartConfigWithDateRange ,
4949 ChartConfigWithDateRange ,
50+ ChartVariable ,
5051 DashboardContainer as DashboardContainerSchema ,
5152 DashboardFilter ,
5253 DisplayType ,
@@ -60,6 +61,7 @@ import {
6061 SQLInterval ,
6162 TSource ,
6263} from '@hyperdx/common-utils/dist/types' ;
64+ import { filterReferencedVariables } from '@hyperdx/common-utils/dist/variables' ;
6365import {
6466 ActionIcon ,
6567 Alert ,
@@ -386,6 +388,7 @@ const Tile = forwardRef(
386388 granularity,
387389 onTimeRangeSelect,
388390 filters,
391+ variables,
389392 showAlertAnnotations,
390393 isLive,
391394 readOnly,
@@ -414,6 +417,7 @@ const Tile = forwardRef(
414417 granularity : SQLInterval | undefined ;
415418 onTimeRangeSelect : ( start : Date , end : Date ) => void ;
416419 filters ?: Filter [ ] ;
420+ variables ?: ChartVariable [ ] ;
417421 // When true, draw alert firing/recovery annotations on this tile's chart.
418422 showAlertAnnotations ?: boolean ;
419423 isLive ?: boolean ;
@@ -530,6 +534,25 @@ const Tile = forwardRef(
530534 displayTypeRequiresSource ( chart . config . displayType ) &&
531535 ! chart . config . source ;
532536
537+ // `variables` is a new reference every time the dashboard's filter change. To ensure
538+ // `tileVariables` is stable unless the tile's *referenced variables* actually change,
539+ // we serialize the referenced subset and use changes in the serialized value to drive
540+ // changes to `tileVariables`.
541+ const serializedTileVariables = useMemo (
542+ ( ) =>
543+ ! ! variables && isRawSqlSavedChartConfig ( chart . config )
544+ ? JSON . stringify ( filterReferencedVariables ( chart . config , variables ) )
545+ : undefined ,
546+ [ chart . config , variables ] ,
547+ ) ;
548+ const tileVariables = useMemo < ChartVariable [ ] | undefined > (
549+ ( ) =>
550+ serializedTileVariables
551+ ? JSON . parse ( serializedTileVariables )
552+ : undefined ,
553+ [ serializedTileVariables ] ,
554+ ) ;
555+
533556 useEffect ( ( ) => {
534557 if ( isPromqlSavedChartConfig ( chart . config ) ) {
535558 if ( source != null ) {
@@ -552,6 +575,7 @@ const Tile = forwardRef(
552575 dateRange,
553576 granularity,
554577 filters,
578+ variables : tileVariables ,
555579 } ) ;
556580 } else if ( source != null ) {
557581 setQueriedConfig ( {
@@ -570,6 +594,7 @@ const Tile = forwardRef(
570594 dateRange,
571595 granularity,
572596 filters,
597+ variables : tileVariables ,
573598 } ) ;
574599 }
575600
@@ -614,7 +639,7 @@ const Tile = forwardRef(
614639 } ) ;
615640 }
616641 }
617- } , [ source , chart , dateRange , granularity , filters ] ) ;
642+ } , [ source , chart , dateRange , granularity , filters , tileVariables ] ) ;
618643
619644 const [ hovered , setHovered ] = useState ( false ) ;
620645
@@ -1472,13 +1497,15 @@ const EditTileModal = ({
14721497 onSave,
14731498 isSaving,
14741499 dateRange,
1500+ variables,
14751501} : {
14761502 dashboardId ?: string ;
14771503 chart : Tile | undefined ;
14781504 onClose : ( ) => void ;
14791505 dateRange : [ Date , Date ] ;
14801506 isSaving ?: boolean ;
14811507 onSave : ( chart : Tile ) => void ;
1508+ variables ?: ChartVariable [ ] ;
14821509} ) => {
14831510 const contextZIndex = useZIndex ( ) ;
14841511 const modalZIndex = contextZIndex + 10 ;
@@ -1533,6 +1560,7 @@ const EditTileModal = ({
15331560 < EditTimeChartForm
15341561 dashboardId = { dashboardId }
15351562 chartConfig = { chart . config }
1563+ variables = { variables }
15361564 dateRange = { dateRange }
15371565 isSaving = { isSaving }
15381566 onSave = { config => {
@@ -1783,6 +1811,7 @@ function DBDashboardPage({ presetConfig }: { presetConfig?: Dashboard }) {
17831811 setFilterQueries,
17841812 ignoredFilterExpressions,
17851813 getFilterQueriesForSource,
1814+ variables,
17861815 } = useDashboardFilters ( filters ) ;
17871816
17881817 const { isLoading : isVariablesFlagLoading , isVariablesEnabled } =
@@ -2234,6 +2263,7 @@ function DBDashboardPage({ presetConfig }: { presetConfig?: Dashboard }) {
22342263 } ,
22352264 ...getFilterQueriesForSource ( tileSourceId ) ,
22362265 ] }
2266+ variables = { showFilterVariableOptions ? variables : undefined }
22372267 onTimeRangeSelect = { onTimeRangeSelect }
22382268 showAlertAnnotations = { showAlertAnnotations }
22392269 isHighlighted = { highlightedTileId === chart . id }
@@ -2334,6 +2364,8 @@ function DBDashboardPage({ presetConfig }: { presetConfig?: Dashboard }) {
23342364 onTimeRangeSelect ,
23352365 showAlertAnnotations ,
23362366 getFilterQueriesForSource ,
2367+ showFilterVariableOptions ,
2368+ variables ,
23372369 moveTargetContainers ,
23382370 handleMoveTileToGroup ,
23392371 selectedTileIds ,
@@ -3012,6 +3044,7 @@ function DBDashboardPage({ presetConfig }: { presetConfig?: Dashboard }) {
30123044 if ( ! isSaving ) setEditedTile ( undefined ) ;
30133045 } }
30143046 dateRange = { searchedTimeRange }
3047+ variables = { showFilterVariableOptions ? variables : undefined }
30153048 isSaving = { isSaving }
30163049 onSave = { newChart => {
30173050 if ( dashboard == null ) {
0 commit comments