import { Avatar, Flex, Box, Button, Divider, HStack, Text, useDisclosure, VStack, } from "@chakra-ui/react"; import { useWallet } from "@solana/wallet-adapter-react"; import { useWalletModal } from "@solana/wallet-adapter-react-ui"; import { roundToDecimals, useErrorHandler, useMint, useTokenMetadata, } from "@strata-foundation/react"; import { toNumber } from "@strata-foundation/spl-token-bonding"; import { useChatPermissionsFromChat } from "../../hooks/useChatPermissionsFromChat"; import React, { useRef, useState } from "react"; import { useLoadDelegate } from "../../hooks/useLoadDelegate"; import { useChatOwnedAmounts } from "../../hooks/useChatOwnedAmounts"; import { BuyMoreButton } from "../BuyMoreButton"; import { LoadWalletModal } from "../LoadWalletModal"; import { Chatbox, chatProps } from "./Chatbox"; import { NATIVE_MINT } from "@solana/spl-token"; const DARK_BG = { bg: "linear-gradient(0deg, rgba(17,24,39) 40%, rgba(21,24,38,0) 100%)", }; export function ChatboxWithGuards({ chatKey, onAddPendingMessage, files, setFiles, onUploadFile, }: chatProps) { const { isOpen: loadWalletIsOpen, onOpen: onOpenLoadWallet, onClose: onCloseLoadWallet, } = useDisclosure(); const { connected, publicKey } = useWallet(); const { setVisible } = useWalletModal(); const { delegateWallet, needsTopOff, needsInit, loading: loadingDelegate, error: delegateError, } = useLoadDelegate(); const { isOpen: delegateIsOpen, onClose: closeDelegate, onOpen: openDelegate, } = useDisclosure({ defaultIsOpen: false, }); const { handleErrors } = useErrorHandler(); const { info: chatPermissions } = useChatPermissionsFromChat(chatKey); const readMintKey = chatPermissions?.readPermissionKey; const postMintKey = chatPermissions?.postPermissionKey; const readMint = useMint(readMintKey); const postMint = useMint(postMintKey); const { metadata: readMetadata, image: readImage } = useTokenMetadata(readMintKey); const { metadata: postMetadata, image: postImage } = useTokenMetadata(postMintKey); const { ownedReadAmount, ownedPostAmount, isSame } = useChatOwnedAmounts( publicKey || undefined, chatKey ); const postAmount = chatPermissions?.postPermissionAmount && postMint && toNumber(chatPermissions?.postPermissionAmount, postMint); const hasEnough = typeof postAmount == "undefined" || typeof ownedPostAmount == "undefined" || ownedPostAmount >= postAmount; handleErrors(delegateError); return ( {!connected || !hasEnough || needsTopOff ? ( {!connected ? ( <> ) : !hasEnough ? ( <> In order to participate in this chat: {readMetadata && ( Read Message Hold{" "} {chatPermissions?.defaultReadPermissionAmount && readMint && roundToDecimals( toNumber( chatPermissions.defaultReadPermissionAmount, readMint ), 4 )} )} {postMetadata && ( Post Message { Object.keys( chatPermissions?.postPermissionAction || {} )[0] }{" "} {chatPermissions?.postPermissionAmount && postMint && roundToDecimals( toNumber( chatPermissions.postPermissionAmount, postMint ), 4 )} )} {readMetadata && ( You currently have {ownedReadAmount ? roundToDecimals(ownedReadAmount, 4) : 0} )} {!isSame && postMetadata && ( You currently have {ownedPostAmount ? roundToDecimals(ownedPostAmount, 4) : 0} )} {!isSame && chatPermissions && !NATIVE_MINT.equals(chatPermissions?.postPermissionKey) && ( )} ) : needsTopOff ? ( <> onCloseLoadWallet()} /> ) : null} ) : ( {!delegateWallet && ( Tired of approving transactions? )} {(needsTopOff || needsInit) && ( )} )} ); }