Skip to content

Commit

Permalink
feat: add network connection context provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Oct 24, 2024
1 parent 0d18f11 commit c1cd57f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 40 deletions.
45 changes: 24 additions & 21 deletions src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { BitcoinTransactionConfirmationsProvider } from '@providers/bitcoin-quer
import { BitcoinWalletContextProvider } from '@providers/bitcoin-wallet-context-provider';
import { EthereumNetworkConfigurationContextProvider } from '@providers/ethereum-network-configuration.provider';
import { NetworkConfigurationContextProvider } from '@providers/network-configuration.provider';
import { NetworkConnectionContextProvider } from '@providers/network-connection.provider';
import { ProofOfReserveContextProvider } from '@providers/proof-of-reserve-context-provider';
import { RippleNetworkConfigurationContextProvider } from '@providers/ripple-network-configuration.provider';
import { XRPWalletContextProvider } from '@providers/xrp-wallet-context-provider';
Expand All @@ -35,27 +36,29 @@ export function App(): React.JSX.Element {
<EthereumNetworkConfigurationContextProvider>
<XRPWalletContextProvider>
<BitcoinWalletContextProvider>
<VaultContextProvider>
<BitcoinTransactionConfirmationsProvider>
<BalanceContextProvider>
<ProofOfReserveContextProvider>
<AppLayout>
<Route path="/" element={<PointsPage />} />
<Route path="/my-vaults" element={<MyVaults />} />
{/* <Route path="/how-it-works" element={<About />} /> */}
<Route path="/proof-of-reserve" element={<ProofOfReservePage />} />
<Route path="/attestor-details" element={<AttestorDetailsPage />} />
<Route
path="/attestor-details-select"
element={<AttestorDetailsSelectPage />}
/>
<Route path="/merchant-details/:name" element={<MerchantDetails />} />
<Route path="/mint-withdraw" element={<Dashboard />} />
</AppLayout>
</ProofOfReserveContextProvider>
</BalanceContextProvider>
</BitcoinTransactionConfirmationsProvider>
</VaultContextProvider>
<NetworkConnectionContextProvider>
<VaultContextProvider>
<BitcoinTransactionConfirmationsProvider>
<BalanceContextProvider>
<ProofOfReserveContextProvider>
<AppLayout>
<Route path="/" element={<PointsPage />} />
<Route path="/my-vaults" element={<MyVaults />} />
{/* <Route path="/how-it-works" element={<About />} /> */}
<Route path="/proof-of-reserve" element={<ProofOfReservePage />} />
<Route path="/attestor-details" element={<AttestorDetailsPage />} />
<Route
path="/attestor-details-select"
element={<AttestorDetailsSelectPage />}
/>
<Route path="/merchant-details/:name" element={<MerchantDetails />} />
<Route path="/mint-withdraw" element={<Dashboard />} />
</AppLayout>
</ProofOfReserveContextProvider>
</BalanceContextProvider>
</BitcoinTransactionConfirmationsProvider>
</VaultContextProvider>
</NetworkConnectionContextProvider>
</BitcoinWalletContextProvider>
</XRPWalletContextProvider>
</EthereumNetworkConfigurationContextProvider>
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/account/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useDispatch } from 'react-redux';

