Skip to content

Commit

Permalink
Merge pull request #13 from storyprotocol/feat/update-provider
Browse files Browse the repository at this point in the history
Update Provider with `options` param to enable users to choose the APIs they want to call
  • Loading branch information
allenchuang authored May 31, 2024
2 parents 0a0713a + fe12652 commit 5640798
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions packages/storykit/src/providers/IpProvider/IpProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import { RESOURCE_TYPE } from "../../types/api"
import { Asset, IPAPolicy, License, Policy, RoyaltyPolicy } from "../../types/assets"
import { NFTMetadata } from "../../types/simplehash"

export interface IpProviderOptions {
assetData?: boolean
policyData?: boolean
licenseData?: boolean
royaltyData?: boolean
}

const IpContext = React.createContext<{
// activeTab: string
// setActiveTab: React.Dispatch<React.SetStateAction<string>>
assetData: Asset | undefined
nftData: NFTMetadata | undefined
isNftDataLoading: boolean
Expand All @@ -26,11 +31,27 @@ const IpContext = React.createContext<{
isRoyaltyDataLoading: boolean
} | null>(null)

export const IpProvider = ({ children, ipId }: { children: React.ReactNode; ipId: Address }) => {
export const IpProvider = ({
children,
ipId,
options = {},
}: {
children: React.ReactNode
ipId: Address
options?: IpProviderOptions
}) => {
const queryOptions = {
assetData: true,
policyData: true,
licenseData: true,
royaltyData: true,
...options,
}
// Fetch asset data
const { isLoading: isAssetDataLoading, data: assetData } = useQuery({
queryKey: [RESOURCE_TYPE.ASSET, ipId],
queryFn: () => getResource(RESOURCE_TYPE.ASSET, ipId),
enabled: queryOptions.assetData,
})

const ipaPolicyQueryOptions = {
Expand All @@ -46,6 +67,7 @@ export const IpProvider = ({ children, ipId }: { children: React.ReactNode; ipId
const { isLoading: isIPAPolicyDataLoading, data: ipPolicyData } = useQuery({
queryKey: [RESOURCE_TYPE.IPA_POLICY, ipaPolicyQueryOptions],
queryFn: () => listResource(RESOURCE_TYPE.IPA_POLICY, ipaPolicyQueryOptions),
enabled: queryOptions.policyData,
})

async function fetchPolicyDetails(data: IPAPolicy[]) {
Expand Down Expand Up @@ -74,7 +96,7 @@ export const IpProvider = ({ children, ipId }: { children: React.ReactNode; ipId
const { isLoading: isPolicyDataLoading, data: policyData } = useQuery({
queryKey: ["fetchPolicyDetails", ipPolicyData?.data],
queryFn: () => fetchPolicyDetails(ipPolicyData?.data),
enabled: Boolean(ipPolicyData) && Boolean(ipPolicyData.data),
enabled: Boolean(ipPolicyData) && Boolean(ipPolicyData.data) && queryOptions.policyData,
})

const licenseQueryOptions = {
Expand All @@ -90,6 +112,7 @@ export const IpProvider = ({ children, ipId }: { children: React.ReactNode; ipId
const { isLoading: isLicenseDataLoading, data: licenseData } = useQuery({
queryKey: [RESOURCE_TYPE.LICENSE, licenseQueryOptions],
queryFn: () => listResource(RESOURCE_TYPE.LICENSE, licenseQueryOptions),
enabled: queryOptions.licenseData,
})

// Fetch Royalty Data
Expand All @@ -107,17 +130,14 @@ export const IpProvider = ({ children, ipId }: { children: React.ReactNode; ipId
},
],
queryFn: () => getResource(RESOURCE_TYPE.ROYALTY_POLICY, ipId),
enabled: queryOptions.royaltyData,
})

// const { isLoading: isNftDataLoading, data: nftData } = useQuery({
// queryKey: ["getNFTByTokenId", assetData?.data?.tokenContract, assetData?.data?.tokenId],
// queryFn: () => getNFTByTokenId(assetData.data.tokenContract, assetData.data.tokenId),
// enabled: Boolean(assetData) && Boolean(assetData.data.tokenContract) && Boolean(assetData.data.tokenId),
// })
const { isLoading: isNftDataLoading, data: nftData } = useQuery({
queryKey: ["getNFTByTokenId", assetData?.data?.nftMetadata?.tokenContract, assetData?.data?.nftMetadata?.tokenId],
queryFn: () => getNFTByTokenId(assetData.data.nftMetadata.tokenContract, assetData.data.nftMetadata.tokenId),
enabled:
queryOptions.assetData &&
Boolean(assetData) &&
Boolean(assetData.data.nftMetadata.tokenContract) &&
Boolean(assetData.data.nftMetadata.tokenId),
Expand All @@ -126,8 +146,6 @@ export const IpProvider = ({ children, ipId }: { children: React.ReactNode; ipId
return (
<IpContext.Provider
value={{
// activeTab,
// setActiveTab,
nftData,
isNftDataLoading,
assetData: assetData?.data,
Expand Down

0 comments on commit 5640798

Please sign in to comment.