import { motion } from 'framer-motion'; import { useCallback, useLayoutEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import { defaultViewerStreamActionTransition, defaultSlideUpVariant } from '../../../pages/Channel/ViewerStreamActions/viewerStreamActionsTheme'; import { clsm, isTextColorInverted, range } from '../../../utils'; import { createAnimationProps } from '../../../helpers/animationPropsHelper'; import { PROFILE_COLORS } from '../../../constants'; import { usePlayerContext } from '../contexts/Player'; import useResizeObserver from '../../../hooks/useResizeObserver'; const PARAGRAPH_BASE_CLASSES = [ 'font-bold', 'pr-2', 'py-4', 'uppercase', 'whitespace-nowrap' ]; const DEFAULT_ANIMATION_DURATION = 5; // in seconds const Notice = ({ color, message, onClickPlayerHandler, shouldShowStream, title }) => { const messageRef = useRef(); const marqueeRef = useRef(); const [maxMessages, setMaxMessages] = useState(0); const { isOverlayVisible } = usePlayerContext(); const shouldInvertColors = isTextColorInverted(color); // The animation duration is calculated dynamically to allow for ~5s per message const animationDuration = `${DEFAULT_ANIMATION_DURATION * maxMessages}s`; const updateMaxMessages = useCallback(() => { const { width: messageWidth } = messageRef.current.getBoundingClientRect(); const { width: marqueeWidth } = marqueeRef.current.getBoundingClientRect(); const maxMessages = Math.ceil(marqueeWidth / messageWidth); setMaxMessages(maxMessages); }, []); useResizeObserver(marqueeRef, updateMaxMessages); useLayoutEffect(() => { updateMaxMessages(); }, [updateMaxMessages]); return (

{title}

{/* We use this message to get the length of one message */}

{message}

{range(maxMessages - 1).map((index) => (

{message}

))}
{range(maxMessages).map((index) => (

{message}

))}
); }; Notice.propTypes = { color: PropTypes.oneOf([...PROFILE_COLORS, 'default']), message: PropTypes.string, onClickPlayerHandler: PropTypes.func.isRequired, shouldShowStream: PropTypes.bool, title: PropTypes.string }; Notice.defaultProps = { color: 'default', message: '', shouldShowStream: false, title: '' }; export default Notice;