|
| 1 | +import { Label } from '@/components/ui/label'; |
| 2 | +import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; |
| 3 | +import { getSlicedAddress } from '@/lib/utils'; |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | +import { useAccount, useConnect, useConnectors, useDisconnect } from 'wagmi'; |
| 6 | +import { WalletPreference, getConnector } from './wallet-type'; |
| 7 | + |
| 8 | +export function WalletType() { |
| 9 | + const { disconnectAsync } = useDisconnect(); |
| 10 | + const connectors = useConnectors(); |
| 11 | + const account = useAccount(); |
| 12 | + const { connect } = useConnect(); |
| 13 | + |
| 14 | + const [walletType, setWalletType] = useState<WalletPreference>(); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const storedWalletType = localStorage.getItem('walletType'); |
| 18 | + if (storedWalletType) { |
| 19 | + setWalletType(storedWalletType as WalletPreference); |
| 20 | + } |
| 21 | + }, []); |
| 22 | + |
| 23 | + async function handleConnect(value: WalletPreference) { |
| 24 | + setWalletType(value); |
| 25 | + connect( |
| 26 | + { |
| 27 | + connector: getConnector(value as WalletPreference, connectors), |
| 28 | + }, |
| 29 | + { |
| 30 | + // Set localStorage ONLY when user has connected |
| 31 | + // otherwise, could result in walletType being set to smart wallet when user intended to connect eoa wallet |
| 32 | + onSuccess: () => { |
| 33 | + localStorage.setItem('walletType', value); |
| 34 | + }, |
| 35 | + }, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + async function clearWalletType() { |
| 40 | + localStorage.removeItem('walletType'); |
| 41 | + setWalletType(undefined); |
| 42 | + } |
| 43 | + |
| 44 | + async function disconnectAll() { |
| 45 | + await disconnectAsync({ connector: connectors[0] }); |
| 46 | + await disconnectAsync({ connector: connectors[1] }); |
| 47 | + clearWalletType?.(); |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="grid gap-2"> |
| 52 | + <Label htmlFor="wallet-type">Wallet Type</Label> |
| 53 | + <RadioGroup |
| 54 | + id="wallet-type" |
| 55 | + value={walletType} |
| 56 | + className="flex items-center justify-between" |
| 57 | + onValueChange={(value) => { |
| 58 | + handleConnect(value as WalletPreference); |
| 59 | + }} |
| 60 | + > |
| 61 | + <div className="flex items-center gap-2"> |
| 62 | + <Label |
| 63 | + htmlFor="wallet-type-smart" |
| 64 | + className="flex cursor-pointer items-center gap-2 rounded-md border p-2 [&:has(:checked)]:bg-muted" |
| 65 | + > |
| 66 | + <RadioGroupItem |
| 67 | + id="wallet-type-smart" |
| 68 | + value={WalletPreference.SMART_WALLET} |
| 69 | + /> |
| 70 | + Smart Wallet |
| 71 | + </Label> |
| 72 | + <Label |
| 73 | + htmlFor="wallet-type-eoa" |
| 74 | + className="flex cursor-pointer items-center gap-2 rounded-md border p-2 [&:has(:checked)]:bg-muted" |
| 75 | + > |
| 76 | + <RadioGroupItem id="wallet-type-eoa" value={WalletPreference.EOA} /> |
| 77 | + EOA |
| 78 | + </Label> |
| 79 | + </div> |
| 80 | + <button |
| 81 | + type="button" |
| 82 | + className="text-xs hover:underline" |
| 83 | + onClick={disconnectAll} |
| 84 | + > |
| 85 | + Disconnect all |
| 86 | + </button> |
| 87 | + </RadioGroup> |
| 88 | + <div className="text-neutral-500 text-xs"> |
| 89 | + {account?.address |
| 90 | + ? `Connected: ${getSlicedAddress(account.address)}` |
| 91 | + : 'Click a type to connect'} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + ); |
| 95 | +} |
0 commit comments