import { Add, Check, Close, InfoOutlined } from '@mui/icons-material' import clsx from 'clsx' import { useFieldArray, useFormContext } from 'react-hook-form' import { useTranslation } from 'react-i18next' import { ProfileAddChainsForm, ProfileAddChainsProps } from '@dao-dao/types' import { Button } from '../buttons' import { ChainLabel } from '../chain' import { IconButton } from '../icon_buttons' import { Loader } from '../logo' import { ChainPickerPopup } from '../popup' export const ProfileAddChains = ({ prompt, promptTooltip, promptClassName, disabled, chains, onAddChains, status, size = 'default', onlySupported = false, autoAdd = false, textPrompt = false, className, }: ProfileAddChainsProps) => { const { t } = useTranslation() const { control, watch, handleSubmit } = useFormContext() const chainsBeingAdded = watch('chains') const { append: appendChain, remove: removeChain } = useFieldArray({ control, name: 'chains', }) const formActive = chainsBeingAdded.length > 0 return (
{formActive && (
{chainsBeingAdded.map(({ chainId, status }, index) => (
{status === 'idle' ? ( removeChain(index)} size="xs" variant="ghost" /> ) : status === 'loading' ? (
) : status === 'done' ? ( ) : null}
))}
)} chainId)), ...chainsBeingAdded.map(({ chainId }) => chainId), ], }} onSelect={(chainId) => { // Type-check. None option is disabled so should not be possible. if (!chainId || chainsBeingAdded.some((c) => c.chainId === chainId)) { return } appendChain({ chainId, status: 'idle', }) if (onAddChains && autoAdd) { handleSubmit(onAddChains)() } }} trigger={ chainsBeingAdded.length === 0 ? { type: 'button', tooltip: promptTooltip, props: { className: 'self-end', contentContainerClassName: clsx( textPrompt && '!secondary-text', promptClassName ), children: ( <> {!!promptTooltip && ( // Show info icon to indicate tooltip is available. )} {prompt} ), variant: textPrompt ? 'none' : 'brand', size: 'lg', disabled, }, } : { type: 'icon_button', props: { // Round to match checkbox. className: clsx('self-end !rounded', { '-mt-1': size === 'sm', '-mt-3': size === 'default', }), Icon: Add, variant: 'primary', size: 'xs', disabled: status !== 'idle', }, } } /> {formActive && ( )} ) }