@@ -17,7 +17,6 @@ import {
1717 displayTypeSupportsRawSqlAlerts ,
1818 isTimeSeriesDisplayType ,
1919} from '@hyperdx/common-utils/dist/core/utils' ;
20- import { getBlockingRequiredFilters } from '@hyperdx/common-utils/dist/dashboardFilterValues' ;
2120import {
2221 displayTypeRequiresSource ,
2322 isRawSqlSavedChartConfig ,
@@ -49,7 +48,7 @@ import {
4948 Text ,
5049 Textarea ,
5150} from '@mantine/core' ;
52- import { useDebouncedValue , useDisclosure , usePrevious } from '@mantine/hooks' ;
51+ import { useDisclosure , usePrevious } from '@mantine/hooks' ;
5352import { notifications } from '@mantine/notifications' ;
5453import {
5554 IconBracketsContain ,
@@ -118,10 +117,12 @@ import { ChartPreviewPanel } from './ChartPreviewPanel';
118117import { ChartSaveActions } from './ChartSaveActions' ;
119118import { ErrorNotificationMessage } from './ErrorNotificationMessage' ;
120119import { useBuilderToSqlConversion } from './useBuilderToSqlConversion' ;
120+ import { useDebouncedFormValues } from './useDebouncedFormValues' ;
121121import {
122122 buildChartConfigForExplanations ,
123123 computeDbTimeChartConfig ,
124124 displayTypeToActiveTab ,
125+ getPreviewBlockingFilters ,
125126 resolveTilePreviewFilters ,
126127 TABS_WITH_GENERATED_SQL ,
127128 withBroadcastFilters ,
@@ -411,6 +412,17 @@ export default function EditTimeChartForm({
411412 ? `Select a source for $${ sourceVariableName } in the filter bar to save this tile`
412413 : `The source selected for $${ sourceVariableName } can't be used with this chart type` ;
413414
415+ // A variable can select a source on another connection than the one a raw
416+ // SQL query runs on, which the dashboard blocks too
417+ const previewBlockedReason =
418+ isRawSqlInput &&
419+ usesReferenceVariable &&
420+ tableSource != null &&
421+ resolvedConnection != null &&
422+ tableSource . connection !== resolvedConnection
423+ ? "The selected data source is on a different connection than this tile's query. Select a source on the same connection."
424+ : undefined ;
425+
414426 const databaseName = tableSource ?. from . databaseName ;
415427 const tableName = tableSource ?. from . tableName ;
416428
@@ -619,37 +631,40 @@ export default function EditTimeChartForm({
619631 } ;
620632 } , [ queriedConfig , previewDashboardFilters ] ) ;
621633
634+ const debouncedForm = useDebouncedFormValues ( watch , getValues , 300 ) ;
635+
622636 // With no selection for a variable it references the tile has nothing to
623637 // query, so there may be no queried config to compute the block from.
624- const missingRequiredFilterNames = useMemo ( ( ) => {
625- if ( previewDashboardFilters ) {
626- return previewDashboardFilters . missingRequiredFilterNames ;
627- }
628- const unresolved = referenceVariableNames . filter (
629- name => ! referenceVariableValues . get ( name ) ,
630- ) ;
631- return unresolved . length > 0
632- ? getBlockingRequiredFilters ( unsatisfiedRequiredFilters ?? [ ] , {
633- referenceVariableNames : unresolved ,
634- consumesBroadcastFilters : false ,
635- } ) . map ( filter => filter . name )
636- : undefined ;
638+ const previewBlockingFilters = useMemo ( ( ) => {
639+ if ( previewDashboardFilters ) return previewDashboardFilters ;
640+ if (
641+ ! referenceVariableNames . some ( name => ! referenceVariableValues . get ( name ) )
642+ )
643+ return undefined ;
644+ return getPreviewBlockingFilters ( {
645+ config : convertFormStateToSavedChartConfig ( debouncedForm , tableSource ) ,
646+ sourceId : undefined ,
647+ variables,
648+ unsatisfiedRequiredFilters,
649+ applySelections : resolvedApplyFilters ,
650+ referenceVariableNames,
651+ } ) ;
637652 } , [
638653 previewDashboardFilters ,
639654 referenceVariableNames ,
640655 referenceVariableValues ,
656+ debouncedForm ,
657+ tableSource ,
658+ variables ,
641659 unsatisfiedRequiredFilters ,
660+ resolvedApplyFilters ,
642661 ] ) ;
643662
644663 const dbTimeChartConfig = useMemo (
645664 ( ) => computeDbTimeChartConfig ( previewConfig , alert ) ,
646665 [ previewConfig , alert ] ,
647666 ) ;
648667
649- // Casting because `useWatch` returns a deep partial type, but we know that the
650- // form state is complete due to default values set above.
651- const watchedForm = useWatch ( { control } ) as ChartEditorFormState ;
652- const [ debouncedForm ] = useDebouncedValue ( watchedForm , 300 ) ;
653668 const additionalAlertWarnings = useMemo ( ( ) => {
654669 if ( alert == null ) return [ ] ;
655670 const config = convertFormStateToSavedChartConfig (
@@ -1510,7 +1525,13 @@ export default function EditTimeChartForm({
15101525 showSampleEvents = { showSampleEvents }
15111526 showGeneratedPromql = { isPromqlInput }
15121527 dbTimeChartConfig = { dbTimeChartConfig }
1513- missingRequiredFilterNames = { missingRequiredFilterNames }
1528+ missingRequiredFilterNames = {
1529+ previewBlockingFilters ?. missingRequiredFilterNames
1530+ }
1531+ canPreviewWithoutFilters = {
1532+ previewBlockingFilters ?. canPreviewWithoutFilters
1533+ }
1534+ blockedReason = { previewBlockedReason }
15141535 setValue = { ( name , value ) => setValue ( name , value ) }
15151536 setDisplayType = { type =>
15161537 setValue ( 'displayType' , type , { shouldDirty : true } )
0 commit comments