2020-09-22 04:23:38 +03:00
|
|
|
import React, { CSSProperties, PureComponent } from 'react';
|
2020-08-04 23:13:22 +03:00
|
|
|
import * as themes from 'redux-devtools-themes';
|
2020-12-21 17:08:08 +03:00
|
|
|
import {
|
|
|
|
ActionCreators,
|
|
|
|
LiftedAction,
|
|
|
|
LiftedState,
|
|
|
|
} from '@redux-devtools/core';
|
2020-08-04 23:13:22 +03:00
|
|
|
import deepmerge from 'deepmerge';
|
2020-09-22 04:23:38 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2023-01-02 22:19:13 +03:00
|
|
|
import type { Options } from 'd3-state-visualizer';
|
2020-08-04 23:13:22 +03:00
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
import reducer, { ChartMonitorState } from './reducers';
|
|
|
|
import Chart, { Props } from './Chart';
|
2023-12-18 06:03:02 +03:00
|
|
|
|
2020-08-04 23:13:22 +03:00
|
|
|
const { reset, rollback, commit, sweep, toggleAction } = ActionCreators;
|
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
const styles: { container: CSSProperties } = {
|
2020-08-04 23:13:22 +03:00
|
|
|
container: {
|
|
|
|
fontFamily: 'monaco, Consolas, Lucida Console, monospace',
|
|
|
|
position: 'relative',
|
|
|
|
overflowY: 'hidden',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
2020-08-08 23:26:39 +03:00
|
|
|
minWidth: 300,
|
|
|
|
},
|
2020-08-04 23:13:22 +03:00
|
|
|
};
|
|
|
|
|
2021-09-20 00:59:01 +03:00
|
|
|
function invertColors(theme: themes.Base16Theme) {
|
2020-08-04 23:13:22 +03:00
|
|
|
return {
|
|
|
|
...theme,
|
|
|
|
base00: theme.base07,
|
|
|
|
base01: theme.base06,
|
|
|
|
base02: theme.base05,
|
|
|
|
base03: theme.base04,
|
|
|
|
base04: theme.base03,
|
|
|
|
base05: theme.base02,
|
|
|
|
base06: theme.base01,
|
2020-08-08 23:26:39 +03:00
|
|
|
base07: theme.base00,
|
2020-08-04 23:13:22 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
export interface ChartMonitorProps<S, A extends Action<string>>
|
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;
|
|
|
|
|
|
|
|
defaultIsVisible?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
class ChartMonitor<S, A extends Action<string>> extends PureComponent<
|
2020-09-22 04:23:38 +03:00
|
|
|
ChartMonitorProps<S, A>
|
|
|
|
> {
|
2020-08-04 23:13:22 +03:00
|
|
|
static update = reducer;
|
|
|
|
|
|
|
|
static defaultProps = {
|
2020-09-22 04:23:38 +03:00
|
|
|
select: (state: unknown) => state,
|
2020-08-04 23:13:22 +03:00
|
|
|
theme: 'nicinabox',
|
|
|
|
preserveScrollTop: true,
|
2020-08-08 23:26:39 +03:00
|
|
|
invertTheme: false,
|
2020-08-04 23:13:22 +03:00
|
|
|
};
|
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
handleRollback = () => {
|
2020-08-04 23:13:22 +03:00
|
|
|
this.props.dispatch(rollback());
|
2020-09-22 04:23:38 +03:00
|
|
|
};
|
2020-08-04 23:13:22 +03:00
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
handleSweep = () => {
|
2020-08-04 23:13:22 +03:00
|
|
|
this.props.dispatch(sweep());
|
2020-09-22 04:23:38 +03:00
|
|
|
};
|
2020-08-04 23:13:22 +03:00
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
handleCommit = () => {
|
2020-08-04 23:13:22 +03:00
|
|
|
this.props.dispatch(commit());
|
2020-09-22 04:23:38 +03:00
|
|
|
};
|
2020-08-04 23:13:22 +03:00
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
handleToggleAction = (id: number) => {
|
2020-08-04 23:13:22 +03:00
|
|
|
this.props.dispatch(toggleAction(id));
|
2020-09-22 04:23:38 +03:00
|
|
|
};
|
2020-08-04 23:13:22 +03:00
|
|
|
|
2020-09-22 04:23:38 +03:00
|
|
|
handleReset = () => {
|
2020-08-04 23:13:22 +03:00
|
|
|
this.props.dispatch(reset());
|
2020-09-22 04:23:38 +03:00
|
|
|
};
|
2020-08-04 23:13:22 +03:00
|
|
|
|
|
|
|
getTheme() {
|
2020-09-22 04:23:38 +03:00
|
|
|
const { theme, invertTheme } = this.props;
|
2020-08-04 23:13:22 +03:00
|
|
|
if (typeof theme !== 'string') {
|
|
|
|
return invertTheme ? invertColors(theme) : theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof themes[theme] !== 'undefined') {
|
|
|
|
return invertTheme ? invertColors(themes[theme]) : themes[theme];
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:12:31 +03:00
|
|
|
console.warn(
|
2023-07-12 21:03:20 +03:00
|
|
|
'DevTools theme ' + theme + ' not found, defaulting to nicinabox',
|
2020-08-05 16:12:31 +03:00
|
|
|
);
|
2020-08-04 23:13:22 +03:00
|
|
|
return invertTheme ? invertColors(themes.nicinabox) : themes.nicinabox;
|
|
|
|
}
|
|
|
|
|
2023-01-02 22:19:13 +03:00
|
|
|
getChartOptions(props = this.props): Props<S, A> {
|
|
|
|
const { computedStates } = props;
|
2020-08-04 23:13:22 +03:00
|
|
|
const theme = this.getTheme();
|
|
|
|
|
2023-01-02 22:19:13 +03:00
|
|
|
const defaultOptions = {
|
|
|
|
state: computedStates.length
|
|
|
|
? computedStates[props.currentStateIndex].state
|
|
|
|
: null,
|
|
|
|
isSorted: false,
|
|
|
|
heightBetweenNodesCoeff: 1,
|
|
|
|
widthBetweenNodesCoeff: 1.3,
|
|
|
|
tooltipOptions: {
|
|
|
|
disabled: false,
|
|
|
|
offset: { left: 30, top: 10 },
|
|
|
|
indentationSize: 2,
|
|
|
|
styles: {
|
|
|
|
'background-color': theme.base06,
|
|
|
|
opacity: '0.7',
|
|
|
|
'border-radius': '5px',
|
|
|
|
padding: '5px',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
chartStyles: {
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
},
|
|
|
|
nodeStyleOptions: {
|
2020-08-04 23:13:22 +03:00
|
|
|
colors: {
|
2020-08-05 16:12:31 +03:00
|
|
|
default: theme.base0B,
|
2020-08-04 23:13:22 +03:00
|
|
|
collapsed: theme.base0B,
|
2020-08-08 23:26:39 +03:00
|
|
|
parent: theme.base0E,
|
2020-08-04 23:13:22 +03:00
|
|
|
},
|
2020-08-08 23:26:39 +03:00
|
|
|
radius: 7,
|
2020-08-04 23:13:22 +03:00
|
|
|
},
|
2023-01-02 22:19:13 +03:00
|
|
|
textStyleOptions: {
|
2020-08-04 23:13:22 +03:00
|
|
|
colors: {
|
2020-08-05 16:12:31 +03:00
|
|
|
default: theme.base0D,
|
2020-08-08 23:26:39 +03:00
|
|
|
hover: theme.base06,
|
|
|
|
},
|
|
|
|
},
|
2020-08-04 23:13:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return deepmerge(defaultOptions, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const theme = this.getTheme();
|
|
|
|
|
|
|
|
return (
|
2020-08-05 16:12:31 +03:00
|
|
|
<div style={{ ...styles.container, backgroundColor: theme.base07 }}>
|
2023-01-02 22:19:13 +03:00
|
|
|
<Chart {...this.getChartOptions()} />
|
2020-08-04 23:13:22 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChartMonitor;
|