From 8e885227eccf040b67b6fac72a2e01cadf293c0a Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:20:31 -0500 Subject: [PATCH] refactor useHatsTree to optimize key loading logic and incorporate safeAddress --- src/hooks/DAO/loaders/useDecentTreasury.ts | 42 +++++++++++----------- src/hooks/DAO/loaders/useHatsTree.ts | 31 +++++++++------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/hooks/DAO/loaders/useDecentTreasury.ts b/src/hooks/DAO/loaders/useDecentTreasury.ts index 5b743079e..647e46a89 100644 --- a/src/hooks/DAO/loaders/useDecentTreasury.ts +++ b/src/hooks/DAO/loaders/useDecentTreasury.ts @@ -128,15 +128,15 @@ export const useDecentTreasury = () => { ...uniqueEthereumTransactions.flatMap(tx => tx.transfers || []), ]; - if (tokenBalancesError) { - toast.warning(tokenBalancesError, { duration: 2000 }); - } - if (nftBalancesError) { - toast.warning(nftBalancesError, { duration: 2000 }); - } - if (defiBalancesError) { - toast.warning(defiBalancesError, { duration: 2000 }); - } + // if (tokenBalancesError) { + // toast.warning(tokenBalancesError, { duration: 2000 }); + // } + // if (nftBalancesError) { + // toast.warning(nftBalancesError, { duration: 2000 }); + // } + // if (defiBalancesError) { + // toast.warning(defiBalancesError, { duration: 2000 }); + // } const assetsFungible = tokenBalances || []; const assetsNonFungible = nftBalances || []; const assetsDeFi = defiBalances || []; @@ -271,18 +271,18 @@ export const useDecentTreasury = () => { safeAPI, ]); - useEffect(() => { - if (!safeAddress) { - loadKey.current = null; - return; - } - - const newLoadKey = `${chain.id}${safeAddress}`; - if (newLoadKey !== loadKey.current) { - loadKey.current = newLoadKey; - loadTreasury(); - } - }, [action, chain.id, safeAddress, loadTreasury]); + // useEffect(() => { + // if (!safeAddress) { + // loadKey.current = null; + // return; + // } + + // const newLoadKey = `${chain.id}${safeAddress}`; + // if (newLoadKey !== loadKey.current) { + // loadKey.current = newLoadKey; + // loadTreasury(); + // } + // }, [action, chain.id, safeAddress, loadTreasury]); return; }; diff --git a/src/hooks/DAO/loaders/useHatsTree.ts b/src/hooks/DAO/loaders/useHatsTree.ts index ca7a764f0..d84c4fa8b 100644 --- a/src/hooks/DAO/loaders/useHatsTree.ts +++ b/src/hooks/DAO/loaders/useHatsTree.ts @@ -1,6 +1,6 @@ import { useApolloClient } from '@apollo/client'; import { HatsSubgraphClient, Tree } from '@hatsprotocol/sdk-v1-subgraph'; -import { useCallback, useEffect } from 'react'; +import { useCallback, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { toast } from 'sonner'; import { PublicClient } from 'viem'; @@ -8,6 +8,7 @@ import { usePublicClient } from 'wagmi'; import { useFractal } from '../../../providers/App/AppProvider'; import useIPFSClient from '../../../providers/App/hooks/useIPFSClient'; import { useNetworkConfigStore } from '../../../providers/NetworkConfig/useNetworkConfigStore'; +import { useDaoInfoStore } from '../../../store/daoInfo/useDaoInfoStore'; import { DecentHatsError } from '../../../store/roles/rolesStoreUtils'; import { useRolesStore } from '../../../store/roles/useRolesStore'; import { CacheExpiry, CacheKeys } from '../../utils/cache/cacheDefaults'; @@ -150,22 +151,26 @@ const useHatsTree = () => { t, ], ); - + const node = useDaoInfoStore(); + const safeAddress = node.safe?.address; + const loadKey = useRef(undefined); useEffect(() => { + const key = safeAddress && hatsTreeId ? `${safeAddress}:${hatsTreeId}` : null; if ( - hatsTreeId === undefined || - hatsTreeId === null || - publicClient === undefined || - contextChainId === null + !!hatsTreeId && + !!contextChainId && + !!publicClient && + key !== null && + key !== loadKey.current ) { - return; + getHatsTree({ + hatsTreeId, + contextChainId, + publicClient, + }); } - getHatsTree({ - hatsTreeId, - contextChainId, - publicClient, - }); - }, [contextChainId, getHatsTree, hatsTreeId, publicClient]); + loadKey.current = key; + }, [contextChainId, getHatsTree, hatsTreeId, publicClient, safeAddress]); }; export { useHatsTree };