import { Button, HStack, useBreakpointValue } from '@chakra-ui/react';
import { AccountMenu } from '@components/account/components/account-menu';
import { useNetworkConnection } from '@hooks/use-connected';
import { XRPWallet, xrpWallets } from '@models/wallet';
import { NetworkConfigurationContext } from '@providers/network-configuration.provider';
import { NetworkConnectionContext } from '@providers/network-connection.provider';
import { XRPWalletContext } from '@providers/xrp-wallet-context-provider';
import { mintUnmintActions } from '@store/slices/mintunmint/mintunmint.actions';
import { modalActions } from '@store/slices/modal/modal.actions';
Expand All @@ -14,7 +14,7 @@ import { Connector, useAccount, useDisconnect } from 'wagmi';
export function Account(): React.JSX.Element {
const dispatch = useDispatch();

const { isConnected } = useNetworkConnection();
const { isConnected } = useContext(NetworkConnectionContext);
const { networkType } = useContext(NetworkConfigurationContext);

const isMobile = useBreakpointValue({ base: true, md: false });
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, { useContext, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';

import { VStack, useBreakpointValue } from '@chakra-ui/react';
import { useNetworkConnection } from '@hooks/use-connected';
import { NetworkConfigurationContext } from '@providers/network-configuration.provider';
import { NetworkConnectionContext } from '@providers/network-connection.provider';
import { useAccount } from 'wagmi';

import { Banner } from './components/banner';
Expand All @@ -12,7 +12,7 @@ import MobileHeader from './components/mobile-header';

export function Header(): React.JSX.Element {
const navigate = useNavigate();
const { isConnected } = useNetworkConnection();
const { isConnected } = useContext(NetworkConnectionContext);

const { networkType } = useContext(NetworkConfigurationContext);
const { chain: ethereumNetwork } = useAccount();
Expand Down
10 changes: 5 additions & 5 deletions src/app/components/my-vaults/my-vaults-large.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { HStack } from '@chakra-ui/react';
import { VaultsListGroupBlankContainer } from '@components/vaults-list/components/vaults-list-group-blank-container';
import { VaultsListGroupContainer } from '@components/vaults-list/components/vaults-list-group-container';
import { VaultsList } from '@components/vaults-list/vaults-list';
import { useNetworkConnection } from '@hooks/use-connected';
import { BalanceContext } from '@providers/balance-context-provider';
import { NetworkConnectionContext } from '@providers/network-connection.provider';
import { VaultContext } from '@providers/vault-context-provider';

import { MyVaultsLargeHeader } from './components/my-vaults-header/my-vaults-header';
import { MyVaultsLargeLayout } from './components/my-vaults-large.layout';
import { MyVaultsSetupInformationStack } from './components/my-vaults-setup-information-stack';

export function MyVaultsLarge(): React.JSX.Element {
const { isConnected } = useNetworkConnection();
const { isConnected } = useContext(NetworkConnectionContext);
const { dlcBTCBalance, lockedBTCBalance } = useContext(BalanceContext);

const { readyVaults, pendingVaults, fundedVaults, closingVaults, closedVaults, allVaults } =
Expand All @@ -31,7 +31,7 @@ export function MyVaultsLarge(): React.JSX.Element {
<VaultsList
title={'In Process'}
height={'475px'}
isScrollable={!isConnected && allVaults.length > 0}
isScrollable={isConnected && allVaults.length > 0}
>
<VaultsListGroupContainer label="Empty Vaults" vaults={readyVaults} />
<VaultsListGroupContainer label="Pending" vaults={pendingVaults} />
Expand All @@ -43,7 +43,7 @@ export function MyVaultsLarge(): React.JSX.Element {
<VaultsList
title={'Minted dlcBTC'}
height={'475px'}
isScrollable={!isConnected && fundedVaults.length > 0}
isScrollable={isConnected && fundedVaults.length > 0}
>
{isConnected ? (
<VaultsListGroupContainer vaults={fundedVaults} />
Expand All @@ -54,7 +54,7 @@ export function MyVaultsLarge(): React.JSX.Element {
<VaultsList
title={'Closed Vaults'}
height={'475px'}
isScrollable={!isConnected && closedVaults.length > 0}
isScrollable={isConnected && closedVaults.length > 0}
>
{isConnected ? (
<VaultsListGroupContainer vaults={closedVaults} />
Expand Down
7 changes: 3 additions & 4 deletions src/app/components/network/components/networks-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import {
Text,
useBreakpointValue,
} from '@chakra-ui/react';
import { useNetworkConnection } from '@hooks/use-connected';
import { NetworkConfigurationContext } from '@providers/network-configuration.provider';
import { NetworkConnectionContext } from '@providers/network-connection.provider';
import { EthereumNetworkID } from 'dlc-btc-lib/models';
import { chain } from 'ramda';
import { useAccount, useConfig, useSwitchChain } from 'wagmi';

interface NetworksMenuProps {
Expand Down Expand Up @@ -42,7 +41,7 @@ export function NetworksMenu({
setIsMenuOpen,
}: NetworksMenuProps): React.JSX.Element | null {
const { chains } = useConfig();
const { isConnected } = useNetworkConnection();
const { isConnected } = useContext(NetworkConnectionContext);
const { chain: ethereumNetwork } = useAccount();

const { networkType } = useContext(NetworkConfigurationContext);
Expand All @@ -69,7 +68,7 @@ export function NetworksMenu({
) : (
//TODO: what to display in case of not connected?
<HStack justifyContent={'space-evenly'}>
<Text>{chain ? chain?.name : 'Not Connected'}</Text>
<Text>{ethereumNetwork ? ethereumNetwork?.name : 'Not Connected'}</Text>
</HStack>
)}
</MenuButton>
Expand Down
1 change: 0 additions & 1 deletion src/app/hooks/use-proof-of-reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {
queryKey: ['proofOfReserve'],
queryFn: fetchAllProofOfReserve,
refetchInterval: 60000,
enabled: false,
});

async function fetchProofOfReserve(merchantAddress?: string): Promise<number> {
Expand Down
6 changes: 3 additions & 3 deletions src/app/pages/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import React, { useContext } from 'react';

import { HStack } from '@chakra-ui/react';
import { LandingPage } from '@components/mint-unmint/components/landing-page/landing-page';
import { MintUnmint } from '@components/mint-unmint/mint-unmint';
import { MyVaultsSmall } from '@components/my-vaults-small/my-vaults-small';
import { useNetworkConnection } from '@hooks/use-connected';
import { PageLayout } from '@pages/components/page.layout';
import { NetworkConnectionContext } from '@providers/network-connection.provider';

import { breakpoints } from '@shared/utils';

export function Dashboard(): React.JSX.Element {
const { isConnected } = useNetworkConnection();
const { isConnected } = useContext(NetworkConnectionContext);

return (
<PageLayout>
Expand Down
4 changes: 2 additions & 2 deletions src/app/providers/balance-context-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createContext, useContext } from 'react';

import { useNetworkConnection } from '@hooks/use-connected';
import { HasChildren } from '@models/has-children';
import { useQuery } from '@tanstack/react-query';
import {
Expand All @@ -12,6 +11,7 @@ import { useAccount } from 'wagmi';

import { EthereumNetworkConfigurationContext } from './ethereum-network-configuration.provider';
import { NetworkConfigurationContext } from './network-configuration.provider';
import { NetworkConnectionContext } from './network-connection.provider';
import { XRPWalletContext } from './xrp-wallet-context-provider';

interface VaultContextType {
Expand All @@ -29,7 +29,7 @@ export function BalanceContextProvider({ children }: HasChildren): React.JSX.Ele
ethereumNetworkConfiguration: { dlcBTCContract, dlcManagerContract },
} = useContext(EthereumNetworkConfigurationContext);
const { networkType } = useContext(NetworkConfigurationContext);
const { isConnected } = useNetworkConnection();
const { isConnected } = useContext(NetworkConnectionContext);
const { userAddress } = useContext(XRPWalletContext);
const { xrpHandler } = useContext(XRPWalletContext);

Expand Down
25 changes: 25 additions & 0 deletions src/app/providers/network-connection.provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { createContext, useState } from 'react';

Check warning on line 1 in src/app/providers/network-connection.provider.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

'useState' is defined but never used

Check failure on line 1 in src/app/providers/network-connection.provider.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'useState' is declared but its value is never read.

import { useNetworkConnection } from '@hooks/use-connected';
import { HasChildren } from '@models/has-children';

interface NetworConnectionContextProvider {
isConnected: boolean;
}
export const NetworkConnectionContext = createContext<NetworConnectionContextProvider>({
isConnected: false,
});

export function NetworkConnectionContextProvider({ children }: HasChildren): React.JSX.Element {
const { isConnected } = useNetworkConnection();

return (
<NetworkConnectionContext.Provider
value={{
isConnected,
}}
>
{children}
</NetworkConnectionContext.Provider>
);
}

0 comments on commit c1cd57f

Please sign in to comment.