-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor-preview): reset error boundaries on editor content change (#…
- Loading branch information
Showing
4 changed files
with
133 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/plugins/editor-safe-render/components/ErrorBoundary.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, { Component } from 'react'; | ||
|
||
class ErrorBoundary extends Component { | ||
static defaultState = { hasError: false, error: null, editorContent: null }; | ||
|
||
static getDerivedStateFromError(error) { | ||
return { hasError: true, error }; | ||
} | ||
|
||
constructor(...args) { | ||
super(...args); | ||
this.state = this.constructor.defaultState; | ||
} | ||
|
||
componentDidMount() { | ||
const { editorSelectors } = this.props; | ||
|
||
this.setState({ editorContent: editorSelectors.selectContent() }); | ||
} | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
const { editorSelectors } = this.props; | ||
const hasEditorContentChanged = prevState.editorContent !== editorSelectors.selectContent(); | ||
|
||
if (!hasEditorContentChanged) return; | ||
|
||
const newState = { editorContent: editorSelectors.selectContent() }; | ||
|
||
if (prevState.hasError) { | ||
newState.hasError = false; | ||
newState.error = null; | ||
} | ||
|
||
this.setState(newState); | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
const { | ||
fn: { componentDidCatch }, | ||
} = this.props; | ||
|
||
componentDidCatch(error, errorInfo); | ||
} | ||
|
||
render() { | ||
const { hasError, error } = this.state; | ||
const { getComponent, targetName, children } = this.props; | ||
|
||
if (hasError && error) { | ||
const FallbackComponent = getComponent('Fallback'); | ||
return <FallbackComponent name={targetName} />; | ||
} | ||
|
||
return children; | ||
} | ||
} | ||
ErrorBoundary.propTypes = { | ||
targetName: PropTypes.string, | ||
getComponent: PropTypes.func.isRequired, | ||
fn: PropTypes.shape({ | ||
componentDidCatch: PropTypes.func.isRequired, | ||
}).isRequired, | ||
editorSelectors: PropTypes.shape({ | ||
selectContent: PropTypes.func.isRequired, | ||
}).isRequired, | ||
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]), | ||
}; | ||
ErrorBoundary.defaultProps = { | ||
targetName: 'this component', | ||
children: null, | ||
}; | ||
|
||
export default ErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import SwaggerUI from 'swagger-ui-react'; | ||
|
||
import ErrorBoundaryWrapper from './wrap-components/ErrorBoundaryWrapper.jsx'; | ||
|
||
/** | ||
* This is special version of SwaggerUI.plugins.SafeRender. | ||
* In editor context, we want to dismiss the error produced | ||
* in error boundary if editor content has changed. | ||
*/ | ||
const EditorSafeRenderPlugin = () => { | ||
const safeRenderPlugin = () => | ||
SwaggerUI.plugins.SafeRender({ | ||
fullOverride: true, | ||
componentList: [ | ||
'TopBar', | ||
'SwaggerEditorLayout', | ||
'Editor', | ||
'EditorTextarea', | ||
'EditorMonaco', | ||
'EditorPane', | ||
'EditorPaneBarTop', | ||
'EditorPreviewPane', | ||
'ValidationPane', | ||
'AlertDialog', | ||
'ConfirmDialog', | ||
'Dropzone', | ||
], | ||
}); | ||
|
||
const safeRenderPluginOverride = () => ({ | ||
wrapComponents: { | ||
ErrorBoundary: ErrorBoundaryWrapper, | ||
}, | ||
}); | ||
|
||
return [safeRenderPlugin, safeRenderPluginOverride]; | ||
}; | ||
|
||
export default EditorSafeRenderPlugin; |
16 changes: 16 additions & 0 deletions
16
src/plugins/editor-safe-render/wrap-components/ErrorBoundaryWrapper.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
|
||
import ErrorBoundary from '../components/ErrorBoundary.jsx'; | ||
|
||
const ErrorBoundaryWrapper = (Original, system) => { | ||
const ErrorBoundaryOverride = (props) => { | ||
const { editorSelectors } = system; | ||
|
||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
return <ErrorBoundary {...props} editorSelectors={editorSelectors} />; | ||
}; | ||
|
||
return ErrorBoundaryOverride; | ||
}; | ||
|
||
export default ErrorBoundaryWrapper; |