2023-10-26 21:32:57 +02:00
|
|
|
import * as Select from '@radix-ui/react-select'
|
2023-10-28 13:55:30 +02:00
|
|
|
import './TokenSelect.css'
|
|
|
|
import { Token } from './Token'
|
2023-10-26 21:32:57 +02:00
|
|
|
import { ChevronDown, ChevronsDown, ChevronsUp } from '@images/components/react'
|
2023-10-28 19:01:36 +02:00
|
|
|
import { TokenLoading } from './TokenLoading'
|
2023-10-28 23:34:58 +02:00
|
|
|
import { useEffect } from 'react'
|
2023-10-29 23:51:31 +01:00
|
|
|
import { useTokens } from '../../hooks/useTokens'
|
2023-10-29 15:36:48 +01:00
|
|
|
import { useAccount, useNetwork } from 'wagmi'
|
2023-10-29 23:51:31 +01:00
|
|
|
import { $selectedToken, $setSelectedToken } from '../../stores/selectedToken'
|
|
|
|
import { useStore } from '@nanostores/react'
|
2023-10-26 21:32:57 +02:00
|
|
|
|
2023-10-29 23:51:31 +01:00
|
|
|
export function TokenSelect() {
|
2023-10-28 19:01:36 +02:00
|
|
|
const { data: tokens, isLoading } = useTokens()
|
2023-10-29 23:51:31 +01:00
|
|
|
const selectedToken = useStore($selectedToken)
|
2023-10-29 15:36:48 +01:00
|
|
|
const { chain } = useNetwork()
|
|
|
|
const { address } = useAccount()
|
2023-10-26 21:32:57 +02:00
|
|
|
|
2023-10-28 13:55:30 +02:00
|
|
|
const items = tokens?.map((token) => (
|
|
|
|
<Token key={token.address} token={token} />
|
|
|
|
))
|
|
|
|
|
2023-10-28 23:34:58 +02:00
|
|
|
function handleValueChange(value: `0x${string}`) {
|
|
|
|
const token = tokens?.find((token) => token.address === value)
|
|
|
|
if (!token) return
|
2023-10-29 23:51:31 +01:00
|
|
|
$setSelectedToken(token)
|
2023-10-28 23:34:58 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 14:35:17 +01:00
|
|
|
// Set default token data to first item,
|
|
|
|
// which most of time is native token
|
2023-10-29 15:36:48 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!chain?.id || !address || !tokens) return
|
2023-10-28 23:34:58 +02:00
|
|
|
|
2023-10-30 14:35:17 +01:00
|
|
|
if (!selectedToken || !selectedToken?.address)
|
|
|
|
handleValueChange(tokens[0].address)
|
2023-10-29 23:51:31 +01:00
|
|
|
}, [chain?.id, address, tokens, selectedToken])
|
2023-10-29 15:36:48 +01:00
|
|
|
|
|
|
|
return (
|
2023-10-28 13:55:30 +02:00
|
|
|
<Select.Root
|
2023-10-29 15:36:48 +01:00
|
|
|
defaultValue={selectedToken?.address}
|
2023-10-28 23:34:58 +02:00
|
|
|
onValueChange={(value: `0x${string}`) => handleValueChange(value)}
|
2023-10-28 19:01:36 +02:00
|
|
|
disabled={!tokens || isLoading}
|
2023-10-29 23:51:31 +01:00
|
|
|
value={selectedToken?.address}
|
2023-10-28 13:55:30 +02:00
|
|
|
>
|
2023-10-28 19:01:36 +02:00
|
|
|
<Select.Trigger
|
|
|
|
className="SelectTrigger"
|
|
|
|
disabled={!tokens || isLoading}
|
|
|
|
aria-label="Token"
|
|
|
|
>
|
|
|
|
{isLoading ? <TokenLoading /> : <Select.Value />}
|
2023-10-26 21:32:57 +02:00
|
|
|
<Select.Icon>
|
|
|
|
<ChevronDown />
|
|
|
|
</Select.Icon>
|
|
|
|
</Select.Trigger>
|
|
|
|
|
|
|
|
<Select.Portal style={{ zIndex: 10 }}>
|
|
|
|
<Select.Content className="SelectContent">
|
|
|
|
<Select.ScrollUpButton className="SelectScrollButton">
|
|
|
|
<ChevronsUp />
|
|
|
|
</Select.ScrollUpButton>
|
|
|
|
<Select.Viewport className="SelectViewport">
|
|
|
|
<Select.Group>
|
|
|
|
<Select.Label className="SelectLabel">
|
|
|
|
In Your Wallet
|
|
|
|
</Select.Label>
|
2023-10-28 13:55:30 +02:00
|
|
|
{items}
|
2023-10-26 21:32:57 +02:00
|
|
|
</Select.Group>
|
|
|
|
</Select.Viewport>
|
|
|
|
<Select.ScrollDownButton className="SelectScrollButton">
|
|
|
|
<ChevronsDown />
|
|
|
|
</Select.ScrollDownButton>
|
|
|
|
</Select.Content>
|
|
|
|
</Select.Portal>
|
|
|
|
</Select.Root>
|
2023-10-29 15:36:48 +01:00
|
|
|
)
|
2023-10-26 21:32:57 +02:00
|
|
|
}
|