forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frenemies] Optimize + offline (MystenLabs#8201)
- Loading branch information
1 parent
8cc013d
commit c3fc012
Showing
17 changed files
with
271 additions
and
193 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 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useEffect, useState } from "react"; | ||
import { useEpoch } from "../network/queries/epoch"; | ||
import { formatTimeRemaining } from "../utils/format"; | ||
import { Stat } from "./Stat"; | ||
|
||
|
||
/** | ||
* Calculate time left until the next epoch based on the last two timestamps | ||
* for epoch changes (`timestamp` and `prevTimestamp`). | ||
*/ | ||
function getTime(timestamp?: number, prevTimestamp?: number): number | null { | ||
if (!timestamp || !prevTimestamp) return null; | ||
|
||
const prevEpochLength = timestamp - prevTimestamp; | ||
const timePassed = Date.now() - timestamp; | ||
const timeLeft = prevEpochLength - timePassed; | ||
return timeLeft <= 0 ? 0 : timeLeft; | ||
} | ||
|
||
export function TimeRemaining() { | ||
const { data: epoch } = useEpoch(); | ||
|
||
const [timer, setTime] = useState(() => | ||
getTime(epoch?.timestamp, epoch?.prevTimestamp) | ||
); | ||
|
||
useEffect(() => { | ||
if (!epoch) return; | ||
|
||
const interval = setInterval( | ||
() => setTime(getTime(epoch.timestamp, epoch.prevTimestamp)), | ||
1000 | ||
); | ||
return () => clearInterval(interval); | ||
}, [epoch]); | ||
|
||
return ( | ||
<Stat label="Time Remaining"> | ||
<div className="text-steel-dark font-light"> | ||
{formatTimeRemaining(timer || 0)} | ||
</div> | ||
</Stat> | ||
); | ||
} |
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
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
Oops, something went wrong.