Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

79 refactor wrap the button with modal #80

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions apps/sample-react-app/src/Components/SwitchWalletButton.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import type { HTMLChakraProps } from '@chakra-ui/react';
import { Button, Icon, useDisclosure } from '@chakra-ui/react';
import React from 'react';
import {
ConnectWalletModal,
useWallet,
useWalletModal,
} from '@vechainfoundation/dapp-kit-react';
import { useWallet, useWalletModal } from '@vechainfoundation/dapp-kit-react';
import { WalletIcon } from '@heroicons/react/24/solid';
import { AccountDetailModal } from './AccountDetailModal';
import { AddressButton } from './AddressButton';
Expand Down Expand Up @@ -42,16 +38,12 @@ export const SwitchWalletButton: React.FC<SwitchWalletButtonProps> = ({
);

return (
<>
<ConnectWalletModal />

<Button
{...buttonProps}
leftIcon={<Icon as={WalletIcon} />}
onClick={open}
>
Connect Wallet
</Button>
</>
<Button
{...buttonProps}
leftIcon={<Icon as={WalletIcon} />}
onClick={open}
>
Connect Wallet
</Button>
);
};
10 changes: 6 additions & 4 deletions apps/sample-react-app/src/Components/layout/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import {
import type { JSX } from 'react';
import React from 'react';
import { Bars3Icon } from '@heroicons/react/24/solid';
import { useWallet } from '@vechainfoundation/dapp-kit-react';
import {
ConnectWalletButtonWithModal,
useWallet,
} from '@vechainfoundation/dapp-kit-react';
import { AccountDetailBody } from '../AccountDetailBody';
import { SwitchWalletButton } from '../SwitchWalletButton';

export const NavBar = (): JSX.Element => {
const bg = useColorModeValue('gray.50', 'gray.900');
Expand Down Expand Up @@ -96,7 +98,7 @@ const MobileNavBarDrawer = ({
source={source}
/>
) : (
<SwitchWalletButton />
<ConnectWalletButtonWithModal />
)}
</VStack>
</VStack>
Expand All @@ -109,7 +111,7 @@ const MobileNavBarDrawer = ({
const NavBarWalletConnect = (): JSX.Element => {
return (
<HStack spacing={4}>
<SwitchWalletButton />
<ConnectWalletButtonWithModal />
</HStack>
);
};
Original file line number Diff line number Diff line change
@@ -1,66 +1,33 @@
import { useCallback, useContext, useState } from 'react';
import { useCallback, useContext } from 'react';
import type { WalletSource } from '@vechainfoundation/dapp-kit';
import type { SourceInfo } from '@vechainfoundation/dapp-kit-ui';
import { ThemeContext } from '../../../provider/ThemeProvider';
import { useWallet } from '../../../ConnexProvider';
import { ConnectModalWithButtonWrapped } from './Wrapped/ConnectModalWithButtonWrapped';

interface ConnectButtonWithModalProps {
onClose?: () => void;
}

export const ConnectButtonWithModal = ({
onClose,
}: ConnectButtonWithModalProps) => {
export const ConnectButtonWithModal = () => {
const { theme } = useContext(ThemeContext);

const handleSourceClick = (e: SourceInfo | undefined): void => {
if (!e) return;

_connect(e.id);
};

const { setAccount, connect, setSource } = useWallet();

const [connectionLoading, setConnectionLoading] = useState(false);
const [connectionError, setConnectionError] = useState('');
const { setSource, connect, setAccount } = useWallet();

const connectHandler = useCallback(
async (source: WalletSource) => {
try {
setSource(source);
setConnectionError('');
setConnectionLoading(true);
setSource(source);

const { account } = await connect();
setAccount(account);
const { account } = await connect();

onClose?.();
} catch (e) {
if (e instanceof Error) {
setConnectionError(e.message);
} else {
setConnectionError('Failed to connect to wallet');
}
} finally {
setConnectionLoading(false);
}
setAccount(account);
},
[
connect,
onClose,
setAccount,
setConnectionError,
setConnectionLoading,
setSource,
],
[connect, setAccount, setSource],
);

const _connect = useCallback(
(source: WalletSource) => {
connectHandler(source).catch(() => {
// do nothing
});
(source?: SourceInfo) => {
if (source) {
connectHandler(source.id).catch(() => {
// do nothing
});
}
},
[connectHandler],
);
Expand All @@ -69,10 +36,8 @@ export const ConnectButtonWithModal = ({
<>
<ConnectModalWithButtonWrapped
mode={theme.mode}
onSourceClick={handleSourceClick}
onSourceClick={_connect}
/>
{connectionError}
{connectionLoading}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const ConnectWalletButtonWithModal: React.FC<
return (
<ThemeProvider>
<GlobalFonts />
<ConnectButtonWithModal />
<ThemeSelector />
<ConnectButtonWithModal />
</ThemeProvider>
);
};

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion packages/dapp-kit-react/src/Components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './ConnectWalletModal';
export * from './ConnectWalletButtonWithModal';