p5.js-web-editor/client/modules/App/App.jsx

77 lines
2.1 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
2016-05-18 19:37:59 +02:00
import { connect } from 'react-redux';
import getConfig from '../../utils/getConfig';
2016-06-23 00:36:04 +02:00
import DevTools from './components/DevTools';
import { setPreviousPath } from '../IDE/actions/ide';
import { setLanguage } from '../IDE/actions/preferences';
2016-05-18 19:37:59 +02:00
class App extends React.Component {
constructor(props, context) {
super(props, context);
2016-06-24 00:29:55 +02:00
this.state = { isMounted: false };
2016-06-23 00:36:04 +02:00
}
componentDidMount() {
2016-06-27 23:22:54 +02:00
this.setState({ isMounted: true }); // eslint-disable-line react/no-did-mount-set-state
document.body.className = this.props.theme;
2016-05-18 19:37:59 +02:00
}
componentWillReceiveProps(nextProps) {
const locationWillChange = nextProps.location !== this.props.location;
const shouldSkipRemembering = nextProps.location.state && nextProps.location.state.skipSavingPath === true;
if (locationWillChange && !shouldSkipRemembering) {
this.props.setPreviousPath(this.props.location.pathname);
}
if (this.props.language !== nextProps.language) {
this.props.setLanguage(nextProps.language, { persistPreference: false });
}
}
componentDidUpdate(prevProps) {
if (this.props.theme !== prevProps.theme) {
document.body.className = this.props.theme;
}
}
2016-05-18 19:37:59 +02:00
render() {
return (
2016-09-14 18:46:54 +02:00
<div className="app">
2020-08-03 21:34:34 +02:00
{this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
2016-06-24 00:29:55 +02:00
{this.props.children}
2016-05-18 19:37:59 +02:00
</div>
);
}
}
App.propTypes = {
children: PropTypes.element,
location: PropTypes.shape({
2019-09-08 16:43:16 +02:00
pathname: PropTypes.string,
state: PropTypes.shape({
skipSavingPath: PropTypes.bool,
}),
}).isRequired,
setPreviousPath: PropTypes.func.isRequired,
setLanguage: PropTypes.func.isRequired,
language: PropTypes.string,
theme: PropTypes.string,
};
App.defaultProps = {
children: null,
language: null,
theme: 'light'
};
const mapStateToProps = state => ({
theme: state.preferences.theme,
language: state.preferences.language,
});
const mapDispatchToProps = { setPreviousPath, setLanguage };
export default connect(mapStateToProps, mapDispatchToProps)(App);