2020-08-04 23:13:22 +03:00
|
|
|
import React, { Component, createRef } from 'react';
|
2023-01-02 22:19:13 +03:00
|
|
|
import { tree } from 'd3-state-visualizer';
|
|
|
|
import type { Options } from 'd3-state-visualizer';
|
2020-09-22 04:23:38 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2020-12-21 17:08:08 +03:00
|
|
|
import { LiftedAction, LiftedState } from '@redux-devtools/core';
|
2020-09-22 04:23:38 +03:00
|
|
|
import * as themes from 'redux-devtools-themes';
|
|
|
|
import { ChartMonitorState } from './reducers';
|
2020-08-04 23:13:22 +03:00
|
|
|
|
|
|
|
const wrapperStyle = {
|
|
|
|
width: '100%',
|
2020-08-08 23:26:39 +03:00
|
|
|
height: '100%',
|
2020-08-04 23:13:22 +03:00
|
|
|
};
|
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
export interface Props<S, A extends Action<unknown>>
|
2023-01-02 22:19:13 +03:00
|
|
|
extends LiftedState<S, A, ChartMonitorState>,
|
|
|
|
Options {
|
2020-09-22 04:23:38 +03:00
|
|
|
dispatch: Dispatch<LiftedAction<S, A, ChartMonitorState>>;
|
|
|
|
preserveScrollTop: boolean;
|
|
|
|
select: (state: S) => unknown;
|
2021-09-20 00:59:01 +03:00
|
|
|
theme: keyof typeof themes | themes.Base16Theme;
|
2020-09-22 04:23:38 +03:00
|
|
|
invertTheme: boolean;
|
|
|
|
|
|
|
|
state: S | null;
|
|
|
|
defaultIsVisible?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Chart<S, A extends Action<unknown>> extends Component<Props<S, A>> {
|
|
|
|
divRef = createRef<HTMLDivElement>();
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
renderChart?: (state?: {} | null | undefined) => void;
|
2020-08-04 23:13:22 +03:00
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { select, state, defaultIsVisible } = this.props;
|
2020-09-22 04:23:38 +03:00
|
|
|
this.renderChart = tree(this.divRef.current!, this.props);
|
2020-08-04 23:13:22 +03:00
|
|
|
if (defaultIsVisible) {
|
2020-09-22 04:23:38 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
this.renderChart(select(state!) as {} | null | undefined);
|
2020-08-04 23:13:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props<S, A>) {
|
2020-08-04 23:13:22 +03:00
|
|
|
const { state, select, monitorState } = nextProps;
|
|
|
|
|
|
|
|
if (monitorState.isVisible !== false) {
|
2020-09-22 04:23:38 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
this.renderChart!(select(state!) as {} | null | undefined);
|
2020-08-04 23:13:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-08-05 16:12:31 +03:00
|
|
|
return <div style={wrapperStyle} ref={this.divRef} />;
|
2020-08-04 23:13:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Chart;
|