Skip to content

Commit

Permalink
[WIP] stale quote animation (#3105)
Browse files Browse the repository at this point in the history
* animate-quote-refresh

* pair stale quote refresh with each quote

* update progress circle animation
  • Loading branch information
bigboydiamonds authored Sep 7, 2024
1 parent 571b391 commit c7b2981
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAccount } from 'wagmi'
import { useMemo } from 'react'
import { useMemo, useEffect, useState } from 'react'

import { ChainSelector } from '@/components/ui/ChainSelector'
import { TokenSelector } from '@/components/ui/TokenSelector'
Expand All @@ -14,11 +14,13 @@ import { setToChainId, setToToken } from '@/slices/bridge/reducer'
import { useBridgeDisplayState, useBridgeState } from '@/slices/bridge/hooks'
import { useWalletState } from '@/slices/wallet/hooks'
import { useBridgeQuoteState } from '@/slices/bridgeQuote/hooks'
import { BridgeQuote } from '@/utils/types'
import { useBridgeValidations } from './hooks/useBridgeValidations'
import { useConfirmNewBridgePrice } from './hooks/useConfirmNewBridgePrice'

export const OutputContainer = () => {
const { address } = useAccount()
const { bridgeQuote, isLoading } = useBridgeQuoteState()
const { bridgeQuote, previousBridgeQuote, isLoading } = useBridgeQuoteState()
const { showDestinationAddress } = useBridgeDisplayState()
const { hasValidInput, hasValidQuote } = useBridgeValidations()

Expand Down Expand Up @@ -48,6 +50,9 @@ export const OutputContainer = () => {
showValue={showValue}
isLoading={isLoading}
/>
{hasValidQuote && !isLoading && (
<AnimatedProgressCircle bridgeQuoteId={bridgeQuote?.id} />
)}
</BridgeAmountContainer>
</BridgeSectionContainer>
)
Expand Down Expand Up @@ -88,3 +93,43 @@ const ToTokenSelector = () => {
/>
)
}

const AnimatedProgressCircle = ({ bridgeQuoteId }) => {
const [animationKey, setAnimationKey] = useState(0)

useEffect(() => {
setAnimationKey((prevKey) => prevKey + 1)
}, [bridgeQuoteId])

return (
<svg
key={animationKey}
width="36"
height="36"
viewBox="-12 -12 24 24"
stroke="currentcolor"
strokeOpacity=".33"
fill="none"
className="-rotate-90 -scale-y-100"
>
<circle r="8">
<animate
attributeName="opacity"
values="0; 0; 1"
dur="15s"
fill="freeze"
keyTimes="0; .5; 1"
/>
</circle>
<circle r="8" strokeDasharray="1" pathLength="1">
<animate
attributeName="stroke-dashoffset"
values="1; 1; 2"
dur="15s"
keyTimes="0; .67; 1"
fill="freeze"
/>
</circle>
</svg>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ const StateManagedBridge = () => {
bridgeQuote,
getAndSetBridgeQuote,
isLoading,
isWalletPending,
quoteTimeout
)

Expand Down
25 changes: 12 additions & 13 deletions packages/synapse-interface/utils/hooks/useStaleQuoteUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isNull, isNumber } from 'lodash'
import { useEffect, useRef } from 'react'

import { BridgeQuote } from '@/utils/types'
import { calculateTimeBetween } from '@/utils/time'
import { useIntervalTimer } from '@/utils/hooks/useIntervalTimer'
import { convertUuidToUnix } from '@/utils/convertUuidToUnix'

Expand All @@ -14,25 +13,19 @@ export const useStaleQuoteUpdater = (
quote: BridgeQuote,
refreshQuoteCallback: () => Promise<void>,
isQuoteLoading: boolean,
isWalletPending: boolean,
staleTimeout: number = 15000 // Default 15_000ms or 15s
) => {
const eventListenerRef = useRef<null | (() => void)>(null)

const timeoutRef = useRef<null | NodeJS.Timeout>(null)
const quoteTime = quote?.id ? convertUuidToUnix(quote?.id) : null
const isValidQuote = isNumber(quoteTime) && !isNull(quoteTime)

const currentTime = useIntervalTimer(staleTimeout, !isValidQuote)
useIntervalTimer(staleTimeout, !isValidQuote)

useEffect(() => {
if (isValidQuote && !isQuoteLoading && !isWalletPending) {
const timeDifference = calculateTimeBetween(currentTime, quoteTime)
const isStaleQuote = timeDifference >= staleTimeout

if (isStaleQuote) {
if (eventListenerRef.current) {
document.removeEventListener('mousemove', eventListenerRef.current)
}
if (isValidQuote && !isQuoteLoading) {
timeoutRef.current = setTimeout(() => {
eventListenerRef.current = null

const newEventListener = () => {
refreshQuoteCallback()
Expand All @@ -44,7 +37,13 @@ export const useStaleQuoteUpdater = (
})

eventListenerRef.current = newEventListener
}, staleTimeout)
}

return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
}
}, [currentTime, staleTimeout])
}, [quote, isQuoteLoading])
}

0 comments on commit c7b2981

Please sign in to comment.