Skip to content

Commit

Permalink
Example Header includes NetworkSelect and AccountMenu with Signout bu…
Browse files Browse the repository at this point in the history
…tton
  • Loading branch information
corbanbrook committed May 29, 2024
1 parent 4053cb8 commit fcb5a93
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 89 deletions.
2 changes: 2 additions & 0 deletions examples/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
".": "./src/index.ts"
},
"dependencies": {
"@radix-ui/react-popover": "^1.0.7",
"typescript": "latest"
},
"peerDependencies": {
"@0xsequence/design-system": "*",
"@0xsequence/network": "*",
"wagmi": "*"
}
}
126 changes: 107 additions & 19 deletions examples/components/src/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { Box, Image, Text, GradientAvatar, truncateAddress } from '@0xsequence/design-system'
import { useAccount } from 'wagmi'
import {
Box,
Image,
Text,
GradientAvatar,
Select,
truncateAddress,
NetworkImage,
IconButton,
CloseIcon,
Card,
Button,
ChevronDownIcon
} from '@0xsequence/design-system'
import { ChainId } from '@0xsequence/network'
import * as PopoverPrimitive from '@radix-ui/react-popover'
import { useState } from 'react'
import { useAccount, useChainId, useDisconnect, useSwitchChain } from 'wagmi'

