mango-v4-ui/components/shared/SecondaryConnectButton.tsx

69 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-08-15 05:29:10 -07:00
import { useTranslation } from 'react-i18next'
import Button from './Button'
import { useWallet } from '@solana/wallet-adapter-react'
import useLocalStorageState from 'hooks/useLocalStorageState'
import { WalletName, WalletReadyState } from '@solana/wallet-adapter-base'
import { IS_ONBOARDED_KEY, LAST_WALLET_NAME } from 'utils/constants'
2023-08-15 05:29:10 -07:00
import { notify } from 'utils/notifications'
import mangoStore from '@store/mangoStore'
import { useCallback } from 'react'
2023-12-11 19:53:57 -08:00
import WalletIcon from '@components/icons/WalletIcon'
2023-08-15 05:29:10 -07:00
const SecondaryConnectButton = ({
className,
isLarge,
}: {
className?: string
isLarge?: boolean
}) => {
const { t } = useTranslation('common')
const { connect, wallet, wallets, select } = useWallet()
2023-08-15 05:29:10 -07:00
const [lastWalletName] = useLocalStorageState<WalletName | null>(
LAST_WALLET_NAME,
'',
)
const [isOnboarded] = useLocalStorageState(IS_ONBOARDED_KEY)
2023-08-15 05:29:10 -07:00
const handleConnect = useCallback(() => {
2023-11-29 17:37:14 -08:00
const set = mangoStore.getState().set
if (!isOnboarded) {
2023-08-15 05:29:10 -07:00
set((s) => {
s.showUserSetup = true
})
} else if (wallet) {
connect()
2023-08-15 05:29:10 -07:00
} else if (lastWalletName) {
select(lastWalletName)
} else {
const walletToConnect = wallets.find(
(w) =>
w.readyState === WalletReadyState.Installed ||
w.readyState === WalletReadyState.Loadable,
)
if (walletToConnect) {
select(walletToConnect.adapter.name)
} else {
notify({
title: 'No wallet found. Install a Solana wallet and try again',
type: 'error',
})
}
}
}, [connect, isOnboarded, lastWalletName, select, wallet, wallets])
2023-08-15 05:29:10 -07:00
return (
<Button
className={className}
onClick={handleConnect}
size={isLarge ? 'large' : 'medium'}
>
<div className="flex items-center">
2023-12-11 19:53:57 -08:00
<WalletIcon className="mr-2 h-5 w-5" />
2023-08-15 05:29:10 -07:00
{t('connect')}
</div>
</Button>
)
}
export default SecondaryConnectButton