Skip to content

Commit

Permalink
refactor useHatsTree to optimize key loading logic and incorporate sa…
Browse files Browse the repository at this point in the history
…feAddress
  • Loading branch information
Da-Colon committed Dec 18, 2024
1 parent 0cefdf3 commit 8e88522
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
42 changes: 21 additions & 21 deletions src/hooks/DAO/loaders/useDecentTreasury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [];
Expand Down Expand Up @@ -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;
};
31 changes: 18 additions & 13 deletions src/hooks/DAO/loaders/useHatsTree.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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';
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';
Expand Down Expand Up @@ -150,22 +151,26 @@ const useHatsTree = () => {
t,
],
);

const node = useDaoInfoStore();
const safeAddress = node.safe?.address;
const loadKey = useRef<string | null | undefined>(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 };

0 comments on commit 8e88522

Please sign in to comment.