export const Header = () => {
const { address, connector } = useAccount()
const networks = [
{ chainId: ChainId.ARBITRUM_SEPOLIA, title: 'Arbitrum Sepolia' },
{ chainId: ChainId.ARBITRUM_NOVA, title: 'Arbitrum Nova' }
]

export const Header = () => {
return (
<Box
position="fixed"
top="0"
width="full"
padding="5"
padding="4"
justifyContent="space-between"
background="backgroundOverlay"
backdropFilter="blur"
Expand All @@ -28,21 +47,90 @@ export const Header = () => {
disableAnimation
/>
</Box>
<Box>
<Box flexDirection="column">
<Box flexDirection="row" gap="2" justifyContent="flex-end" alignItems="center">
<GradientAvatar address={String(address)} />
<Text variant="normal" fontWeight="bold" color="text100">
{truncateAddress(String(address), 8)}
</Text>
</Box>
<Box alignItems="center" justifyContent="flex-end" flexDirection="row">
<Text variant="small" color="text50">
{connector?.name}
</Text>
</Box>
</Box>
<Box gap="2" alignItems="center">
<NetworkSelect />
<AccountMenu />
</Box>
</Box>
)
}

const AccountMenu = () => {
const [isOpen, toggleOpen] = useState(false)
const { address, connector } = useAccount()
const { disconnect } = useDisconnect()

return (
<PopoverPrimitive.Root open={isOpen} onOpenChange={toggleOpen}>
<PopoverPrimitive.Trigger asChild>
<Box
borderColor={isOpen ? 'borderFocus' : 'borderNormal'}
borderWidth="thin"
borderStyle="solid"
borderRadius="md"
paddingX="4"
paddingY="3"
cursor="pointer"
>
<Box gap="2" alignItems="center">
<Box flexDirection="column">
<Box flexDirection="row" gap="2" justifyContent="flex-end" alignItems="center">
<GradientAvatar address={String(address)} size="sm" />
<Text variant="normal" fontWeight="bold" color="text100">
{truncateAddress(String(address), 4)}
</Text>
</Box>
{/* <Box alignItems="center" justifyContent="flex-end" flexDirection="row">
<Text variant="small" color="text50">
{connector?.name}
</Text>
</Box> */}
</Box>

<Box color="text50">
<ChevronDownIcon />
</Box>
</Box>
</Box>
</PopoverPrimitive.Trigger>

<PopoverPrimitive.Anchor />

{isOpen && (
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content asChild>
<Card zIndex="20">
<Button label="Sign out" onClick={() => disconnect()} />
<PopoverPrimitive.Close asChild>
<IconButton icon={CloseIcon} size="xs" />
</PopoverPrimitive.Close>
</Card>
</PopoverPrimitive.Content>
</PopoverPrimitive.Portal>
)}
</PopoverPrimitive.Root>
)
}

const NetworkSelect = () => {
const chainId = useChainId()
const { switchChain } = useSwitchChain()

return (
<Select
name="chainId"
labelLocation="top"
onValueChange={value => switchChain({ chainId: Number(value) })}
value={String(chainId)}
options={Object.values(networks).map(network => ({
label: (
<Box alignItems="center" gap="2">
<NetworkImage chainId={network.chainId} size="sm" />
<Text display={{ sm: 'none', lg: 'block' }}>{network.title!}</Text>
</Box>
),
value: String(network.chainId)
}))}
/>
)
}
41 changes: 4 additions & 37 deletions examples/next/src/app/components/Connected.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { Box, Text, Card, Button, Select, SignoutIcon } from '@0xsequence/design-system'
import { Box, Text, Card, Button, Select } from '@0xsequence/design-system'
import { signEthAuthProof, useIndexerClient, useStorage, useWaasFeeOptions, validateEthProof } from '@0xsequence/kit'
import { CheckoutSettings } from '@0xsequence/kit-checkout'
import { CardButton, Header } from '@0xsequence/kit-example-shared-components'
import { useOpenWalletModal } from '@0xsequence/kit-wallet'
import { ChainId, allNetworks } from '@0xsequence/network'
import { ComponentProps, useEffect, useState } from 'react'
import { formatUnits, parseUnits } from 'viem'
import {
useAccount,
useChainId,
useDisconnect,
usePublicClient,
useSendTransaction,
useSwitchChain,
useWalletClient,
useWriteContract
} from 'wagmi'
import { useAccount, useChainId, usePublicClient, useSendTransaction, useWalletClient, useWriteContract } from 'wagmi'

import { isDebugMode } from '../../config'

Expand All @@ -24,12 +15,10 @@ import { abi } from '@/constants/nft-abi'

export const Connected = () => {
const { address } = useAccount()
const { disconnect } = useDisconnect()
const { setOpenWalletModal } = useOpenWalletModal()
// const { setOpenConnectModal } = useOpenConnectModal()
// const { triggerCheckout } = useCheckoutModal()
const { data: walletClient } = useWalletClient()
const { switchChain } = useSwitchChain()
const storage = useStorage()

const { data: txnData, sendTransaction, isPending: isPendingSendTxn, error } = useSendTransaction()
Expand Down Expand Up @@ -205,17 +194,11 @@ export const Connected = () => {
// triggerCheckout(getCheckoutSettings(address))
// }

const onSwitchNetwork = () => {
if (chainId === ChainId.ARBITRUM_SEPOLIA) {
switchChain({ chainId: ChainId.ARBITRUM_NOVA })
} else {
switchChain({ chainId: ChainId.ARBITRUM_SEPOLIA })
}

useEffect(() => {
setLastTxnDataHash(undefined)
setLastTxnDataHash2(undefined)
setIsMessageValid(undefined)
}
}, [chainId])

return (
<>
Expand Down Expand Up @@ -305,12 +288,6 @@ export const Connected = () => {
{isDebugMode && (
<CardButton title="Generate EthAuth proof" description="Generate EthAuth proof" onClick={generateEthAuthProof} />
)}

<CardButton
title="Switch network"
description={`Current network: ${networkForCurrentChainId.title}`}
onClick={onSwitchNetwork}
/>
</Box>

{pendingFeeOptionConfirmation && feeOptionBalances.length > 0 && (
Expand Down Expand Up @@ -394,16 +371,6 @@ export const Connected = () => {
</Box>
</Box>
)}

<Box width="full" gap="2" flexDirection="row" justifyContent="flex-end">
<Button
onClick={() => {
disconnect()
}}
leftIcon={SignoutIcon}
label="Sign out"
/>
</Box>
</Box>
</Box>
</>
Expand Down
38 changes: 5 additions & 33 deletions examples/react/src/components/Connected.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Button, Card, Modal, Select, SignoutIcon, Switch, Text, TextInput, breakpoints } from '@0xsequence/design-system'
import { Box, Button, Card, Modal, Select, Switch, Text, TextInput, breakpoints } from '@0xsequence/design-system'
import {
useStorage,
useWaasFeeOptions,
Expand All @@ -19,10 +19,8 @@ import {
useAccount,
useChainId,
useConnections,
useDisconnect,
usePublicClient,
useSendTransaction,
useSwitchChain,
useWalletClient,
useWriteContract
} from 'wagmi'
Expand All @@ -39,9 +37,7 @@ export const Connected = () => {
const { setOpenWalletModal } = useOpenWalletModal()
const { triggerCheckout } = useCheckoutModal()
const { triggerAddFunds } = useAddFundsModal()
const { disconnect } = useDisconnect()
const { data: walletClient } = useWalletClient()
const { switchChain } = useSwitchChain()
const storage = useStorage()

const [isCheckoutInfoModalOpen, setIsCheckoutInfoModalOpen] = React.useState(false)
Expand Down Expand Up @@ -263,13 +259,13 @@ export const Connected = () => {
currencyDecimals: '6',
nftId: checkoutTokenId,
nftAddress: checkoutTokenContractAddress,
nftQuantity,
nftQuantity,
isDev: true,
calldata: getOrderbookCalldata({
orderId: checkoutOrderId,
quantity: nftQuantity,
recipient: recipientAddress
}),
})
})
triggerCheckout(checkoutSettings)
}
Expand All @@ -281,17 +277,11 @@ export const Connected = () => {
})
}

const onSwitchNetwork = () => {
if (chainId === ChainId.ARBITRUM_SEPOLIA) {
switchChain({ chainId: ChainId.ARBITRUM_NOVA })
} else {
switchChain({ chainId: ChainId.ARBITRUM_SEPOLIA })
}

useEffect(() => {
setLastTxnDataHash(undefined)
setLastTxnDataHash2(undefined)
setIsMessageValid(undefined)
}
}, [chainId])

return (
<>
Expand Down Expand Up @@ -386,12 +376,6 @@ export const Connected = () => {
/>
</>
)}

<CardButton
title="Switch network"
description={`Current network: ${networkForCurrentChainId.title}`}
onClick={onSwitchNetwork}
/>
</Box>

{pendingFeeOptionConfirmation && feeOptionBalances.length > 0 && (
Expand Down Expand Up @@ -504,18 +488,6 @@ export const Connected = () => {
</Box>
</Box>
)}
<Box width="full" gap="2" flexDirection="row" justifyContent="flex-end">
<Button
onClick={() => {
disconnect()
setLastTxnDataHash(undefined)
setLastTxnDataHash2(undefined)
setIsMessageValid(undefined)
}}
leftIcon={SignoutIcon}
label="Sign out"
/>
</Box>
</Box>
</Box>

Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fcb5a93

Please sign in to comment.