Skip to content

Commit

Permalink
Add disconnect functionality to Account component
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopy1412 committed Mar 7, 2024
1 parent 0572251 commit 2a87ac6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
43 changes: 39 additions & 4 deletions src/components/connect-button/account.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use client';
import { useRef } from 'react';
import { useDisconnect } from 'wagmi';
import Jazzicon, { jsNumberForAddress } from 'react-jazzicon';
import { useCopyToClipboard } from 'react-use';
import { toast } from 'sonner';

import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger
} from '@/components/ui/dropdown-menu';
import Button from '@/components/ui/ghost-button';
import { toShortAddress } from '@/utils';

Expand All @@ -15,20 +22,48 @@ const styles = {
};
const Account = ({ address }: AccountProps) => {
const toastRef = useRef<string | number | null>(null);
const disconnectTimerRef: React.MutableRefObject<NodeJS.Timeout | null> = useRef(null);
const [, copyToClipboard] = useCopyToClipboard();

const { disconnect } = useDisconnect();

const handleCopy = () => {
copyToClipboard(address);
toastRef.current = toast('✅ Address successfully copied to clipboard.', {
duration: 2000
});
};
const handleDisconnect = () => {
disconnectTimerRef?.current && clearTimeout(disconnectTimerRef?.current);
disconnectTimerRef.current = setTimeout(() => {
disconnect();
}, 500);
};

return address ? (
<Button title="Click to copy the address to your clipboard" onClick={handleCopy}>
<Jazzicon diameter={24} seed={jsNumberForAddress(address)} svgStyles={styles} />
<span>{toShortAddress(address)?.toUpperCase()}</span>
</Button>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button>
<Jazzicon diameter={24} seed={jsNumberForAddress(address)} svgStyles={styles} />
<span>{toShortAddress(address)?.toUpperCase()}</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-[10rem] gap-[0.625rem] rounded-[0.3125rem] border border-primary p-[0.625rem]">
<DropdownMenuItem
title="Click to copy the address to your clipboard"
className=" cursor-pointer p-[0.625rem] focus:bg-[rgba(94,214,42,.1)] "
onClick={handleCopy}
>
Copy Address
</DropdownMenuItem>
<DropdownMenuItem
className=" cursor-pointer p-[0.625rem] focus:bg-[rgba(94,214,42,.1)] "
onClick={handleDisconnect}
>
Disconnect
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
) : null;
};
export default Account;
2 changes: 1 addition & 1 deletion src/components/connect-button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ConnectButton = () => {
<Button onClick={openConnectModal}>Connect Wallet</Button>
) : null}
{isConnected ? <SwitchChain /> : null}
{address ? <Account address={address} /> : null}
{isConnected && address ? <Account address={address} /> : null}
</div>
);
};
Expand Down

0 comments on commit 2a87ac6

Please sign in to comment.