-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Playground.tsx
133 lines (124 loc) · 4.11 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
import React, { 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 } from './types';
import type { TSESTree } from '@typescript-eslint/website-eslint';
import type { SourceFile } from 'typescript';
import ASTViewerScope from '@site/src/components/ASTViewerScope';
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,
rules: {},
tsConfig: {},
});
const { colorMode } = useColorMode();
const [esAst, setEsAst] = useState<TSESTree.Program | string | null>();
const [tsAst, setTsAST] = useState<SourceFile | string | null>();
const [scope, setScope] = useState<Record<string, unknown> | string | null>();
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);
return (
<div className={styles.codeContainer}>
<div className={clsx(styles.options, 'thin-scrollbar')}>
<OptionsSelector
isLoading={isLoading}
state={state}
tsVersions={tsVersions}
setState={setState}
ruleOptions={ruleNames}
/>
</div>
<div className={styles.codeBlocks}>
<div
className={clsx(
styles.sourceCode,
state.showAST ? '' : styles.sourceCodeStandalone,
)}
>
{isLoading && <Loader />}
<EditorEmbed />
<LoadingEditor
ts={state.ts}
jsx={state.jsx}
code={state.code}
tsConfig={state.tsConfig}
darkTheme={colorMode === 'dark'}
sourceType={state.sourceType}
rules={state.rules}
showAST={state.showAST}
onEsASTChange={setEsAst}
onTsASTChange={setTsAST}
onScopeChange={setScope}
decoration={selectedRange}
onChange={(code): void => setState({ code: code })}
onLoaded={(ruleNames, tsVersions): void => {
setRuleNames(ruleNames);
setTSVersion(tsVersions);
setIsLoading(false);
}}
onSelect={setPosition}
/>
</div>
{state.showAST && (
<div className={styles.astViewer}>
{(tsAst && state.showAST === 'ts' && (
<ASTViewerTS
value={tsAst}
position={position}
onSelectNode={setSelectedRange}
/>
)) ||
(state.showAST === 'scope' && scope && (
<ASTViewerScope
value={scope}
position={position}
onSelectNode={setSelectedRange}
/>
)) ||
(esAst && (
<ASTViewerESTree
value={esAst}
position={position}
onSelectNode={setSelectedRange}
/>
))}
</div>
)}
</div>
</div>
);
}
export default Playground;