-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ConfigEditor.tsx
178 lines (163 loc) · 5.14 KB
/
ConfigEditor.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React, { useCallback, useEffect, useReducer, useState } from 'react';
import clsx from 'clsx';
import styles from './ConfigEditor.module.css';
import Text from '../inputs/Text';
import Checkbox from '../inputs/Checkbox';
import useFocus from '../hooks/useFocus';
import Modal from '@site/src/components/modals/Modal';
import Dropdown from '@site/src/components/inputs/Dropdown';
export interface ConfigOptionsField {
key: string;
type: 'boolean' | 'string';
label?: string;
defaults?: unknown[];
enum?: string[];
}
export interface ConfigOptionsType {
heading: string;
fields: ConfigOptionsField[];
}
export type ConfigEditorValues = Record<string, unknown>;
export interface ConfigEditorProps {
readonly options: ConfigOptionsType[];
readonly values: ConfigEditorValues;
readonly isOpen: boolean;
readonly header: string;
readonly onClose: (config: ConfigEditorValues) => void;
}
function reducerObject(
state: ConfigEditorValues,
action:
| { type: 'init'; config?: ConfigEditorValues }
| {
type: 'set';
name: string;
value: unknown;
}
| {
type: 'toggle';
checked: boolean;
default: unknown[] | undefined;
name: string;
},
): ConfigEditorValues {
switch (action.type) {
case 'init': {
return action.config ?? {};
}
case 'set': {
const newState = { ...state };
if (action.value === '') {
delete newState[action.name];
} else {
newState[action.name] = action.value;
}
return newState;
}
case 'toggle': {
const newState = { ...state };
if (action.checked) {
newState[action.name] = action.default ? action.default[0] : true;
} else if (action.name in newState) {
delete newState[action.name];
}
return newState;
}
}
}
function filterConfig(
options: ConfigOptionsType[],
filter: string,
): ConfigOptionsType[] {
return options
.map(group => ({
heading: group.heading,
fields: group.fields.filter(item => String(item.key).includes(filter)),
}))
.filter(group => group.fields.length > 0);
}
function isDefault(value: unknown, defaults?: unknown[]): boolean {
return defaults ? defaults.includes(value) : value === true;
}
function ConfigEditor(props: ConfigEditorProps): JSX.Element {
const [filter, setFilter] = useState<string>('');
const [config, setConfig] = useReducer(reducerObject, {});
const [filterInput, setFilterFocus] = useFocus();
const onClose = useCallback(() => {
props.onClose(config);
}, [props.onClose, config]);
useEffect(() => {
setConfig({ type: 'init', config: props.values });
}, [props.values]);
useEffect(() => {
if (props.isOpen) {
setFilterFocus();
}
}, [props.isOpen]);
return (
<Modal header={props.header} isOpen={props.isOpen} onClose={onClose}>
<div className={styles.searchBar}>
<Text
ref={filterInput}
type="text"
name="config-filter"
value={filter}
className={styles.search}
onChange={setFilter}
/>
</div>
<div className={clsx('thin-scrollbar', styles.searchResultContainer)}>
{filterConfig(props.options, filter).map(group => (
<div key={group.heading}>
<h3 className={styles.searchResultGroup}>{group.heading}</h3>
<div>
{group.fields.map(item => (
<label className={styles.searchResult} key={item.key}>
<span className={styles.searchResultDescription}>
<span className={styles.searchResultName}>{item.key}</span>
{item.label && <br />}
{item.label && <span>{item.label}</span>}
</span>
{item.type === 'boolean' && (
<Checkbox
name={`config_${item.key}`}
value={item.key}
indeterminate={
Boolean(config[item.key]) &&
!isDefault(config[item.key], item.defaults)
}
checked={Boolean(config[item.key])}
onChange={(checked, name): void =>
setConfig({
type: 'toggle',
checked,
default: item.defaults,
name,
})
}
/>
)}
{item.type === 'string' && item.enum && (
<Dropdown
name={`config_${item.key}`}
value={String(config[item.key])}
options={item.enum}
onChange={(value): void => {
setConfig({
type: 'set',
value,
name: item.key,
});
}}
/>
)}
</label>
))}
</div>
</div>
))}
</div>
</Modal>
);
}
export default ConfigEditor;