import React, { FC, useRef, useState, useEffect } from 'react'; import { CarouselComponent, CarouselItem } from '../carousel-component'; import { TripCardsLayoutProps } from './types'; import { Container, TitleSection, TitleBar, TitleContent, Title, Subtitle, TitleLink, StaticCardsGrid, StyledLink } from './styles'; import { ChevronRightIcon } from './assets'; import { tealiumTrackingHandler } from '../../helpers/tracking/TrackingHandler'; import { getPreferredEdition } from '../../utils/cookie'; export const TripCardsLayout: FC = ({ element, items, CardComponent, itemsPerPage, widthContainerConfig = {}, widthItemConfig, maxWidthItemConfig, imgHeight, forceStaticGrid }) => { const { titleurl, title, description } = element; const gridRef = useRef(null); const [hasOverflow, setHasOverflow] = useState(false); const [isTabletMobile, setIsTabletMobile] = useState(false); const isStaticGrid = items.length === 2 || forceStaticGrid; // Handle responsive breakpoint detection useEffect(() => { const mediaQuery = window.matchMedia('(max-width: 1024px)'); const handleMediaChange = (e: MediaQueryListEvent | MediaQueryList) => { setIsTabletMobile(e.matches); }; handleMediaChange(mediaQuery); mediaQuery.addEventListener('change', handleMediaChange); return () => { mediaQuery.removeEventListener('change', handleMediaChange); }; }, []); useEffect( () => { if (!isStaticGrid || !gridRef.current) { setHasOverflow(false); return; } const checkOverflow = () => { if (gridRef.current) { const isOverflowing = gridRef.current.scrollWidth > gridRef.current.clientWidth; setHasOverflow(isOverflowing); } }; checkOverflow(); const resizeObserver = new ResizeObserver(checkOverflow); resizeObserver.observe(gridRef.current); return () => { resizeObserver.disconnect(); }; }, [isStaticGrid, items.length] ); const shouldUseCarousel = (isTabletMobile && items.length >= 3) || !isStaticGrid || hasOverflow; const onTitleClick = () => { tealiumTrackingHandler( `trip cards component: ${title}`, 'navigation', 'click', undefined, undefined, { app_content_location: getPreferredEdition() } ); }; return ( {titleurl ? ( {title} ) : ( {title} )} {description} {titleurl && ( )} {!shouldUseCarousel ? ( {items.map(item => ( ))} ) : ( ( ))} options={{ itemsPerPage }} showArrows={!isStaticGrid} showDots={true} /> )} ); };