From 4428a220a36323de79613758ca9bafd2c0e8b4ec Mon Sep 17 00:00:00 2001 From: David Totraev Date: Fri, 27 Sep 2024 00:52:40 +0500 Subject: [PATCH] feat: move wallet provider logic in react context --- .../components/Delegations/Delegations.tsx | 67 +++++--- src/app/components/Header/Header.tsx | 15 +- src/app/components/Modals/ConnectModal.tsx | 10 +- .../Form/States/WalletNotConnected.tsx | 12 +- src/app/components/Staking/Staking.tsx | 29 ++-- src/app/context/Error/ErrorContext.tsx | 13 +- src/app/context/wallet/WalletProvider.tsx | 160 ++++++++++++++++++ src/app/page.tsx | 146 ++-------------- src/app/providers.tsx | 21 ++- 9 files changed, 262 insertions(+), 211 deletions(-) create mode 100644 src/app/context/wallet/WalletProvider.tsx diff --git a/src/app/components/Delegations/Delegations.tsx b/src/app/components/Delegations/Delegations.tsx index 17a31e3b..04716366 100644 --- a/src/app/components/Delegations/Delegations.tsx +++ b/src/app/components/Delegations/Delegations.tsx @@ -1,4 +1,4 @@ -import { networks } from "bitcoinjs-lib"; +import type { networks } from "bitcoinjs-lib"; import { useEffect, useState } from "react"; import InfiniteScroll from "react-infinite-scroll-component"; import { useLocalStorage } from "usehooks-ts"; @@ -7,6 +7,7 @@ import { SignPsbtTransaction } from "@/app/common/utils/psbt"; import { LoadingTableList } from "@/app/components/Loading/Loading"; import { DelegationsPointsProvider } from "@/app/context/api/DelegationsPointsProvider"; import { useError } from "@/app/context/Error/ErrorContext"; +import { useWallet } from "@/app/context/wallet/WalletProvider"; import { useHealthCheck } from "@/app/hooks/useHealthCheck"; import { QueryMeta } from "@/app/types/api"; import { @@ -35,14 +36,10 @@ interface DelegationsProps { delegationsAPI: DelegationInterface[]; delegationsLocalStorage: DelegationInterface[]; globalParamsVersion: GlobalParamsVersion; - publicKeyNoCoord: string; - btcWalletNetwork: networks.Network; - address: string; signPsbtTx: SignPsbtTransaction; pushTx: WalletProvider["pushTx"]; queryMeta: QueryMeta; getNetworkFees: WalletProvider["getNetworkFees"]; - isWalletConnected: boolean; } export const Delegations: React.FC = ({ @@ -53,36 +50,50 @@ export const Delegations: React.FC = ({ pushTx, queryMeta, getNetworkFees, - address, - btcWalletNetwork, - publicKeyNoCoord, - isWalletConnected, }) => { + const { address, publicKeyNoCoord, connected, network } = useWallet(); + return ( - - - + > + + + ) ); }; -const DelegationsContent: React.FC = ({ +interface DelegationsContentProps { + delegationsAPI: DelegationInterface[]; + delegationsLocalStorage: DelegationInterface[]; + globalParamsVersion: GlobalParamsVersion; + publicKeyNoCoord: string; + btcWalletNetwork: networks.Network; + address: string; + signPsbtTx: SignPsbtTransaction; + pushTx: WalletProvider["pushTx"]; + queryMeta: QueryMeta; + getNetworkFees: WalletProvider["getNetworkFees"]; + isWalletConnected: boolean; +} + +const DelegationsContent: React.FC = ({ delegationsAPI, delegationsLocalStorage, globalParamsVersion, diff --git a/src/app/components/Header/Header.tsx b/src/app/components/Header/Header.tsx index a7cdbbd9..451e51cb 100644 --- a/src/app/components/Header/Header.tsx +++ b/src/app/components/Header/Header.tsx @@ -1,3 +1,4 @@ +import { useWallet } from "@/app/context/wallet/WalletProvider"; import { shouldDisplayTestingMsg } from "@/config"; import { ConnectSmall } from "../Connect/ConnectSmall"; @@ -8,19 +9,15 @@ import { ThemeToggle } from "../ThemeToggle/ThemeToggle"; interface HeaderProps { loading?: boolean; - onConnect: () => void; - address: string; btcWalletBalanceSat?: number; - onDisconnect: () => void; } export const Header: React.FC = ({ loading, - onConnect, - address, btcWalletBalanceSat, - onDisconnect, }) => { + const { address, open, disconnect } = useWallet(); + return (