diff --git a/src/licenses/LicenseDialog.tsx b/src/licenses/LicenseDialog.tsx index 1838aa71..d99356b8 100644 --- a/src/licenses/LicenseDialog.tsx +++ b/src/licenses/LicenseDialog.tsx @@ -12,155 +12,133 @@ import { Dialog, NonIdealState, } from '@blueprintjs/core'; -import { WithI18nProps, withI18n } from '@shopify/react-i18n'; +import { useI18n } from '@shopify/react-i18n'; import React from 'react'; -import { connect } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { appName } from '../app/constants'; import { RootState } from '../reducers'; import { fetchList, select } from './actions'; import { LicenseStringId } from './i18n'; import en from './i18n.en.json'; -import { LicenseInfo, LicenseList } from './reducers'; +import { LicenseInfo } from './reducers'; import './license.scss'; -type StateProps = { - licenseList: LicenseList | null; - licenseInfo: LicenseInfo | null; +type LicenseListPanelProps = { + onItemClick(info: LicenseInfo): void; }; -type DispatchProps = { - onOpening: () => void; - onSelectPackage: (info: LicenseInfo) => void; +const LicenseListPanel: React.VoidFunctionComponent = ( + props, +) => { + const licenseList = useSelector((state: RootState) => state.licenses.list); + + return ( +
+ {licenseList === null ? ( + // TODO: this should be translated and hooked to + // state indicating if download is in progress + // or there was an actual failure. + Failed to load license data. + ) : ( + + {licenseList.map((info, i) => ( + + ))} + + )} +
+ ); }; -type OwnProps = { +const LicenseInfoPanel = React.forwardRef((_props, ref) => { + const licenseInfo = useSelector((state: RootState) => state.licenses.selected); + + const [i18n] = useI18n({ id: 'license', translations: { en }, fallback: en }); + + return ( +
+ {licenseInfo == null ? ( + + {i18n.translate(LicenseStringId.SelectPackageHelp)} + + ) : ( +
+ +

+ + {i18n.translate(LicenseStringId.PackageLabel)} + {' '} + {licenseInfo.name}{' '} + + v{licenseInfo.version} + +

+ {licenseInfo.author && ( +

+ + {i18n.translate(LicenseStringId.AuthorLabel)} + {' '} + {licenseInfo.author} +

+ )} +

+ + {i18n.translate(LicenseStringId.LicenseLabel)} + {' '} + {licenseInfo.license} +

+
+
+
{licenseInfo.licenseText}
+
+
+ )} +
+ ); +}); + +LicenseInfoPanel.displayName = 'LicenseInfoPanel'; + +type LicenseDialogProps = { isOpen: boolean; onClose(): void; }; -type LicenseDialogProps = StateProps & DispatchProps & OwnProps & WithI18nProps; +const LicenseDialog: React.VoidFunctionComponent = (props) => { + const infoDiv = React.useRef(null); -class LicenseDialog extends React.Component { - render(): JSX.Element { - const infoDiv = React.createRef(); + const dispatch = useDispatch(); - const { - licenseList, - licenseInfo, - onOpening, - onSelectPackage, - isOpen, - onClose, - i18n, - } = this.props; - return ( - onOpening()} - onClose={() => onClose()} - className="pb-license-dialog" - > -
- - {i18n.translate(LicenseStringId.Description, { - name: appName, - })} - - -
- {licenseList === null ? ( - // TODO: this should be translated and hooked to - // state indicating if download is in progress - // or there was an actual failure. - - Failed to load license data. - - ) : ( - - {licenseList.map((info) => ( - - ))} - - )} -
-
- {licenseInfo == null ? ( - - {i18n.translate(LicenseStringId.SelectPackageHelp)} - - ) : ( -
- -

- - {i18n.translate( - LicenseStringId.PackageLabel, - )} - {' '} - {licenseInfo.name}{' '} - - v{licenseInfo.version} - -

- {licenseInfo.author && ( -

- - {' '} - {i18n.translate( - LicenseStringId.AuthorLabel, - )} - {' '} - {licenseInfo.author} -

- )} -

- - {' '} - {i18n.translate( - LicenseStringId.LicenseLabel, - )} - {' '} - {licenseInfo.license} -

-
-
-
{licenseInfo.licenseText}
-
-
- )} -
-
-
-
- ); - } -} + const [i18n] = useI18n({ id: 'license', translations: { en }, fallback: en }); -const mapStateToProps = (state: RootState): StateProps => ({ - licenseList: state.licenses.list, - licenseInfo: state.licenses.selected, -}); - -const mapDispatchToProps: DispatchProps = { - onOpening: fetchList, - onSelectPackage: select, + return ( + dispatch(fetchList())} + className="pb-license-dialog" + {...props} + > +
+ + {i18n.translate(LicenseStringId.Description, { + name: appName, + })} + + + { + infoDiv.current?.scrollTo(0, 0); + dispatch(select(info)); + }} + /> + + +
+
+ ); }; -export default connect( - mapStateToProps, - mapDispatchToProps, -)(withI18n({ id: 'license', fallback: en, translations: { en } })(LicenseDialog)); +export default LicenseDialog;