-
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(synapse-interface): refresh stale quotes (#2607)
* Track BridgeQuote by initiated timestamp * useStaleQuoteRefresher to manage refreshing stale bridge quotes * Prevent stack overflow when invalid quote * Prevent quote refetch is one is already loading * Fix build * Prevent b2b callbacks if time difference is too great * Pause formatAmount error logging, only retrigger useEffect for stale quote refresher when currentTime changes * Wrap refresh quote callback with mousedown event to reduce resource usage for idle pages * Clean * Prevent refresh quote if browser wallet prompts are open as async callback has already initiated * Listen to stale timeout value
- Loading branch information
1 parent
bc813b7
commit c0de3ca
Showing
7 changed files
with
64 additions
and
4 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
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
34 changes: 34 additions & 0 deletions
34
packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts
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,34 @@ | ||
import { isNull, isNumber } from 'lodash' | ||
import { useEffect } from 'react' | ||
|
||
import { BridgeQuote } from '@/utils/types' | ||
import { calculateTimeBetween } from '@/utils/time' | ||
import { useIntervalTimer } from '@/utils/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 useStaleQuoteRefresher = ( | ||
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) | ||
|
||
useEffect(() => { | ||
if (isValidQuote && !isQuoteLoading && !isWalletPending) { | ||
const timeDifference = calculateTimeBetween(currentTime, quoteTime) | ||
const isStaleQuote = timeDifference >= staleTimeout | ||
if (isStaleQuote) { | ||
document.addEventListener('mousemove', refreshQuoteCallback, { | ||
once: true, | ||
}) | ||
} | ||
} | ||
}, [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