-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsettings.ts
107 lines (98 loc) · 3.36 KB
/
settings.ts
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
100
101
102
103
104
105
106
107
import {TENANT_PAGES_IDS} from '../store/reducers/tenant/constants';
import {
ASIDE_HEADER_COMPACT_KEY,
AUTOCOMPLETE_ON_ENTER,
AUTO_REFRESH_INTERVAL,
BINARY_DATA_IN_PLAIN_TEXT_DISPLAY,
CASE_SENSITIVE_JSON_SEARCH,
ENABLE_AUTOCOMPLETE,
ENABLE_CODE_ASSISTANT,
ENABLE_NETWORK_TABLE_KEY,
ENABLE_QUERY_STREAMING,
INVERTED_DISKS_KEY,
IS_HOTKEYS_HELP_HIDDEN_KEY,
LANGUAGE_KEY,
LAST_QUERY_EXECUTION_SETTINGS_KEY,
LAST_USED_QUERY_ACTION_KEY,
PARTITIONS_HIDDEN_COLUMNS_KEY,
QUERY_EXECUTION_SETTINGS_KEY,
QUERY_SETTINGS_BANNER_LAST_CLOSED_KEY,
SAVED_QUERIES_KEY,
SHOW_DOMAIN_DATABASE_KEY,
TENANT_INITIAL_PAGE_KEY,
THEME_KEY,
USE_CLUSTER_BALANCER_AS_BACKEND_KEY,
USE_SHOW_PLAN_SVG_KEY,
} from '../utils/constants';
import {DEFAULT_QUERY_SETTINGS, QUERY_ACTIONS} from '../utils/query';
import {parseJson} from '../utils/utils';
export type SettingsObject = Record<string, unknown>;
/** User settings keys and their default values */
export const DEFAULT_USER_SETTINGS = {
[THEME_KEY]: 'system',
[LANGUAGE_KEY]: undefined,
[INVERTED_DISKS_KEY]: false,
[BINARY_DATA_IN_PLAIN_TEXT_DISPLAY]: true,
[SAVED_QUERIES_KEY]: [],
[TENANT_INITIAL_PAGE_KEY]: TENANT_PAGES_IDS.query,
[LAST_USED_QUERY_ACTION_KEY]: QUERY_ACTIONS.execute,
[ASIDE_HEADER_COMPACT_KEY]: true,
[PARTITIONS_HIDDEN_COLUMNS_KEY]: [],
[ENABLE_NETWORK_TABLE_KEY]: false,
[USE_SHOW_PLAN_SVG_KEY]: false,
[USE_CLUSTER_BALANCER_AS_BACKEND_KEY]: true,
[ENABLE_AUTOCOMPLETE]: true,
[ENABLE_CODE_ASSISTANT]: true,
[ENABLE_QUERY_STREAMING]: false,
[AUTOCOMPLETE_ON_ENTER]: true,
[IS_HOTKEYS_HELP_HIDDEN_KEY]: false,
[AUTO_REFRESH_INTERVAL]: 0,
[CASE_SENSITIVE_JSON_SEARCH]: false,
[SHOW_DOMAIN_DATABASE_KEY]: false,
[LAST_QUERY_EXECUTION_SETTINGS_KEY]: undefined,
[QUERY_SETTINGS_BANNER_LAST_CLOSED_KEY]: undefined,
[QUERY_EXECUTION_SETTINGS_KEY]: DEFAULT_QUERY_SETTINGS,
} as const satisfies SettingsObject;
class SettingsManager {
/**
* Returns parsed settings value.
* If value cannot be parsed, returns initially stored string.
* If there is no value, return default value
*/
readUserSettingsValue(key: string, defaultValue?: unknown) {
return this.readValueFromLS(key) ?? defaultValue;
}
/**
* Stringify value and set it to LS
*/
setUserSettingsValue(key: string, value: unknown) {
return this.setValueToLS(key, value);
}
/**
* Extract values by provided settings object
*/
extractSettingsFromLS = (settings: SettingsObject) => {
return Object.entries(settings).reduce<SettingsObject>((acc, [key, value]) => {
acc[key] = this.readUserSettingsValue(key, value);
return acc;
}, {});
};
private readValueFromLS = (key: string): unknown => {
try {
const value = localStorage.getItem(key);
return parseJson(value);
} catch {
return undefined;
}
};
private setValueToLS = (key: string, value: unknown): void => {
try {
if (typeof value === 'string') {
localStorage.setItem(key, value);
} else {
localStorage.setItem(key, JSON.stringify(value));
}
} catch {}
};
}
export const settingsManager = new SettingsManager();