Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dashboard] handle markdown error #9350

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = `
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n

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