Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement timer for tBTC Rewards interval #2212

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions solidity/dashboard/src/components/Timer.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
import React from "react"
import React, { useEffect, useState } from "react"
import moment from "moment";

const Timer = ({ target }) => {
// TODO implement timer
return <>{target}</>
const Timer = ({ targetInUnix }) => {
const [remainingDays, setRemainingDays] = useState(0)
const [remainingHours, setRemainingHours] = useState(0)
const [remainingMinutes, setRemainingMinutes] = useState(0)
const [remainingSeconds, setRemainingSeconds] = useState(0)

useEffect(() => {
const myInterval = setInterval(() => {
if (remainingSeconds > 0) {
setRemainingSeconds(remainingSeconds - 1)
}
if (remainingSeconds === 0) {
if (
remainingDays === 0 &&
remainingHours === 0 &&
remainingMinutes === 0
) {
clearInterval(myInterval)
} else {
setRemainingSeconds(59)
if (remainingMinutes > 0) {
setRemainingMinutes(remainingMinutes - 1)
} else if (remainingMinutes === 0) {
setRemainingMinutes(59)
if (remainingHours > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nested ifs... 🤯 out of idea now how we can do better, but let's keep 👀 on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to get cleaner code would be to keep the remainingSeconds only and then when displaying we could format it with moment js. However, I don't think it will be efficient to do such thing every re-render (which means every sec). Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I don't think it will be efficient to do such thing every re-render (which means every sec)

Agree. Let's keep it in mind, maybe someday we'll do it better 😆

setRemainingHours(remainingHours - 1)
} else if (remainingHours === 0) {
setRemainingHours(23)
if (remainingDays > 0) {
setRemainingDays(remainingDays - 1)
}
}
}
}
}
}, 1000)
return () => {
clearInterval(myInterval)
}
})

useEffect(() => {
const timerDuration = calculateTimerDuration()
setRemainingDays(timerDuration.days)
setRemainingHours(timerDuration.hours)
setRemainingMinutes(timerDuration.minutes)
setRemainingSeconds(timerDuration.seconds)
}, [targetInUnix])

const zeroPad = (num, places) => String(num).padStart(places, "0")

const calculateTimerDuration = () => {
const currentDate = moment()

const remainingTimeMs = targetInUnix.diff(currentDate)
const duration = moment.duration(remainingTimeMs)

const days = Math.floor(duration.asDays())
const hours = Math.floor(duration.asHours() % 24)
const minutes = Math.floor(duration.asMinutes() % 60)
const seconds = Math.floor(duration.asSeconds() % 60)

return {
days: days > 0 ? days : 0,
hours: hours > 0 ? hours : 0,
minutes: minutes > 0 ? minutes : 0,
seconds: seconds > 0 ? seconds : 0,
}
}

return (
<>
{zeroPad(remainingDays, 2)}:{zeroPad(remainingHours, 2)}:
{zeroPad(remainingMinutes, 2)}:{zeroPad(remainingSeconds, 2)}
</>
)
}

export default Timer
7 changes: 6 additions & 1 deletion solidity/dashboard/src/pages/rewards/TBTCRewardsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import ProgressBar from "../../components/ProgressBar"
import Timer from "../../components/Timer"
import TBTCRewardsDataTable from "../../components/TBTCRewardsDataTable"
import { colors } from "../../constants/colors"
import { ECDSARewardsHelper } from "../../utils/rewardsHelper";

const TBTCRewardsPage = () => {
const currentIntervalEndOf = ECDSARewardsHelper.intervalEndOf(
ECDSARewardsHelper.currentInterval
)

return (
<>
<RewardsOverview />
<section className="tile rewards-countdown">
<h2 className="h2--alt">
Next rewards release:&nbsp;
<Timer target="06:23:59:59" />
<Timer targetInUnix={currentIntervalEndOf} />
</h2>
</section>
<section className="rewards-calc">
Expand Down