@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.
34 lines (33 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useNFTMetadata = 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 swrConfigurationDefault = {
refreshInterval: 0,
revalidateOnFocus: false,
errorRetryInterval: 5000,
errorRetryCount: 5,
onErrorRetry: ErrorUtils_1.onErrorRetry
};
/**
* Fetches NFT Metadata from IPFS.
* This hook is cached, it can be called
* multiple times with no issue on one page.
*
* @param uri URI of metadata to fetch
* @returns @type useNFTMetadataType
*/
function useNFTMetadata(uri, options = swrConfigurationDefault) {
const fetcher = react_1.useContext(NFTFetchContext_1.NFTFetchContext);
const { error, data } = swr_1.default(uri ? ['loadMetadata', uri] : null, (_, uri) => fetcher.fetchIPFSMetadata(uri), options);
return {
loading: !error && !data,
error,
metadata: data,
};
}
exports.useNFTMetadata = useNFTMetadata;