-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widget): refresh stale quotes (#2763)
* useBridgeQuoteUpdater * Abstract fetchAndStoreBridgeQuote() * Implement useBridgeQuoteUpdater to refresh stale bridge quotes * Clean * Add missing default prop * Ensure quote refresher only fires single callback * Clean logs
- Loading branch information
1 parent
02767e6
commit 2651119
Showing
6 changed files
with
119 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { isNull, isNumber } from 'lodash' | ||
import { useEffect, useRef } from 'react' | ||
|
||
import { type BridgeQuote } from '@/state/slices/bridgeQuote/reducer' | ||
import { calculateTimeBetween } from '@/utils/calculateTimeBetween' | ||
import { useIntervalTimer } from '@/hooks/useIntervalTimer' | ||
|
||
/** | ||
* Refreshes quotes based on selected stale timeout duration. | ||
* Will refresh quote when browser is active and wallet prompt is not pending. | ||
*/ | ||
export const useBridgeQuoteUpdater = ( | ||
quote: BridgeQuote, | ||
refreshQuoteCallback: () => Promise<void>, | ||
isQuoteLoading: boolean, | ||
isWalletPending: boolean, | ||
staleTimeout: number = 15000 // 15_000ms or 15s | ||
) => { | ||
const quoteTime = quote?.timestamp | ||
const isValidQuote = isNumber(quoteTime) && !isNull(quoteTime) | ||
const currentTime = useIntervalTimer(staleTimeout, !isValidQuote) | ||
const eventListenerRef = useRef<null | (() => void)>(null) | ||
|
||
useEffect(() => { | ||
if (isValidQuote && !isQuoteLoading && !isWalletPending) { | ||
const timeDifference = calculateTimeBetween(currentTime, quoteTime) | ||
const isStaleQuote = timeDifference >= staleTimeout | ||
|
||
if (isStaleQuote) { | ||
if (eventListenerRef.current) { | ||
document.removeEventListener('mousemove', eventListenerRef.current) | ||
} | ||
|
||
const newEventListener = () => { | ||
refreshQuoteCallback() | ||
eventListenerRef.current = null | ||
} | ||
|
||
document.addEventListener('mousemove', newEventListener, { | ||
once: true, | ||
}) | ||
|
||
eventListenerRef.current = newEventListener | ||
} | ||
} | ||
}, [currentTime, staleTimeout]) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const calculateTimeBetween = ( | ||
timeBefore: number, | ||
timeAfter: number | ||
): number => { | ||
return Math.abs(timeBefore - timeAfter) * 1000 | ||
} |