Skip to content

Commit

Permalink
Merge branch 'main' into 69-feat-implement-the-address-badge-as-a-van…
Browse files Browse the repository at this point in the history
…illa-component
  • Loading branch information
Valazan authored Nov 21, 2023
2 parents 7ef3530 + 5f59d5e commit ec9ca28
Show file tree
Hide file tree
Showing 51 changed files with 1,624 additions and 256 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/*
dist/*
web-dev-server.config.js
tsup.config.ts
**/test/**
vite.config.ts
3 changes: 3 additions & 0 deletions .github/workflows/lint-build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ jobs:
- name: Lint
run: yarn run lint

- name: Test
run: yarn run test

- name: Build
run: yarn run build
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>
);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prepare": "husky install",
"purge": "npx turbo@latest run purge && rm -rf node_modules",
"reinstall": "yarn clean && yarn purge && yarn && yarn run build:deps",
"test": "turbo run test"
"test": "turbo run test --filter='@vechainfoundation/*'"
},
"husky": {
"hooks": {
Expand Down
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';
2 changes: 2 additions & 0 deletions packages/dapp-kit-ui/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules/*
dist/*
web-dev-server.config.js
tsup.config.ts
test
vite.config.ts
3 changes: 1 addition & 2 deletions packages/dapp-kit-ui/.gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/node_modules/
/lib/
/test/
custom-elements.json
# top level source
my-element.js
my-element.js.map
my-element.d.ts
my-element.d.ts.map
# only generated for size check
my-element.bundled.js
my-element.bundled.js
7 changes: 6 additions & 1 deletion packages/dapp-kit-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"format": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path ./.eslintignore --write",
"lint": "tsc --noEmit && eslint src --ext .js,.jsx,.ts,.tsx",
"purge": "yarn clean && rm -rf node_modules",
"test": "vitest run --coverage",
"watch": "tsup --watch"
},
"dependencies": {
Expand All @@ -38,10 +39,14 @@
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"@vechain/repo-config": "https://github.com/vechainfoundation/repo-config#v0.0.1",
"@vitest/coverage-v8": "^0.34.6",
"eslint": "^8.15.0",
"parcel": "^2.10.2",
"prettier": "^2.6.2",
"tsup": "^7.2.0",
"typescript": "~5.2.0"
"typechain": "^8.3.2",
"typescript": "~5.2.0",
"vite": "^4.5.0",
"vitest": "^0.34.6"
}
}
1 change: 1 addition & 0 deletions packages/dapp-kit-ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import './vwk-connected-address-modal';
import './vwk-connected-address-badge-with-modal';
import './vwk-source-card';
import './vwk-fonts';
import './vwk-wallet-connect-qrcode';

export * from './base';
export * from './vwk-connect-modal';
Expand Down
2 changes: 2 additions & 0 deletions packages/dapp-kit-ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './components';

export * from './client';
export * from './assets';
export * from './components';
Expand Down
57 changes: 57 additions & 0 deletions packages/dapp-kit-ui/test/connect-buton-with-modal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { beforeEach, describe, expect, it } from 'vitest';

import {
ConnectButton,
ConnectButtonWithModal,
ConnectModal,
DAppKit,
SourceInfo,
} from '../src';
import { elementQueries } from './helpers/elemnt-queries';
import { WalletSource } from '@vechainfoundation/dapp-kit/src';

describe('connect-button-with-modal', () => {
beforeEach(() => {
DAppKit.configure({ nodeUrl: 'https://mainnet.vechain.org/' });
});

it('Should callback with source when user clicks a wallet', async () => {
const element: ConnectButtonWithModal = window.document.createElement(
'vwk-connect-button-with-modal',
);

let selectedSource: WalletSource | undefined;

element.onSourceClick = (source?: SourceInfo) => {
selectedSource = source?.id;
};

window.document.body.appendChild(element);

const connectButton =
(await elementQueries.getConnectButton()) as ConnectButton;
const connectModal =
(await elementQueries.getConnectModal()) as ConnectModal;

expect(connectModal).toBeDefined();
expect(connectButton).toBeDefined();

expect(connectModal.open).toBe(false);

connectButton.shadowRoot?.querySelector('button')?.click();

await new Promise((resolve) => setTimeout(resolve, 1000));

expect(connectModal.open).toBe(true);

const sourceCards = await elementQueries.getAllSourceCards();

expect(sourceCards.length).toBeGreaterThan(0);

sourceCards[0]?.shadowRoot?.querySelector('div')?.click();

await new Promise((resolve) => setTimeout(resolve, 1000));

expect(selectedSource).toBeDefined();
});
});
Loading

0 comments on commit ec9ca28

Please sign in to comment.