import React, { useState, useEffect, createRef, useRef } from 'react' import PropTypes from 'prop-types' import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed' import classNames from 'classnames' import OutlineList from './outline-list' function getChildrenLines(children) { return (children || []) .map(child => { return getChildrenLines(child.children).concat(child.line) }) .flat() } function OutlineItem({ outlineItem, jumpToLine, highlightedLine }) { const [expanded, setExpanded] = useState(true) const titleElementRef = createRef() const isHighlightedRef = useRef(false) const mainItemClasses = classNames('outline-item', { 'outline-item-no-children': !outlineItem.children }) const expandCollapseIconClasses = classNames('fa', 'outline-caret-icon', { 'fa-angle-down': expanded, 'fa-angle-right': !expanded }) const hasHighlightedChild = !expanded && getChildrenLines(outlineItem.children).includes(highlightedLine) const itemLinkClasses = classNames('outline-item-link', { 'outline-item-link-highlight': highlightedLine === outlineItem.line || hasHighlightedChild }) function handleExpandCollapseClick() { setExpanded(!expanded) } function handleOutlineItemLinkClick() { jumpToLine(outlineItem.line, false) } function handleOutlineItemLinkDoubleClick() { jumpToLine(outlineItem.line, true) } useEffect( () => { const wasHighlighted = isHighlightedRef.current const isNowHighlighted = highlightedLine === outlineItem.line isHighlightedRef.current = isNowHighlighted if (!wasHighlighted && isNowHighlighted) { scrollIntoViewIfNeeded(titleElementRef.current, { scrollMode: 'if-needed', block: 'center' }) } }, [highlightedLine, titleElementRef, isHighlightedRef] ) return (