Files
js/components/buttons/ExtensionDetectButton.tsx
T
752b2c2634 Refactor for react/sdk v3 (#457)
* Refactor claim conditions

* Add util function

* Full NFT pages refactor

* Improve onDashboard

* Token refactor fun

* Fix some logic

* Make token drop use custom contract pages

* Remove all the things

* Fix checks

* Fix delayed reveal

* Fix marketplace embed

* Fix lgtm

* Change detection strategy

* Improve some checks

* Reduce use of actualContract

* Fix lgtm

* Fix build

* Some renamings and some more cleanup

* Default sources to release source if found

* Fix lgtm

* Rename multiwrap

* Use useTokenDecimals hook

* Fix token forms not accepting decimals

* Check decimals for inputs

* Fix logic

* Update packages

* update SDK + add Optimism/Arbitrum goerli testnets

* Add goerli l2s to disabled chains of gnosis

* Add comment

* Fix todo

* Rename detection

* Fix code tab on multiwrap and pack

* Add deprecated text

* Add faucet links

* Prettier comments

* Check isDirty for all forms

* add latest nightly

* upgrade nightly and use stable for storage

Co-authored-by: Joaquim Verges <[email protected]>
Co-authored-by: Jonas Daniels <[email protected]>
2022-09-09 18:16:09 -07:00

68 lines
2.1 KiB
TypeScript

import { useContract } from "@thirdweb-dev/react";
import { detectContractFeature } from "@thirdweb-dev/sdk";
import { FeatureName } from "@thirdweb-dev/sdk/dist/declarations/src/constants/contract-features";
import { ContractWrapper } from "@thirdweb-dev/sdk/dist/declarations/src/core/classes/contract-wrapper";
import { DetectableFeature } from "@thirdweb-dev/sdk/dist/declarations/src/core/interfaces/DetectableFeature";
import { ButtonProps } 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
*/
contractQuery: ReturnType<typeof useContract> | DetectableFeature;
/**
* the feature match strategy (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({
contractQuery,
feature,
matchStrategy = "any",
}: ExtensionDetectedStateParams): ExtensionDetectedState {
// if contract is loading return "loading"
if ("contract" in contractQuery && contractQuery.isLoading) {
return "loading";
}
const actualContract =
"contract" in contractQuery ? contractQuery.contract : contractQuery;
// we're not loading but don't have a contract, so we'll assumed feature is disabled (really this is an error state?)
if (!actualContract) {
return "disabled";
}
const contractWrapper = (actualContract as any)
.contractWrapper as ContractWrapper<any>;
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";
}