glitchier-soc/app/javascript/mastodon/features/ui/util/react_router_helpers.jsx

102 lines
2.6 KiB
React
Raw Normal View History

2023-05-23 11:52:27 +03:00
import { Component, PureComponent, cloneElement, Children } from 'react';
import PropTypes from 'prop-types';
import { Switch, Route } from 'react-router-dom';
import StackTrace from 'stacktrace-js';
import ColumnLoading from '../components/column_loading';
import BundleColumnError from '../components/bundle_column_error';
import BundleContainer from '../containers/bundle_container';
// Small wrapper to pass multiColumn to the route components
2023-05-23 11:52:27 +03:00
export class WrappedSwitch extends PureComponent {
render () {
const { multiColumn, children } = this.props;
return (
<Switch>
2023-05-23 11:52:27 +03:00
{Children.map(children, child => cloneElement(child, { multiColumn }))}
</Switch>
);
}
}
WrappedSwitch.propTypes = {
multiColumn: PropTypes.bool,
children: PropTypes.node,
};
// Small Wrapper to extract the params from the route and pass
// them to the rendered component, together with the content to
// be rendered inside (the children)
2023-05-23 11:52:27 +03:00
export class WrappedRoute extends Component {
static propTypes = {
component: PropTypes.func.isRequired,
content: PropTypes.node,
multiColumn: PropTypes.bool,
componentParams: PropTypes.object,
};
static defaultProps = {
componentParams: {},
};
static getDerivedStateFromError () {
return {
hasError: true,
};
}
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);
});
}
renderComponent = ({ match }) => {
const { component, content, multiColumn, componentParams } = this.props;
const { hasError, stacktrace } = this.state;
if (hasError) {
return (
<BundleColumnError
stacktrace={stacktrace}
multiColumn={multiColumn}
errorType='error'
/>
);
}
return (
<BundleContainer fetchComponent={component} loading={this.renderLoading} error={this.renderError}>
{Component => <Component params={match.params} multiColumn={multiColumn} {...componentParams}>{content}</Component>}
</BundleContainer>
);
2023-01-30 02:45:35 +02:00
};
renderLoading = () => {
const { multiColumn } = this.props;
return <ColumnLoading multiColumn={multiColumn} />;
2023-01-30 02:45:35 +02:00
};
renderError = (props) => {
return <BundleColumnError {...props} errorType='network' />;
2023-01-30 02:45:35 +02:00
};
render () {
const { component: Component, content, ...rest } = this.props;
return <Route {...rest} render={this.renderComponent} />;
}
}