From 8e4045449cd6fd5d33df355326f28d3bb78964c5 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 12 Mar 2022 16:07:31 -0600 Subject: [PATCH] src: use prop unpacking pattern This makes the code a bit shorter by not having to use `props.` all of the time. Also make everything VoidFunctionComponent while we are touching this. --- src/about/AboutDialog.tsx | 9 ++-- src/app/App.tsx | 6 +-- src/explorer/Explorer.tsx | 34 +++++++------ src/explorer/FileNameFormGroup.tsx | 32 ++++++------ src/explorer/NewFileWizard.tsx | 11 ++-- src/explorer/RenameFileDialog.tsx | 20 ++++---- src/firmware/FlashButton.tsx | 6 +-- src/hub/BluetoothButton.tsx | 6 +-- src/hub/RunButton.tsx | 11 ++-- src/hub/StopButton.tsx | 13 +++-- src/licenses/LicenseDialog.tsx | 22 ++++---- src/notifications/NotificationAction.tsx | 9 ++-- src/notifications/NotificationMessage.tsx | 9 ++-- .../UnexpectedErrorNotification.tsx | 18 +++---- src/settings/SettingsButton.tsx | 16 ++++-- src/settings/SettingsDrawer.tsx | 9 ++-- src/toolbar/ActionButton.tsx | 50 +++++++++++-------- src/toolbar/OpenFileButton.tsx | 43 ++++++++++------ 18 files changed, 193 insertions(+), 131 deletions(-) diff --git a/src/about/AboutDialog.tsx b/src/about/AboutDialog.tsx index 2081e9e5..48aa2bab 100644 --- a/src/about/AboutDialog.tsx +++ b/src/about/AboutDialog.tsx @@ -24,7 +24,10 @@ import './about.scss'; type AboutDialogProps = { isOpen: boolean; onClose: () => void }; -const AboutDialog: React.FunctionComponent = (props) => { +const AboutDialog: React.VoidFunctionComponent = ({ + isOpen, + onClose, +}) => { const [isLicenseDialogOpen, setIsLicenseDialogOpen] = useState(false); const [i18n] = useI18n({ id: 'about', translations: { en }, fallback: en }); @@ -32,8 +35,8 @@ const AboutDialog: React.FunctionComponent = (props) => { return (
diff --git a/src/app/App.tsx b/src/app/App.tsx index 64445183..f020eae1 100644 --- a/src/app/App.tsx +++ b/src/app/App.tsx @@ -127,7 +127,7 @@ type AppProps = { onEditorChanged?: (editor: EditorType) => void; }; -const App: React.VoidFunctionComponent = (props) => { +const App: React.VoidFunctionComponent = ({ onEditorChanged }) => { const darkMode = useSelector((s): boolean => s.settings.darkMode); const showDocs = useSelector((s): boolean => s.settings.showDocs); const [isDragging, setIsDragging] = useState(false); @@ -139,8 +139,8 @@ const App: React.VoidFunctionComponent = (props) => { setEditor: (editor) => { setEditor(editor); - if (props.onEditorChanged) { - props.onEditorChanged(editor); + if (onEditorChanged) { + onEditorChanged(editor); } }, }), diff --git a/src/explorer/Explorer.tsx b/src/explorer/Explorer.tsx index 5fdafd4c..8b80180c 100644 --- a/src/explorer/Explorer.tsx +++ b/src/explorer/Explorer.tsx @@ -46,16 +46,22 @@ type ActionButtonProps = { onClick: () => void; }; -const ActionButton: React.VoidFunctionComponent = (props) => { +const ActionButton: React.VoidFunctionComponent = ({ + icon, + toolTipId, + toolTipReplacements, + disabled, + onClick, +}) => { const [i18n] = useI18n({ id: 'explorer', translations: { en }, fallback: en }); return ( ))} @@ -107,7 +107,10 @@ type LicenseDialogProps = { onClose(): void; }; -const LicenseDialog: React.VoidFunctionComponent = (props) => { +const LicenseDialog: React.VoidFunctionComponent = ({ + isOpen, + onClose, +}) => { const infoDiv = React.useRef(null); const dispatch = useDispatch(); @@ -116,10 +119,11 @@ const LicenseDialog: React.VoidFunctionComponent = (props) = return ( dispatch(fetchList())} className="pb-license-dialog" - {...props} + title={i18n.translate(LicenseStringId.Title)} + isOpen={isOpen} + onOpening={() => dispatch(fetchList())} + onClose={onClose} >
diff --git a/src/notifications/NotificationAction.tsx b/src/notifications/NotificationAction.tsx index 2f50465e..e884067b 100644 --- a/src/notifications/NotificationAction.tsx +++ b/src/notifications/NotificationAction.tsx @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2021 The Pybricks Authors +// Copyright (c) 2021-2022 The Pybricks Authors // provides translation for notification text @@ -13,14 +13,17 @@ type NotificationActionProps = { replacements?: Replacements; }; -const NotificationAction: React.FC = (props) => { +const NotificationAction: React.VoidFunctionComponent = ({ + messageId, + replacements, +}) => { const [i18n] = useI18n({ id: 'notification', translations: { en }, fallback: en, }); - return <>{i18n.translate(props.messageId, props.replacements)}; + return <>{i18n.translate(messageId, replacements)}; }; export default NotificationAction; diff --git a/src/notifications/NotificationMessage.tsx b/src/notifications/NotificationMessage.tsx index 2eb20119..5c8f4f95 100644 --- a/src/notifications/NotificationMessage.tsx +++ b/src/notifications/NotificationMessage.tsx @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2021 The Pybricks Authors +// Copyright (c) 2021-2022 The Pybricks Authors // provides translation for notification text @@ -13,14 +13,17 @@ type NotificationMessageProps = { replacements?: Replacements; }; -const NotificationMessage: React.FC = (props) => { +const NotificationMessage: React.VoidFunctionComponent = ({ + messageId, + replacements, +}) => { const [i18n] = useI18n({ id: 'notification', translations: { en }, fallback: en, }); - let message = i18n.translate(props.messageId, props.replacements) as + let message = i18n.translate(messageId, replacements) as | React.ReactElement | string; diff --git a/src/notifications/UnexpectedErrorNotification.tsx b/src/notifications/UnexpectedErrorNotification.tsx index 8747b284..49ed8ace 100644 --- a/src/notifications/UnexpectedErrorNotification.tsx +++ b/src/notifications/UnexpectedErrorNotification.tsx @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2021 The Pybricks Authors +// Copyright (c) 2021-2022 The Pybricks Authors // Provides special notification contents for unexpected errors. @@ -14,9 +14,9 @@ type UnexpectedErrorNotificationProps = { err: Error; }; -const UnexpectedErrorNotification: React.FC = ( - props, -) => { +const UnexpectedErrorNotification: React.VoidFunctionComponent< + UnexpectedErrorNotificationProps +> = ({ messageId, err }) => { const [i18n] = useI18n({ id: 'notification', translations: { en }, @@ -25,9 +25,7 @@ const UnexpectedErrorNotification: React.FC = return ( <> -

- {i18n.translate(props.messageId, { errorMessage: props.err.message })} -

+

{i18n.translate(messageId, { errorMessage: err.message })}