1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-22 01:03:37 +01:00
This commit is contained in:
Matthias Kretschmann 2023-10-26 20:45:57 +01:00
parent 0ca6558def
commit 2f6e4f0b3c
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 39 additions and 25 deletions

View File

@ -2,35 +2,13 @@ import * as Select from '@radix-ui/react-select'
import './Select.css'
import { SelectItem } from './SelectItem'
import { ChevronDown, ChevronsDown, ChevronsUp } from '@images/components/react'
import { useEffect, useState } from 'react'
import { getBalance } from '../../api/getBalance'
import { useAccount, useNetwork } from 'wagmi'
import { useTokens } from '../../hooks/useTokens'
export function TokenSelect() {
const { address } = useAccount()
const { chain } = useNetwork()
const [tokens, setTokens] = useState()
useEffect(() => {
if (!address || !chain) return
async function getTokens() {
try {
const response = await getBalance(address)
const tokens = response.filter(
(token: any) => parseInt(token.chainId) === chain?.id
)
setTokens(tokens)
} catch (error) {
console.error((error as Error).message)
}
}
getTokens()
}, [address, chain])
const { data: tokens } = useTokens()
return (
<Select.Root disabled={!address}>
<Select.Root disabled={!tokens}>
<Select.Trigger className="SelectTrigger" aria-label="Token">
<Select.Value placeholder="…" />
<Select.Icon>

View File

@ -0,0 +1,36 @@
import { useState, useEffect } from 'react'
import { useAccount, useNetwork } from 'wagmi'
import { getBalance } from '../api/getBalance'
export function useTokens() {
const { address } = useAccount()
const { chain } = useNetwork()
const [data, setData] = useState()
const [isLoading, setIsLoading] = useState<boolean>()
const [isError, setIsError] = useState<boolean>()
useEffect(() => {
if (!address || !chain) return
async function getTokens() {
setIsLoading(true)
try {
const response = await getBalance(address)
const tokens = response.filter(
(token: any) => parseInt(token.chainId) === chain?.id
)
setData(tokens)
setIsLoading(false)
} catch (error) {
setIsError(true)
setIsLoading(false)
console.error((error as Error).message)
}
}
getTokens()
}, [address, chain])
return { data, isLoading, isError }
}