Skip to content

Commit

Permalink
[dashboard] handle markdown error
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace committed Mar 23, 2020
1 parent 232925b commit 6dd2258
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import cx from 'classnames';
import AceEditor from 'react-ace';
import 'brace/mode/markdown';
import 'brace/theme/textmate';
import { t } from '@superset-ui/translation';

import DeleteComponentButton from '../DeleteComponentButton';
import DragDroppable from '../dnd/DragDroppable';
Expand All @@ -49,6 +50,9 @@ const propTypes = {

// from redux
logEvent: PropTypes.func.isRequired,
addDangerToast: PropTypes.func.isRequired,
undoLength: PropTypes.number.isRequired,
redoLength: PropTypes.number.isRequired,

// grid related
availableColumnCount: PropTypes.number.isRequired,
Expand All @@ -73,6 +77,10 @@ const markdownPlaceHolder = `# ✨Markdown
Click here to edit [markdown](https://bit.ly/1dQOfRK)`;

const emergencyCode = `
This markdown component has an error.
`;

function isSafeMarkup(node) {
if (node.type === 'html') {
return /href="(javascript|vbscript|file):.*"/gim.test(node.value) === false;
Expand All @@ -88,6 +96,8 @@ class Markdown extends React.PureComponent {
markdownSource: props.component.meta.code,
editor: null,
editorMode: 'preview',
undoLength: props.undoLength,
redoLength: props.redoLength,
};
this.renderStartTime = Logger.getTimestamp();

Expand All @@ -107,11 +117,46 @@ class Markdown extends React.PureComponent {
});
}

UNSAFE_componentWillReceiveProps(nextProps) {
const nextSource = nextProps.component.meta.code;
if (this.state.markdownSource !== nextSource) {
this.setState({ markdownSource: nextSource });
static getDerivedStateFromProps(nextProps, state) {
const {
hasError,
editorMode,
markdownSource,
undoLength,
redoLength,
} = state;
const {
component: nextComponent,
undoLength: nextUndoLength,
redoLength: nextRedoLength,
} = nextProps;
// user click undo or redo ?
if (nextUndoLength !== undoLength || nextRedoLength !== redoLength) {
return {
...state,
undoLength: nextUndoLength,
redoLength: nextRedoLength,
markdownSource: nextComponent.meta.code,
hasError: false,
};
} else if (
!hasError &&
editorMode === 'preview' &&
nextComponent.meta.code !== markdownSource
) {
return {
...state,
markdownSource: nextComponent.meta.code,
};
}

return state;
}

static getDerivedStateFromError() {
return {
hasError: true,
};
}

componentDidUpdate(prevProps) {
Expand All @@ -124,6 +169,16 @@ class Markdown extends React.PureComponent {
}
}

componentDidCatch(error, info) {
if (this.state.editor && this.state.editorMode === 'preview') {
this.props.addDangerToast(
t(
'This markdown component has an error. Please revert your recent changes.',
),
);
}
}

setEditor(editor) {
editor.getSession().setUseWrapMode(true);
this.setState({
Expand All @@ -139,7 +194,12 @@ class Markdown extends React.PureComponent {
}

handleChangeEditorMode(mode) {
if (this.state.editorMode === 'edit') {
const nextState = {
...this.state,
editorMode: mode,
};

if (mode === 'preview') {
const { updateComponents, component } = this.props;
if (component.meta.code !== this.state.markdownSource) {
updateComponents({
Expand All @@ -152,11 +212,10 @@ class Markdown extends React.PureComponent {
},
});
}
nextState.hasError = false;
}

this.setState(() => ({
editorMode: mode,
}));
this.setState(nextState);
}

handleMarkdownChange(nextValue) {
Expand Down Expand Up @@ -193,9 +252,14 @@ class Markdown extends React.PureComponent {
}

renderPreviewMode() {
const { hasError } = this.state;
return (
<ReactMarkdown
source={this.state.markdownSource || markdownPlaceHolder}
source={
hasError
? emergencyCode
: this.state.markdownSource || markdownPlaceHolder
}
escapeHtml={false}
allowNode={isSafeMarkup}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../actions/dashboardLayout';
import { setDirectPathToChild } from '../actions/dashboardState';
import { logEvent } from '../../logger/actions';
import { addDangerToast } from '../../messageToasts/actions';

const propTypes = {
component: componentShape.isRequired,
Expand Down Expand Up @@ -66,6 +67,8 @@ function mapStateToProps(
component,
parentComponent: dashboardLayout[parentId],
editMode: dashboardState.editMode,
undoLength: undoableLayout.past.length,
redoLength: undoableLayout.future.length,
filters: getActiveFilters(),
directPathToChild: dashboardState.directPathToChild,
directPathLastUpdated: dashboardState.directPathLastUpdated,
Expand Down Expand Up @@ -97,6 +100,7 @@ function mapStateToProps(
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
addDangerToast,
createComponent,
deleteComponent,
updateComponents,
Expand Down

0 comments on commit 6dd2258

Please sign in to comment.