UNPKG

@cross-nft-marketplace/auction-house-nft-hooks

Version:

Generic react hooks for fetching cross-nft-marketplace auctions, nfts, and data on arbitary 721s. Powers nft-components.

32 lines (31 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useNFTContent = void 0; const tslib_1 = require("tslib"); const react_1 = require("react"); const swr_1 = tslib_1.__importDefault(require("swr")); const NFTFetchContext_1 = require("../context/NFTFetchContext"); const ErrorUtils_1 = require("../fetcher/ErrorUtils"); const mimeTypeOptionsDefault = { refreshInterval: 0, revalidateOnFocus: false, errorRetryInterval: 5000, onErrorRetry: ErrorUtils_1.onErrorRetry }; const fetchContentDefault = { refreshInterval: 0, revalidateOnFocus: false, errorRetryInterval: 5000, onErrorRetry: ErrorUtils_1.onErrorRetry }; /** * Hook to fetch NFT content from uri and mimetype * Fetches text mime types and returns uris for non-text media * that should be embedded * * @param uri URI of content to load or return URI for * @param mimeType MIME type expected for content * @returns useNFTContentType */ function useNFTContent(uri, mimeType, mimeTypeOptions = mimeTypeOptionsDefault, fetchContentOptions = fetchContentDefault) { const fetcher = react_1.useContext(NFTFetchContext_1.NFTFetchContext); const mimeTypeFetched = swr_1.default(uri && !mimeType ? ['fetchContentMimeType', uri] : null, (_, uri) => fetcher.fetchContentMimeType(uri), mimeTypeOptions); const mimeTypeResult = mimeType || mimeTypeFetched.data; const content = swr_1.default(uri && mimeTypeResult ? ['fetchContent', uri, mimeTypeResult] : null, (_, uri, mimeTypeResult) => fetcher.fetchContent(uri, mimeTypeResult), fetchContentOptions); const error = mimeTypeFetched.error || content.error; return { error, content: content.data, }; } exports.useNFTContent = useNFTContent;