-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a3d8e5
commit fe2ef4c
Showing
5 changed files
with
141 additions
and
18 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
app/components/Views/SmartTransactionStatus/useRemainingTime.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,68 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectSwapsChainFeatureFlags } from '../../../reducers/swaps'; | ||
|
||
export const FALLBACK_STX_ESTIMATED_DEADLINE_SEC = 45; | ||
export const FALLBACK_STX_MAX_DEADLINE_SEC = 150; | ||
|
||
interface Props { | ||
creationTime: number | undefined; | ||
isStxPending: boolean; | ||
} | ||
|
||
const useRemainingTime = ({ creationTime, isStxPending }: Props) => { | ||
const swapFeatureFlags = useSelector(selectSwapsChainFeatureFlags); | ||
|
||
const [isStxPastEstimatedDeadline, setIsStxPastEstimatedDeadline] = | ||
useState(false); | ||
|
||
const stxEstimatedDeadlineSec = | ||
swapFeatureFlags?.smartTransactions?.expectedDeadline || | ||
FALLBACK_STX_ESTIMATED_DEADLINE_SEC; | ||
const stxMaxDeadlineSec = | ||
swapFeatureFlags?.smartTransactions?.maxDeadline || | ||
FALLBACK_STX_MAX_DEADLINE_SEC; | ||
|
||
// Calc time left for progress bar and timer display | ||
const stxDeadlineSec = isStxPastEstimatedDeadline | ||
? stxMaxDeadlineSec | ||
: stxEstimatedDeadlineSec; | ||
|
||
const [timeLeftForPendingStxInSec, setTimeLeftForPendingStxInSec] = useState( | ||
stxEstimatedDeadlineSec, | ||
); | ||
|
||
useEffect(() => { | ||
let intervalId: NodeJS.Timeout; | ||
if (isStxPending && creationTime) { | ||
const calculateRemainingTime = () => { | ||
const secondsAfterStxSubmission = Math.round( | ||
(Date.now() - creationTime) / 1000, | ||
); | ||
if (secondsAfterStxSubmission > stxDeadlineSec) { | ||
if (isStxPastEstimatedDeadline) { | ||
setTimeLeftForPendingStxInSec(0); | ||
clearInterval(intervalId); | ||
return; | ||
} | ||
setIsStxPastEstimatedDeadline(true); | ||
} | ||
setTimeLeftForPendingStxInSec( | ||
stxDeadlineSec - secondsAfterStxSubmission, | ||
); | ||
}; | ||
intervalId = setInterval(calculateRemainingTime, 1000); | ||
calculateRemainingTime(); | ||
} | ||
|
||
return () => clearInterval(intervalId); | ||
}, [isStxPending, isStxPastEstimatedDeadline, creationTime, stxDeadlineSec]); | ||
|
||
return { | ||
timeLeftForPendingStxInSec, | ||
stxDeadlineSec, | ||
isStxPastEstimatedDeadline, | ||
}; | ||
}; | ||
|
||
export default useRemainingTime; |
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