-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2076 from decentdao/issue/2049-read-hat-details-f…
…rom-da-ipfs Read hat details from IPFS and cache it, then read it from the cache
- Loading branch information
Showing
3 changed files
with
107 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,98 @@ | ||
import { Tree } from '@hatsprotocol/sdk-v1-subgraph'; | ||
import { useEffect } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
import useIPFSClient from '../../../providers/App/hooks/useIPFSClient'; | ||
import { useNetworkConfig } from '../../../providers/NetworkConfig/NetworkConfigProvider'; | ||
import { DecentHatsError, useRolesState } from '../../../state/useRolesState'; | ||
import { CacheExpiry, CacheKeys } from '../../utils/cache/cacheDefaults'; | ||
import { getValue, setValue } from '../../utils/cache/useLocalStorage'; | ||
import { useHatsSubgraphClient } from './useHatsSubgraphClient'; | ||
|
||
const useHatsTree = () => { | ||
const { hatsTreeId } = useRolesState(); | ||
const { chain } = useNetworkConfig(); | ||
const hatsSubgraphClient = useHatsSubgraphClient(); | ||
const { setHatsTree } = useRolesState(); | ||
const ipfsClient = useIPFSClient(); | ||
|
||
useEffect(() => { | ||
if (hatsTreeId === undefined || hatsTreeId === null) { | ||
return; | ||
} | ||
hatsSubgraphClient | ||
.getTree({ | ||
chainId: chain.id, | ||
treeId: hatsTreeId, | ||
props: { | ||
hats: { | ||
props: { | ||
prettyId: true, | ||
status: true, | ||
createdAt: true, | ||
details: true, | ||
maxSupply: true, | ||
eligibility: true, | ||
toggle: true, | ||
mutable: true, | ||
levelAtLocalTree: true, | ||
currentSupply: true, | ||
wearers: { | ||
props: {}, | ||
filters: { | ||
first: 1, | ||
async function getHatsTree() { | ||
if (hatsTreeId === undefined || hatsTreeId === null) { | ||
return; | ||
} | ||
try { | ||
const tree = await hatsSubgraphClient.getTree({ | ||
chainId: chain.id, | ||
treeId: hatsTreeId, | ||
props: { | ||
hats: { | ||
props: { | ||
prettyId: true, | ||
status: true, | ||
details: true, | ||
wearers: { | ||
props: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
.then(tree => { | ||
}); | ||
const hatsWithFetchedDetails = tree.hats | ||
? await Promise.all( | ||
tree.hats.map(async hat => { | ||
const ipfsPrefix = 'ipfs://'; | ||
if (hat.details && hat.details.includes(ipfsPrefix)) { | ||
const hash = hat.details.split(ipfsPrefix)[1]; | ||
if (hash) { | ||
const cacheKey = { | ||
cacheName: CacheKeys.IPFS_HASH, | ||
hash, | ||
chainId: chain.id, | ||
} as const; | ||
const cachedDetails = getValue(cacheKey); | ||
if (cachedDetails) { | ||
return { ...hat, details: cachedDetails }; | ||
} else { | ||
try { | ||
const detailsFromIpfs = await ipfsClient.cat(hash); | ||
const jsonStringDetails = JSON.stringify(detailsFromIpfs); | ||
setValue(cacheKey, jsonStringDetails, CacheExpiry.NEVER); | ||
return { ...hat, details: jsonStringDetails }; | ||
} catch (e) { | ||
// Fuck it =/ | ||
} | ||
} | ||
} | ||
} | ||
return hat; | ||
}), | ||
) | ||
: tree.hats; | ||
|
||
const treeWithFetchedDetails: Tree = { ...tree, hats: hatsWithFetchedDetails }; | ||
try { | ||
setHatsTree(tree); | ||
setHatsTree(treeWithFetchedDetails); | ||
} catch (e) { | ||
if (e instanceof DecentHatsError) { | ||
toast(e.message); | ||
} | ||
} | ||
}) | ||
.catch(() => { | ||
} catch (e) { | ||
setHatsTree(undefined); | ||
const message = 'Hats Tree ID is not valid'; | ||
toast(message); | ||
console.error({ | ||
message, | ||
args: { | ||
network: chain.id, | ||
hatsTreeId: hatsTreeId, | ||
hatsTreeId, | ||
}, | ||
}); | ||
}); | ||
}, [chain.id, hatsSubgraphClient, hatsTreeId, setHatsTree]); | ||
} | ||
} | ||
|
||
getHatsTree(); | ||
}, [chain.id, hatsSubgraphClient, hatsTreeId, setHatsTree, ipfsClient]); | ||
}; | ||
|
||
export { useHatsTree }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters