import React, { useCallback, useEffect, useRef, useState } from 'react'
import Button from 'semantic-ui-react/dist/commonjs/elements/Button/Button'
import ModalContent from 'semantic-ui-react/dist/commonjs/modules/Modal/ModalContent'
import classNames from 'classnames'
import { ChainId, getChainName } from '@dcl/schemas'
import { useTabletAndBelowMediaQuery } from '../Media'
import { Modal } from '../Modal/Modal'
import { ModalNavigation } from '../ModalNavigation/ModalNavigation'
import { ChainSelectorProps } from './ChainSelector.props'
import './ChainSelector.css'
const ChainNameIconMap = {
[ChainId.ETHEREUM_MAINNET]: 'ethereum',
[ChainId.ETHEREUM_SEPOLIA]: 'ethereum',
[ChainId.MATIC_MAINNET]: 'polygon',
[ChainId.MATIC_MUMBAI]: 'polygon',
[ChainId.MATIC_AMOY]: 'polygon',
[ChainId.ARBITRUM_MAINNET]: 'arbitrum',
[ChainId.OPTIMISM_MAINNET]: 'optimism',
[ChainId.FANTOM_MAINNET]: 'fantom',
[ChainId.BSC_MAINNET]: 'bsc',
[ChainId.AVALANCHE_MAINNET]: 'avalanche'
}
/**
* @deprecated Should start using the same component migrated to UI2.
*/
export const ChainSelector = (props: ChainSelectorProps) => {
const { chains, selectedChain, chainBeingConfirmed, i18n, onSelectChain } =
props
const chainBeingConfirmedRef = useRef(chainBeingConfirmed)
const isMobileOrTablet = useTabletAndBelowMediaQuery()
// This effect is used to close the modal when the chain being confirmed changes
useEffect(() => {
if (selectedChain && selectedChain === chainBeingConfirmedRef.current) {
chainBeingConfirmedRef.current = undefined
setShowModal(false)
}
}, [selectedChain])
const [showModal, setShowModal] = useState(false)
const title: React.ReactNode = i18n?.title || 'Select Network'
const onButtonClick = useCallback(() => {
setShowModal(!showModal)
}, [])
const onSelectChainHandler = useCallback((chainId: ChainId) => {
onSelectChain(chainId)
chainBeingConfirmedRef.current = chainId
}, [])
return (
<>
setShowModal(false)}
>
setShowModal(false)} />
{chains.map((chain) => {
const chainName =
chain === ChainId.ETHEREUM_MAINNET
? 'Ethereum'
: getChainName(chain)
return (
)
})}
>
)
}