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

feat: add useAccounts hook #45

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions packages/web3/src/connect-button/connect-button.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import { Button } from 'antd';
import useCurrentAccount from '../hooks/useCurrentAccount';
import useAccounts from '../hooks/useAccounts';
import useProvider from '../hooks/useProvider';
import { Address } from '../address';
import type { ConnectButtonProps } from './interface';
import { UnconnectedButton } from './unconnected-button';

export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
const { account } = useCurrentAccount();
const { currentAccount } = useAccounts();
const { provider } = useProvider();

if (!account) {
if (!currentAccount) {
return <UnconnectedButton {...props} />;
}

Expand All @@ -26,7 +26,7 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
await provider?.disconnect();
}}
>
<Address ellipsis address={account.address} />
<Address ellipsis address={currentAccount.address} />
</Button>
);
};
Expand Down
10 changes: 10 additions & 0 deletions packages/web3/src/hooks/demos/useAccounts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useAccounts, Account } from '@ant-design/web3';
import { Space } from 'antd';

export default () => {
const { accounts } = useAccounts();
if (accounts.length === 0) {
return <div>Not Connected</div>;
}
return <Space direction="vertical">{accounts.map((account: Account) => account.address)}</Space>;
};
6 changes: 5 additions & 1 deletion packages/web3/src/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ order: 2

# Hooks

Components used to display NFT images more conveniently and simply.
Expose some commonly used Hooks that you can use independently or in conjunction with components.

## useNFT

<code src="./demos/useNFT.tsx"></code>

## useAccounts

<code src="./demos/useAccounts.tsx"></code>
1 change: 1 addition & 0 deletions packages/web3/src/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as useNFT } from './useNFT';
export { default as useAccounts } from './useAccounts';
6 changes: 6 additions & 0 deletions packages/web3/src/hooks/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ order: 2

# Hooks

暴露一些常用的 Hooks,你可以单独使用或者配合组件使用。

## useNFT

<code src="./demos/useNFT.tsx"></code>

## useAccounts

<code src="./demos/useAccounts.tsx"></code>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import useProvider from './useProvider';

export default function useCurrentAccount() {
export default function useAccounts() {
const { accounts } = useProvider();

return {
accounts,
account: accounts?.[0],
currentAccount: accounts?.[0],
};
}
2 changes: 2 additions & 0 deletions packages/web3/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from './browser-link';
export * from './nft-image';
export * from './constants';
export * from './hooks';

export type { Account } from '@ant-design/web3-common';
Loading