@@ -10,13 +10,14 @@ import {Illustration} from '../../../../components/Illustration';
1010import { LoaderWrapper } from '../../../../components/LoaderWrapper/LoaderWrapper' ;
1111import { QueryExecutionStatus } from '../../../../components/QueryExecutionStatus' ;
1212import { disableFullscreen } from '../../../../store/reducers/fullscreen' ;
13+ import { selectResultTab , setResultTab } from '../../../../store/reducers/query/query' ;
1314import type { QueryResult } from '../../../../store/reducers/query/types' ;
1415import type { ValueOf } from '../../../../types/common' ;
1516import type { QueryAction } from '../../../../types/store/query' ;
1617import { cn } from '../../../../utils/cn' ;
1718import { USE_SHOW_PLAN_SVG_KEY } from '../../../../utils/constants' ;
1819import { getStringifiedData } from '../../../../utils/dataFormatters/dataFormatters' ;
19- import { useSetting , useTypedDispatch } from '../../../../utils/hooks' ;
20+ import { useSetting , useTypedDispatch , useTypedSelector } from '../../../../utils/hooks' ;
2021import { PaneVisibilityToggleButtons } from '../../utils/paneVisibilityToggleHelpers' ;
2122import { QuerySettingsBanner } from '../QuerySettingsBanner/QuerySettingsBanner' ;
2223import { QueryStoppedBanner } from '../QueryStoppedBanner/QueryStoppedBanner' ;
@@ -98,27 +99,43 @@ export function QueryResultViewer({
9899 onExpandResults,
99100} : ExecuteResultProps ) {
100101 const dispatch = useTypedDispatch ( ) ;
102+ const selectedTabs = useTypedSelector ( selectResultTab ) ;
101103
102104 const isExecute = resultType === 'execute' ;
103105 const isExplain = resultType === 'explain' ;
104106
105107 const [ selectedResultSet , setSelectedResultSet ] = React . useState ( 0 ) ;
106- const [ activeSection , setActiveSection ] = React . useState < SectionID > ( ( ) => {
107- return isExecute ? RESULT_OPTIONS_IDS . result : RESULT_OPTIONS_IDS . schema ;
108- } ) ;
109108 const [ useShowPlanToSvg ] = useSetting < boolean > ( USE_SHOW_PLAN_SVG_KEY ) ;
110109
110+ // Get the saved tab for the current query type, or use default
111+ const getDefaultSection = ( ) : SectionID => {
112+ return isExecute ? RESULT_OPTIONS_IDS . result : RESULT_OPTIONS_IDS . schema ;
113+ } ;
114+
115+ const activeSection : SectionID = React . useMemo ( ( ) => {
116+ const savedTab = selectedTabs ?. [ resultType ] ;
117+ if ( savedTab ) {
118+ // Validate that the saved tab is valid for the current result type
119+ const validSections = isExecute ? EXECUTE_SECTIONS : EXPLAIN_SECTIONS ;
120+ if ( validSections . includes ( savedTab as SectionID ) ) {
121+ return savedTab as SectionID ;
122+ }
123+ }
124+ return getDefaultSection ( ) ;
125+ } , [ selectedTabs , resultType , isExecute ] ) ;
126+
111127 const { error, isLoading, data = { } } = result ;
112128 const { preparedPlan, simplifiedPlan, stats, resultSets, ast} = data ;
113129
114130 React . useEffect ( ( ) => {
115- if ( resultType === 'execute' && ! EXECUTE_SECTIONS . includes ( activeSection ) ) {
116- setActiveSection ( 'result' ) ;
117- }
118- if ( resultType === 'explain' && ! EXPLAIN_SECTIONS . includes ( activeSection ) ) {
119- setActiveSection ( 'schema' ) ;
120- }
121- } , [ activeSection , resultType ] ) ;
131+ return ( ) => {
132+ dispatch ( disableFullscreen ( ) ) ;
133+ } ;
134+ } , [ dispatch ] ) ;
135+
136+ const onSelectSection = ( value : SectionID ) => {
137+ dispatch ( setResultTab ( { queryType : resultType , tabId : value } ) ) ;
138+ } ;
122139
123140 const radioButtonOptions : ControlGroupOption < SectionID > [ ] = React . useMemo ( ( ) => {
124141 let sections : SectionID [ ] = [ ] ;
@@ -137,16 +154,6 @@ export function QueryResultViewer({
137154 } ) ;
138155 } , [ isExecute , isExplain ] ) ;
139156
140- React . useEffect ( ( ) => {
141- return ( ) => {
142- dispatch ( disableFullscreen ( ) ) ;
143- } ;
144- } , [ dispatch ] ) ;
145-
146- const onSelectSection = ( value : SectionID ) => {
147- setActiveSection ( value ) ;
148- } ;
149-
150157 const getStatsToCopy = ( ) => {
151158 switch ( activeSection ) {
152159 case RESULT_OPTIONS_IDS . result : {
0 commit comments