import { Error, Info, TurnedIn } from '@mui/icons-material'; import { LoadingButton } from '@mui/lab'; import { Button, Typography } from '@mui/material'; import type { ChangeEvent, MutableRefObject } from 'react'; import { forwardRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { AlertMessage } from '../../components/AlertMessage/AlertMessage.js'; import { BottomSheet } from '../../components/BottomSheet/BottomSheet.js'; import type { BottomSheetBase } from '../../components/BottomSheet/types.js'; import { Input } from '../../components/Input.js'; import { useAddressValidation } from '../../hooks/useAddressValidation.js'; import type { Bookmark } from '../../stores/bookmarks/types.js'; import { useBookmarkActions } from '../../stores/bookmarks/useBookmarkActions.js'; import { AddressInput, BookmarkInputFields, IconContainer, SendToWalletButtonRow, SendToWalletCard, SendToWalletSheetContainer, SheetAddressContainer, SheetTitle, ValidationAlert, } from './SendToWalletPage.style.js'; import type { BookmarkError } from './types.js'; interface BookmarkAddressProps { onAddBookmark: (bookmark: Bookmark) => void; validatedWallet?: Bookmark; } export const BookmarkAddressSheet = forwardRef< BottomSheetBase, BookmarkAddressProps >(({ validatedWallet, onAddBookmark }, ref) => { const { t } = useTranslation(); const [name, setName] = useState(''); const [address, setAddress] = useState(''); const [error, setError] = useState(); const { validateAddress, isValidating } = useAddressValidation(); const { getBookmark } = useBookmarkActions(); const nameValue = name.trim() || validatedWallet?.name || ''; const addressValue = address || validatedWallet?.address || ''; const handleCancel = () => { setError(undefined); (ref as MutableRefObject).current?.close(); }; const validateWithAddressFromInput = async () => { const validationResult = await validateAddress({ value: address }); if (!validationResult.isValid) { setError({ type: 'address', message: validationResult.error }); return; } return { name: nameValue, address: validationResult.address, chainType: validationResult.chainType, }; }; const validateWithValidatedWallet = () => { if (error) { setError(undefined); } return { name: nameValue, address: validatedWallet!.address, chainType: validatedWallet!.chainType, }; }; const handleBookmark = async () => { if (isValidating) { return; } if (!nameValue) { setError({ type: 'name', message: t('error.title.bookmarkNameRequired'), }); return; } if (!addressValue) { setError({ type: 'address', message: t('error.title.walletAddressRequired'), }); return; } // If the validatedWallet is supplied as a prop then we should assume // the address has already been validated const validatedBookmark = validatedWallet ? validateWithValidatedWallet() : await validateWithAddressFromInput(); if (validatedBookmark) { const existingBookmark = getBookmark(validatedBookmark.address); if (existingBookmark) { setError({ type: 'address', message: t('error.title.bookmarkAlreadyExists', { name: existingBookmark.name, }), }); return; } (ref as MutableRefObject).current?.close(); onAddBookmark({ name: validatedBookmark.name, address: validatedBookmark.address, chainType: validatedBookmark.chainType, }); } }; const handleAddressInputChange = (e: ChangeEvent) => { if (error) { setError(undefined); } setAddress((e.target as HTMLInputElement).value.trim()); }; const handleNameInputChange = (e: ChangeEvent) => { if (error) { setError(undefined); } setName((e.target as HTMLInputElement).value); }; const resetValues = () => { setName(''); setAddress(''); }; return ( {t('sendToWallet.bookmarkWallet')} {validatedWallet ? ( {validatedWallet?.name ? ( {validatedWallet?.name} ) : null} {validatedWallet?.address} ) : null} {!validatedWallet && ( )} {error ? ( }>{error.message} ) : null} {t('info.message.fundsToExchange')} } icon={} /> {t('button.bookmark')} ); });