mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
hub/ReplButton: convert to function component
This commit is contained in:
+23
-25
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user