import { Box, Center, Code, Flex, Icon, Popover, PopoverArrow, PopoverBody, PopoverContent, PopoverTrigger, useDisclosure, } from "@chakra-ui/react"; import { useContract } from "@thirdweb-dev/react"; import { detectContractFeature } from "@thirdweb-dev/sdk"; import type { FeatureName } from "@thirdweb-dev/sdk/dist/src/constants/contract-features"; import type { ContractWrapper } from "@thirdweb-dev/sdk/dist/src/core/classes/contract-wrapper"; import { DetectableFeature } from "@thirdweb-dev/sdk/dist/src/core/interfaces/DetectableFeature"; import React, { useRef } from "react"; import { FiCode } from "react-icons/fi"; import { Button, ButtonProps, Card, CodeBlock, Heading, LinkButton, Text, } from "tw-components"; export interface FeatureDetectButtonProps extends ButtonProps, ExtensionDetectedStateParams {} export interface ExtensionDetectedStateParams { /** * The feature or features to check */ feature: FeatureName | FeatureName[]; /** * The contract instance to check */ contract: ReturnType | DetectableFeature; /** * the feature match stragegy (default: any) * - any: any of the features must be available * - all: all of the features must be available */ matchStrategy?: "any" | "all"; } export type ExtensionDetectedState = "enabled" | "disabled" | "loading"; export function extensionDetectedState({ contract, feature, matchStrategy = "any", }: ExtensionDetectedStateParams): ExtensionDetectedState { // if contract is loading return "loading" if ("contract" in contract && contract.isLoading) { return "loading"; } const actualContract = "contract" in contract ? contract.contract : contract; // we're not loading but don't have a contract, so we'll assumed feture is disabled (really this is an error state?) if (!actualContract) { return "disabled"; } const contractWrapper = (actualContract as any) .contractWrapper as ContractWrapper; if (!Array.isArray(feature)) { return detectContractFeature(contractWrapper, feature) ? "enabled" : "disabled"; } if (matchStrategy === "all") { return feature.every((f) => detectContractFeature(contractWrapper, f)) ? "enabled" : "disabled"; } return feature.some((f) => detectContractFeature(contractWrapper, f)) ? "enabled" : "disabled"; } export const ExtensionDetectButton = React.forwardRef< HTMLButtonElement, FeatureDetectButtonProps >( ( { children, isDisabled, onClick, loadingText, type, feature, contract, matchStrategy = "any", isLoading, leftIcon, rightIcon, colorScheme, // variant, ...restProps }, ref, ) => { const detectedState = extensionDetectedState({ contract, feature, matchStrategy, }); const initialFocusRef = useRef(null); const { isOpen, onOpen, onClose } = useDisclosure(); return ( ); }, ); ExtensionDetectButton.displayName = "ExtensionDetectButton"; interface ExtensionMissingContentProps extends Omit {} const ExtensionMissingContent: React.FC = ({ feature, matchStrategy, }) => { const arrayFeatures = Array.isArray(feature) ? feature : [feature]; return ( How to enable this functionality Please add{" "} {arrayFeatures.length === 1 ? ( "this extension" ) : ( <> {matchStrategy === "any" ? "one of" : ""} these extensions )}{" "} to your contract. {arrayFeatures.map((feat, idx, arr) => ( {feat} Learn more {arr.length - 1 !== idx && (
{matchStrategy === "any" ? "OR" : "AND"}
)}
))}
); };