2017-07-08 08:06:02 +10:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-24 01:15:17 +10:00
|
|
|
import { Component, PureComponent, cloneElement, Children } from 'react';
|
|
|
|
|
2017-10-08 11:55:58 +11:00
|
|
|
import { Switch, Route } from 'react-router-dom';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
2022-10-23 08:18:32 +11:00
|
|
|
import StackTrace from 'stacktrace-js';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
2017-07-08 08:06:02 +10:00
|
|
|
import BundleColumnError from '../components/bundle_column_error';
|
2023-05-24 01:15:17 +10:00
|
|
|
import ColumnLoading from '../components/column_loading';
|
2017-07-08 08:06:02 +10:00
|
|
|
import BundleContainer from '../containers/bundle_container';
|
|
|
|
|
|
|
|
// Small wrapper to pass multiColumn to the route components
|
2023-05-23 18:52:27 +10:00
|
|
|
export class WrappedSwitch extends PureComponent {
|
2023-07-14 01:18:09 +10:00
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object,
|
|
|
|
};
|
2017-11-01 22:17:53 +11:00
|
|
|
|
|
|
|
render () {
|
|
|
|
const { multiColumn, children } = this.props;
|
2023-07-14 01:18:09 +10:00
|
|
|
const { location } = this.context.router.route;
|
|
|
|
|
|
|
|
const decklessLocation = multiColumn && location.pathname.startsWith('/deck')
|
|
|
|
? {...location, pathname: location.pathname.slice(5)}
|
|
|
|
: location;
|
2017-11-01 22:17:53 +11:00
|
|
|
|
|
|
|
return (
|
2023-07-14 01:18:09 +10:00
|
|
|
<Switch location={decklessLocation}>
|
|
|
|
{Children.map(children, child => child ? cloneElement(child, { multiColumn }) : null)}
|
2017-11-01 22:17:53 +11:00
|
|
|
</Switch>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-07-08 08:06:02 +10:00
|
|
|
|
|
|
|
WrappedSwitch.propTypes = {
|
|
|
|
multiColumn: PropTypes.bool,
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
2018-09-14 08:53:09 +10:00
|
|
|
// Small Wrapper to extract the params from the route and pass
|
2017-07-08 08:06:02 +10:00
|
|
|
// them to the rendered component, together with the content to
|
|
|
|
// be rendered inside (the children)
|
2023-05-23 18:52:27 +10:00
|
|
|
export class WrappedRoute extends Component {
|
2017-07-08 08:06:02 +10:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
component: PropTypes.func.isRequired,
|
|
|
|
content: PropTypes.node,
|
|
|
|
multiColumn: PropTypes.bool,
|
2018-03-01 12:48:44 +11:00
|
|
|
componentParams: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
componentParams: {},
|
|
|
|
};
|
2017-07-08 08:06:02 +10:00
|
|
|
|
2022-10-23 08:18:32 +11:00
|
|
|
static getDerivedStateFromError () {
|
|
|
|
return {
|
|
|
|
hasError: true,
|
|
|
|
};
|
2022-12-19 02:51:37 +11:00
|
|
|
}
|
2022-10-23 08:18:32 +11: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-08 08:06:02 +10:00
|
|
|
renderComponent = ({ match }) => {
|
2018-03-01 12:48:44 +11:00
|
|
|
const { component, content, multiColumn, componentParams } = this.props;
|
2022-10-23 08:18:32 +11:00
|
|
|
const { hasError, stacktrace } = this.state;
|
|
|
|
|
|
|
|
if (hasError) {
|
|
|
|
return (
|
|
|
|
<BundleColumnError
|
|
|
|
stacktrace={stacktrace}
|
|
|
|
multiColumn={multiColumn}
|
|
|
|
errorType='error'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2017-07-08 08:06:02 +10:00
|
|
|
|
|
|
|
return (
|
|
|
|
<BundleContainer fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
|
2018-03-01 12:48:44 +11:00
|
|
|
{Component => <Component params={match.params} multiColumn={multiColumn} {...componentParams}>{content}</Component>}
|
2017-07-08 08:06:02 +10:00
|
|
|
</BundleContainer>
|
|
|
|
);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2017-07-08 08:06:02 +10:00
|
|
|
|
|
|
|
renderLoading = () => {
|
2022-10-20 23:35:29 +11:00
|
|
|
const { multiColumn } = this.props;
|
|
|
|
|
|
|
|
return <ColumnLoading multiColumn={multiColumn} />;
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2017-07-08 08:06:02 +10:00
|
|
|
|
|
|
|
renderError = (props) => {
|
2022-10-23 08:18:32 +11:00
|
|
|
return <BundleColumnError {...props} errorType='network' />;
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2017-07-08 08:06:02 +10:00
|
|
|
|
|
|
|
render () {
|
|
|
|
const { component: Component, content, ...rest } = this.props;
|
|
|
|
|
|
|
|
return <Route {...rest} render={this.renderComponent} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|