redux-devtools/packages/redux-devtools-inspector/src/ActionListRow.jsx

168 lines
4.9 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import { PropTypes } from 'prop-types';
import shouldPureComponentUpdate from 'react-pure-render/function';
import dateformat from 'dateformat';
import debounce from 'lodash.debounce';
import RightSlider from './RightSlider';
const BUTTON_SKIP = 'Skip';
const BUTTON_JUMP = 'Jump';
export default class ActionListRow extends Component {
state = { hover: false };
static propTypes = {
styling: PropTypes.func.isRequired,
isSelected: PropTypes.bool.isRequired,
action: PropTypes.object.isRequired,
isInFuture: PropTypes.bool.isRequired,
isInitAction: PropTypes.bool.isRequired,
onSelect: PropTypes.func.isRequired,
timestamps: PropTypes.shape({
current: PropTypes.number.isRequired,
previous: PropTypes.number.isRequired,
}).isRequired,
isSkipped: PropTypes.bool.isRequired,
};
2019-01-10 21:51:14 +03:00
shouldComponentUpdate = shouldPureComponentUpdate;
render() {
2019-01-10 21:51:14 +03:00
const {
styling,
isSelected,
action,
actionId,
isInitAction,
onSelect,
timestamps,
isSkipped,
isInFuture,
hideActionButtons,
2019-01-10 21:51:14 +03:00
} = this.props;
const { hover } = this.state;
const timeDelta = timestamps.current - timestamps.previous;
2019-01-10 21:51:14 +03:00
const showButtons = (hover && !isInitAction) || isSkipped;
const isButtonSelected = (btn) => btn === BUTTON_SKIP && isSkipped;
let actionType = action.type;
if (typeof actionType === 'undefined') actionType = '<UNDEFINED>';
else if (actionType === null) actionType = '<NULL>';
else actionType = actionType.toString() || '<EMPTY>';
return (
2019-01-10 21:51:14 +03:00
<div
onClick={onSelect}
onMouseEnter={!hideActionButtons && this.handleMouseEnter}
onMouseLeave={!hideActionButtons && this.handleMouseLeave}
onMouseDown={this.handleMouseDown}
onMouseUp={this.handleMouseEnter}
data-id={actionId}
2019-01-10 21:51:14 +03:00
{...styling(
[
'actionListItem',
isSelected && 'actionListItemSelected',
isSkipped && 'actionListItemSkipped',
isInFuture && 'actionListFromFuture',
2019-01-10 21:51:14 +03:00
],
isSelected,
action
)}
>
<div
{...styling([
'actionListItemName',
isSkipped && 'actionListItemNameSkipped',
2019-01-10 21:51:14 +03:00
])}
>
{actionType}
</div>
2019-01-10 21:51:14 +03:00
{hideActionButtons ? (
<RightSlider styling={styling} shown>
<div {...styling('actionListItemTime')}>
2019-01-10 21:51:14 +03:00
{timeDelta === 0
? '+00:00:00'
: dateformat(
timeDelta,
timestamps.previous ? '+MM:ss.L' : 'h:MM:ss.L'
)}
</div>
</RightSlider>
2019-01-10 21:51:14 +03:00
) : (
<div {...styling('actionListItemButtons')}>
<RightSlider styling={styling} shown={!showButtons} rotate>
<div {...styling('actionListItemTime')}>
2019-01-10 21:51:14 +03:00
{timeDelta === 0
? '+00:00:00'
: dateformat(
timeDelta,
timestamps.previous ? '+MM:ss.L' : 'h:MM:ss.L'
)}
</div>
</RightSlider>
<RightSlider styling={styling} shown={showButtons} rotate>
<div {...styling('actionListItemSelector')}>
2019-01-10 21:51:14 +03:00
{[BUTTON_JUMP, BUTTON_SKIP].map(
(btn) =>
2019-01-10 21:51:14 +03:00
(!isInitAction || btn !== BUTTON_SKIP) && (
<div
key={btn}
onClick={this.handleButtonClick.bind(this, btn)}
{...styling(
[
'selectorButton',
isButtonSelected(btn) && 'selectorButtonSelected',
'selectorButtonSmall',
2019-01-10 21:51:14 +03:00
],
isButtonSelected(btn),
true
)}
>
{btn}
</div>
)
)}
</div>
</RightSlider>
</div>
2019-01-10 21:51:14 +03:00
)}
</div>
);
}
handleButtonClick(btn, e) {
e.stopPropagation();
2019-01-10 21:51:14 +03:00
switch (btn) {
case BUTTON_SKIP:
this.props.onToggleClick();
break;
case BUTTON_JUMP:
this.props.onJumpClick();
break;
}
}
handleMouseEnter = (e) => {
if (this.hover) return;
this.handleMouseLeave.cancel();
this.handleMouseEnterDebounced(e.buttons);
2019-01-10 21:51:14 +03:00
};
handleMouseEnterDebounced = debounce((buttons) => {
if (buttons) return;
this.setState({ hover: true });
2019-01-10 21:51:14 +03:00
}, 150);
handleMouseLeave = debounce(() => {
this.handleMouseEnterDebounced.cancel();
if (this.state.hover) this.setState({ hover: false });
2019-01-10 21:51:14 +03:00
}, 100);
handleMouseDown = (e) => {
if (e.target.className.indexOf('selectorButton') === 0) return;
this.handleMouseLeave();
2019-01-10 21:51:14 +03:00
};
}