-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: seperate CountdownUntilMigration
- Loading branch information
1 parent
df34aec
commit 4cbf42a
Showing
2 changed files
with
67 additions
and
62 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
frontend/components/ManageStakingPage/StakingContractSection/CountdownUntilMigration.tsx
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,65 @@ | ||
import { Flex } from 'antd'; | ||
import { isNil } from 'lodash'; | ||
import { useState } from 'react'; | ||
import { useInterval } from 'usehooks-ts'; | ||
|
||
import { StakingContractInfo } from '@/types/Autonolas'; | ||
|
||
export const CountdownUntilMigration = ({ | ||
activeStakingContractInfo, | ||
}: { | ||
activeStakingContractInfo: Partial<StakingContractInfo>; | ||
}) => { | ||
const [secondsUntilReady, setSecondsUntilMigration] = useState<number>(); | ||
|
||
useInterval(() => { | ||
if (!activeStakingContractInfo) return; | ||
|
||
const { serviceStakingStartTime, minimumStakingDuration } = | ||
activeStakingContractInfo; | ||
|
||
if (isNil(minimumStakingDuration)) return; | ||
if (isNil(serviceStakingStartTime)) return; | ||
|
||
const now = Math.round(Date.now() / 1000); | ||
const timeSinceLastStaked = now - serviceStakingStartTime; | ||
|
||
const timeUntilMigration = minimumStakingDuration - timeSinceLastStaked; | ||
|
||
if (timeUntilMigration < 0) { | ||
setSecondsUntilMigration(0); | ||
return; | ||
} | ||
|
||
setSecondsUntilMigration(timeUntilMigration); | ||
}, 1000); | ||
|
||
if (!secondsUntilReady) return "You're ready to switch contracts!"; // Shouldn't happen, but just in case | ||
|
||
return ( | ||
<Flex vertical gap={1}> | ||
<strong>Can't switch because you unstaked too recently.</strong> | ||
<span>This may be because your agent was suspended.</span> | ||
<span>Keep running your agent and you'll be able to switch in</span> | ||
<span>{countdownDisplayFormat(secondsUntilReady)}</span> | ||
</Flex> | ||
); | ||
}; | ||
|
||
const countdownDisplayFormat = (totalSeconds: number) => { | ||
const days = Math.floor(totalSeconds / (24 * 3600)); | ||
totalSeconds %= 24 * 3600; | ||
|
||
const hours = Math.floor(totalSeconds / 3600); | ||
totalSeconds %= 3600; | ||
|
||
const minutes = Math.floor(totalSeconds / 60); | ||
const seconds = totalSeconds % 60; | ||
|
||
// Ensure double digits for hours, minutes, and seconds | ||
const formattedHours = String(hours).padStart(2, '0'); | ||
const formattedMinutes = String(minutes).padStart(2, '0'); | ||
const formattedSeconds = String(seconds).padStart(2, '0'); | ||
|
||
return `${days} days ${formattedHours} hours ${formattedMinutes} minutes ${formattedSeconds} seconds`; | ||
}; |
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