diff --git a/src/components/NumCard.js b/src/components/NumCard.js index 375318b..b1db336 100644 --- a/src/components/NumCard.js +++ b/src/components/NumCard.js @@ -1,6 +1,6 @@ import React from "react" import { Flex, Stack, Icon, Text, VStack, HStack } from "@chakra-ui/react" - +import { useCountup } from '../hooks' /** * */ @@ -31,11 +31,14 @@ export const NumCard = ({ icon, title, num }) => { ) } -export const MultiChainCard = ({ icon, title, num }) => { +export const MultiChainCard = ({ icon, title, num, enabledCountup = true }) => { + const [counter, setCounter] = useCountup() React.useEffect(() => { - - }, []) + if (enabledCountup) { + setCounter(parseFloat(num || 0.00)) + } + }, [num]) return ( { fontSize="22" fontWeight="900" > - {num} + {enabledCountup ? counter : num} ) } -export const StateCard = ({ icon, title, num }) => { +export const StateCard = ({ icon, title, num, enabledCountup = true }) => { + const [counter, setCounter] = useCountup() React.useEffect(() => { - - }, []) + enabledCountup && setCounter(parseFloat(num || 0.00)) + }, [num]) return ( { fontSize="18" fontWeight="700" > - {num} + {enabledCountup ? counter : num} diff --git a/src/components/StakingCoinCard.js b/src/components/StakingCoinCard.js index af99c53..8582268 100644 --- a/src/components/StakingCoinCard.js +++ b/src/components/StakingCoinCard.js @@ -278,8 +278,8 @@ export const StakingCoinCard = ({ - - + + { + const [dest, setDest] = React.useState(0) + const [cur, setCur] = React.useState(start) + const hi = React.useRef() + + const stopInterval = () => { + if (hi.current) { + clearInterval(hi.current) + hi.current = null + } + } + + React.useEffect(() => { + const times = mill_secs / interval + const step = (dest - start) / times + const step_min = step / 2 + const step_max = step * 1.5 + + setCur((cur) => start) + + if (dest <= 0 || hi.current) { + return + } + + hi.current = setInterval(() => { + let new_delta = Math.random() * (step_max - step_min) + step_min + // console.log('new_delta:', new_delta) + setCur((cur) => { + let new_val = new_delta + cur + if (new_val >= dest) { + new_val = dest + stopInterval() + } + // console.log('new_val:', new_val) + return toF(new_val, 2) + }) + }, interval) + + return stopInterval + }, [dest]) + + return [cur, setDest] +} \ No newline at end of file diff --git a/src/pages/Home.js b/src/pages/Home.js index 190344a..1a356ac 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -7,6 +7,7 @@ import { Auditors, Partners, Carousel, MultiChainCard, StateCard, MiningListCard import { useApp } from '../AppContext' import { get_coins_platform } from '../api' import { ImPieChart, ImFire } from 'react-icons/im' +import { useCountup } from "../hooks" const HomeFooter = () => { diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..a09479d --- /dev/null +++ b/src/utils.js @@ -0,0 +1,2 @@ + +export const toF = (org, pn) => parseFloat(parseFloat(org).toFixed(pn)) \ No newline at end of file