import { Link, usePrevious } from "@chakra-ui/react"; import { FormControl, HStack, Center, Avatar, Circle, VStack, Text, Icon, } from "@chakra-ui/react"; import { PublicKey } from "@solana/web3.js"; import { WUMBO_TWITTER_VERIFIER, WUMBO_TWITTER_TLD, usePrimaryClaimedTokenRef, usePublicKey, useReverseName, useTokenMetadata, useTokenRef, } from "@strata-foundation/react"; import React, { useEffect, useMemo, useState } from "react"; import { FormEvent } from "react"; import { AiOutlineExclamation } from "react-icons/ai"; import { BiCheck } from "react-icons/bi"; export const Recipient = ({ value, onChange, name, }: { name: string; value: string; onChange: (e: FormEvent) => void; }) => { const [internalValue, setInternalValue] = useState(value); const recipient = usePublicKey(internalValue); const { info: tokenRefForOwner } = usePrimaryClaimedTokenRef(recipient); const { info: recipientAsTokenRef } = useTokenRef(recipient); const tokenRef = useMemo( () => tokenRefForOwner || recipientAsTokenRef, [tokenRefForOwner, recipientAsTokenRef] ); const { metadata, image, loading: metadataLoading, } = useTokenMetadata(tokenRef?.mint); const prevValue = usePrevious(value); const { nameString: handle, loading: handleLoading } = useReverseName( recipient, WUMBO_TWITTER_VERIFIER, WUMBO_TWITTER_TLD ); const invalidAddress = Boolean(!recipient && internalValue); const recipientRef = React.useRef(null); const prevRecipientRef = usePrevious(recipientRef); useEffect(() => { if (value != internalValue && prevValue != value) { if (recipientRef.current && value) { recipientRef.current.innerText = value; } setInternalValue(value); } }, [recipientRef, value, internalValue, prevValue]); useEffect(() => { if ( (!prevRecipientRef || !prevRecipientRef.current) && recipientRef.current ) { if (internalValue) { recipientRef.current.innerText = internalValue; } } }, [prevRecipientRef, recipientRef, internalValue]); return (
{metadata && } {!metadata && ( {recipient && (
)} {invalidAddress && (
)}
)}
{metadata && ( {metadata.data.name} )} {!metadata && handle && ( @{handle} )} {invalidAddress && ( Invalid address )} { // @ts-ignore e.target.value = e.target.innerText; // @ts-ignore e.target.name = name; // @ts-ignore setInternalValue(e.target.value); try { // See if valid // @ts-ignore new PublicKey(e.target.innerText); onChange(e); } catch (err) { // @ts-ignore e.target.value = null; onChange(e); } }} wordBreak="break-word" flexGrow={1} className="input" role="textbox" contentEditable w="full" border="none" overflow="auto" outline="none" resize="none" boxShadow="none" ring="none" placeholder="Enter Address" _focus={{ boxShadow: "none" }} />
); };