mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 17:45:22 +00:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2020 The Pybricks Authors
|
|
|
|
import { connect } from 'react-redux';
|
|
import { Dispatch } from '../actions';
|
|
import * as editor from '../actions/editor';
|
|
import * as notification from '../actions/notification';
|
|
import { RootState } from '../reducers';
|
|
import OpenFileButton, { OpenFileButtonProps } from './OpenFileButton';
|
|
import { TooltipId } from './button-i18n';
|
|
import openIcon from './images/open.svg';
|
|
|
|
type StateProps = Pick<OpenFileButtonProps, 'enabled'>;
|
|
type DispatchProps = Pick<OpenFileButtonProps, 'onFile' | 'onReject'>;
|
|
type OwnProps = Pick<OpenFileButtonProps, 'id'>;
|
|
|
|
const mapStateToProps = (state: RootState): StateProps => ({
|
|
enabled: state.editor.current !== null,
|
|
});
|
|
|
|
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
|
|
onFile: (data): void => {
|
|
dispatch(editor.open(data));
|
|
},
|
|
onReject: (file): void => {
|
|
dispatch(
|
|
notification.add('error', `'${file.name}' is not a valid python file.`),
|
|
);
|
|
},
|
|
});
|
|
|
|
const mergeProps = (
|
|
stateProps: StateProps,
|
|
dispatchProps: DispatchProps,
|
|
ownProps: OwnProps,
|
|
): OpenFileButtonProps => ({
|
|
fileExtension: '.py',
|
|
tooltip: TooltipId.Open,
|
|
icon: openIcon,
|
|
...ownProps,
|
|
...stateProps,
|
|
...dispatchProps,
|
|
});
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(OpenFileButton);
|