import { useConnectModal, useAccountState, useDisconnectModal, GeneralAccount, useCsbDetailModal, useIsOpSignEnabled, useOpSignSettingsModal, useUpgradeAccountModal, useSelectCharactersModal, useAccountBalance, } from "@crossbell/connect-kit" import { useAccountSites } from "~/queries/site" import { Avatar } from "~/components/ui/Avatar" import { Button, type VariantColor, type Variant } from "~/components/ui/Button" import { DashboardIcon } from "../icons/DashboardIcon" import { UniLink } from "../ui/UniLink" import { useEffect, useState } from "react" import { Square2StackIcon, ArrowRightOnRectangleIcon, BellIcon, CurrencyDollarIcon, UsersIcon, FaceFrownIcon, FaceSmileIcon, ArrowUpCircleIcon, ArrowPathRoundedSquareIcon, ChevronDownIcon, } from "@heroicons/react/24/outline" import { BellAlertIcon } from "@heroicons/react/24/solid" import { SITE_URL } from "~/lib/env" import { useRefCallback } from "@crossbell/util-hooks" import { useShowNotificationModal, useNotifications, } from "@crossbell/notification" import { Menu } from "~/components/ui/Menu" import { useTranslation } from "next-i18next" type HeaderLinkType = { icon?: React.ReactNode label: string | JSX.Element url?: string onClick?: () => void } export const ConnectButton: React.FC<{ left?: boolean variant?: Variant variantColor?: VariantColor size?: "base" | "sm" hideNotification?: boolean mobileSimplification?: boolean hideName?: boolean }> = ({ left, variant, variantColor, size = "sm", hideNotification, mobileSimplification, hideName, }) => { let avatarSize let sizeDecrease switch (size) { case "base": avatarSize = 40 sizeDecrease = "sm" break default: avatarSize = 30 sizeDecrease = "xs" } const [ssrReady, account] = useAccountState(({ ssrReady, computed }) => [ ssrReady, computed.account, ]) const isOpSignEnabled = useIsOpSignEnabled({ characterId: account?.characterId, }) const { show: openConnectModal } = useConnectModal() const { show: disconnect } = useDisconnectModal() const { balance } = useAccountBalance() const [copyLabelDisplay, copyLabel] = useCopyLabel(account) const csbDetailModal = useCsbDetailModal() const opSignSettingsModal = useOpSignSettingsModal() const upgradeAccountModal = useUpgradeAccountModal() const selectCharactersModal = useSelectCharactersModal() const userSites = useAccountSites() const showNotificationModal = useShowNotificationModal() const { isAllRead } = useNotifications() const [InsufficientBalance, setInsufficientBalance] = useState(false) const { t } = useTranslation("common") useEffect(() => { if (balance) { if ( BigInt(balance.value.toString()) > BigInt("1" + "0".repeat(balance.decimals - 2)) ) { setInsufficientBalance(false) } else { setInsufficientBalance(true) } } }, [balance]) const dropdownLinks: HeaderLinkType[] = [ { icon: , label: t("Dashboard") || "", url: `${SITE_URL}/dashboard`, }, { icon: , label: t(copyLabelDisplay) || "", onClick: copyLabel, }, ...(account?.type === "wallet" ? [ { icon: , label: ( <> {t("Operator Sign")} ( {isOpSignEnabled ? ( ) : ( )} ) ), onClick: () => { if (account?.characterId) { opSignSettingsModal.show(account?.characterId) } }, }, { icon: , label: ( {balance?.formatted.replace(/\.(\d{5})\d*$/, ".$1") || "0.00000"}{" "} CSB ), onClick: csbDetailModal.show, }, { icon: , label: t("Switch Characters") || "", onClick: selectCharactersModal.show, }, ] : [ { icon: , label: t("Upgrade to Wallet") || "", onClick: upgradeAccountModal.show, }, ]), { icon: , label: t("Disconnect") || "", onClick: disconnect, }, ] return (
{(() => { if (!account) { return ( ) } return (
{userSites.isSuccess ? ( <> {!hideNotification && ( <> {isAllRead ? ( ) : ( )}
)} {!hideName && ( <>
{userSites.data?.[0]?.name || getAccountDisplayName(account)} {userSites.data?.[0]?.username && ( {"@" + userSites.data?.[0]?.username || getAccountDisplayName(account)} )}
)} } dropdown={
{dropdownLinks.map((link, i) => { return ( {link.icon} {link.label} ) })}
} /> ) : ( "" )}
) })()}
) } function useCopyLabel(account: GeneralAccount | null) { const [isShowCopied, setIsShowCopied] = useState(false) const copyLabel = useRefCallback(() => { const value = (account?.type === "email" ? account.email : account?.address) || "" navigator.clipboard.writeText(value) setIsShowCopied(true) setTimeout(() => setIsShowCopied(false), 1000) }) return [ isShowCopied ? "Copied!" : getAccountDisplayName(account), copyLabel, ] as const } function getAccountDisplayName(account: GeneralAccount | null) { const value = (account?.type === "email" ? account.email : account?.address) || "" return value.slice(0, 5) + "..." + value.slice(-4) }