2017-07-07 18:06:02 -04:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-10-07 20:55:58 -04:00
|
|
|
import { Switch, Route } from 'react-router-dom';
|
2022-10-22 17:18:32 -04:00
|
|
|
import StackTrace from 'stacktrace-js';
|
2017-12-04 02:26:40 -05:00
|
|
|
import ColumnLoading from 'flavours/glitch/features/ui/components/column_loading';
|
|
|
|
import BundleColumnError from 'flavours/glitch/features/ui/components/bundle_column_error';
|
|
|
|
import BundleContainer from 'flavours/glitch/features/ui/containers/bundle_container';
|
2017-07-07 18:06:02 -04:00
|
|
|
|
|
|
|
// Small wrapper to pass multiColumn to the route components
|
2017-11-01 07:17:53 -04:00
|
|
|
export class WrappedSwitch extends React.PureComponent {
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { multiColumn, children } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Switch>
|
|
|
|
{React.Children.map(children, child => React.cloneElement(child, { multiColumn }))}
|
|
|
|
</Switch>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-07-07 18:06:02 -04:00
|
|
|
|
|
|
|
WrappedSwitch.propTypes = {
|
|
|
|
multiColumn: PropTypes.bool,
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Small Wraper to extract the params from the route and pass
|
|
|
|
// them to the rendered component, together with the content to
|
|
|
|
// be rendered inside (the children)
|
|
|
|
export class WrappedRoute extends React.Component {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
component: PropTypes.func.isRequired,
|
|
|
|
content: PropTypes.node,
|
|
|
|
multiColumn: PropTypes.bool,
|
2018-03-16 14:54:00 -04:00
|
|
|
componentParams: PropTypes.object,
|
2023-02-03 14:52:07 -05:00
|
|
|
};
|
2017-07-07 18:06:02 -04:00
|
|
|
|
2018-03-16 14:54:00 -04:00
|
|
|
static defaultProps = {
|
|
|
|
componentParams: {},
|
|
|
|
};
|
|
|
|
|
2022-10-22 17:18:32 -04:00
|
|
|
static getDerivedStateFromError () {
|
|
|
|
return {
|
|
|
|
hasError: true,
|
|
|
|
};
|
2023-02-03 14:52:07 -05:00
|
|
|
}
|
2022-10-22 17:18:32 -04:00
|
|
|
|
|
|
|
state = {
|
|
|
|
hasError: false,
|
|
|
|
stacktrace: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidCatch (error) {
|
|
|
|
StackTrace.fromError(error).then(stackframes => {
|
|
|
|
this.setState({ stacktrace: error.toString() + '\n' + stackframes.map(frame => frame.toString()).join('\n') });
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-07 18:06:02 -04:00
|
|
|
renderComponent = ({ match }) => {
|
2018-03-16 14:54:00 -04:00
|
|
|
const { component, content, multiColumn, componentParams } = this.props;
|
2022-10-22 17:18:32 -04:00
|
|
|
const { hasError, stacktrace } = this.state;
|
|
|
|
|
|
|
|
if (hasError) {
|
|
|
|
return (
|
|
|
|
<BundleColumnError
|
|
|
|
stacktrace={stacktrace}
|
|
|
|
multiColumn={multiColumn}
|
|
|
|
errorType='error'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2017-07-07 18:06:02 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<BundleContainer fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
|
2018-03-16 14:54:00 -04:00
|
|
|
{Component => <Component params={match.params} multiColumn={multiColumn} {...componentParams}>{content}</Component>}
|
2017-07-07 18:06:02 -04:00
|
|
|
</BundleContainer>
|
|
|
|
);
|
2023-02-03 14:52:07 -05:00
|
|
|
};
|
2017-07-07 18:06:02 -04:00
|
|
|
|
|
|
|
renderLoading = () => {
|
2022-10-20 08:35:29 -04:00
|
|
|
const { multiColumn } = this.props;
|
|
|
|
|
|
|
|
return <ColumnLoading multiColumn={multiColumn} />;
|
2023-02-03 14:52:07 -05:00
|
|
|
};
|
2017-07-07 18:06:02 -04:00
|
|
|
|
|
|
|
renderError = (props) => {
|
2022-10-22 17:18:32 -04:00
|
|
|
return <BundleColumnError {...props} errorType='network' />;
|
2023-02-03 14:52:07 -05:00
|
|
|
};
|
2017-07-07 18:06:02 -04:00
|
|
|
|
|
|
|
render () {
|
|
|
|
const { component: Component, content, ...rest } = this.props;
|
|
|
|
|
|
|
|
return <Route {...rest} render={this.renderComponent} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|