import type { FC } from '../../lib/teact/teact'; import React, { memo, useEffect, useRef, useState, } from '../../lib/teact/teact'; import type { IconName } from '../../types/icons'; import buildClassName from '../../util/buildClassName'; import buildStyle from '../../util/buildStyle'; import useOldLang from '../../hooks/useOldLang'; import useResizeObserver from '../../hooks/useResizeObserver'; import Icon from './icons/Icon'; import styles from './PremiumProgress.module.scss'; type OwnProps = { leftText?: string; rightText?: string; floatingBadgeIcon?: IconName; floatingBadgeText?: string; progress?: number; isPrimary?: boolean; className?: string; }; const PremiumProgress: FC = ({ leftText, rightText, floatingBadgeText, floatingBadgeIcon, progress, isPrimary, className, }) => { const lang = useOldLang(); // eslint-disable-next-line no-null/no-null const floatingBadgeRef = useRef(null); // eslint-disable-next-line no-null/no-null const parentContainerRef = useRef(null); const [shiftX, setShiftX] = useState(0); const [tailPosition, setTailPosition] = useState(0); const updateBadgePosition = () => { if (floatingBadgeRef.current && parentContainerRef.current && progress !== undefined) { const badgeWidth = floatingBadgeRef.current.offsetWidth; const parentWidth = parentContainerRef.current.offsetWidth; const minShift = badgeWidth / 2; const maxShift = parentWidth - badgeWidth / 2; const currentShift = progress * parentWidth; const safeShift = Math.max(minShift, Math.min(currentShift, maxShift)); setShiftX(safeShift / parentWidth); let newTailPosition; if (currentShift < minShift) { newTailPosition = (progress * parentWidth) / (minShift * 2); } else if (currentShift > maxShift) { const progressMapped = (progress - (maxShift / parentWidth)) / (1 - maxShift / parentWidth); newTailPosition = 0.5 + (progressMapped * 0.4); } else { newTailPosition = 0.5; } setTailPosition(newTailPosition); } }; useEffect(updateBadgePosition, [progress]); useResizeObserver(parentContainerRef, updateBadgePosition); const hasFloatingBadge = Boolean(floatingBadgeIcon || floatingBadgeText); const isProgressFull = Boolean(progress) && progress > 0.99; return (
{hasFloatingBadge && (
{floatingBadgeIcon && } {floatingBadgeText && (
{floatingBadgeText}
)}
)}
{leftText}
{rightText}
{leftText}
{rightText}
); }; export default memo(PremiumProgress);