2019-01-10 20:23:33 +03:00
|
|
|
import React, { Component } from 'react';
|
2019-01-03 17:14:25 +03:00
|
|
|
import { withTheme } from 'styled-components';
|
2020-12-21 17:08:08 +03:00
|
|
|
import { LiftedAction, LiftedState } from '@redux-devtools/core';
|
2020-10-26 02:32:04 +03:00
|
|
|
import { Action } from 'redux';
|
2019-01-03 17:14:25 +03:00
|
|
|
import getMonitor from '../utils/getMonitor';
|
2020-10-26 02:32:04 +03:00
|
|
|
import { InitMonitorAction } from '../actions';
|
|
|
|
import { Features, State } from '../reducers/instances';
|
|
|
|
import { MonitorStateMonitorState } from '../reducers/monitor';
|
2021-09-18 17:00:58 +03:00
|
|
|
import { ThemeFromProvider } from '@redux-devtools/ui';
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
interface Props {
|
|
|
|
monitor: string;
|
|
|
|
liftedState: State;
|
|
|
|
monitorState: MonitorStateMonitorState | undefined;
|
|
|
|
dispatch: (
|
|
|
|
action: LiftedAction<unknown, Action<unknown>, unknown> | InitMonitorAction
|
|
|
|
) => void;
|
|
|
|
features: Features | undefined;
|
|
|
|
theme: ThemeFromProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DevTools extends Component<Props> {
|
|
|
|
monitorProps: unknown;
|
|
|
|
Monitor?: React.ComponentType<
|
|
|
|
LiftedState<unknown, Action<unknown>, unknown>
|
|
|
|
> & {
|
|
|
|
update(
|
|
|
|
monitorProps: unknown,
|
|
|
|
state: unknown | undefined,
|
|
|
|
action: Action<unknown>
|
|
|
|
): unknown;
|
|
|
|
};
|
|
|
|
preventRender?: boolean;
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
2019-01-03 17:14:25 +03:00
|
|
|
super(props);
|
|
|
|
this.getMonitor(props, props.monitorState);
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
getMonitor(props: Props, skipUpdate?: unknown) {
|
2019-01-03 17:14:25 +03:00
|
|
|
const monitorElement = getMonitor(props);
|
|
|
|
this.monitorProps = monitorElement.props;
|
|
|
|
this.Monitor = monitorElement.type;
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
|
|
const update = this.Monitor!.update;
|
2019-01-03 17:14:25 +03:00
|
|
|
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 {
|
2020-10-26 02:32:04 +03:00
|
|
|
newMonitorState = update(
|
|
|
|
this.monitorProps,
|
|
|
|
undefined,
|
|
|
|
{} as Action<unknown>
|
|
|
|
);
|
2019-01-03 17:14:25 +03:00
|
|
|
if (newMonitorState !== monitorState) {
|
|
|
|
this.preventRender = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.dispatch({
|
|
|
|
type: '@@INIT_MONITOR',
|
|
|
|
newMonitorState,
|
|
|
|
update,
|
2020-08-08 23:26:39 +03:00
|
|
|
monitorProps: this.monitorProps,
|
2019-01-03 17:14:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
UNSAFE_componentWillUpdate(nextProps: Props) {
|
2019-01-03 17:14:25 +03:00
|
|
|
if (nextProps.monitor !== this.props.monitor) this.getMonitor(nextProps);
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
shouldComponentUpdate(nextProps: Props) {
|
2019-01-03 17:14:25 +03:00
|
|
|
return (
|
|
|
|
nextProps.monitor !== this.props.monitor ||
|
|
|
|
nextProps.liftedState !== this.props.liftedState ||
|
2020-10-26 02:32:04 +03:00
|
|
|
nextProps.monitorState !== this.props.monitorState ||
|
2019-01-03 17:14:25 +03:00
|
|
|
nextProps.features !== this.props.features ||
|
|
|
|
nextProps.theme.scheme !== this.props.theme.scheme
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:32:04 +03:00
|
|
|
dispatch = (
|
|
|
|
action: LiftedAction<unknown, Action<unknown>, unknown> | InitMonitorAction
|
|
|
|
) => {
|
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,
|
2020-08-08 23:26:39 +03:00
|
|
|
monitorState: this.props.monitorState,
|
2019-01-03 17:14:25 +03:00
|
|
|
};
|
2020-10-26 02:32:04 +03:00
|
|
|
const MonitorAsAny = this.Monitor as any;
|
2019-01-03 17:14:25 +03:00
|
|
|
return (
|
|
|
|
<div className={`monitor monitor-${this.props.monitor}`}>
|
2020-10-26 02:32:04 +03:00
|
|
|
<MonitorAsAny
|
2019-01-03 17:14:25 +03:00
|
|
|
{...liftedState}
|
|
|
|
{...this.monitorProps}
|
|
|
|
features={this.props.features}
|
|
|
|
dispatch={this.dispatch}
|
|
|
|
theme={this.props.theme}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(DevTools);
|