|
1 | | -import {ReactNode} from 'react'; |
2 | | -import {connect} from 'react-redux'; |
3 | 1 | import cn from 'bem-cn-lite'; |
4 | 2 |
|
5 | | -import {RadioButton, Switch} from '@gravity-ui/uikit'; |
6 | 3 | import {Settings} from '@gravity-ui/navigation'; |
7 | 4 |
|
8 | 5 | import favoriteFilledIcon from '../../assets/icons/star.svg'; |
9 | 6 | import flaskIcon from '../../assets/icons/flask.svg'; |
10 | 7 |
|
11 | | -import {LabelWithPopover} from '../../components/LabelWithPopover/LabelWithPopover'; |
12 | | - |
13 | 8 | import { |
14 | 9 | ENABLE_QUERY_MODES_FOR_EXPLAIN, |
15 | 10 | INVERTED_DISKS_KEY, |
16 | 11 | THEME_KEY, |
17 | 12 | USE_NODES_ENDPOINT_IN_DIAGNOSTICS_KEY, |
18 | 13 | } from '../../utils/constants'; |
19 | 14 |
|
20 | | -import {setSettingValue} from '../../store/reducers/settings'; |
| 15 | +import {Setting, SettingProps} from './Setting'; |
21 | 16 |
|
22 | 17 | import './UserSettings.scss'; |
23 | 18 |
|
24 | | -const b = cn('ydb-user-settings'); |
| 19 | +export const b = cn('ydb-user-settings'); |
25 | 20 |
|
26 | 21 | enum Theme { |
27 | 22 | light = 'light', |
28 | 23 | dark = 'dark', |
29 | 24 | system = 'system', |
30 | 25 | } |
31 | 26 |
|
32 | | -function UserSettings(props: any) { |
33 | | - const _onThemeChangeHandler = (value: string) => { |
34 | | - props.setSettingValue(THEME_KEY, value); |
35 | | - }; |
36 | | - |
37 | | - const _onInvertedDisksChangeHandler = (value: boolean) => { |
38 | | - props.setSettingValue(INVERTED_DISKS_KEY, String(value)); |
39 | | - }; |
40 | | - |
41 | | - const _onNodesEndpointChangeHandler = (value: boolean) => { |
42 | | - props.setSettingValue(USE_NODES_ENDPOINT_IN_DIAGNOSTICS_KEY, String(value)); |
43 | | - }; |
44 | | - |
45 | | - const _onExplainQueryModesChangeHandler = (value: boolean) => { |
46 | | - props.setSettingValue(ENABLE_QUERY_MODES_FOR_EXPLAIN, String(value)); |
47 | | - }; |
48 | | - |
49 | | - const renderBreakNodesSettingsItem = (title: ReactNode) => { |
50 | | - return ( |
51 | | - <LabelWithPopover |
52 | | - className={b('item-with-popup')} |
53 | | - contentClassName={b('popup')} |
54 | | - text={title} |
55 | | - popoverContent={ |
56 | | - 'Use /viewer/json/nodes endpoint for Nodes Tab in diagnostics. It returns incorrect data on older versions' |
57 | | - } |
58 | | - /> |
59 | | - ); |
60 | | - }; |
| 27 | +const themeValues = [ |
| 28 | + { |
| 29 | + value: Theme.system, |
| 30 | + content: 'System', |
| 31 | + }, |
| 32 | + { |
| 33 | + value: Theme.light, |
| 34 | + content: 'Light', |
| 35 | + }, |
| 36 | + { |
| 37 | + value: Theme.dark, |
| 38 | + content: 'Dark', |
| 39 | + }, |
| 40 | +]; |
| 41 | + |
| 42 | +export enum SettingsSection { |
| 43 | + general = 'general', |
| 44 | + experiments = 'experiments', |
| 45 | +} |
61 | 46 |
|
62 | | - const renderEnableExplainQueryModesItem = (title: ReactNode) => { |
63 | | - return ( |
64 | | - <LabelWithPopover |
65 | | - className={b('item-with-popup')} |
66 | | - contentClassName={b('popup')} |
67 | | - text={title} |
68 | | - popoverContent={ |
69 | | - 'Enable script | scan query mode selector for both run and explain. May not work on some versions' |
70 | | - } |
71 | | - /> |
72 | | - ); |
73 | | - }; |
| 47 | +interface UserSettingsProps { |
| 48 | + settings?: Partial<Record<SettingsSection, SettingProps[]>>; |
| 49 | +} |
74 | 50 |
|
| 51 | +export const UserSettings = ({settings}: UserSettingsProps) => { |
75 | 52 | return ( |
76 | 53 | <Settings> |
77 | 54 | <Settings.Page |
78 | | - id="general" |
| 55 | + id={SettingsSection.general} |
79 | 56 | title="General" |
80 | 57 | icon={{data: favoriteFilledIcon, height: 14, width: 14}} |
81 | 58 | > |
82 | 59 | <Settings.Section title="General"> |
83 | | - <Settings.Item title="Interface theme"> |
84 | | - <RadioButton value={props.theme} onUpdate={_onThemeChangeHandler}> |
85 | | - <RadioButton.Option value={Theme.system}>System</RadioButton.Option> |
86 | | - <RadioButton.Option value={Theme.light}>Light</RadioButton.Option> |
87 | | - <RadioButton.Option value={Theme.dark}>Dark</RadioButton.Option> |
88 | | - </RadioButton> |
89 | | - </Settings.Item> |
| 60 | + <Setting |
| 61 | + settingKey={THEME_KEY} |
| 62 | + title="Interface theme" |
| 63 | + type="radio" |
| 64 | + values={themeValues} |
| 65 | + /> |
| 66 | + {settings?.[SettingsSection.general]?.map((setting) => ( |
| 67 | + <Setting key={setting.settingKey} {...setting} /> |
| 68 | + ))} |
90 | 69 | </Settings.Section> |
91 | 70 | </Settings.Page> |
92 | | - <Settings.Page id="experiments" title="Experiments" icon={{data: flaskIcon}}> |
| 71 | + <Settings.Page |
| 72 | + id={SettingsSection.experiments} |
| 73 | + title="Experiments" |
| 74 | + icon={{data: flaskIcon}} |
| 75 | + > |
93 | 76 | <Settings.Section title="Experiments"> |
94 | | - <Settings.Item title="Inverted disks space indicators"> |
95 | | - <Switch |
96 | | - checked={props.invertedDisks} |
97 | | - onUpdate={_onInvertedDisksChangeHandler} |
98 | | - /> |
99 | | - </Settings.Item> |
100 | | - <Settings.Item |
101 | | - title="Break the Nodes tab in Diagnostics" |
102 | | - renderTitleComponent={renderBreakNodesSettingsItem} |
103 | | - > |
104 | | - <Switch |
105 | | - checked={props.useNodesEndpointInDiagnostics} |
106 | | - onUpdate={_onNodesEndpointChangeHandler} |
107 | | - /> |
108 | | - </Settings.Item> |
109 | | - <Settings.Item |
110 | | - title="Enable query modes for explain" |
111 | | - renderTitleComponent={renderEnableExplainQueryModesItem} |
112 | | - > |
113 | | - <Switch |
114 | | - checked={props.enableQueryModesForExplain} |
115 | | - onUpdate={_onExplainQueryModesChangeHandler} |
116 | | - /> |
117 | | - </Settings.Item> |
| 77 | + <Setting |
| 78 | + settingKey={INVERTED_DISKS_KEY} |
| 79 | + title={'Inverted disks space indicators'} |
| 80 | + /> |
| 81 | + <Setting |
| 82 | + settingKey={USE_NODES_ENDPOINT_IN_DIAGNOSTICS_KEY} |
| 83 | + title={'Break the Nodes tab in Diagnostics'} |
| 84 | + helpPopoverContent={ |
| 85 | + 'Use /viewer/json/nodes endpoint for Nodes Tab in diagnostics. It returns incorrect data on older versions' |
| 86 | + } |
| 87 | + /> |
| 88 | + <Setting |
| 89 | + settingKey={ENABLE_QUERY_MODES_FOR_EXPLAIN} |
| 90 | + title={'Enable query modes for explain'} |
| 91 | + helpPopoverContent={ |
| 92 | + 'Enable script | scan query mode selector for both run and explain. May not work on some versions' |
| 93 | + } |
| 94 | + /> |
| 95 | + {settings?.[SettingsSection.experiments]?.map((setting) => ( |
| 96 | + <Setting key={setting.settingKey} {...setting} /> |
| 97 | + ))} |
118 | 98 | </Settings.Section> |
119 | 99 | </Settings.Page> |
120 | 100 | </Settings> |
121 | 101 | ); |
122 | | -} |
123 | | - |
124 | | -const mapStateToProps = (state: any) => { |
125 | | - const {theme, invertedDisks, useNodesEndpointInDiagnostics, enableQueryModesForExplain} = |
126 | | - state.settings.userSettings; |
127 | | - |
128 | | - return { |
129 | | - theme, |
130 | | - invertedDisks: JSON.parse(invertedDisks), |
131 | | - useNodesEndpointInDiagnostics: JSON.parse(useNodesEndpointInDiagnostics), |
132 | | - enableQueryModesForExplain: JSON.parse(enableQueryModesForExplain), |
133 | | - }; |
134 | 102 | }; |
135 | | - |
136 | | -const mapDispatchToProps = { |
137 | | - setSettingValue, |
138 | | -}; |
139 | | - |
140 | | -export default connect(mapStateToProps, mapDispatchToProps)(UserSettings); |
0 commit comments