import { Avatar, Box, Divider, FormControl, FormLabel, Heading, HStack, Popover, PopoverBody, PopoverContent, PopoverTrigger, Portal, Switch, Text, useColorMode, useMediaQuery, VStack, Flex, } from "@chakra-ui/react"; import { useWallet } from "@solana/wallet-adapter-react"; import { PublicKey } from "@solana/web3.js"; import { Cluster } from "@strata-foundation/accelerator"; import { roundToDecimals, useAccelerator, useEndpoint, useLocalStorage, useMint, useTokenMetadata, } from "@strata-foundation/react"; import { toNumber } from "@strata-foundation/spl-token-bonding"; import debounce from "lodash/debounce"; import React, { useEffect } from "react"; import { RiSettings4Fill } from "react-icons/ri"; import { useChatSdk } from "../../contexts/chatSdk"; import { useChatOwnedAmounts } from "../../hooks/useChatOwnedAmounts"; import { useChat } from "../../hooks/useChat"; import { BuyMoreButton } from "../BuyMoreButton"; import { useChatPermissionsFromChat } from "../../hooks/useChatPermissionsFromChat"; import { NATIVE_MINT } from "@solana/spl-token"; const playSound = debounce(() => { const audio = new Audio("/notification.mp3"); audio.addEventListener("canplaythrough", (event) => { // the audio is now playable; play it if permissions allow audio.play(); }); }, 500); interface ISettings { soundEnabled: boolean; visualEnabled: boolean; } export const RoomsHeader = ({ chatKey }: { chatKey?: PublicKey }) => { const { info: chat } = useChat(chatKey); const { info: chatPermissions } = useChatPermissionsFromChat(chatKey); const readMintKey = chatPermissions?.readPermissionKey; const postMintKey = chatPermissions?.postPermissionKey; const [isMobile] = useMediaQuery("(max-width: 680px)"); const readMint = useMint(readMintKey); const postMint = useMint(postMintKey); const { metadata: readMetadata, image: readImage } = useTokenMetadata(readMintKey); const { metadata: postMetadata, image: postImage } = useTokenMetadata(postMintKey); const { colorMode } = useColorMode(); const { accelerator } = useAccelerator(); const { cluster } = useEndpoint(); const { chatSdk } = useChatSdk(); const { publicKey } = useWallet(); const { ownedReadAmount, ownedPostAmount, isSame } = useChatOwnedAmounts( publicKey || undefined, chatKey ); const [settings, setSettings] = useLocalStorage("settings", { soundEnabled: true, visualEnabled: false, }); useEffect(() => { const subId = (async () => { if ( accelerator && chatKey && chatSdk && publicKey && (settings.soundEnabled || settings.visualEnabled) ) { const subId = await accelerator.onTransaction( cluster as Cluster, chatKey, async ({ logs, transaction, txid, blockTime }) => { const parts = await chatSdk.getMessagePartsFromInflatedTx({ chat: chatKey, txid, blockTime, logs, transaction: { signatures: [txid], message: transaction.compileMessage(), }, }); // Only notify for other people sending message if ( !document.hasFocus() && parts.some((part) => !part.sender.equals(publicKey)) ) { playSound(); } } ); return subId; } })(); return () => { (async () => { const id = await subId; if (id && accelerator) { accelerator.unsubscribeTransaction(id); } })(); }; }, [settings, publicKey, accelerator, chatSdk, chatKey]); return ( {chat?.name} {chat?.name && ( {/* @ts-ignore */} Welcome To {chat.name} In order to participate in actions 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) && ( )} Settings Sound notifications setSettings({ ...settings, soundEnabled: e.target.checked, }) } /> {/* Desktop notifications setSettings({ ...settings, visualEnabled: e.target.checked, }) } /> */} )} ); };