stargaze-studio/components/WalletLoader.tsx

109 lines
3.8 KiB
TypeScript
Raw Normal View History

import type { Coin } from '@cosmjs/proto-signing'
2022-07-13 13:56:36 +00:00
import { Popover, Transition } from '@headlessui/react'
import clsx from 'clsx'
2023-12-13 10:01:39 +00:00
import { tokensList } from 'config/token'
import { Fragment, useEffect, useState } from 'react'
2022-07-13 13:56:36 +00:00
import { FaCopy, FaPowerOff, FaRedo } from 'react-icons/fa'
import { copy } from 'utils/clipboard'
import { convertDenomToReadable } from 'utils/convertDenomToReadable'
import { getShortAddress } from 'utils/getShortAddress'
2023-12-13 10:01:39 +00:00
import { truncateMiddle } from 'utils/text'
import { useWallet } from 'utils/wallet'
2022-07-13 13:56:36 +00:00
import { WalletButton } from './WalletButton'
import { WalletPanelButton } from './WalletPanelButton'
export const WalletLoader = () => {
const {
address = '',
username,
connect,
disconnect,
isWalletConnecting,
isWalletConnected,
getStargateClient,
} = useWallet()
2022-07-13 13:56:36 +00:00
// Once wallet connects, load balances.
const [balances, setBalances] = useState<readonly Coin[] | undefined>()
useEffect(() => {
if (!isWalletConnected) {
setBalances(undefined)
return
}
const loadBalances = async () => {
const client = await getStargateClient()
setBalances(await client.getAllBalances(address))
}
loadBalances().catch(console.error)
}, [isWalletConnected, getStargateClient, address])
2022-07-13 13:56:36 +00:00
return (
2023-06-28 18:29:42 +00:00
<Popover className="mt-4 mb-2">
2022-07-13 13:56:36 +00:00
{({ close }) => (
<>
<div className="grid -mx-4">
{isWalletConnected ? (
<Popover.Button as={WalletButton} className="w-full">
{username || address}
</Popover.Button>
) : (
<WalletButton
className="w-full"
isLoading={isWalletConnecting}
onClick={() => void connect().catch(console.error)}
>
2022-07-13 13:56:36 +00:00
Connect Wallet
</WalletButton>
)}
</div>
<Transition
as={Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0 -translate-y-1"
enterTo="opacity-100 translate-y-0"
leave="transition ease-in duration-150"
leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 -translate-y-1"
>
<Popover.Panel
className={clsx(
2023-02-14 18:27:33 +00:00
'absolute inset-x-4 z-50 mt-2',
2022-07-13 13:56:36 +00:00
'bg-stone-800/80 rounded shadow-lg shadow-black/90 backdrop-blur-sm',
'flex flex-col items-stretch text-sm divide-y divide-white/10',
)}
>
<div className="flex flex-col items-center py-2 px-4 space-y-1 text-center">
<span className="py-px px-2 mb-2 font-mono text-xs text-white/50 rounded-full border border-white/25">
{getShortAddress(address)}
</span>
<div className="font-bold">Your Balances</div>
{balances?.map((val) => (
2022-07-13 13:56:36 +00:00
<span key={`balance-${val.denom}`}>
2023-12-13 10:01:39 +00:00
{convertDenomToReadable(val.amount)}{' '}
{tokensList.find((t) => t.denom === val.denom)?.displayName
? tokensList.find((t) => t.denom === val.denom)?.displayName
: truncateMiddle(val.denom ? val.denom : '', 28)}
2022-07-13 13:56:36 +00:00
</span>
))}
</div>
<WalletPanelButton Icon={FaCopy} onClick={() => void copy(address)}>
Copy wallet address
</WalletPanelButton>
<WalletPanelButton Icon={FaRedo} onClick={() => void connect()}>
Reconnect
</WalletPanelButton>
<WalletPanelButton Icon={FaPowerOff} onClick={() => [disconnect(), close()]}>
Disconnect
</WalletPanelButton>
</Popover.Panel>
</Transition>
</>
)}
</Popover>
)
}