-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
78 lines (67 loc) · 1.53 KB
/
index.js
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
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';
import { doAction } from '@wordpress/hooks';
function CopyButton( { text, children } ) {
const ref = useCopyToClipboard( text );
return (
<Button variant="secondary" ref={ ref }>
{ children }
</Button>
);
}
function ErrorBoundaryWarning( { message, error, reboot } ) {
const actions = [];
if ( reboot ) {
actions.push(
<Button key="recovery" onClick={ reboot } variant="secondary">
{ __( 'Attempt Recovery' ) }
</Button>
);
}
if ( error ) {
actions.push(
<CopyButton key="copy-error" text={ error.stack }>
{ __( 'Copy Error' ) }
</CopyButton>
);
}
return (
<Warning className="edit-widgets-error-boundary" actions={ actions }>
{ message }
</Warning>
);
}
export default class ErrorBoundary extends Component {
constructor() {
super( ...arguments );
this.state = {
error: null,
};
}
componentDidCatch( error ) {
doAction( 'editor.ErrorBoundary.errorLogged', error );
}
static getDerivedStateFromError( error ) {
return { error };
}
render() {
if ( ! this.state.error ) {
return this.props.children;
}
return (
<ErrorBoundaryWarning
message={ __(
'The editor has encountered an unexpected error.'
) }
error={ this.state.error }
reboot={ this.props.onError }
/>
);
}
}