/
Playground.tsx
175 lines (165 loc) · 5.39 KB
/
Playground.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
import React, { useCallback, useReducer, useState } from 'react';
import type Monaco from 'monaco-editor';
import clsx from 'clsx';
import { useColorMode } from '@docusaurus/theme-common';
import styles from './Playground.module.css';
import Loader from './layout/Loader';
import useHashState from './hooks/useHashState';
import OptionsSelector from './OptionsSelector';
import { LoadingEditor } from './editor/LoadingEditor';
import { EditorEmbed } from './editor/EditorEmbed';
import { shallowEqual } from './lib/shallowEqual';
import ASTViewerESTree from './ASTViewerESTree';
import ASTViewerTS from './ASTViewerTS';
import type {
RuleDetails,
SelectedRange,
ErrorGroup,
TabType,
ConfigModel,
} from './types';
import type { TSESTree } from '@typescript-eslint/utils';
import type { SourceFile } from 'typescript';
import ASTViewerScope from '@site/src/components/ASTViewerScope';
import ErrorsViewer from '@site/src/components/ErrorsViewer';
import EditorTabs from '@site/src/components/EditorTabs';
import ConfigEslint from '@site/src/components/config/ConfigEslint';
import ConfigTypeScript from '@site/src/components/config/ConfigTypeScript';
import {
defaultEslintConfig,
defaultTsConfig,
} from '@site/src/components/config/utils';
function rangeReducer<T extends SelectedRange | null>(
prevState: T,
action: T,
): T {
if (prevState !== action) {
if (
!prevState ||
!action ||
!shallowEqual(prevState.start, action.start) ||
!shallowEqual(prevState.end, action.end)
) {
return action;
}
}
return prevState;
}
function Playground(): JSX.Element {
const [state, setState] = useHashState({
jsx: false,
showAST: false,
sourceType: 'module',
code: '',
ts: process.env.TS_VERSION!,
tsconfig: defaultTsConfig,
eslintrc: defaultEslintConfig,
});
const { colorMode } = useColorMode();
const [esAst, setEsAst] = useState<TSESTree.Program | null>();
const [tsAst, setTsAST] = useState<SourceFile | null>();
const [scope, setScope] = useState<Record<string, unknown> | null>();
const [markers, setMarkers] = useState<ErrorGroup[] | Error>();
const [ruleNames, setRuleNames] = useState<RuleDetails[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [tsVersions, setTSVersion] = useState<readonly string[]>([]);
const [selectedRange, setSelectedRange] = useReducer(rangeReducer, null);
const [position, setPosition] = useState<Monaco.Position | null>(null);
const [activeTab, setTab] = useState<TabType>('code');
const [showModal, setShowModal] = useState<TabType | false>(false);
const updateModal = useCallback(
(config?: Partial<ConfigModel>) => {
if (config) {
setState(config);
}
setShowModal(false);
},
[setState],
);
return (
<div className={styles.codeContainer}>
{ruleNames.length > 0 && (
<ConfigEslint
isOpen={showModal === 'eslintrc'}
ruleOptions={ruleNames}
config={state.eslintrc}
onClose={updateModal}
/>
)}
<ConfigTypeScript
isOpen={showModal === 'tsconfig'}
config={state.tsconfig}
onClose={updateModal}
/>
<div className={clsx(styles.options, 'thin-scrollbar')}>
<OptionsSelector
isLoading={isLoading}
state={state}
tsVersions={tsVersions}
setState={setState}
/>
</div>
<div className={styles.codeBlocks}>
<div className={clsx(styles.sourceCode)}>
{isLoading && <Loader />}
<EditorTabs
tabs={['code', 'tsconfig', 'eslintrc']}
activeTab={activeTab}
change={setTab}
showModal={(): void => setShowModal(activeTab)}
/>
<div className={styles.tabCode}>
<EditorEmbed />
</div>
<LoadingEditor
ts={state.ts}
jsx={state.jsx}
activeTab={activeTab}
code={state.code}
tsconfig={state.tsconfig}
eslintrc={state.eslintrc}
darkTheme={colorMode === 'dark'}
sourceType={state.sourceType}
showAST={state.showAST}
onEsASTChange={setEsAst}
onTsASTChange={setTsAST}
onScopeChange={setScope}
onMarkersChange={setMarkers}
decoration={selectedRange}
onChange={setState}
onLoaded={(ruleNames, tsVersions): void => {
setRuleNames(ruleNames);
setTSVersion(tsVersions);
setIsLoading(false);
}}
onSelect={setPosition}
/>
</div>
<div className={styles.astViewer}>
{(state.showAST === 'ts' && tsAst && (
<ASTViewerTS
value={tsAst}
position={position}
onSelectNode={setSelectedRange}
/>
)) ||
(state.showAST === 'scope' && scope && (
<ASTViewerScope
value={scope}
position={position}
onSelectNode={setSelectedRange}
/>
)) ||
(state.showAST === 'es' && esAst && (
<ASTViewerESTree
value={esAst}
position={position}
onSelectNode={setSelectedRange}
/>
)) || <ErrorsViewer value={markers} />}
</div>
</div>
</div>
);
}
export default Playground;