From bcdf9eddc455d444b02a4f0b9c22c9389b18df2f Mon Sep 17 00:00:00 2001 From: Adam Gall Date: Mon, 8 Jul 2024 15:58:17 -0400 Subject: [PATCH] syntax cleanup on some logic --- src/hooks/DAO/loaders/useHatsTree.ts | 65 ++++++++++++++-------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/src/hooks/DAO/loaders/useHatsTree.ts b/src/hooks/DAO/loaders/useHatsTree.ts index e0186ac9d..dd2c45be8 100644 --- a/src/hooks/DAO/loaders/useHatsTree.ts +++ b/src/hooks/DAO/loaders/useHatsTree.ts @@ -37,39 +37,38 @@ const useHatsTree = () => { }, }, }); - 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); - if (typeof detailsFromIpfs !== 'string') { - const jsonStringDetails = JSON.stringify(detailsFromIpfs); - setValue(cacheKey, jsonStringDetails, CacheExpiry.NEVER); - return { ...hat, details: jsonStringDetails }; - } - } catch (e) { - // Fuck it =/ - } - } - } - } - return hat; - }), - ) - : tree.hats; + + const hatsWithFetchedDetails = await Promise.all( + (tree.hats || []).map(async hat => { + const ipfsPrefix = 'ipfs://'; + + if (hat.details === undefined || !hat.details.startsWith(ipfsPrefix)) { + return hat; + } + + const hash = hat.details.split(ipfsPrefix)[1]; + const cacheKey = { + cacheName: CacheKeys.IPFS_HASH, + hash, + chainId: chain.id, + } as const; + + const cachedDetails = getValue(cacheKey); + + if (cachedDetails) { + return { ...hat, details: cachedDetails }; + } + + try { + const detailsFromIpfs = await ipfsClient.cat(hash); + const jsonStringDetails = JSON.stringify(detailsFromIpfs); + setValue(cacheKey, jsonStringDetails, CacheExpiry.NEVER); + return { ...hat, details: jsonStringDetails }; + } catch { + return hat; + } + }), + ); const treeWithFetchedDetails: Tree = { ...tree, hats: hatsWithFetchedDetails }; try {