import Link from 'next/link'; import { FC, MutableRefObject, useRef } from 'react'; import { useInView } from 'react-intersection-observer'; import { z } from 'zod'; import BeerStyleQueryResult from '@/services/posts/BeerStyles/schema/BeerStyleQueryResult'; import useBeerPostsByBeerStyle from '@/hooks/data-fetching/beer-posts/useBeerPostsByBeerStyles'; import BeerRecommendationLoadingComponent from '../BeerById/BeerRecommendationLoadingComponent'; interface BeerStyleBeerSectionProps { beerStyle: z.infer; } const BeerStyleBeerSection: FC = ({ beerStyle }) => { const PAGE_SIZE = 2; const { beerPosts, isAtEnd, isLoadingMore, setSize, size } = useBeerPostsByBeerStyle({ beerStyleId: beerStyle.id, pageSize: PAGE_SIZE, }); const { ref: penultimateBeerPostRef } = useInView({ /** * When the last beer post comes into view, call setSize from useBeerPostsByBeerStyle * to load more beer posts. */ onChange: (visible) => { if (!visible || isAtEnd) return; setSize(size + 1); }, }); const beerRecommendationsRef: MutableRefObject = useRef(null); return (
<>

Brews

{!!beerPosts.length && (
{beerPosts.map((beerPost, index) => { const isPenultimateBeerPost = index === beerPosts.length - 2; /** * Attach a ref to the second last beer post in the list. When it comes * into view, the component will call setSize to load more beer posts. */ return (
{beerPost.name}
{beerPost.brewery.name}
{beerPost.abv}% ABV {beerPost.ibu} IBU
); })}
)} { /** * If there are more beer posts to load, show a loading component with a * skeleton loader and a loading spinner. */ !!isLoadingMore && !isAtEnd && ( ) }
); }; export default BeerStyleBeerSection;