From d43bd604f777952ca0fe77035f0b6bfe9f1a1d23 Mon Sep 17 00:00:00 2001 From: Ross Bulat Date: Sat, 2 Nov 2024 14:29:04 +0700 Subject: [PATCH] feat(refactor): Update `withProviders` (#2314) --- package.json | 1 + src/Providers.tsx | 98 +++++++++++++++---------------- src/hooks/withProviders/index.tsx | 22 ------- yarn.lock | 10 ++++ 4 files changed, 60 insertions(+), 71 deletions(-) delete mode 100644 src/hooks/withProviders/index.tsx diff --git a/package.json b/package.json index cf0f6222f5..f4ce70d9c1 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@polkawatch/ddp-client": "^2.0.20", "@substrate/connect": "^1.1.0", "@w3ux/extension-assets": "^0.4.0", + "@w3ux/factories": "1.0.0-beta.0", "@w3ux/hooks": "1.2.1-beta.0", "@w3ux/react-connect-kit": "^1.8.0", "@w3ux/react-odometer": "^1.1.0", diff --git a/src/Providers.tsx b/src/Providers.tsx index c0b21b0a20..2fd36ae415 100644 --- a/src/Providers.tsx +++ b/src/Providers.tsx @@ -41,8 +41,7 @@ import { DappName } from 'consts'; import { ImportedAccountsProvider } from 'contexts/Connect/ImportedAccounts'; import { PoolPerformanceProvider } from 'contexts/Pools/PoolPerformance'; import { ExternalAccountsProvider } from 'contexts/Connect/ExternalAccounts'; -import type { Provider } from 'hooks/withProviders'; -import { withProviders } from 'hooks/withProviders'; +import { withProviders } from '@w3ux/factories'; import { CommunityProvider } from 'contexts/Community'; import { OverlayProvider } from 'kits/Overlay/Provider'; import { JoinPoolsProvider } from 'contexts/Pools/JoinPools'; @@ -52,53 +51,54 @@ export const Providers = () => { const { network } = useNetwork(); const { activeAccount, setActiveAccount } = useActiveAccounts(); - // !! Provider order matters. - const providers: Provider[] = [ - UIProvider, - [APIProvider, { network }], - LedgerHardwareProvider, + return withProviders( + // !! Provider order matters. [ - ExtensionsProvider, - { options: { chainSafeSnapEnabled: true, polkagateSnapEnabled: true } }, + UIProvider, + [APIProvider, { network }], + LedgerHardwareProvider, + [ + ExtensionsProvider, + { options: { chainSafeSnapEnabled: true, polkagateSnapEnabled: true } }, + ], + [ + ExtensionAccountsProvider, + { dappName: DappName, network, activeAccount, setActiveAccount }, + ], + WCAccountsProvider, + VaultAccountsProvider, + LedgerAccountsProvider, + ExternalAccountsProvider, + OtherAccountsProvider, + ImportedAccountsProvider, + WalletConnectProvider, + ProxiesProvider, + HelpProvider, + PluginsProvider, + BondedProvider, + BalancesProvider, + StakingProvider, + FavoritePoolsProvider, + BondedPoolsProvider, + PoolMembersProvider, + ActivePoolProvider, + TransferOptionsProvider, + ValidatorsProvider, + FavoriteValidatorsProvider, + FastUnstakeProvider, + PayoutsProvider, + PoolPerformanceProvider, + JoinPoolsProvider, + SetupProvider, + MenuProvider, + TooltipProvider, + TxMetaProvider, + OverlayProvider, + PromptProvider, + MigrateProvider, + FiltersProvider, + CommunityProvider, ], - [ - ExtensionAccountsProvider, - { dappName: DappName, network, activeAccount, setActiveAccount }, - ], - WCAccountsProvider, - VaultAccountsProvider, - LedgerAccountsProvider, - ExternalAccountsProvider, - OtherAccountsProvider, - ImportedAccountsProvider, - WalletConnectProvider, - ProxiesProvider, - HelpProvider, - PluginsProvider, - BondedProvider, - BalancesProvider, - StakingProvider, - FavoritePoolsProvider, - BondedPoolsProvider, - PoolMembersProvider, - ActivePoolProvider, - TransferOptionsProvider, - ValidatorsProvider, - FavoriteValidatorsProvider, - FastUnstakeProvider, - PayoutsProvider, - PoolPerformanceProvider, - JoinPoolsProvider, - SetupProvider, - MenuProvider, - TooltipProvider, - TxMetaProvider, - OverlayProvider, - PromptProvider, - MigrateProvider, - FiltersProvider, - CommunityProvider, - ]; - - return withProviders(providers, ThemedRouter); + ThemedRouter + ); }; diff --git a/src/hooks/withProviders/index.tsx b/src/hooks/withProviders/index.tsx deleted file mode 100644 index 280e9b6ebe..0000000000 --- a/src/hooks/withProviders/index.tsx +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors -// SPDX-License-Identifier: GPL-3.0-only - -import type { FC } from 'react'; -import type { AnyJson } from '@w3ux/types'; - -// `providers` accepts standalone functional components or an array of a functional component and its props. -export type Provider = FC | [FC, AnyJson]; - -// A pure function that applies an arbitrary amount of context providers to a wrapped component. -export const withProviders = (providers: Provider[], Wrapped: FC) => - providers.reduceRight( - (acc, prov) => { - if (Array.isArray(prov)) { - const Provider = prov[0]; - return {acc}; - } - const Provider = prov; - return {acc}; - }, - - ); diff --git a/yarn.lock b/yarn.lock index 315381894c..d811dfe589 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3673,6 +3673,15 @@ __metadata: languageName: node linkType: hard +"@w3ux/factories@npm:1.0.0-beta.0": + version: 1.0.0-beta.0 + resolution: "@w3ux/factories@npm:1.0.0-beta.0" + peerDependencies: + react: ^18 + checksum: 10c0/1c2a8adb966300fe7e36410e4b91381221a70f2470dfa3c1b5612ddc2e84c4579b6928ac7d4c9cad34291b7d8e1cbc97cbba66ae46fd04627042df50e9646b66 + languageName: node + linkType: hard + "@w3ux/hooks@npm:1.2.1-beta.0": version: 1.2.1-beta.0 resolution: "@w3ux/hooks@npm:1.2.1-beta.0" @@ -8563,6 +8572,7 @@ __metadata: "@typescript-eslint/parser": "npm:^7.12.0" "@vitejs/plugin-react-swc": "npm:^3.7.0" "@w3ux/extension-assets": "npm:^0.4.0" + "@w3ux/factories": "npm:1.0.0-beta.0" "@w3ux/hooks": "npm:1.2.1-beta.0" "@w3ux/react-connect-kit": "npm:^1.8.0" "@w3ux/react-odometer": "npm:^1.1.0"