import { motion, useAnimation, useReducedMotion } from 'framer-motion'; import { useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import { createAnimationProps } from '../../../../../../helpers/animationPropsHelper'; import { clsm } from '../../../../../../utils'; import { dashboard as $dashboardContent } from '../../../../../../content'; import { ErrorIcon, Check } from '../../../../../../assets/icons'; import { formatDate, formatTime } from '../../../../../../hooks/useDateTime'; import Button from '../../../../../../components/Button'; import Tooltip from '../../../../../../components/Tooltip'; import usePrevious from '../../../../../../hooks/usePrevious'; import useStringOverflow from '../../../../../../hooks/useStringOverflow'; const $content = $dashboardContent.stream_session_page.stream_events; const StreamEventItem = ({ handleEventClick, isLive, selectedEventId, setSelectedEventRef, streamEvent, toggleLearnMore, isLearnMoreVisible }) => { const controls = useAnimation(); const shouldReduceMotion = useReducedMotion(); const learnMoreBtnRef = useRef(); const prevIsLearnMoreVisible = usePrevious(isLearnMoreVisible); const [isNameOverflowing, eventNameRef] = useStringOverflow(); const { id, name, error, success, eventTime, shortMsg, longMsg } = streamEvent; const isSelected = id === selectedEventId; const isExpandable = !!shortMsg; const hasLearnMore = !!longMsg; const date = formatDate(eventTime); const time = formatTime(eventTime, null, isLive); let eventTimestamp = isLive ? time : `${date} ${time}`; eventTimestamp = eventTimestamp.charAt(0).toUpperCase() + eventTimestamp.slice(1); const handleEvent = (e) => { if ( e.target.id !== 'learn-more-button' && (e.key === ' ' || e.key === 'Enter' || e.type === 'click') ) { handleEventClick(id); } }; // Return focus back to the "learn more" button after closing the "learn more" panel useEffect(() => { if ( isExpandable && isSelected && prevIsLearnMoreVisible && !isLearnMoreVisible ) { learnMoreBtnRef.current?.focus(); } }, [isExpandable, isLearnMoreVisible, isSelected, prevIsLearnMoreVisible]); useEffect(() => { const variant = isSelected ? 'visible' : 'hidden-initial'; if (shouldReduceMotion) { controls.set(variant); } else { controls.start(variant); } }, [controls, isSelected, shouldReduceMotion]); const renderEventName = () => (

{name}

); return (
{isNameOverflowing ? ( {renderEventName()} ) : ( renderEventName() )} {error && ( )} {success && ( )}

{eventTimestamp}

{isExpandable && (

{shortMsg}

{hasLearnMore && isSelected && (
)}
)}
); }; StreamEventItem.defaultProps = { isLive: false, selectedEventId: null }; StreamEventItem.propTypes = { handleEventClick: PropTypes.func.isRequired, isLearnMoreVisible: PropTypes.bool.isRequired, isLive: PropTypes.bool, selectedEventId: PropTypes.string, setSelectedEventRef: PropTypes.func.isRequired, streamEvent: PropTypes.object.isRequired, toggleLearnMore: PropTypes.func.isRequired }; export default StreamEventItem;