import type { FC } from '../../lib/teact/teact'; import React, { useEffect, useMemo, useRef, } from '../../lib/teact/teact'; import { getGlobal } from '../../global'; import { selectCanAnimateInterface } from '../../global/selectors'; import buildClassName from '../../util/buildClassName'; import useFlag from '../../hooks/useFlag'; import useLang from '../../hooks/useLang'; import styles from './AnimatedCounter.module.scss'; type OwnProps = { text: string; className?: string; }; const AnimatedCounter: FC = ({ text, className, }) => { const lang = useLang(); const prevTextRef = useRef(); const [isAnimating, markAnimating, unmarkAnimating] = useFlag(false); const shouldAnimate = selectCanAnimateInterface(getGlobal()); const textElement = useMemo(() => { if (!shouldAnimate) { return text; } if (!isAnimating) { return prevTextRef.current || text; } const prevText = prevTextRef.current; const elements = []; for (let i = 0; i < text.length; i++) { if (prevText && text[i] !== prevText[i]) { elements.push(
{text[i]}
{prevText[i]}
{text[i]}
, ); } else { elements.push({text[i]}); } } prevTextRef.current = text; return elements; }, [shouldAnimate, isAnimating, text]); useEffect(() => { markAnimating(); }, [text]); return ( {textElement} ); }; export default AnimatedCounter;