@@ -506,18 +506,39 @@ export function QueryEditor({
506506
507507 const isLoading = fetcher . state === "submitting" || fetcher . state === "loading" ;
508508
509- // Create a stable key from columns to detect schema changes
510- const columnsKey = results ?. columns
511- ? results . columns . map ( ( c ) => ` ${ c . name } : ${ c . type } ` ) . join ( "," )
509+ // Stable string key of result column names (types excluded — they can vary between runs)
510+ const columnNamesKey = results ?. columns
511+ ? results . columns . map ( ( c ) => c . name ) . join ( "," )
512512 : "" ;
513513
514- // Reset chart config only when column schema actually changes
515- // This allows re-running queries with different WHERE clauses without losing config
514+ // Use a ref so the effect can read chartConfig without re-firing on every config tweak
515+ const chartConfigRef = useRef ( chartConfig ) ;
516+ chartConfigRef . current = chartConfig ;
517+
518+ // Reset chart config only when a column referenced by the current config is no
519+ // longer present in the results. This means:
520+ // - Re-running the same query preserves all settings
521+ // - Adding new columns preserves settings (existing config columns still exist)
522+ // - Removing/renaming a column used in the config triggers a reset
516523 useEffect ( ( ) => {
517- if ( columnsKey && ! initialChartConfig ) {
524+ if ( ! columnNamesKey || initialChartConfig ) return ;
525+
526+ const config = chartConfigRef . current ;
527+ const configColumns = [
528+ config . xAxisColumn ,
529+ ...config . yAxisColumns ,
530+ config . groupByColumn ,
531+ config . sortByColumn ,
532+ ] . filter ( ( col ) : col is string => col != null ) ;
533+
534+ // Nothing configured yet — ChartConfigPanel will auto-select defaults
535+ if ( configColumns . length === 0 ) return ;
536+
537+ const availableColumns = new Set ( columnNamesKey . split ( "," ) ) ;
538+ if ( configColumns . some ( ( col ) => ! availableColumns . has ( col ) ) ) {
518539 setChartConfig ( defaultChartConfig ) ;
519540 }
520- } , [ columnsKey , initialChartConfig ] ) ;
541+ } , [ columnNamesKey , initialChartConfig ] ) ;
521542
522543 const handleChartConfigChange = useCallback ( ( config : ChartConfiguration ) => {
523544 setChartConfig ( config ) ;
0 commit comments