From 030009a07c53d3b0c986e0d3ad33d7ed4a31796c Mon Sep 17 00:00:00 2001 From: painterpuppets Date: Tue, 10 Oct 2023 13:12:47 +0800 Subject: [PATCH] optimize: halving over logic --- src/components/Banner/HalvingBanner.tsx | 25 ++++++---------- src/constants/common.ts | 2 ++ src/pages/Halving/index.tsx | 39 +++++++++--------------- src/utils/hook.ts | 40 +++++++++++++++++-------- 4 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/components/Banner/HalvingBanner.tsx b/src/components/Banner/HalvingBanner.tsx index 59a3df1ff..402d51581 100644 --- a/src/components/Banner/HalvingBanner.tsx +++ b/src/components/Banner/HalvingBanner.tsx @@ -6,7 +6,6 @@ import halvingSuccessAni from '../../assets/halving_success_ani.gif' import SimpleButton from '../SimpleButton' import { useCountdown, useHalving } from '../../utils/hook' import i18n from '../../utils/i18n' -import { fetchCachedData } from '../../utils/cache' function numberToOrdinal(number: number) { switch (number) { @@ -31,10 +30,8 @@ function numberToOrdinal(number: number) { } export const HalvingBanner = () => { - const { estimatedDate, nextHalvingCount } = useHalving() + const { estimatedDate, halvingCount, inCelebration } = useHalving() const [days, hours, minutes, seconds] = useCountdown(estimatedDate) - const lastedHavingKey = `lasted-having-${nextHalvingCount - 1}` - const unreadLastedHaving = nextHalvingCount > 1 && fetchCachedData(lastedHavingKey) === null const shortCountdown = () => { if (days > 0) { @@ -46,29 +43,25 @@ export const HalvingBanner = () => { if (minutes > 0) { return `${minutes}${i18n.t('symbol.char_space')}${i18n.t('unit.minutes')}` } - if (seconds > 0) { - return `${seconds}${i18n.t('symbol.char_space')}${i18n.t('unit.seconds')}` - } - return `${i18n.t('halving.halving')}!` + + return `${seconds}${i18n.t('symbol.char_space')}${i18n.t('unit.seconds')}` } return (
- {unreadLastedHaving && ( - animation - )} - {unreadLastedHaving ? ( + {inCelebration && animation} + {inCelebration ? (
{i18n .t('halving.banner_congratulation', { - times: i18n.t(`ordinal.${numberToOrdinal(nextHalvingCount - 1)}`), + times: i18n.t(`ordinal.${numberToOrdinal(halvingCount)}`), }) .toUpperCase()}
@@ -79,7 +72,7 @@ export const HalvingBanner = () => { )} - {unreadLastedHaving + {inCelebration ? i18n.t('halving.learn_more') : `${i18n.t('halving.halving_countdown')} ${shortCountdown()}`} diff --git a/src/constants/common.ts b/src/constants/common.ts index 5e35126a2..eb98f5f3f 100644 --- a/src/constants/common.ts +++ b/src/constants/common.ts @@ -13,6 +13,8 @@ export const EPOCH_HOURS = 4 export const ONE_DAY_SECOND = 24 * 60 * 60 export const ONE_HOUR_SECOND = 60 * 60 export const ONE_MINUTE_SECOND = 60 +export const EPOCHS_PER_HALVING = 8760 +export const THEORETICAL_EPOCH_TIME = 1000 * 60 * 60 * 4 // 4 hours export const IS_MAINTAINING = process.env.REACT_APP_IS_MAINTAINING === 'true' export function getPrimaryColor() { diff --git a/src/pages/Halving/index.tsx b/src/pages/Halving/index.tsx index be7b19ce6..9710a9fb6 100644 --- a/src/pages/Halving/index.tsx +++ b/src/pages/Halving/index.tsx @@ -16,8 +16,7 @@ import { HalvingInfo } from './HalvingInfo' import { useStatistics } from '../../services/ExplorerService' import { HalvingCountdown } from './HalvingCountdown' import { useCountdown, useHalving, useIsMobile } from '../../utils/hook' -import { fetchCachedData, storeCachedData } from '../../utils/cache' -import { getPrimaryColor } from '../../constants/common' +import { getPrimaryColor, EPOCHS_PER_HALVING, THEORETICAL_EPOCH_TIME } from '../../constants/common' import styles from './index.module.scss' function numberToOrdinal(number: number) { @@ -45,20 +44,12 @@ function numberToOrdinal(number: number) { export const HalvingCountdownPage = () => { const isMobile = useIsMobile() const statistics = useStatistics() - const { - currentEpoch, - estimatedDate, - singleEpochAverageTime, - currentEpochUsedTime, - EPOCHS_PER_HALVING, - nextHalvingCount, - } = useHalving() + const { currentEpoch, estimatedDate, currentEpochUsedTime, halvingCount, inCelebration, skipCelebration } = + useHalving() - const lastedHavingKey = `lasted-having-${nextHalvingCount - 1}` - const unreadLastedHaving = nextHalvingCount > 1 && fetchCachedData(lastedHavingKey) === null const percent = - (((currentEpoch % EPOCHS_PER_HALVING) * singleEpochAverageTime - currentEpochUsedTime) / - (EPOCHS_PER_HALVING * singleEpochAverageTime)) * + (((currentEpoch % EPOCHS_PER_HALVING) * THEORETICAL_EPOCH_TIME - currentEpochUsedTime) / + (EPOCHS_PER_HALVING * THEORETICAL_EPOCH_TIME)) * 100 const [days, hours, minutes, seconds] = useCountdown(estimatedDate) @@ -76,7 +67,7 @@ export const HalvingCountdownPage = () => { } const shareText = i18n.t('halving.share_text', { - times: i18n.t(`ordinal.${numberToOrdinal(nextHalvingCount)}`), + times: i18n.t(`ordinal.${numberToOrdinal(halvingCount)}`), date: estimatedDate.toUTCString(), countdown: shortCountdown(), }) @@ -90,7 +81,7 @@ export const HalvingCountdownPage = () => { } const renderHalvingPanel = () => { - if (unreadLastedHaving) { + if (inCelebration) { return (
{
@@ -114,9 +105,7 @@ export const HalvingCountdownPage = () => {