Skip to content

Commit ccd1eda

Browse files
fix(SchemaTree): set required query mode for tree actions (#491)
1 parent 79ad7be commit ccd1eda

File tree

11 files changed

+134
-31
lines changed

11 files changed

+134
-31
lines changed

src/containers/Tenant/Query/QueryEditor/QueryEditor.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ import {
2424
DEFAULT_IS_QUERY_RESULT_COLLAPSED,
2525
DEFAULT_SIZE_RESULT_PANE_KEY,
2626
SAVED_QUERIES_KEY,
27-
QUERY_INITIAL_MODE_KEY,
2827
ENABLE_ADDITIONAL_QUERY_MODES,
2928
LAST_USED_QUERY_ACTION_KEY,
3029
} from '../../../../utils/constants';
31-
import {useSetting} from '../../../../utils/hooks';
32-
import {QUERY_ACTIONS, QUERY_MODES} from '../../../../utils/query';
30+
import {useSetting, useQueryModes} from '../../../../utils/hooks';
31+
import {QUERY_ACTIONS, QUERY_MODES, isNewQueryMode} from '../../../../utils/query';
3332

3433
import {
3534
PaneVisibilityActionTypes,
@@ -91,13 +90,12 @@ function QueryEditor(props) {
9190
const [resultType, setResultType] = useState(RESULT_TYPES.EXECUTE);
9291

9392
const [isResultLoaded, setIsResultLoaded] = useState(false);
94-
const [queryMode, setQueryMode] = useSetting(QUERY_INITIAL_MODE_KEY);
93+
const [queryMode, setQueryMode] = useQueryModes();
9594
const [enableAdditionalQueryModes] = useSetting(ENABLE_ADDITIONAL_QUERY_MODES);
9695
const [lastUsedQueryAction, setLastUsedQueryAction] = useSetting(LAST_USED_QUERY_ACTION_KEY);
9796

9897
useEffect(() => {
99-
const isNewQueryMode = queryMode !== QUERY_MODES.script && queryMode !== QUERY_MODES.scan;
100-
if (!enableAdditionalQueryModes && isNewQueryMode) {
98+
if (isNewQueryMode(queryMode) && !enableAdditionalQueryModes) {
10199
setQueryMode(QUERY_MODES.script);
102100
}
103101
}, [enableAdditionalQueryModes, queryMode, setQueryMode]);

src/containers/Tenant/Schema/SchemaTree/SchemaTree.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {useDispatch} from 'react-redux';
33

44
import {NavigationTree} from 'ydb-ui-components';
55

6-
import {setCurrentSchemaPath, preloadSchemas} from '../../../../store/reducers/schema/schema';
76
import type {EPathType, TEvDescribeSchemeResult} from '../../../../types/api/schema';
7+
import {setCurrentSchemaPath, preloadSchemas} from '../../../../store/reducers/schema/schema';
8+
import {useQueryModes} from '../../../../utils/hooks';
89

910
import {isChildlessPathType, mapPathTypeToNavigationTreeType} from '../../utils/schema';
1011
import {getActions} from '../../utils/schemaActions';
@@ -21,6 +22,8 @@ export function SchemaTree(props: SchemaTreeProps) {
2122

2223
const dispatch = useDispatch();
2324

25+
const [_, setQueryMode] = useQueryModes();
26+
2427
const fetchPath = (path: string) =>
2528
window.api
2629
.getSchema({path}, {concurrentId: `NavigationTree.getSchema|${path}`})
@@ -71,7 +74,10 @@ export function SchemaTree(props: SchemaTreeProps) {
7174
collapsed: false,
7275
}}
7376
fetchPath={fetchPath}
74-
getActions={getActions(dispatch, handleActivePathUpdate)}
77+
getActions={getActions(dispatch, {
78+
setActivePath: handleActivePathUpdate,
79+
setQueryMode,
80+
})}
7581
activePath={currentPath}
7682
onActivePathUpdate={handleActivePathUpdate}
7783
cache={false}

src/containers/Tenant/i18n/en.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
"summary.showPreview": "Show preview",
77
"summary.copySchemaPath": "Copy schema path",
88

9-
"actions.copied" : "The path is copied to the clipboard",
10-
"actions.notCopied" : "Couldn’t copy the path"
9+
"actions.copied": "The path is copied to the clipboard",
10+
"actions.notCopied": "Couldn’t copy the path",
11+
"actions.externalTableSelectUnavailable": "Select query for external tables available only with 'YQL - QueryService' query mode. You need to turn in additional query modes in settings to enable it",
12+
13+
"actions.copyPath": "Copy path",
14+
"actions.openPreview": "Open preview",
15+
"actions.createTable": "Create table...",
16+
"actions.alterTable": "Alter table...",
17+
"actions.selectQuery": "Select query...",
18+
"actions.upsertQuery": "Upsert query..."
1119
}

src/containers/Tenant/i18n/ru.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@
77
"summary.copySchemaPath": "Скопировать путь",
88

99
"actions.copied": "Путь успешно скопирован",
10-
"actions.notCopied": "Не получилось скопировать путь"
10+
"actions.notCopied": "Не получилось скопировать путь",
11+
"actions.externalTableSelectUnavailable": "Select запрос для внешних таблиц доступен только в режиме 'YQL - QueryService'. Вам необходимо включить дополнительные режимы выполнения запросов в настройках",
12+
13+
"actions.copyPath": "Скопировать путь",
14+
"actions.openPreview": "Открыть превью",
15+
"actions.createTable": "Создать таблицу...",
16+
"actions.alterTable": "Изменить таблицу...",
17+
"actions.selectQuery": "Select запрос...",
18+
"actions.upsertQuery": "Upsert запрос..."
1119
}

src/containers/Tenant/utils/schemaActions.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import copy from 'copy-to-clipboard';
33

44
import type {NavigationTreeNodeType, NavigationTreeProps} from 'ydb-ui-components';
55

6+
import type {QueryMode} from '../../../types/store/query';
7+
import type {SetQueryModeIfAvailable} from '../../../utils/hooks';
68
import {changeUserInput} from '../../../store/reducers/executeQuery';
79
import {setShowPreview} from '../../../store/reducers/schema/schema';
810
import {setQueryTab, setTenantPage} from '../../../store/reducers/tenant/tenant';
@@ -34,23 +36,41 @@ const upsertQueryTemplate = (path: string) => {
3436
VALUES ( );`;
3537
};
3638

39+
interface ActionsAdditionalEffects {
40+
setQueryMode: SetQueryModeIfAvailable;
41+
setActivePath: (path: string) => void;
42+
}
43+
3744
const bindActions = (
3845
path: string,
3946
dispatch: Dispatch<any>,
40-
setActivePath: (path: string) => void,
47+
additionalEffects: ActionsAdditionalEffects,
4148
) => {
42-
const inputQuery = (tmpl: (path: string) => string) => () => {
43-
dispatch(changeUserInput({input: tmpl(path)}));
44-
dispatch(setTenantPage(TENANT_PAGES_IDS.query));
45-
dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery));
46-
setActivePath(path);
47-
};
49+
const {setActivePath, setQueryMode} = additionalEffects;
50+
51+
const inputQuery =
52+
(tmpl: (path: string) => string, mode?: QueryMode, setQueryModeErrorMessage?: string) =>
53+
() => {
54+
const isNewQueryModeSet = mode && setQueryMode(mode, setQueryModeErrorMessage);
55+
56+
if (!mode || isNewQueryModeSet) {
57+
dispatch(changeUserInput({input: tmpl(path)}));
58+
dispatch(setTenantPage(TENANT_PAGES_IDS.query));
59+
dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery));
60+
setActivePath(path);
61+
}
62+
};
4863

4964
return {
50-
createTable: inputQuery(createTableTemplate),
51-
alterTable: inputQuery(alterTableTemplate),
65+
createTable: inputQuery(createTableTemplate, 'script'),
66+
alterTable: inputQuery(alterTableTemplate, 'script'),
5267
selectQuery: inputQuery(selectQueryTemplate),
5368
upsertQuery: inputQuery(upsertQueryTemplate),
69+
selectQueryFromExternalTable: inputQuery(
70+
selectQueryTemplate,
71+
'query',
72+
i18n('actions.externalTableSelectUnavailable'),
73+
),
5474
copyPath: () => {
5575
try {
5676
copy(path);
@@ -79,27 +99,34 @@ const bindActions = (
7999
type ActionsSet = ReturnType<Required<NavigationTreeProps>['getActions']>;
80100

81101
export const getActions =
82-
(dispatch: Dispatch<any>, setActivePath: (path: string) => void) =>
102+
(dispatch: Dispatch<any>, additionalEffects: ActionsAdditionalEffects) =>
83103
(path: string, type: NavigationTreeNodeType) => {
84-
const actions = bindActions(path, dispatch, setActivePath);
85-
const copyItem = {text: 'Copy path', action: actions.copyPath};
86-
const openPreview = {text: 'Open preview', action: actions.openPreview};
87-
const selectQuery = {text: 'Select query...', action: actions.selectQuery};
104+
const actions = bindActions(path, dispatch, additionalEffects);
105+
const copyItem = {text: i18n('actions.copyPath'), action: actions.copyPath};
106+
const openPreview = {text: i18n('actions.openPreview'), action: actions.openPreview};
88107

89108
const DIR_SET: ActionsSet = [
90109
[copyItem],
91-
[{text: 'Create table...', action: actions.createTable}],
110+
[{text: i18n('actions.createTable'), action: actions.createTable}],
92111
];
93112
const TABLE_SET: ActionsSet = [
94113
[openPreview, copyItem],
95114
[
96-
{text: 'Alter table...', action: actions.alterTable},
97-
selectQuery,
98-
{text: 'Upsert query...', action: actions.upsertQuery},
115+
{text: i18n('actions.alterTable'), action: actions.alterTable},
116+
{text: i18n('actions.selectQuery'), action: actions.selectQuery},
117+
{text: i18n('actions.upsertQuery'), action: actions.upsertQuery},
99118
],
100119
];
101120

102-
const EXTERNAL_TABLE_SET = [[openPreview, copyItem], [selectQuery]];
121+
const EXTERNAL_TABLE_SET = [
122+
[openPreview, copyItem],
123+
[
124+
{
125+
text: i18n('actions.selectQuery'),
126+
action: actions.selectQueryFromExternalTable,
127+
},
128+
],
129+
];
103130

104131
const JUST_COPY: ActionsSet = [copyItem];
105132

src/utils/hooks/i18n/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"useQueryModes.queryModeCannotBeSet": "Query mode \"{{mode}}\" cannot be set. You need to turn in additional query modes in settings to enable it"
3+
}

src/utils/hooks/i18n/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {i18n, Lang} from '../../../utils/i18n';
2+
3+
import en from './en.json';
4+
import ru from './ru.json';
5+
6+
const COMPONENT = 'ydb-hooks';
7+
8+
i18n.registerKeyset(Lang.En, COMPONENT, en);
9+
i18n.registerKeyset(Lang.Ru, COMPONENT, ru);
10+
11+
export default i18n.keyset(COMPONENT);

src/utils/hooks/i18n/ru.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"useQueryModes.queryModeCannotBeSet": "Режим выполнения запроса \"{{mode}}\" недоступен. Вам необходимо включить дополнительные режимы выполнения запросов в настройках"
3+
}

src/utils/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './useAutofetcher';
22
export * from './useTypedSelector';
33
export * from './useSetting';
4+
export * from './useQueryModes';

src/utils/hooks/useQueryModes.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type {QueryMode} from '../../types/store/query';
2+
import {ENABLE_ADDITIONAL_QUERY_MODES, QUERY_INITIAL_MODE_KEY} from '../constants';
3+
import {isNewQueryMode} from '../query';
4+
import createToast from '../createToast';
5+
import {useSetting} from './useSetting';
6+
import i18n from './i18n';
7+
8+
export type SetQueryModeIfAvailable = (
9+
value: QueryMode,
10+
errorMessage?: string | undefined,
11+
) => boolean;
12+
13+
export const useQueryModes = (): [QueryMode, SetQueryModeIfAvailable] => {
14+
const [queryMode, setQueryMode] = useSetting<QueryMode>(QUERY_INITIAL_MODE_KEY);
15+
const [enableAdditionalQueryModes] = useSetting<boolean>(ENABLE_ADDITIONAL_QUERY_MODES);
16+
17+
const setQueryModeIfAvailable: SetQueryModeIfAvailable = (value, errorMessage) => {
18+
if (isNewQueryMode(value) && !enableAdditionalQueryModes) {
19+
createToast({
20+
name: 'QueryModeCannotBeSet',
21+
title: errorMessage ?? i18n('useQueryModes.queryModeCannotBeSet', {mode: value}),
22+
type: 'error',
23+
});
24+
25+
return false;
26+
} else {
27+
setQueryMode(value);
28+
29+
return true;
30+
}
31+
};
32+
33+
return [queryMode, setQueryModeIfAvailable];
34+
};

0 commit comments

Comments
 (0)