-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from arjanjohan/wallet-dropdown
Wallet dropdown
- Loading branch information
Showing
13 changed files
with
347 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/nextjs/components/scaffold-move/CustomConnectButton/AddressInfoDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { useRef, useState } from "react"; | ||
import { NetworkOptions } from "./NetworkOptions"; | ||
import CopyToClipboard from "react-copy-to-clipboard"; | ||
import { | ||
ArrowLeftOnRectangleIcon, | ||
ArrowTopRightOnSquareIcon, | ||
ArrowsRightLeftIcon, | ||
CheckCircleIcon, | ||
ChevronDownIcon, | ||
DocumentDuplicateIcon, | ||
QrCodeIcon, | ||
} from "@heroicons/react/24/outline"; | ||
import { BlockieAvatar, isENS } from "~~/components/scaffold-eth"; | ||
import { useOutsideClick } from "~~/hooks/scaffold-move"; | ||
import { getTargetNetworks } from "~~/utils/scaffold-eth"; | ||
import { | ||
useWallet | ||
} from "@aptos-labs/wallet-adapter-react"; | ||
|
||
const allowedNetworks = getTargetNetworks(); | ||
|
||
type AddressInfoDropdownProps = { | ||
address: string; | ||
blockExplorerAddressLink: string | undefined; | ||
displayName: string; | ||
ensAvatar?: string; | ||
}; | ||
|
||
export const AddressInfoDropdown = ({ | ||
address, | ||
ensAvatar, | ||
displayName, | ||
blockExplorerAddressLink, | ||
}: AddressInfoDropdownProps) => { | ||
|
||
const [addressCopied, setAddressCopied] = useState(false); | ||
|
||
const [selectingNetwork, setSelectingNetwork] = useState(false); | ||
const dropdownRef = useRef<HTMLDetailsElement>(null); | ||
const closeDropdown = () => { | ||
setSelectingNetwork(false); | ||
dropdownRef.current?.removeAttribute("open"); | ||
}; | ||
useOutsideClick(dropdownRef, closeDropdown); | ||
|
||
|
||
const { disconnect, wallet } = useWallet(); | ||
return ( | ||
<> | ||
<details ref={dropdownRef} className="dropdown dropdown-end leading-3"> | ||
<summary tabIndex={0} className="btn btn-secondary btn-sm pl-0 pr-2 shadow-md dropdown-toggle gap-0 !h-auto"> | ||
<BlockieAvatar address={address} size={30} ensImage={ensAvatar} /> | ||
<span className="ml-2 mr-1"> | ||
{isENS(displayName) ? displayName : address?.slice(0, 6) + "..." + address?.slice(-4)} | ||
</span> | ||
<ChevronDownIcon className="h-6 w-4 ml-2 sm:ml-0" /> | ||
</summary> | ||
<ul | ||
tabIndex={0} | ||
className="dropdown-content menu z-[2] p-2 mt-2 shadow-center shadow-accent bg-base-200 rounded-box gap-1" | ||
> | ||
<NetworkOptions hidden={!selectingNetwork} /> | ||
<li className={selectingNetwork ? "hidden" : ""}> | ||
{addressCopied ? ( | ||
<div className="btn-sm !rounded-xl flex gap-3 py-3"> | ||
<CheckCircleIcon | ||
className="text-xl font-normal h-6 w-4 cursor-pointer ml-2 sm:ml-0" | ||
aria-hidden="true" | ||
/> | ||
<span className=" whitespace-nowrap">Copy address</span> | ||
</div> | ||
) : ( | ||
<CopyToClipboard | ||
text={address} | ||
onCopy={() => { | ||
setAddressCopied(true); | ||
setTimeout(() => { | ||
setAddressCopied(false); | ||
}, 800); | ||
}} | ||
> | ||
<div className="btn-sm !rounded-xl flex gap-3 py-3"> | ||
<DocumentDuplicateIcon | ||
className="text-xl font-normal h-6 w-4 cursor-pointer ml-2 sm:ml-0" | ||
aria-hidden="true" | ||
/> | ||
<span className=" whitespace-nowrap">Copy address</span> | ||
</div> | ||
</CopyToClipboard> | ||
)} | ||
</li> | ||
<li className={selectingNetwork ? "hidden" : ""}> | ||
<label htmlFor="qrcode-modal" className="btn-sm !rounded-xl flex gap-3 py-3"> | ||
<QrCodeIcon className="h-6 w-4 ml-2 sm:ml-0" /> | ||
<span className="whitespace-nowrap">View QR Code</span> | ||
</label> | ||
</li> | ||
<li className={selectingNetwork ? "hidden" : ""}> | ||
<button className="menu-item btn-sm !rounded-xl flex gap-3 py-3" type="button"> | ||
<ArrowTopRightOnSquareIcon className="h-6 w-4 ml-2 sm:ml-0" /> | ||
<a | ||
target="_blank" | ||
href={blockExplorerAddressLink} | ||
rel="noopener noreferrer" | ||
className="whitespace-nowrap" | ||
> | ||
View on Block Explorer | ||
</a> | ||
</button> | ||
</li> | ||
{allowedNetworks.length > 1 ? ( | ||
<li className={selectingNetwork ? "hidden" : ""}> | ||
<button | ||
className="btn-sm !rounded-xl flex gap-3 py-3" | ||
type="button" | ||
onClick={() => { | ||
setSelectingNetwork(true); | ||
}} | ||
> | ||
<ArrowsRightLeftIcon className="h-6 w-4 ml-2 sm:ml-0" /> <span>Switch Network</span> | ||
</button> | ||
</li> | ||
) : null} | ||
<li className={selectingNetwork ? "hidden" : ""}> | ||
<button | ||
className="menu-item text-error btn-sm !rounded-xl flex gap-3 py-3" | ||
type="button" | ||
onClick={() => disconnect()} | ||
> | ||
<ArrowLeftOnRectangleIcon className="h-6 w-4 ml-2 sm:ml-0" /> <span>Disconnect</span> | ||
</button> | ||
</li> | ||
</ul> | ||
</details> | ||
</> | ||
); | ||
}; |
32 changes: 32 additions & 0 deletions
32
packages/nextjs/components/scaffold-move/CustomConnectButton/AddressQRCodeModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { QRCodeSVG } from "qrcode.react"; | ||
import { Address } from "~~/components/scaffold-move"; | ||
|
||
type AddressQRCodeModalProps = { | ||
address: string; | ||
modalId: string; | ||
}; | ||
|
||
export const AddressQRCodeModal = ({ address, modalId }: AddressQRCodeModalProps) => { | ||
return ( | ||
<> | ||
<div> | ||
<input type="checkbox" id={`${modalId}`} className="modal-toggle" /> | ||
<label htmlFor={`${modalId}`} className="modal cursor-pointer"> | ||
<label className="modal-box relative"> | ||
{/* dummy input to capture event onclick on modal box */} | ||
<input className="h-0 w-0 absolute top-0 left-0" /> | ||
<label htmlFor={`${modalId}`} className="btn btn-ghost btn-sm btn-circle absolute right-3 top-3"> | ||
✕ | ||
</label> | ||
<div className="space-y-3 py-6"> | ||
<div className="flex flex-col items-center gap-6"> | ||
<QRCodeSVG value={address} size={256} /> | ||
<Address address={address} format="long" disableAddressLink /> | ||
</div> | ||
</div> | ||
</label> | ||
</label> | ||
</div> | ||
</> | ||
); | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/nextjs/components/scaffold-move/CustomConnectButton/NetworkOptions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useTheme } from "next-themes"; | ||
import { useAccount, useSwitchChain } from "wagmi"; | ||
import { ArrowsRightLeftIcon } from "@heroicons/react/24/solid"; | ||
import { getTargetNetworks } from "~~/utils/scaffold-eth"; | ||
|
||
const allowedNetworks = getTargetNetworks(); | ||
|
||
type NetworkOptionsProps = { | ||
hidden?: boolean; | ||
}; | ||
|
||
export const NetworkOptions = ({ hidden = false }: NetworkOptionsProps) => { | ||
const { switchChain } = useSwitchChain(); | ||
const { chain } = useAccount(); | ||
const { resolvedTheme } = useTheme(); | ||
const isDarkMode = resolvedTheme === "dark"; | ||
|
||
return ( | ||
<> | ||
{allowedNetworks | ||
.filter(allowedNetwork => allowedNetwork.id !== chain?.id) | ||
.map(allowedNetwork => ( | ||
<li key={allowedNetwork.id} className={hidden ? "hidden" : ""}> | ||
<button | ||
className="menu-item btn-sm !rounded-xl flex gap-3 py-3 whitespace-nowrap" | ||
type="button" | ||
onClick={() => { | ||
switchChain?.({ chainId: allowedNetwork.id }); | ||
}} | ||
> | ||
<ArrowsRightLeftIcon className="h-6 w-4 ml-2 sm:ml-0" /> | ||
<span> | ||
Switch to{" "} | ||
<span> | ||
{allowedNetwork.name} | ||
</span> | ||
</span> | ||
</button> | ||
</li> | ||
))} | ||
</> | ||
); | ||
}; |
32 changes: 32 additions & 0 deletions
32
packages/nextjs/components/scaffold-move/CustomConnectButton/WrongNetworkDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NetworkOptions } from "./NetworkOptions"; | ||
import { useDisconnect } from "wagmi"; | ||
import { ArrowLeftOnRectangleIcon, ChevronDownIcon } from "@heroicons/react/24/outline"; | ||
|
||
export const WrongNetworkDropdown = () => { | ||
const { disconnect } = useDisconnect(); | ||
|
||
return ( | ||
<div className="dropdown dropdown-end mr-2"> | ||
<label tabIndex={0} className="btn btn-error btn-sm dropdown-toggle gap-1"> | ||
<span>Wrong network</span> | ||
<ChevronDownIcon className="h-6 w-4 ml-2 sm:ml-0" /> | ||
</label> | ||
<ul | ||
tabIndex={0} | ||
className="dropdown-content menu p-2 mt-1 shadow-center shadow-accent bg-base-200 rounded-box gap-1" | ||
> | ||
<NetworkOptions /> | ||
<li> | ||
<button | ||
className="menu-item text-error btn-sm !rounded-xl flex gap-3 py-3" | ||
type="button" | ||
onClick={() => disconnect()} | ||
> | ||
<ArrowLeftOnRectangleIcon className="h-6 w-4 ml-2 sm:ml-0" /> | ||
<span>Disconnect</span> | ||
</button> | ||
</li> | ||
</ul> | ||
</div> | ||
); | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/nextjs/components/scaffold-move/CustomConnectButton/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { Balance } from "../Balance"; | ||
import { AddressInfoDropdown } from "./AddressInfoDropdown"; | ||
import { WrongNetworkDropdown } from "./WrongNetworkDropdown"; | ||
import { useTargetNetwork } from "~~/hooks/scaffold-eth/useTargetNetwork"; | ||
import { getBlockExplorerAddressLink } from "~~/utils/scaffold-eth"; | ||
import { WalletSelector } from "@aptos-labs/wallet-adapter-ant-design"; | ||
|
||
|
||
import { | ||
useWallet | ||
} from "@aptos-labs/wallet-adapter-react"; | ||
|
||
export const CustomConnectButton = () => { | ||
const { targetNetwork } = useTargetNetwork(); | ||
// const [account, setAccount] = useState(null); | ||
const chain = {name: "devnet"}; // TODO: replace | ||
|
||
|
||
const { account, connected} = useWallet(); | ||
|
||
|
||
const blockExplorerAddressLink = account ? getBlockExplorerAddressLink(targetNetwork, account?.address) : undefined; | ||
|
||
return ( | ||
<> | ||
{!connected ? ( | ||
<div className="flex flex-col items-center mr-1 btn btn-primary"> | ||
<WalletSelector /> | ||
</div> | ||
) | ||
// : chainId !== targetNetwork.id ? ( | ||
// <WrongNetworkDropdown /> | ||
// ) | ||
: ( | ||
<> | ||
<div className="flex flex-col items-center mr-1"> | ||
{/* <Balance address={account} className="min-h-0 h-auto" /> */} | ||
<span className="text-xs">{chain ? chain.name : "Loading..."}</span> | ||
</div> | ||
<AddressInfoDropdown | ||
address={account?.address || ""} | ||
displayName={account?.address || ""} | ||
ensAvatar={""} // Update this with ENS Avatar if available | ||
blockExplorerAddressLink={blockExplorerAddressLink} | ||
/> | ||
</> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./Address"; | ||
export * from "./Balance"; | ||
export * from "./Balance"; | ||
export * from "./CustomConnectButton"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./useDeployedContractInfo"; | ||
export * from "./useGetAccountAPTBalance"; | ||
export * from "./useGetAccountResources"; | ||
export * from "./useGetAccountResources"; | ||
export * from "./useOutsideClick"; |
Oops, something went wrong.