import { FC, useCallback } from 'react'
import * as TooltipPrimitive from '@radix-ui/react-tooltip'
import {
ToggleGroup,
ToggleGroupItem,
Box,
Text,
Button,
Flex,
} from '../primitives'
import supportedChains from 'utils/chains'
import { useContext } from 'react'
import { ChainContext } from 'context/ChainContextProvider'
import { TooltipArrow } from 'components/primitives/Tooltip'
import { useMounted } from 'hooks'
import { useTheme } from 'next-themes'
import { useRouter } from 'next/router'
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faChevronDown } from '@fortawesome/free-solid-svg-icons'
import {
DropdownMenuContent,
DropdownMenuItem,
} from 'components/primitives/Dropdown'
const ChainToggle: FC = () => {
const router = useRouter()
const { chain, switchCurrentChain } = useContext(ChainContext)
const isMounted = useMounted()
const { theme } = useTheme()
const switchChains = useCallback(
(chainOption: (typeof supportedChains)[0]) => {
if (router.query.chain) {
Object.keys(router.query).forEach((param) => delete router.query[param])
router.replace(
{
pathname: router.pathname,
query: router.query,
},
undefined,
{ shallow: true }
)
}
switchCurrentChain(chainOption.id)
},
[router.query, switchCurrentChain]
)
if (!isMounted || supportedChains.length === 1) {
return null
}
if (supportedChains.length > 1) {
return (
{supportedChains.map((supportedChain) => (
{
const newUrl = router.asPath.replace(
chain.routePrefix,
supportedChain.routePrefix
)
router.replace(newUrl, undefined, { scroll: false })
switchCurrentChain(supportedChain.id)
}}
>
{supportedChain.name}
))}
)
}
return (
{supportedChains.map((chainOption) => (
{
const newUrl = router.asPath.replace(
chain.routePrefix,
chainOption.routePrefix
)
switchCurrentChain(chainOption.id)
router.replace(newUrl, undefined, { scroll: false })
}}
>
{chainOption?.name}
))}
)
}
export default ChainToggle