-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
OptionsSelector.tsx
153 lines (143 loc) · 4.22 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
/* eslint-disable jsx-a11y/label-has-associated-control */
import {
NavbarSecondaryMenuFiller,
useWindowSize,
} from '@docusaurus/theme-common';
import CopyIcon from '@site/src/icons/copy.svg';
import React, { useCallback } from 'react';
import useDebouncedToggle from './hooks/useDebouncedToggle';
import Checkbox from './inputs/Checkbox';
import Dropdown from './inputs/Dropdown';
import Tooltip from './inputs/Tooltip';
import Expander from './layout/Expander';
import { createMarkdown, createMarkdownParams } from './lib/markdown';
import styles from './OptionsSelector.module.css';
import type { ConfigModel } from './types';
export interface OptionsSelectorParams {
readonly state: ConfigModel;
readonly setState: (cfg: Partial<ConfigModel>) => void;
readonly tsVersions: readonly string[];
readonly isLoading: boolean;
}
function OptionsSelectorContent({
state,
setState,
tsVersions,
isLoading,
}: OptionsSelectorParams): JSX.Element {
const [copyLink, setCopyLink] = useDebouncedToggle<boolean>(false);
const [copyMarkdown, setCopyMarkdown] = useDebouncedToggle<boolean>(false);
const updateTS = useCallback(
(version: string) => {
setState({ ts: version });
},
[setState],
);
const copyLinkToClipboard = useCallback(() => {
void navigator.clipboard
.writeText(document.location.toString())
.then(() => {
setCopyLink(true);
});
}, [setCopyLink]);
const copyMarkdownToClipboard = useCallback(() => {
if (isLoading) {
return;
}
void navigator.clipboard.writeText(createMarkdown(state)).then(() => {
setCopyMarkdown(true);
});
}, [isLoading, state, setCopyMarkdown]);
const openIssue = useCallback(() => {
if (isLoading) {
return;
}
window
.open(
`https://github.com/typescript-eslint/typescript-eslint/issues/new?${createMarkdownParams(
state,
)}`,
'_blank',
)
?.focus();
}, [state, isLoading]);
return (
<>
<Expander label="Info">
<label className={styles.optionLabel}>
TypeScript
<Dropdown
name="ts"
className="text--right"
value={state.ts}
disabled={!tsVersions.length}
onChange={updateTS}
options={(tsVersions.length && tsVersions) || [state.ts]}
/>
</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}>
Source type
<Dropdown
name="sourceType"
value={state.sourceType}
onChange={(e): void => setState({ sourceType: e })}
options={['script', 'module']}
/>
</label>
</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>
</>
);
}
function OptionsSelector(props: OptionsSelectorParams): JSX.Element {
const windowSize = useWindowSize();
if (windowSize === 'mobile') {
return (
<NavbarSecondaryMenuFiller
component={OptionsSelectorContent}
props={props}
/>
);
}
return <OptionsSelectorContent {...props} />;
}
export default OptionsSelector;