From 390650fd6f175539e653647b66306872294a215a Mon Sep 17 00:00:00 2001 From: Amir Ekbatanifard Date: Sun, 24 Nov 2024 17:07:09 +0330 Subject: [PATCH] refactor: move JSX components out side another component --- packages/extension-ui/src/Popup/index.tsx | 431 ++++++++++++++++++---- 1 file changed, 349 insertions(+), 82 deletions(-) diff --git a/packages/extension-ui/src/Popup/index.tsx b/packages/extension-ui/src/Popup/index.tsx index d0fbc8a85..36cd09f3d 100644 --- a/packages/extension-ui/src/Popup/index.tsx +++ b/packages/extension-ui/src/Popup/index.tsx @@ -1,6 +1,8 @@ // Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors // SPDX-License-Identifier: Apache-2.0 +/* eslint-disable react/jsx-max-props-per-line */ + import type { AccountJson, AccountsContext, AuthorizeRequest, MetadataRequest, SigningRequest } from '@polkadot/extension-base/background/types'; import type { CurrencyItemType } from '@polkadot/extension-polkagate/src/fullscreen/homeFullScreen/partials/Currency'; import type { AlertType, APIs, Fetching, LatestRefs, Prices, PricesInCurrencies, UserAddedChains } from '@polkadot/extension-polkagate/src/util/types'; @@ -118,6 +120,50 @@ function initAccountContext (accounts: AccountJson[]): AccountsContext { }; } +interface WrapWithErrorBoundaryProps { + children: React.ReactElement; + isExtensionMode: boolean; + trigger?: string; +} + +const WrapWithErrorBoundary = React.memo(function WrapWithErrorBoundary ({ children, isExtensionMode, trigger }: WrapWithErrorBoundaryProps): React.ReactElement { + return ( + + <> + {children} + {!isExtensionMode && } + + + ); +}); + +interface RootProps { + hasAuthRequest: boolean; + isExtensionMode: boolean; + hasMetaRequest: boolean; + hasSignRequest: boolean; +} + +const Root = React.memo(function Root ({ hasAuthRequest, hasMetaRequest, hasSignRequest, isExtensionMode }: RootProps) { + return ( + hasAuthRequest + ? + + + : hasMetaRequest + ? + + + : hasSignRequest + ? + + + : + + + ); +}); + export default function Popup (): React.ReactElement { const [accounts, setAccounts] = useState(null); const priceIds = usePriceIds(); @@ -284,25 +330,6 @@ export default function Popup (): React.ReactElement { .catch(console.error); }, [cameraOn]); - const wrapWithErrorBoundary = useCallback((component: React.ReactElement, trigger?: string): React.ReactElement => { - return - <> - {component} - {!isExtensionMode && } - - ; - }, [isExtensionMode]); - - const Root = useCallback(() => - authRequests?.length - ? wrapWithErrorBoundary(, 'authorize') - : metaRequests?.length - ? wrapWithErrorBoundary(, 'metadata') - : signRequests?.length - ? wrapWithErrorBoundary(, 'signing') - : wrapWithErrorBoundary(, 'accounts') - , [authRequests?.length, metaRequests?.length, signRequests?.length, wrapWithErrorBoundary]); - return ( @@ -326,69 +353,309 @@ export default function Popup (): React.ReactElement { - {wrapWithErrorBoundary(, 'add-new-chain')} - {wrapWithErrorBoundary(, 'account')} - {wrapWithErrorBoundary(, 'account-creation')} - {wrapWithErrorBoundary(, 'export-all-address')} - {wrapWithErrorBoundary(, 'import-ledger')} - {wrapWithErrorBoundary(, 'import-seed')} - {wrapWithErrorBoundary(, 'import-raw-seed')} - {wrapWithErrorBoundary(, 'restore-json')} - {wrapWithErrorBoundary(, 'account')} - {wrapWithErrorBoundary(, 'auth-list')} - {wrapWithErrorBoundary(, 'crowdloans')} - {wrapWithErrorBoundary(, 'derived-address-locked')} - {wrapWithErrorBoundary(, 'derive-address')} - {wrapWithErrorBoundary(, 'fullscreen-account-derive')} - {wrapWithErrorBoundary(, 'export-address')} - {wrapWithErrorBoundary(, 'forget-address')} - {wrapWithErrorBoundary(, 'forgot-password')} - {wrapWithErrorBoundary(, 'reset-wallet')} - {wrapWithErrorBoundary(, 'fullscreen-proxy-management')} - {wrapWithErrorBoundary(, 'governance')} - {wrapWithErrorBoundary(, 'governance')} - {wrapWithErrorBoundary(, 'history')} - {wrapWithErrorBoundary(, 'import-add-watch-only')} - {wrapWithErrorBoundary(, 'import-add-watch-only-full-screen')} - {wrapWithErrorBoundary(, 'attach-qr')} - {wrapWithErrorBoundary(, 'attach-qr-full-screen')} - {wrapWithErrorBoundary(, 'import-proxied')} - {wrapWithErrorBoundary(, 'import-add-watch-only-full-screen')} - {wrapWithErrorBoundary(, 'manage-login-password')} - {wrapWithErrorBoundary(, 'manageProxies')} - {wrapWithErrorBoundary(, 'manage-identity')} - {wrapWithErrorBoundary(, 'manage-validators-fullscreen')} - {wrapWithErrorBoundary(, 'manage-validators-fullscreen')} - {wrapWithErrorBoundary(, 'nft-album')} - {wrapWithErrorBoundary(, 'onboarding')} - {wrapWithErrorBoundary(, 'pool-create')} - {wrapWithErrorBoundary(, 'pool-join')} - {wrapWithErrorBoundary(, 'pool-stake')} - {wrapWithErrorBoundary(, 'pool-poolInfromation')} - {wrapWithErrorBoundary(, 'pool-nominations')} - {wrapWithErrorBoundary(, 'pool-unstake')} - {wrapWithErrorBoundary(, 'pool-staking')} - {wrapWithErrorBoundary(, 'pool-staking-fullscreen')} - {wrapWithErrorBoundary(, 'rename')} - {wrapWithErrorBoundary(, 'receive')} - {wrapWithErrorBoundary(, 'send')} - {wrapWithErrorBoundary(, 'send')} - {wrapWithErrorBoundary(, 'stake')} - {wrapWithErrorBoundary(, 'social-recovery')} - {wrapWithErrorBoundary(, 'solo-fast-unstake')} - {wrapWithErrorBoundary(, 'solo-nominations')} - {wrapWithErrorBoundary(, 'solo-payout')} - {wrapWithErrorBoundary(, 'solo-restake')} - {wrapWithErrorBoundary(, 'solo-stake')} - {wrapWithErrorBoundary(, 'solo-unstake')} - {wrapWithErrorBoundary(, 'solo-staking')} - {wrapWithErrorBoundary(, 'solo-staking-fullscreen')} - {wrapWithErrorBoundary(, 'tuneup')} - {wrapWithErrorBoundary(, 'phishing-page-redirect')} - {Root} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +