import { fromBech32 } from '@cosmjs/encoding' import { useFormContext } from 'react-hook-form' import { useTranslation } from 'react-i18next' import { ImportMultisigForm, ImportMultisigModalProps } from '@dao-dao/types' import { formatPercentOf100, getConfiguredChains, isValidBech32Address, makeValidateAddress, } from '@dao-dao/utils' import { useChain } from '../../../contexts' import { useProcessTQ } from '../../../hooks' import { Button } from '../../buttons' import { InputErrorMessage, InputLabel } from '../../inputs' import { Loader } from '../../logo' import { Modal } from '../../modals' import { ChainPickerPopup } from '../../popup' export const ImportMultisigModal = ({ loadingMultisig, onImport, AddressInput, EntityDisplay, ...props }: ImportMultisigModalProps) => { const { t } = useTranslation() const { register, watch, setValue, formState: { errors }, } = useFormContext() const address = watch('address') const { chainId, bech32Prefix } = useChain() const processTQ = useProcessTQ() const processedMultisigTQ = loadingMultisig.loading || loadingMultisig.errored ? undefined : processTQ(loadingMultisig.data.config.threshold) const multisigType = loadingMultisig.loading || loadingMultisig.errored || !processedMultisigTQ ? 'unknown' : 'absolute_count' in loadingMultisig.data.config.threshold ? t('info.xOfYMultisig', { x: processedMultisigTQ.threshold.display, y: loadingMultisig.data.config.totalWeight, }) : [ t('title.threshold') + ': ' + processedMultisigTQ.threshold.display, processedMultisigTQ.quorum && t('info.quorum') + ': ' + processedMultisigTQ.quorum.display, ] .filter(Boolean) .join(', ') return (
{t('title.chain')} { // Type-check. None option is disabled so should not be possible. if (!chainId) { return } setValue('chainId', chainId) }} selectedChainId={chainId} />
{t('form.address')} { // Find first chain with matching prefix and update if needed. if (newAddress) { try { const { prefix } = fromBech32(newAddress) const matchingChainId = getConfiguredChains().find( ({ chain }) => chain.bech32Prefix === prefix )?.chainId if (matchingChainId && matchingChainId !== chainId) { setValue('chainId', matchingChainId) } } catch {} } }} placeholder={bech32Prefix + '...'} register={register} setValue={setValue} validation={[makeValidateAddress(bech32Prefix)]} watch={watch} />
{isValidBech32Address(address, bech32Prefix) && (loadingMultisig.loading ? ( ) : ( !loadingMultisig.errored && ( <>
{t('title.type')}

{multisigType}

{t('title.members')} {loadingMultisig.data.config.members.map( ({ address, weight }) => (

{formatPercentOf100( (weight / loadingMultisig.data.config.totalWeight) * 100 )}

) )}
) ))}
) }