-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Preview.tsx
142 lines (121 loc) · 3.27 KB
/
Preview.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import PlaygroundError from 'rsg-components/PlaygroundError';
import ReactExample from 'rsg-components/ReactExample';
import Context, { StyleGuideContextContents } from 'rsg-components/Context';
import { createRoot, Root } from 'react-dom/client';
const improveErrorMessage = (message: string) =>
message.replace(
'Check the render method of `StateHolder`.',
'Check the code of your example in a Markdown file or in the editor below.'
);
interface PreviewProps {
code: string;
evalInContext(code: string): () => any;
}
interface PreviewState {
error: string | null;
}
export default class Preview extends Component<PreviewProps, PreviewState> {
public static propTypes = {
code: PropTypes.string.isRequired,
evalInContext: PropTypes.func.isRequired,
};
public static contextType = Context;
private mountNode: Element | null = null;
private reactRoot: Root | null = null;
private timeoutId: number | null = null;
public state: PreviewState = {
error: null,
};
public componentDidMount() {
// Clear console after hot reload, do not clear on the first load
// to keep any warnings
if ((this.context as StyleGuideContextContents).codeRevision > 0) {
// eslint-disable-next-line no-console
console.clear();
}
this.executeCode();
}
public shouldComponentUpdate(nextProps: PreviewProps, nextState: PreviewState) {
return this.state.error !== nextState.error || this.props.code !== nextProps.code;
}
public componentDidUpdate(prevProps: PreviewProps) {
if (this.props.code !== prevProps.code) {
this.executeCode();
}
}
public componentWillUnmount() {
this.unmountPreview();
}
public unmountPreview() {
const self = this;
if (self.timeoutId) {
clearTimeout(self.timeoutId);
}
const id = setTimeout(() => {
if (self.reactRoot) {
self.reactRoot.unmount();
self.reactRoot = null;
}
});
self.timeoutId = id;
}
private executeCode() {
this.setState({
error: null,
});
const { code } = this.props;
if (!code) {
return;
}
const wrappedComponent: React.FunctionComponentElement<any> = (
<ReactExample
code={code}
evalInContext={this.props.evalInContext}
onError={this.handleError}
compilerConfig={(this.context as StyleGuideContextContents).config.compilerConfig}
/>
);
/* istanbul ignore next */
window.requestAnimationFrame(() => {
if (!this.mountNode) {
return;
}
try {
if (this.reactRoot === null) {
this.reactRoot = createRoot(this.mountNode);
this.reactRoot.render(wrappedComponent);
} else {
this.reactRoot.render(wrappedComponent);
}
} catch (err) {
if (err instanceof Error) {
this.handleError(err);
}
}
});
}
private handleError = (err: Error) => {
this.unmountPreview();
this.setState({
error: improveErrorMessage(err.toString()),
});
console.error(err); // eslint-disable-line no-console
};
private callbackRef = (ref: HTMLDivElement | null) => {
this.mountNode = ref;
if (!this.reactRoot && ref) {
this.reactRoot = createRoot(ref);
}
};
public render() {
const { error } = this.state;
return (
<>
<div data-testid="mountNode" ref={this.callbackRef} />
{error && <PlaygroundError message={error} />}
</>
);
}
}