hub/ReplButton: convert to function component

This commit is contained in:
David Lechner
2022-03-12 16:18:38 -06:00
parent 8e4045449c
commit ecbcb09846
+23 -25
View File
@@ -1,37 +1,35 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
// Copyright (c) 2020-2022 The Pybricks Authors
import { connect } from 'react-redux';
import { RootState } from '../reducers';
import React, { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { useSelector } from '../reducers';
import ActionButton, { ActionButtonProps } from '../toolbar/ActionButton';
import { TooltipId } from '../toolbar/i18n';
import { repl } from './actions';
import { HubRuntimeState } from './reducers';
import replIcon from './repl.svg';
type StateProps = Pick<ActionButtonProps, 'enabled'>;
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
type OwnProps = Pick<ActionButtonProps, 'id'> &
Pick<ActionButtonProps, 'keyboardShortcut'>;
type ReplButtonProps = Pick<ActionButtonProps, 'id' | 'keyboardShortcut'>;
const mapStateToProps = (state: RootState): StateProps => ({
enabled: state.hub.runtime === HubRuntimeState.Idle,
});
const ReplButton: React.VoidFunctionComponent<ReplButtonProps> = ({
id,
keyboardShortcut,
}) => {
const enabled = useSelector((s) => s.hub.runtime === HubRuntimeState.Idle);
const dispatch = useDispatch();
const action = useCallback(() => dispatch(repl()), [dispatch]);
const mapDispatchToProps: DispatchProps = {
onAction: repl,
return (
<ActionButton
id={id}
keyboardShortcut={keyboardShortcut}
tooltip={TooltipId.Repl}
icon={replIcon}
enabled={enabled}
onAction={action}
/>
);
};
const mergeProps = (
stateProps: StateProps,
dispatchProps: DispatchProps,
ownProps: OwnProps,
): ActionButtonProps => ({
tooltip: TooltipId.Repl,
icon: replIcon,
...ownProps,
...stateProps,
...dispatchProps,
});
export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(ActionButton);
export default ReplButton;