From 8654cb423d83bc5f24a5bf88b8a2de0b2c3a9e33 Mon Sep 17 00:00:00 2001 From: Adam Gall Date: Thu, 28 Mar 2024 22:12:50 -0400 Subject: [PATCH] Add better typing to `useAsyncRetry` --- src/components/pages/DaoHierarchy/useFetchNodes.tsx | 2 +- .../DAO/loaders/governance/useAzoriusProposals.ts | 10 ++++++---- src/hooks/utils/useAsyncRetry.ts | 12 +++--------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/components/pages/DaoHierarchy/useFetchNodes.tsx b/src/components/pages/DaoHierarchy/useFetchNodes.tsx index f3f5790016..7b5be8631b 100644 --- a/src/components/pages/DaoHierarchy/useFetchNodes.tsx +++ b/src/components/pages/DaoHierarchy/useFetchNodes.tsx @@ -96,7 +96,7 @@ export function useFetchNodes(address?: string) { for await (const subDAO of nodes) { try { const safeInfo = await requestWithRetries(() => fetchDAOInfo(subDAO.address), 5, 5000); - if (safeInfo.guard) { + if (safeInfo && safeInfo.guard) { if (safeInfo.guard === ethers.constants.AddressZero) { subDAOs.push(safeInfo); } else { diff --git a/src/hooks/DAO/loaders/governance/useAzoriusProposals.ts b/src/hooks/DAO/loaders/governance/useAzoriusProposals.ts index 6928af6f11..59e58df70c 100644 --- a/src/hooks/DAO/loaders/governance/useAzoriusProposals.ts +++ b/src/hooks/DAO/loaders/governance/useAzoriusProposals.ts @@ -167,10 +167,12 @@ export const useAzoriusProposals = () => { ); }; const proposal = await requestWithRetries(func, 5, 7000); - action.dispatch({ - type: FractalGovernanceAction.UPDATE_PROPOSALS_NEW, - payload: proposal, - }); + if (proposal !== undefined) { + action.dispatch({ + type: FractalGovernanceAction.UPDATE_PROPOSALS_NEW, + payload: proposal, + }); + } }, [ baseContracts, diff --git a/src/hooks/utils/useAsyncRetry.ts b/src/hooks/utils/useAsyncRetry.ts index 9ed3f83345..950f156c19 100644 --- a/src/hooks/utils/useAsyncRetry.ts +++ b/src/hooks/utils/useAsyncRetry.ts @@ -5,17 +5,11 @@ function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } -export type RequestWithRetries = ( - func: () => Promise, - retries: number, - secondsToWait?: number, -) => Promise; - export function useAsyncRetry() { - const requestWithRetries: RequestWithRetries = useCallback( - async (func: () => Promise, retries: number, secondsToWait: number = 2000) => { + const requestWithRetries = useCallback( + async (func: () => Promise, retries: number, secondsToWait: number = 2000) => { let currentRetries = 0; - let result = null; + let result; while (currentRetries <= retries) { try {