Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/containers/Tenant/Query/QueryResult/QueryResultViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
// Query result viewer with tab persistence functionality
Copy link

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This single-line comment doesn't add meaningful documentation value. Consider either removing it or expanding it into a proper JSDoc comment that explains the component's purpose, props, and key functionality.

Suggested change
// Query result viewer with tab persistence functionality
/**
* QueryResultViewer is a React component that displays the results of a query execution.
* It supports multiple result views (e.g., result sets, schema, JSON, etc.) with tab persistence functionality.
*
* Props:
* - `result` (QueryResult): The query result data to display.
* - `resultType` (QueryAction, optional): The type of query result (default: 'execute').
* - `isResultsCollapsed` (boolean, optional): Whether the results section is collapsed.
* - `theme` (string, optional): The theme to apply to the component.
* - `tenantName` (string): The name of the tenant associated with the query.
* - `queryText` (string, optional): The text of the executed query.
* - `tableSettings` (Partial<Settings>, optional): Settings for the result table display.
* - `onCollapseResults` (VoidFunction): Callback to collapse the results section.
* - `onExpandResults` (VoidFunction): Callback to expand the results section.
*/

Copilot uses AI. Check for mistakes.

import type {Settings} from '@gravity-ui/react-data-table';
import type {ControlGroupOption} from '@gravity-ui/uikit';
Expand All @@ -10,13 +11,14 @@ import {Illustration} from '../../../../components/Illustration';
import {LoaderWrapper} from '../../../../components/LoaderWrapper/LoaderWrapper';
import {QueryExecutionStatus} from '../../../../components/QueryExecutionStatus';
import {disableFullscreen} from '../../../../store/reducers/fullscreen';
import {selectResultTab, setResultTab} from '../../../../store/reducers/query/query';
import type {QueryResult} from '../../../../store/reducers/query/types';
import type {ValueOf} from '../../../../types/common';
import type {QueryAction} from '../../../../types/store/query';
import {cn} from '../../../../utils/cn';
import {USE_SHOW_PLAN_SVG_KEY} from '../../../../utils/constants';
import {getStringifiedData} from '../../../../utils/dataFormatters/dataFormatters';
import {useSetting, useTypedDispatch} from '../../../../utils/hooks';
import {useSetting, useTypedDispatch, useTypedSelector} from '../../../../utils/hooks';
import {PaneVisibilityToggleButtons} from '../../utils/paneVisibilityToggleHelpers';
import {QuerySettingsBanner} from '../QuerySettingsBanner/QuerySettingsBanner';
import {QueryStoppedBanner} from '../QueryStoppedBanner/QueryStoppedBanner';
Expand Down Expand Up @@ -98,27 +100,43 @@ export function QueryResultViewer({
onExpandResults,
}: ExecuteResultProps) {
const dispatch = useTypedDispatch();
const selectedTabs = useTypedSelector(selectResultTab);

const isExecute = resultType === 'execute';
const isExplain = resultType === 'explain';

const [selectedResultSet, setSelectedResultSet] = React.useState(0);
const [activeSection, setActiveSection] = React.useState<SectionID>(() => {
return isExecute ? RESULT_OPTIONS_IDS.result : RESULT_OPTIONS_IDS.schema;
});
const [useShowPlanToSvg] = useSetting<boolean>(USE_SHOW_PLAN_SVG_KEY);

// Get the saved tab for the current query type, or use default
const getDefaultSection = (): SectionID => {
return isExecute ? RESULT_OPTIONS_IDS.result : RESULT_OPTIONS_IDS.schema;
};

const activeSection: SectionID = React.useMemo(() => {
const savedTab = selectedTabs?.[resultType];
if (savedTab) {
// Validate that the saved tab is valid for the current result type
const validSections = isExecute ? EXECUTE_SECTIONS : EXPLAIN_SECTIONS;
if (validSections.includes(savedTab as SectionID)) {
return savedTab as SectionID;
}
}
return getDefaultSection();
}, [selectedTabs, resultType, isExecute]);

const {error, isLoading, data = {}} = result;
const {preparedPlan, simplifiedPlan, stats, resultSets, ast} = data;

React.useEffect(() => {
Copy link

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This useEffect only runs cleanup logic and doesn't depend on any values, but it's placed in the middle of the component logic. Consider moving it closer to other useEffect hooks or to the end of the component for better code organization.

Copilot uses AI. Check for mistakes.
if (resultType === 'execute' && !EXECUTE_SECTIONS.includes(activeSection)) {
setActiveSection('result');
}
if (resultType === 'explain' && !EXPLAIN_SECTIONS.includes(activeSection)) {
setActiveSection('schema');
}
}, [activeSection, resultType]);
return () => {
dispatch(disableFullscreen());
};
}, [dispatch]);

const onSelectSection = (value: SectionID) => {
dispatch(setResultTab({queryType: resultType, tabId: value}));
};

const radioButtonOptions: ControlGroupOption<SectionID>[] = React.useMemo(() => {
let sections: SectionID[] = [];
Expand All @@ -137,16 +155,6 @@ export function QueryResultViewer({
});
}, [isExecute, isExplain]);

React.useEffect(() => {
return () => {
dispatch(disableFullscreen());
};
}, [dispatch]);

const onSelectSection = (value: SectionID) => {
setActiveSection(value);
};

