redux-devtools/packages/redux-devtools-core/src/app/containers/DevTools.js

93 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-01-10 20:23:33 +03:00
import React, { Component } from 'react';
2019-01-03 18:28:31 +03:00
import PropTypes from 'prop-types';
2019-01-03 17:14:25 +03:00
import { withTheme } from 'styled-components';
import getMonitor from '../utils/getMonitor';
class DevTools extends Component {
constructor(props) {
super(props);
this.getMonitor(props, props.monitorState);
}
getMonitor(props, skipUpdate) {
const monitorElement = getMonitor(props);
this.monitorProps = monitorElement.props;
this.Monitor = monitorElement.type;
const update = this.Monitor.update;
if (update) {
let newMonitorState;
const monitorState = props.monitorState;
2019-01-10 21:51:14 +03:00
if (
skipUpdate ||
(monitorState && monitorState.__overwritten__ === props.monitor)
) {
2019-01-03 17:14:25 +03:00
newMonitorState = monitorState;
} else {
newMonitorState = update(this.monitorProps, undefined, {});
if (newMonitorState !== monitorState) {
this.preventRender = true;
}
}
this.dispatch({
type: '@@INIT_MONITOR',
newMonitorState,
update,
monitorProps: this.monitorProps,
2019-01-03 17:14:25 +03:00
});
}
}
UNSAFE_componentWillUpdate(nextProps) {
2019-01-03 17:14:25 +03:00
if (nextProps.monitor !== this.props.monitor) this.getMonitor(nextProps);
}
shouldComponentUpdate(nextProps) {
return (
nextProps.monitor !== this.props.monitor ||
nextProps.liftedState !== this.props.liftedState ||
nextProps.monitorState !== this.props.liftedState ||
nextProps.features !== this.props.features ||
nextProps.theme.scheme !== this.props.theme.scheme
);
}
dispatch = (action) => {
2019-01-03 17:14:25 +03:00
this.props.dispatch(action);
};
render() {
if (this.preventRender) {
this.preventRender = false;
return null;
}
const liftedState = {
...this.props.liftedState,
monitorState: this.props.monitorState,
2019-01-03 17:14:25 +03:00
};
return (
<div className={`monitor monitor-${this.props.monitor}`}>
<this.Monitor
{...liftedState}
{...this.monitorProps}
features={this.props.features}
dispatch={this.dispatch}
theme={this.props.theme}
/>
</div>
);
}
}
DevTools.propTypes = {
liftedState: PropTypes.object,
monitorState: PropTypes.object,
dispatch: PropTypes.func.isRequired,
monitor: PropTypes.string,
features: PropTypes.object.isRequired,
theme: PropTypes.object.isRequired,
2019-01-03 17:14:25 +03:00
};
export default withTheme(DevTools);