import React, { useState, MouseEvent, useRef, useEffect } from 'react'; import cx from 'classnames'; import ReactGA from 'react-ga'; import { MarketComment } from '../store/markets.types'; interface Props { comment: MarketComment; } const ShowMore = ( { comment }: Props) => { const [ isExpanded, setIsExpanded ] = useState(false); const [ isTextTruncatable, setIsTextTruncatable ] = useState(false); const paragraphRef = useRef(null); useEffect(() => { if (paragraphRef?.current) { setIsTextTruncatable(paragraphRef.current.scrollHeight > paragraphRef.current.clientHeight); } }, [ paragraphRef ]); const toggle = (e: MouseEvent) => { e.preventDefault(); if (!isExpanded) { ReactGA.event({ category: 'OnPage function', action: 'ReadFullComment', label: comment.id.toString(), }); } setIsExpanded(!isExpanded); }; const toggleText = isExpanded ? 'Collapse' : 'Read full comment' ; const toggleIcon = isExpanded ? 'icon-flat-arrow-up' : 'icon-flat-arrow-down'; return ( <>

{comment.comment}

{ isTextTruncatable && { toggleText } } ); }; export { ShowMore };