import { useShuttle } from '@delphi-labs/shuttle-react'
import Image from 'next/image'
import React, { useEffect, useState } from 'react'
import QRCode from 'react-qr-code'
import Button from 'components/Button'
import FullOverlayContent from 'components/FullOverlayContent'
import { ChevronLeft, ChevronRight } from 'components/Icons'
import Text from 'components/Text'
import WalletConnecting from 'components/Wallet/WalletConnecting'
import { CHAINS } from 'constants/chains'
import { ENV } from 'constants/env'
import { WALLETS } from 'constants/wallets'
import useStore from 'store'
import { WalletID } from 'types/enums/wallet'
import { isAndroid, isIOS, isMobile } from 'utils/mobile'
interface Props {
error?: ErrorObject
}
interface ErrorObject {
title: string
message: string
}
interface WalletOptionProps {
name: string
imageSrc: string
handleClick: () => void
}
const currentChainId = ENV.CHAIN_ID
const currentChain = CHAINS[currentChainId]
function WalletOption(props: WalletOptionProps) {
return (
)
}
export default function WalletSelect(props: Props) {
const { extensionProviders, mobileProviders, mobileConnect } = useShuttle()
const [qrCodeUrl, setQRCodeUrl] = useState('')
const [error, setError] = useState(props.error)
const sortedExtensionProviders = extensionProviders.sort((a, b) => +b - +a)
const handleConnectClick = (extensionProviderId: string) => {
useStore.setState({
focusComponent: ,
})
}
const handleMobileConnectClick = async (mobileProviderId: string, chainId: string) => {
try {
const urls = await mobileConnect({
mobileProviderId,
chainId,
})
if (isMobile()) {
if (isAndroid()) {
window.location.href = urls.androidUrl
} else if (isIOS()) {
window.location.href = urls.iosUrl
} else {
window.location.href = urls.androidUrl
}
} else {
setQRCodeUrl(urls.qrCodeUrl)
}
} catch (error) {
if (error instanceof Error) {
setError({ title: 'Failed to connect to wallet', message: error.message })
}
}
}
useEffect(() => {
if (error?.message && error?.title) {
useStore.setState({
toast: {
isError: true,
title: error.title,
message: error.message,
},
})
}
}, [error?.message, error?.title])
if (qrCodeUrl)
return (
,
iconClassName: 'w-3',
onClick: () => setQRCodeUrl(''),
text: 'Back',
}}
>
)
return (
{!isMobile() && (
<>
{sortedExtensionProviders.map((provider) => {
const walletId = provider.id as WalletID
return (
{Array.from(provider.networks.values())
.filter((network) => network.chainId === currentChainId)
.map((network) => {
if (!provider.initialized && !provider.initializing) {
return (
{
window.open(WALLETS[walletId].installURL ?? '/', '_blank')
}}
imageSrc={WALLETS[walletId].imageURL}
name={WALLETS[walletId].install ?? 'Install Wallet'}
/>
)
}
return (
handleConnectClick(walletId)}
imageSrc={WALLETS[walletId].imageURL}
name={WALLETS[walletId].name ?? 'Conenct Wallet'}
/>
)
})}
)
})}
>
)}
{mobileProviders.map((provider) => {
const walletId = provider.id as WalletID
return (
{Array.from(provider.networks.values())
.filter((network) => network.chainId === currentChainId)
.map((network) => {
return (
handleMobileConnectClick(walletId, network.chainId)}
/>
)
})}
)
})}
)
}