const getStatsToCopy = () => {
switch (activeSection) {
case RESULT_OPTIONS_IDS.result: {
Expand Down
55 changes: 55 additions & 0 deletions src/store/reducers/query/__test__/resultTab.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import queryReducer, {setResultTab} from '../query';
import type {QueryState} from '../types';

describe('resultTab functionality', () => {
const initialState: QueryState = {
input: '',
history: {
queries: [],
currentIndex: -1,
},
};

test('should set result tab for explain query type', () => {
const action = setResultTab({queryType: 'explain', tabId: 'json'});
const newState = queryReducer(initialState, action);

expect(newState.selectedResultTab).toEqual({
explain: 'json',
});
});

test('should set result tab for execute query type', () => {
const action = setResultTab({queryType: 'execute', tabId: 'stats'});
const newState = queryReducer(initialState, action);

expect(newState.selectedResultTab).toEqual({
execute: 'stats',
});
});

test('should maintain separate tabs for different query types', () => {
const state1 = queryReducer(
initialState,
setResultTab({queryType: 'explain', tabId: 'json'}),
);
const state2 = queryReducer(state1, setResultTab({queryType: 'execute', tabId: 'stats'}));

expect(state2.selectedResultTab).toEqual({
explain: 'json',
execute: 'stats',
});
});

test('should update existing tab preference', () => {
const state1 = queryReducer(
initialState,
setResultTab({queryType: 'explain', tabId: 'json'}),
);
const state2 = queryReducer(state1, setResultTab({queryType: 'explain', tabId: 'ast'}));

expect(state2.selectedResultTab).toEqual({
explain: 'ast',
});
});
});
79 changes: 79 additions & 0 deletions src/store/reducers/query/__test__/tabPersistence.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import queryReducer, {setResultTab} from '../query';
import type {QueryState} from '../types';

describe('QueryResultViewer tab persistence integration', () => {
const initialState: QueryState = {
input: '',
history: {
queries: [],
currentIndex: -1,
},
};

it('should save and retrieve tab selection for explain queries', () => {
// Test that we can set and get the tab preference
let state = queryReducer(initialState, setResultTab({queryType: 'explain', tabId: 'json'}));

expect(state.selectedResultTab).toEqual({
explain: 'json',
});

// Test updating the same query type
state = queryReducer(state, setResultTab({queryType: 'explain', tabId: 'ast'}));

expect(state.selectedResultTab).toEqual({
explain: 'ast',
});
});

it('should save and retrieve tab selection for execute queries', () => {
const state = queryReducer(
initialState,
setResultTab({queryType: 'execute', tabId: 'stats'}),
);

expect(state.selectedResultTab).toEqual({
execute: 'stats',
});
});

it('should maintain separate preferences for different query types', () => {
let state = initialState;

// Set explain tab
state = queryReducer(state, setResultTab({queryType: 'explain', tabId: 'json'}));
expect(state.selectedResultTab).toEqual({
explain: 'json',
});

// Set execute tab - should not override explain
state = queryReducer(state, setResultTab({queryType: 'execute', tabId: 'stats'}));
expect(state.selectedResultTab).toEqual({
explain: 'json',
execute: 'stats',
});

// Update explain tab - should not affect execute
state = queryReducer(state, setResultTab({queryType: 'explain', tabId: 'ast'}));
expect(state.selectedResultTab).toEqual({
explain: 'ast',
execute: 'stats',
});
});

it('should handle multiple updates to the same query type', () => {
let state = initialState;

// Set initial value
state = queryReducer(state, setResultTab({queryType: 'explain', tabId: 'schema'}));
expect(state.selectedResultTab?.explain).toBe('schema');

// Update to different tab
state = queryReducer(state, setResultTab({queryType: 'explain', tabId: 'json'}));
expect(state.selectedResultTab?.explain).toBe('json');

// Update again
state = queryReducer(state, setResultTab({queryType: 'explain', tabId: 'ast'}));
expect(state.selectedResultTab?.explain).toBe('ast');
});
});
13 changes: 13 additions & 0 deletions src/store/reducers/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ const slice = createSlice({
setQueryHistoryFilter: (state, action: PayloadAction<string>) => {
state.history.filter = action.payload;
},
setResultTab: (
state,
action: PayloadAction<{queryType: 'execute' | 'explain'; tabId: string}>,
) => {
const {queryType, tabId} = action.payload;
if (!state.selectedResultTab) {
state.selectedResultTab = {};
}
state.selectedResultTab[queryType] = tabId;
},
setStreamSession: setStreamSessionReducer,
addStreamingChunks: addStreamingChunksReducer,
setStreamQueryResponse: setStreamQueryResponseReducer,
Expand All @@ -149,6 +159,7 @@ const slice = createSlice({
selectUserInput: (state) => state.input,
selectIsDirty: (state) => state.isDirty,
selectQueriesHistoryCurrentIndex: (state) => state.history?.currentIndex,
selectResultTab: (state) => state.selectedResultTab,
},
});

Expand Down Expand Up @@ -177,6 +188,7 @@ export const {
setStreamQueryResponse,
setStreamSession,
setIsDirty,
setResultTab,
} = slice.actions;

export const {
Expand All @@ -187,6 +199,7 @@ export const {
selectResult,
selectUserInput,
selectIsDirty,
selectResultTab,
} = slice.selectors;

interface SendQueryParams extends QueryRequestParams {
Expand Down
4 changes: 4 additions & 0 deletions src/store/reducers/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ export interface QueryState {
filter?: string;
};
tenantPath?: string;
selectedResultTab?: {
execute?: string;
explain?: string;
};
}
Loading