import { forwardRef } from 'react'; import PropTypes from 'prop-types'; import { BREAKPOINTS, STREAM_ACTION_NAME } from '../../../../constants'; import { BUTTON_OUTLINE_CLASSES } from '../../../../components/Button/ButtonTheme'; import { clsm, isTextColorInverted } from '../../../../utils'; import { streamManager as $streamManagerContent } from '../../../../content'; import { useResponsiveDevice } from '../../../../contexts/ResponsiveDevice'; import { useStreamManagerActions } from '../../../../contexts/StreamManagerActions'; import { useUser } from '../../../../contexts/User'; import useCountdown from '../../../../hooks/useCountdown'; import { usePoll } from '../../../../contexts/StreamManagerActions/Poll'; const $content = $streamManagerContent.stream_manager_actions; const PROGRESS_PIE_RADIUS = 14; const PROGRESS_PIE_DIAMETER = PROGRESS_PIE_RADIUS * 2; const STROKE_DASHARRAY_MAX = Math.round(PROGRESS_PIE_DIAMETER * Math.PI); const DEFAULT_TRANSITION_CLASSES = [ 'duration-[0.15s]', 'ease-in-out', 'transition' ]; const StreamManagerActionButton = forwardRef( ({ ariaLabel, icon, label, name, onClick }, ref) => { const Icon = icon; const { hasPollEnded, savedPollData } = usePoll(); const { hasFetchedInitialUserData, userData } = useUser(); const { color = 'default' } = userData || {}; const { currentBreakpoint } = useResponsiveDevice(); const isSmallBreakpoint = currentBreakpoint < BREAKPOINTS.sm; const { activeStreamManagerActionData, stopStreamAction, endPollOnExpiry, cancelActivePoll } = useStreamManagerActions(); let activeStreamManagerActionDuration; let activeStreamManagerActionName; let activeStreamManagerActionExpiry; if (name !== STREAM_ACTION_NAME.POLL) { const { duration, name, expiry } = activeStreamManagerActionData || {}; activeStreamManagerActionDuration = duration; activeStreamManagerActionName = name; activeStreamManagerActionExpiry = expiry; } else { const { duration, name, expiry } = (savedPollData?.isActive && savedPollData) || {}; activeStreamManagerActionDuration = duration; activeStreamManagerActionName = name; activeStreamManagerActionExpiry = expiry; } const isActive = name === activeStreamManagerActionName; const isPerpetual = isActive && !activeStreamManagerActionExpiry; const isCountingDown = isActive && !isPerpetual; const [textFormattedTimeLeft, currentProgress] = useCountdown({ expiry: activeStreamManagerActionExpiry, formatter: (timeLeft) => [ `${Math.ceil(timeLeft / 1000)}${$content.unit_seconds}`, (timeLeft / (activeStreamManagerActionDuration * 1000)) * STROKE_DASHARRAY_MAX ], isEnabled: isCountingDown, onExpiry: name === STREAM_ACTION_NAME.POLL && !hasPollEnded ? endPollOnExpiry : stopStreamAction }); const handleClick = () => { if (isActive) name === STREAM_ACTION_NAME.POLL ? cancelActivePoll() : stopStreamAction(); else onClick(); }; const currentLabel = ( <> {!isActive && label.default} {isActive && !hasPollEnded && label.active} {isActive && hasPollEnded && $content.poll.showing_results} {![STREAM_ACTION_NAME.AMAZON_PRODUCT, STREAM_ACTION_NAME.POLL].includes( name ) && `a ${name}`} > ); let statusLabel = isActive && (isPerpetual ? $content.on : textFormattedTimeLeft); if (!isActive) statusLabel = $content.off; const shouldInvertColors = isTextColorInverted(color); if (!hasFetchedInitialUserData) return null; return ( {!isSmallBreakpoint && currentLabel && ( {currentLabel} )} {icon && !isCountingDown && ( )} {isCountingDown && ( {/* Hide aliasing on Chrome */} )} {!isSmallBreakpoint && statusLabel} ); } ); StreamManagerActionButton.defaultProps = { icon: null, label: { default: '', active: '' } }; StreamManagerActionButton.propTypes = { ariaLabel: PropTypes.string.isRequired, icon: PropTypes.elementType, label: PropTypes.shape({ default: PropTypes.string, active: PropTypes.string }), name: PropTypes.string.isRequired, onClick: PropTypes.func.isRequired }; export default StreamManagerActionButton;
{currentLabel}
{!isSmallBreakpoint && statusLabel}