import { memo, MutableRefObject, PropsWithChildren, useLayoutEffect, useRef, useState } from 'react'; import { getAccessibleColour } from '../../../common/utils/styleUtils'; import style from '../Cuesheet.module.scss'; const pastOpacity = '0.2'; interface EventRowProps { eventIndex: number; isPast?: boolean; selectedRef?: MutableRefObject; skip?: boolean; colour?: string; } function EventRow(props: PropsWithChildren) { const { children, eventIndex, isPast, selectedRef, skip, colour } = props; const ownRef = useRef(null); const [isVisible, setIsVisible] = useState(false); const textColour = getAccessibleColour(colour); const bgColour = textColour.backgroundColor; useLayoutEffect(() => { const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { setIsVisible(true); } }, { root: null, threshold: 0.01, }, ); const handleRefCurrent = ownRef.current; if (selectedRef) { setIsVisible(true); } else if (handleRefCurrent) { observer.observe(handleRefCurrent); } return () => { if (handleRefCurrent) { observer.unobserve(handleRefCurrent); } }; }, [ownRef, selectedRef]); return ( {eventIndex} {isVisible ? children : null} ); } export default memo(EventRow);