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, NumberContainer, StyledShowAllButton } from './styles'; import { PlaceholderContainer, Label, Headline, ListContainer, List, Copy, ShowAllContainer } from '../common-styles'; import { isStandard, isWide } from '../../helpers/layout-size/layoutSize'; import { DeckData } from '../../helpers/fetch/types'; type BigNumbersData = { type: string; data: { number: string; copy: string; }; }; type BigNumbersDeckData = DeckData; const scrollEvent = { attrs: { event_navigation_name: 'in-article component displayed : big numbers', event_navigation_browsing_method: 'scroll' } }; const clickEvent = (buttonLabel: string) => ({ action: 'Clicked', attrs: { event_navigation_name: `button : ${buttonLabel}`, event_navigation_browsing_method: 'click' } }); export const BigNumbers: React.FC<{ sectionColour: string; }> = ({ sectionColour }) => { const { loading, error, data } = useFetch(); if (loading) { return ( ); } if (error || data === undefined) { return null; } const { headline, label, size } = data.fields; const bigNumbersData = 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, setDisplayShowAll] = useState(false); const maxHeight = isWide(size) ? 250 : 350; useEffect(() => { const listContainer = showAllRef.current; if (listContainer) { setDisplayShowAll(listContainer.clientHeight > maxHeight); } }, []); return ( {({ fireAnalyticsEvent, intersectObserverRef }) => ( {headline && {headline}} {bigNumbersData.map((row: BigNumbersData, index: number) => ( {row.data.number} ))} handleShowAll( fireAnalyticsEvent, showAll ? 'Collapse' : 'Show all' ) } > {showAll ? 'Collapse' : 'Show all'} )} ); };