forked from fkling/astexplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTOutput.js
More file actions
101 lines (89 loc) · 2.37 KB
/
ASTOutput.js
File metadata and controls
101 lines (89 loc) · 2.37 KB
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
import PropTypes from 'prop-types';
import React from 'react';
import cx from 'classnames';
import visualizations from './visualization';
const {useState} = React;
function formatTime(time) {
if (!time) {
return null;
}
if (time < 1000) {
return `${time}ms`;
}
return `${(time / 1000).toFixed(2)}s`;
}
export default function ASTOutput({parseResult={}, position=null}) {
const [selectedOutput, setSelectedOutput] = useState(0);
const {ast=null} = parseResult;
let output;
if (parseResult.error) {
output =
<div style={{padding: 20}}>
{parseResult.error.message}
</div>;
} else if (ast) {
output = (
<ErrorBoundary>
{
React.createElement(
visualizations[selectedOutput],
{parseResult, position}
)
}
</ErrorBoundary>
)
}
let buttons = visualizations.map(
(cls, index) =>
<button
key={index}
value={index}
onClick={event => setSelectedOutput(event.target.value)}
className={cx({
active: selectedOutput == index,
})}>
{cls.name}
</button>
);
return (
<div className="output highlight">
<div className="toolbar">
{buttons}
<span className="time">
{formatTime(parseResult.time)}
</span>
</div>
{output}
</div>
);
}
ASTOutput.propTypes = {
parseResult: PropTypes.object,
position: PropTypes.number,
};
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return (
<div style={{padding: 20}}>
An error was caught while rendering the AST. This usually is an issue with
astexplorer itself. Have a look at the console for more information.
Consider <a href="https://github.com/fkling/astexplorer/issues/new?template=bug_report.md">filing a bug report</a>, but <a href="https://github.com/fkling/astexplorer/issues/">check first</a> if one doesn"t already exist. Thank you!
</div>
);
}
return this.props.children;
}
}
ErrorBoundary.propTypes = {
children: PropTypes.node,
};