This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: avoid loading safe multiple times
Lint
- Loading branch information
Showing
6 changed files
with
33 additions
and
53 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
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,37 +1,25 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useEffect } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
|
||
import addViewedSafe from 'src/logic/currentSession/store/actions/addViewedSafe' | ||
import fetchLatestMasterContractVersion from 'src/logic/safe/store/actions/fetchLatestMasterContractVersion' | ||
import { fetchSafe } from 'src/logic/safe/store/actions/fetchSafe' | ||
import fetchTransactions from 'src/logic/safe/store/actions/transactions/fetchTransactions' | ||
import { Dispatch } from 'src/logic/safe/store/actions/types.d' | ||
import { updateAvailableCurrencies } from 'src/logic/currencyValues/store/actions/updateAvailableCurrencies' | ||
import { currentChainId } from 'src/logic/config/store/selectors' | ||
import { fetchSafeTokens } from 'src/logic/tokens/store/actions/fetchSafeTokens' | ||
|
||
export const useLoadSafe = (safeAddress?: string): boolean => { | ||
export const useLoadSafe = (safeAddress?: string): void => { | ||
const dispatch = useDispatch<Dispatch>() | ||
const chainId = useSelector(currentChainId) | ||
const [isSafeLoaded, setIsSafeLoaded] = useState(false) | ||
|
||
useEffect(() => { | ||
setIsSafeLoaded(false) | ||
}, [safeAddress]) | ||
if (!safeAddress) return | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
if (safeAddress) { | ||
await dispatch(fetchLatestMasterContractVersion()) | ||
await dispatch(fetchSafe(safeAddress, isSafeLoaded)) | ||
|
||
setIsSafeLoaded(true) | ||
await dispatch(updateAvailableCurrencies()) | ||
await dispatch(fetchTransactions(chainId, safeAddress)) | ||
dispatch(addViewedSafe(safeAddress)) | ||
} | ||
} | ||
fetchData() | ||
}, [chainId, dispatch, safeAddress, isSafeLoaded]) | ||
|
||
return isSafeLoaded | ||
dispatch(fetchLatestMasterContractVersion()) | ||
dispatch(fetchSafe(safeAddress)) | ||
dispatch(fetchSafeTokens(safeAddress)) | ||
dispatch(updateAvailableCurrencies()) | ||
dispatch(addViewedSafe(safeAddress)) | ||
}, [dispatch, safeAddress, chainId]) | ||
} |
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,33 +1,26 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { useEffect, useState } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { currentChainId } from 'src/logic/config/store/selectors' | ||
import { fetchSafe } from 'src/logic/safe/store/actions/fetchSafe' | ||
import { fetchSafeTokens } from 'src/logic/tokens/store/actions/fetchSafeTokens' | ||
import { TIMEOUT } from 'src/utils/constants' | ||
import { SAFE_POLLING_INTERVAL } from 'src/utils/constants' | ||
|
||
export const useSafeScheduledUpdates = (safeLoaded: boolean, safeAddress?: string): void => { | ||
export const useSafeScheduledUpdates = (safeAddress?: string): void => { | ||
const dispatch = useDispatch() | ||
const timer = useRef<number>() | ||
const [pollCount, setPollCount] = useState<number>(0) | ||
const chainId = useSelector(currentChainId) | ||
|
||
useEffect(() => { | ||
// using this variable to prevent setting a timeout when the component is already unmounted or the effect | ||
// has to run again | ||
let mounted = true | ||
const fetchSafeData = (address: string): void => { | ||
dispatch(fetchSafe(address, safeLoaded)) | ||
dispatch(fetchSafeTokens(address)) | ||
|
||
if (mounted) { | ||
timer.current = window.setTimeout(() => fetchSafeData(address), TIMEOUT * 3) | ||
const timer = setTimeout(() => { | ||
if (safeAddress) { | ||
dispatch(fetchSafe(safeAddress)) | ||
dispatch(fetchSafeTokens(safeAddress)) | ||
} | ||
} | ||
|
||
if (safeAddress && safeLoaded) { | ||
fetchSafeData(safeAddress) | ||
} | ||
setPollCount((prev) => prev + 1) | ||
}, SAFE_POLLING_INTERVAL) | ||
|
||
return () => { | ||
mounted = false | ||
clearTimeout(timer.current) | ||
clearTimeout(timer) | ||
} | ||
}, [dispatch, safeAddress, safeLoaded]) | ||
}, [dispatch, safeAddress, chainId, pollCount, setPollCount]) | ||
} |
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
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