import React, { useRef, useState, useEffect } from 'react'; import { Placeholder } from '@times-components/image'; import { useFetch } from '../../helpers/fetch/FetchProvider'; import { TrackingContext, TrackingContextProvider } from '../../helpers/tracking/TrackingContextProvider'; import { sanitiseCopy } from '../../helpers/text-formatting/SanitiseCopy'; import { Container, ContentContainer, ListItem, LeftPanel, RightPanel, Date, SubHeading, StyledShowAllButton } from './styles'; import { PlaceholderContainer, Label, Headline, ListContainer, List, Copy, ShowAllContainer } from '../common-styles'; import { DeckData } from '../../helpers/fetch/types'; type TimelinesData = { type: string; data: { date: string; eventHeading: string; image: string; copy: string; }; }; type TimelinesDeckData = DeckData; const scrollEvent = { attrs: { event_navigation_name: 'in-article component displayed : timelines', event_navigation_browsing_method: 'scroll' } }; const clickEvent = (buttonLabel: string) => ({ action: 'Clicked', attrs: { event_navigation_name: `button : ${buttonLabel}`, event_navigation_browsing_method: 'click' } }); export const Timelines: React.FC<{ sectionColour: string; }> = ({ sectionColour }) => { const { loading, error, data } = useFetch(); if (loading) { return ( ); } if (error || data === undefined) { return null; } const { headline, label } = data.fields; const timelinesData = data.body.data; const [showAll, setShowAll] = useState(false); const handleShowAll = ( fireAnalyticsEvent: (evt: TrackingContext) => void, buttonLabel: string ) => { fireAnalyticsEvent && fireAnalyticsEvent(clickEvent(buttonLabel)); setShowAll(!showAll); }; const showAllRef = useRef(null); const [displayShowAll, setShowShowAll] = useState(false); const maxHeight = 375; useEffect(() => { const listContainer = showAllRef.current; if (listContainer) { setShowShowAll(listContainer.clientHeight > maxHeight); } }, []); return ( {({ fireAnalyticsEvent, intersectObserverRef }) => ( {headline && {headline}} {timelinesData.map((row: TimelinesData, index: number) => ( {row.data.image && } {row.data.date} {row.data.eventHeading} ))} handleShowAll( fireAnalyticsEvent, showAll ? 'Collapse' : 'Show all' ) } > {showAll ? 'Collapse' : 'Show all'} )} ); };