mango-v4-ui/components/modals/DelegateModal.tsx

127 lines
4.0 KiB
TypeScript
Raw Normal View History

2022-11-16 08:59:34 -08:00
import { ModalProps } from '../../types/modal'
import Modal from '../shared/Modal'
import mangoStore from '@store/mangoStore'
import { notify } from '../../utils/notifications'
2022-12-15 19:16:05 -08:00
import Button, { LinkButton } from '../shared/Button'
2022-11-16 08:59:34 -08:00
import { useTranslation } from 'next-i18next'
import { ChangeEvent, useState } from 'react'
import Input from '../forms/Input'
import Label from '../forms/Label'
import { PublicKey } from '@solana/web3.js'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-12-15 19:16:05 -08:00
import { abbreviateAddress } from 'utils/formatting'
import InlineNotification from '@components/shared/InlineNotification'
2023-02-27 23:20:11 -08:00
import { isMangoError } from 'types'
2022-12-15 19:16:05 -08:00
2022-12-15 20:04:26 -08:00
export const DEFAULT_DELEGATE = '11111111111111111111111111111111'
2022-11-16 08:59:34 -08:00
const DelegateModal = ({ isOpen, onClose }: ModalProps) => {
const { t } = useTranslation('common')
2022-11-18 09:09:39 -08:00
const { mangoAccount } = useMangoAccount()
2022-11-16 08:59:34 -08:00
const [delegateAddress, setDelegateAddress] = useState(
2022-12-15 19:16:05 -08:00
mangoAccount?.delegate?.toString() !== DEFAULT_DELEGATE
? mangoAccount!.delegate.toString()
2022-11-16 08:59:34 -08:00
: ''
)
2022-12-15 19:16:05 -08:00
const handleDelegateAccount = async (address: string) => {
2022-11-16 08:59:34 -08:00
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const actions = mangoStore.getState().actions
if (!mangoAccount || !group) return
2022-12-15 19:16:05 -08:00
if (address && address !== '' && !PublicKey.isOnCurve(address)) {
2022-11-16 08:59:34 -08:00
notify({
type: 'error',
2022-12-15 19:16:05 -08:00
title: 'Invalid delegate address',
description: 'Check the public key of your delegate wallet is correct',
2022-11-16 08:59:34 -08:00
})
}
try {
const tx = await client.editMangoAccount(
group,
mangoAccount,
undefined,
2022-12-15 19:16:05 -08:00
delegateAddress ? new PublicKey(address) : undefined
2022-11-16 08:59:34 -08:00
)
onClose()
notify({
2022-12-15 19:16:05 -08:00
title:
address !== DEFAULT_DELEGATE
2022-12-15 20:04:26 -08:00
? t('delegate-account-info', {
delegate: abbreviateAddress(new PublicKey(address)),
2022-12-15 20:04:26 -08:00
})
2022-12-15 19:16:05 -08:00
: 'Account delegation removed',
2022-11-16 08:59:34 -08:00
type: 'success',
txid: tx,
})
await actions.reloadMangoAccount()
2023-02-27 23:20:11 -08:00
} catch (e) {
console.error(e)
if (!isMangoError(e)) return
2022-11-16 08:59:34 -08:00
notify({
title: t('account-update-failed'),
2022-12-02 15:47:08 -08:00
txid: e?.txid,
2022-11-16 08:59:34 -08:00
type: 'error',
})
}
}
return (
<Modal isOpen={isOpen} onClose={onClose}>
2022-12-15 19:16:05 -08:00
<div className="h-80">
2022-11-16 08:59:34 -08:00
<div className="flex h-full flex-col justify-between">
<div className="pb-4">
<h2 className="mb-1">{t('delegate-account')}</h2>
2022-11-16 18:18:50 -08:00
<p className="mb-4">{t('delegate-desc')}</p>
2022-12-15 19:16:05 -08:00
{mangoAccount &&
mangoAccount.delegate.toString() !== DEFAULT_DELEGATE ? (
<div className="mb-4">
<InlineNotification
type="info"
desc={`Account is delegated to ${abbreviateAddress(
mangoAccount.delegate
)}`}
/>
</div>
) : null}
2022-11-16 18:18:50 -08:00
<Label text={t('wallet-address')} />
2022-11-16 08:59:34 -08:00
<Input
type="text"
name="address"
id="address"
2022-11-16 08:59:34 -08:00
value={delegateAddress}
onChange={(e: ChangeEvent<HTMLInputElement>) =>
setDelegateAddress(e.target.value)
}
/>
</div>
2022-12-15 19:16:05 -08:00
<div className="flex flex-col items-center">
<Button
className="w-full"
onClick={() => handleDelegateAccount(delegateAddress)}
size="large"
>
{mangoAccount?.delegate.toString() !== DEFAULT_DELEGATE
? t('update-delegate')
: t('delegate')}
</Button>
{mangoAccount?.delegate.toString() !== DEFAULT_DELEGATE ? (
<LinkButton
className="mt-4"
onClick={() => handleDelegateAccount(DEFAULT_DELEGATE)}
>
{t('remove-delegate')}
</LinkButton>
) : null}
</div>
2022-11-16 08:59:34 -08:00
</div>
</div>
</Modal>
)
}
export default DelegateModal