-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
OptionsSelector.tsx
213 lines (196 loc) · 5.78 KB
/
OptionsSelector.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* eslint-disable jsx-a11y/label-has-associated-control */
import React, { useCallback, useState } from 'react';
import ConfigEslint from './config/ConfigEslint';
import ConfigTypeScript from './config/ConfigTypeScript';
import Expander from './layout/Expander';
import Dropdown from './inputs/Dropdown';
import Checkbox from './inputs/Checkbox';
import Tooltip from './inputs/Tooltip';
import EditIcon from '@site/src/icons/edit.svg';
import CopyIcon from '@site/src/icons/copy.svg';
import useDebouncedToggle from './hooks/useDebouncedToggle';
import { createMarkdown, createMarkdownParams } from './lib/markdown';
import type { RuleDetails } from './types';
import styles from './OptionsSelector.module.css';
import type { CompilerFlags, ConfigModel, RulesRecord } from './types';
export interface OptionsSelectorParams {
readonly ruleOptions: RuleDetails[];
readonly state: ConfigModel;
readonly setState: (cfg: Partial<ConfigModel>) => void;
readonly tsVersions: readonly string[];
readonly isLoading: boolean;
}
const ASTOptions = [
{ value: false, label: 'Disabled' },
{ value: 'es', label: 'ESTree' },
{ value: 'ts', label: 'TypeScript' },
{ value: 'scope', label: 'Scope' },
] as const;
function OptionsSelector({
ruleOptions,
state,
setState,
tsVersions,
isLoading,
}: OptionsSelectorParams): JSX.Element {
const [eslintModal, setEslintModal] = useState<boolean>(false);
const [typeScriptModal, setTypeScriptModal] = useState<boolean>(false);
const [copyLink, setCopyLink] = useDebouncedToggle<boolean>(false);
const [copyMarkdown, setCopyMarkdown] = useDebouncedToggle<boolean>(false);
const updateTS = useCallback(
(version: string) => {
setState({ ts: version });
},
[setState],
);
const updateRules = useCallback(
(rules?: RulesRecord) => {
if (rules) {
setState({ rules: rules });
}
setEslintModal(false);
},
[setState],
);
const updateTsConfig = useCallback(
(config?: CompilerFlags) => {
if (config) {
setState({ tsConfig: config });
}
setTypeScriptModal(false);
},
[setState],
);
const copyLinkToClipboard = useCallback(() => {
void navigator.clipboard
.writeText(document.location.toString())
.then(() => {
setCopyLink(true);
});
}, []);
const copyMarkdownToClipboard = useCallback(() => {
if (isLoading) {
return;
}
void navigator.clipboard.writeText(createMarkdown(state)).then(() => {
setCopyMarkdown(true);
});
}, [state, isLoading]);
const openIssue = useCallback(() => {
if (isLoading) {
return;
}
window
.open(
`https://github.com/typescript-eslint/typescript-eslint/issues/new?${createMarkdownParams(
state,
)}`,
'_blank',
)
?.focus();
}, [state, isLoading]);
return (
<>
{state.rules && ruleOptions.length > 0 && (
<ConfigEslint
isOpen={eslintModal}
ruleOptions={ruleOptions}
rules={state.rules}
onClose={updateRules}
/>
)}
<ConfigTypeScript
isOpen={typeScriptModal}
config={state.tsConfig}
onClose={updateTsConfig}
/>
<Expander label="Info">
<label className={styles.optionLabel}>
TypeScript
<Dropdown
name="ts"
className="text--right"
value={state.ts}
onChange={updateTS}
options={((tsVersions.length && tsVersions) || [state.ts]).filter(
item => parseFloat(item) >= 3.3,
)}
/>
</label>
<label className={styles.optionLabel}>
Eslint
<span>{process.env.ESLINT_VERSION}</span>
</label>
<label className={styles.optionLabel}>
TSEslint
<span>{process.env.TS_ESLINT_VERSION}</span>
</label>
</Expander>
<Expander label="Options">
<label className={styles.optionLabel}>
Enable jsx
<Checkbox
name="jsx"
checked={state.jsx}
onChange={(e): void => setState({ jsx: e })}
className={styles.optionCheckbox}
/>
</label>
<label className={styles.optionLabel}>
AST Viewer
<Dropdown
name="showAST"
value={state.showAST}
onChange={(e): void => setState({ showAST: e })}
options={ASTOptions}
/>
</label>
<label className={styles.optionLabel}>
Source type
<Dropdown
name="sourceType"
value={state.sourceType}
onChange={(e): void => setState({ sourceType: e })}
options={['script', 'module']}
/>
</label>
<button
className={styles.optionLabel}
onClick={(): void => setEslintModal(true)}
>
Eslint Config
<EditIcon />
</button>
<button
className={styles.optionLabel}
onClick={(): void => setTypeScriptModal(true)}
>
TypeScript Config
<EditIcon />
</button>
</Expander>
<Expander label="Actions">
<button className={styles.optionLabel} onClick={copyLinkToClipboard}>
Copy Link
<Tooltip open={copyLink} text="Copied">
<CopyIcon />
</Tooltip>
</button>
<button
className={styles.optionLabel}
onClick={copyMarkdownToClipboard}
>
Copy Markdown
<Tooltip open={copyMarkdown} text="Copied">
<CopyIcon />
</Tooltip>
</button>
<button className={styles.optionLabel} onClick={openIssue}>
Report as Issue
<CopyIcon />
</button>
</Expander>
</>
);
}
export default OptionsSelector;