-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcontrols.tsx
99 lines (79 loc) · 2.97 KB
/
controls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type {ButtonSize} from '@gravity-ui/uikit';
import {Button, Icon} from '@gravity-ui/uikit';
import type {NavigationTreeNodeType, NavigationTreeProps} from 'ydb-ui-components';
import {api} from '../../../store/reducers/api';
import {setShowPreview} from '../../../store/reducers/schema/schema';
import {TENANT_PAGES_IDS, TENANT_QUERY_TABS_ID} from '../../../store/reducers/tenant/constants';
import {setQueryTab, setTenantPage} from '../../../store/reducers/tenant/tenant';
import i18n from '../i18n';
import EyeIcon from '@gravity-ui/icons/svgs/eye.svg';
interface ControlsAdditionalEffects {
setActivePath: (path: string) => void;
}
const bindActions = (
path: string,
dispatch: React.Dispatch<any>,
additionalEffects: ControlsAdditionalEffects,
) => {
const {setActivePath} = additionalEffects;
return {
openPreview: () => {
dispatch(api.util.invalidateTags(['PreviewData']));
dispatch(setShowPreview(true));
dispatch(setTenantPage(TENANT_PAGES_IDS.query));
dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery));
setActivePath(path);
},
};
};
type Controls = ReturnType<Required<NavigationTreeProps>['renderAdditionalNodeElements']>;
type SummaryType = 'preview';
const getPreviewControl = (options: ReturnType<typeof bindActions>, size?: ButtonSize) => {
return (
<Button
view="flat-secondary"
onClick={options.openPreview}
title={i18n('actions.openPreview')}
size={size || 's'}
>
<Icon data={EyeIcon} />
</Button>
);
};
export const getSchemaControls =
(
dispatch: React.Dispatch<any>,
additionalEffects: ControlsAdditionalEffects,
size?: ButtonSize,
) =>
(path: string, type: NavigationTreeNodeType) => {
const options = bindActions(path, dispatch, additionalEffects);
const openPreview = getPreviewControl(options, size);
const nodeTypeToControls: Record<NavigationTreeNodeType, Controls> = {
async_replication: undefined,
database: undefined,
directory: undefined,
table: openPreview,
column_table: openPreview,
index_table: undefined,
topic: undefined,
stream: undefined,
index: undefined,
external_table: openPreview,
external_data_source: undefined,
view: openPreview,
};
return nodeTypeToControls[type];
};
export const getSummaryControls =
(
dispatch: React.Dispatch<any>,
additionalEffects: ControlsAdditionalEffects,
size?: ButtonSize,
) =>
(path: string, type: SummaryType) => {
const options = bindActions(path, dispatch, additionalEffects);
const openPreview = getPreviewControl(options, size);
const summaryControls: Record<SummaryType, Controls> = {preview: openPreview};
return summaryControls[type];
};