2020-08-27 16:19:37 +03:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-12-22 03:50:57 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-08-27 16:19:37 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2018-12-22 03:50:57 +03:00
|
|
|
import * as themes from 'redux-devtools-themes';
|
2020-08-27 16:19:37 +03:00
|
|
|
import { Base16Theme } from 'redux-devtools-themes';
|
2020-12-21 17:08:08 +03:00
|
|
|
import {
|
|
|
|
ActionCreators,
|
|
|
|
LiftedAction,
|
|
|
|
LiftedState,
|
|
|
|
} from '@redux-devtools/core';
|
2020-09-21 02:05:37 +03:00
|
|
|
import debounce from 'lodash.debounce';
|
2020-08-27 16:19:37 +03:00
|
|
|
import {
|
|
|
|
updateScrollTop,
|
|
|
|
startConsecutiveToggle,
|
|
|
|
LogMonitorAction,
|
|
|
|
} from './actions';
|
|
|
|
import reducer, { LogMonitorState } from './reducers';
|
2018-12-22 03:50:57 +03:00
|
|
|
import LogMonitorButtonBar from './LogMonitorButtonBar';
|
|
|
|
import LogMonitorEntryList from './LogMonitorEntryList';
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
2018-12-22 03:50:57 +03:00
|
|
|
const { toggleAction, setActionsActive } = ActionCreators;
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
const styles: {
|
|
|
|
container: React.CSSProperties;
|
|
|
|
elements: React.CSSProperties;
|
|
|
|
} = {
|
2018-12-22 03:50:57 +03:00
|
|
|
container: {
|
|
|
|
fontFamily: 'monaco, Consolas, Lucida Console, monospace',
|
|
|
|
position: 'relative',
|
|
|
|
overflowY: 'hidden',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
minWidth: 300,
|
2020-08-08 23:26:39 +03:00
|
|
|
direction: 'ltr',
|
2018-12-22 03:50:57 +03:00
|
|
|
},
|
|
|
|
elements: {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
overflowX: 'hidden',
|
2020-08-08 23:26:39 +03:00
|
|
|
overflowY: 'auto',
|
|
|
|
},
|
2018-12-22 03:50:57 +03:00
|
|
|
};
|
|
|
|
|
2020-08-28 01:44:02 +03:00
|
|
|
interface ExternalProps<S, A extends Action<unknown>> {
|
|
|
|
dispatch: Dispatch<LogMonitorAction | LiftedAction<S, A, LogMonitorState>>;
|
|
|
|
|
|
|
|
preserveScrollTop: boolean;
|
|
|
|
select: (state: S) => unknown;
|
|
|
|
theme: keyof typeof themes | Base16Theme;
|
|
|
|
expandActionRoot: boolean;
|
|
|
|
expandStateRoot: boolean;
|
|
|
|
markStateDiff: boolean;
|
|
|
|
hideMainButtons?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DefaultProps<S> {
|
|
|
|
select: (state: unknown) => unknown;
|
|
|
|
theme: keyof typeof themes | Base16Theme;
|
|
|
|
preserveScrollTop: boolean;
|
|
|
|
expandActionRoot: boolean;
|
|
|
|
expandStateRoot: boolean;
|
|
|
|
markStateDiff: boolean;
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
export interface LogMonitorProps<S, A extends Action<unknown>>
|
|
|
|
extends LiftedState<S, A, LogMonitorState> {
|
|
|
|
dispatch: Dispatch<LogMonitorAction | LiftedAction<S, A, LogMonitorState>>;
|
|
|
|
|
|
|
|
preserveScrollTop: boolean;
|
|
|
|
select: (state: S) => unknown;
|
|
|
|
theme: keyof typeof themes | Base16Theme;
|
|
|
|
expandActionRoot: boolean;
|
|
|
|
expandStateRoot: boolean;
|
|
|
|
markStateDiff: boolean;
|
|
|
|
hideMainButtons?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-08-28 01:44:02 +03:00
|
|
|
class LogMonitor<S, A extends Action<unknown>> extends PureComponent<
|
|
|
|
LogMonitorProps<S, A>
|
|
|
|
> {
|
2018-12-22 03:50:57 +03:00
|
|
|
static update = reducer;
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func,
|
|
|
|
computedStates: PropTypes.array,
|
|
|
|
actionsById: PropTypes.object,
|
|
|
|
stagedActionIds: PropTypes.array,
|
|
|
|
skippedActionIds: PropTypes.array,
|
|
|
|
monitorState: PropTypes.shape({
|
|
|
|
initialScrollTop: PropTypes.number,
|
2020-08-08 23:26:39 +03:00
|
|
|
consecutiveToggleStartId: PropTypes.number,
|
2018-12-22 03:50:57 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
preserveScrollTop: PropTypes.bool,
|
|
|
|
select: PropTypes.func,
|
2019-01-10 21:51:14 +03:00
|
|
|
theme: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
|
2018-12-22 03:50:57 +03:00
|
|
|
expandActionRoot: PropTypes.bool,
|
|
|
|
expandStateRoot: PropTypes.bool,
|
|
|
|
markStateDiff: PropTypes.bool,
|
2020-08-08 23:26:39 +03:00
|
|
|
hideMainButtons: PropTypes.bool,
|
2018-12-22 03:50:57 +03:00
|
|
|
};
|
|
|
|
|
2020-08-28 01:44:02 +03:00
|
|
|
static defaultProps: DefaultProps<unknown> = {
|
2020-08-27 16:19:37 +03:00
|
|
|
select: (state: unknown) => state,
|
2018-12-22 03:50:57 +03:00
|
|
|
theme: 'nicinabox',
|
|
|
|
preserveScrollTop: true,
|
|
|
|
expandActionRoot: true,
|
|
|
|
expandStateRoot: true,
|
2020-08-08 23:26:39 +03:00
|
|
|
markStateDiff: false,
|
2018-12-22 03:50:57 +03:00
|
|
|
};
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
scrollDown?: boolean;
|
|
|
|
node?: HTMLDivElement | null;
|
2018-12-22 03:50:57 +03:00
|
|
|
|
|
|
|
updateScrollTop = debounce(() => {
|
|
|
|
const node = this.node;
|
|
|
|
this.props.dispatch(updateScrollTop(node ? node.scrollTop : 0));
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
scroll() {
|
|
|
|
const node = this.node;
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.scrollDown) {
|
|
|
|
const { offsetHeight, scrollHeight } = node;
|
|
|
|
node.scrollTop = scrollHeight - offsetHeight;
|
|
|
|
this.scrollDown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const node = this.node;
|
|
|
|
if (!node || !this.props.monitorState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.preserveScrollTop) {
|
|
|
|
node.scrollTop = this.props.monitorState.initialScrollTop;
|
|
|
|
node.addEventListener('scroll', this.updateScrollTop);
|
|
|
|
} else {
|
|
|
|
this.scrollDown = true;
|
|
|
|
this.scroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
const node = this.node;
|
|
|
|
if (node && this.props.preserveScrollTop) {
|
|
|
|
node.removeEventListener('scroll', this.updateScrollTop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: LogMonitorProps<S, A>) {
|
2018-12-22 03:50:57 +03:00
|
|
|
const node = this.node;
|
|
|
|
if (!node) {
|
|
|
|
this.scrollDown = true;
|
|
|
|
} else if (
|
2019-01-10 21:51:14 +03:00
|
|
|
this.props.stagedActionIds.length < nextProps.stagedActionIds.length
|
2018-12-22 03:50:57 +03:00
|
|
|
) {
|
|
|
|
const { scrollTop, offsetHeight, scrollHeight } = node;
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
this.scrollDown =
|
|
|
|
Math.abs(scrollHeight - (scrollTop + offsetHeight)) < 20;
|
2018-12-22 03:50:57 +03:00
|
|
|
} else {
|
|
|
|
this.scrollDown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.scroll();
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
handleToggleAction = (id: number) => {
|
2018-12-22 03:50:57 +03:00
|
|
|
this.props.dispatch(toggleAction(id));
|
2020-08-27 16:19:37 +03:00
|
|
|
};
|
2018-12-22 03:50:57 +03:00
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
handleToggleConsecutiveAction = (id: number) => {
|
2018-12-22 03:50:57 +03:00
|
|
|
const { monitorState, actionsById } = this.props;
|
|
|
|
const { consecutiveToggleStartId } = monitorState;
|
|
|
|
if (consecutiveToggleStartId && actionsById[consecutiveToggleStartId]) {
|
|
|
|
const { skippedActionIds } = this.props;
|
|
|
|
const start = Math.min(consecutiveToggleStartId, id);
|
|
|
|
const end = Math.max(consecutiveToggleStartId, id);
|
|
|
|
const active = skippedActionIds.indexOf(consecutiveToggleStartId) > -1;
|
|
|
|
this.props.dispatch(setActionsActive(start, end + 1, active));
|
|
|
|
this.props.dispatch(startConsecutiveToggle(null));
|
|
|
|
} else if (id > 0) {
|
|
|
|
this.props.dispatch(startConsecutiveToggle(id));
|
|
|
|
}
|
2020-08-27 16:19:37 +03:00
|
|
|
};
|
2018-12-22 03:50:57 +03:00
|
|
|
|
|
|
|
getTheme() {
|
2020-08-27 16:19:37 +03:00
|
|
|
const { theme } = this.props;
|
2018-12-22 03:50:57 +03:00
|
|
|
if (typeof theme !== 'string') {
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof themes[theme] !== 'undefined') {
|
|
|
|
return themes[theme];
|
|
|
|
}
|
|
|
|
|
2019-01-10 20:23:33 +03:00
|
|
|
// eslint-disable-next-line no-console
|
2019-01-10 21:51:14 +03:00
|
|
|
console.warn(
|
|
|
|
'DevTools theme ' + theme + ' not found, defaulting to nicinabox'
|
|
|
|
);
|
2018-12-22 03:50:57 +03:00
|
|
|
return themes.nicinabox;
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:19:37 +03:00
|
|
|
getRef: React.RefCallback<HTMLDivElement> = (node) => {
|
2018-12-22 03:50:57 +03:00
|
|
|
this.node = node;
|
2020-08-27 16:19:37 +03:00
|
|
|
};
|
2018-12-22 03:50:57 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const theme = this.getTheme();
|
|
|
|
const { consecutiveToggleStartId } = this.props.monitorState;
|
|
|
|
|
|
|
|
const {
|
|
|
|
dispatch,
|
|
|
|
actionsById,
|
|
|
|
skippedActionIds,
|
|
|
|
stagedActionIds,
|
|
|
|
computedStates,
|
|
|
|
currentStateIndex,
|
|
|
|
select,
|
|
|
|
expandActionRoot,
|
|
|
|
expandStateRoot,
|
2020-08-08 23:26:39 +03:00
|
|
|
markStateDiff,
|
2018-12-22 03:50:57 +03:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const entryListProps = {
|
|
|
|
theme,
|
|
|
|
actionsById,
|
|
|
|
skippedActionIds,
|
|
|
|
stagedActionIds,
|
|
|
|
computedStates,
|
|
|
|
currentStateIndex,
|
|
|
|
consecutiveToggleStartId,
|
|
|
|
select,
|
|
|
|
expandActionRoot,
|
|
|
|
expandStateRoot,
|
|
|
|
markStateDiff,
|
|
|
|
onActionClick: this.handleToggleAction,
|
2020-08-08 23:26:39 +03:00
|
|
|
onActionShiftClick: this.handleToggleConsecutiveAction,
|
2018-12-22 03:50:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2019-01-10 21:51:14 +03:00
|
|
|
<div style={{ ...styles.container, backgroundColor: theme.base00 }}>
|
|
|
|
{!this.props.hideMainButtons && (
|
2018-12-22 03:50:57 +03:00
|
|
|
<LogMonitorButtonBar
|
|
|
|
theme={theme}
|
|
|
|
dispatch={dispatch}
|
|
|
|
hasStates={computedStates.length > 1}
|
|
|
|
hasSkippedActions={skippedActionIds.length > 0}
|
|
|
|
/>
|
2019-01-10 21:51:14 +03:00
|
|
|
)}
|
2018-12-22 03:50:57 +03:00
|
|
|
<div
|
2019-01-10 21:51:14 +03:00
|
|
|
style={
|
|
|
|
this.props.hideMainButtons
|
|
|
|
? styles.elements
|
|
|
|
: { ...styles.elements, top: 30 }
|
|
|
|
}
|
2018-12-22 03:50:57 +03:00
|
|
|
ref={this.getRef}
|
|
|
|
>
|
|
|
|
<LogMonitorEntryList {...entryListProps} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 01:44:02 +03:00
|
|
|
|
2021-06-18 06:56:36 +03:00
|
|
|
export default LogMonitor as unknown as React.ComponentType<
|
2020-08-28 01:44:02 +03:00
|
|
|
ExternalProps<unknown, Action<unknown>>
|
|
|
|
> & {
|
|
|
|
update(
|
|
|
|
monitorProps: ExternalProps<unknown, Action<unknown>>,
|
2020-09-21 02:05:37 +03:00
|
|
|
state: LogMonitorState | undefined,
|
|
|
|
action: LogMonitorAction
|
|
|
|
): LogMonitorState;
|
2020-08-28 01:44:02 +03:00
|
|
|
defaultProps: DefaultProps<unknown>;
|
|
|
|
};
|