| import { useState, useEffect, useRef } from 'react'; |
| import { scrollToComments } from 'calypso/blocks/reader-full-post/scroll-to-comments'; |
| import ReaderPostActions from 'calypso/blocks/reader-post-actions'; |
| import { READER_SHARE_MENU_CLOSE } from 'calypso/blocks/reader-share'; |
| import { recordAction, recordGaEvent, recordTrackForPost } from 'calypso/reader/stats'; |
| import { useSelector } from 'calypso/state'; |
| import { getPostByKey } from 'calypso/state/reader/posts/selectors'; |
|
|
| interface EngagementBarProps { |
| className?: string; |
| feedId?: string | number; |
| postId?: string | number; |
| } |
|
|
| const EngagementBar = ( { className = '', feedId, postId }: EngagementBarProps ) => { |
| const barRef = useRef< HTMLDivElement >( null ); |
| const post = useSelector( ( state ) => |
| feedId && postId ? getPostByKey( state, { feedId, postId } ) : null |
| ); |
|
|
| const [ isActionsVisible, setIsActionsVisible ] = useState( false ); |
| const [ actionsElement, setActionsElement ] = useState< Element | null >( null ); |
|
|
| const handleCommentClick = () => { |
| recordAction( 'click_comments' ); |
| recordGaEvent( 'Clicked Post Comment Button' ); |
| recordTrackForPost( 'calypso_reader_post_comments_button_clicked', post ); |
| scrollToComments( { focusTextArea: true } ); |
| }; |
|
|
| |
| useEffect( () => { |
| if ( ! barRef.current ) { |
| return; |
| } |
|
|
| const updateWidth = ( parentWidth: number ) => { |
| if ( barRef.current ) { |
| barRef.current.style.width = `${ parentWidth }px`; |
| const actionsElement = barRef.current.querySelector( '.reader-post-actions' ); |
| if ( actionsElement ) { |
| ( actionsElement as HTMLElement ).style.width = `${ parentWidth }px`; |
| } |
| } |
| }; |
|
|
| |
| const resizeObserver = new ResizeObserver( ( entries ) => { |
| for ( const entry of entries ) { |
| const parentWidth = entry.contentRect.width; |
| updateWidth( parentWidth ); |
| } |
| } ); |
|
|
| |
| if ( barRef.current.parentElement ) { |
| resizeObserver.observe( barRef.current.parentElement ); |
| } |
|
|
| |
| return () => { |
| resizeObserver.disconnect(); |
| }; |
| }, [] ); |
|
|
| |
| useEffect( () => { |
| |
| |
| const observer = new MutationObserver( () => { |
| const element = document.querySelector( '.reader-full-post .reader-post-actions' ); |
| if ( element ) { |
| setActionsElement( element ); |
| |
| observer.disconnect(); |
| } |
| } ); |
|
|
| |
| observer.observe( document.body, { |
| childList: true, |
| subtree: true, |
| } ); |
|
|
| |
| return () => observer.disconnect(); |
| }, [] ); |
|
|
| |
| useEffect( () => { |
| |
| if ( ! actionsElement ) { |
| return; |
| } |
|
|
| |
| const intersectionObserver = new IntersectionObserver( |
| ( [ entry ] ) => { |
| |
| setIsActionsVisible( entry.isIntersecting ); |
| }, |
| { |
| |
| threshold: 0.1, |
| |
| rootMargin: '0px', |
| } |
| ); |
|
|
| |
| intersectionObserver.observe( actionsElement ); |
|
|
| |
| return () => { |
| intersectionObserver.disconnect(); |
| }; |
| }, [ actionsElement ] ); |
|
|
| |
| useEffect( () => { |
| READER_SHARE_MENU_CLOSE.trigger(); |
| }, [ isActionsVisible ] ); |
|
|
| return ( |
| <div |
| ref={ barRef } |
| className={ `recent-feed__post-column-engagement-bar ${ |
| isActionsVisible ? 'engagement-bar-is-hidden' : '' |
| } ${ className }` } |
| > |
| { post && ( |
| <ReaderPostActions |
| className="engagement-bar__actions" |
| post={ post } |
| onCommentClick={ handleCommentClick } |
| /> |
| ) } |
| </div> |
| ); |
| }; |
|
|
| export default EngagementBar; |
|
|