/* eslint-disable no-nested-ternary */ import UserContext from '@/contexts/userContext'; import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult'; import { FC, MutableRefObject, useContext, useRef } from 'react'; import { z } from 'zod'; import useBeerPostComments from '@/hooks/useBeerPostComments'; import { useRouter } from 'next/router'; import { useInView } from 'react-intersection-observer'; import { FaArrowUp } from 'react-icons/fa'; import BeerCommentForm from './BeerCommentForm'; import CommentCardBody from './CommentCardBody'; import NoCommentsCard from './NoCommentsCard'; import LoadingComponent from './LoadingComponent'; interface BeerPostCommentsSectionProps { beerPost: z.infer; } const BeerPostCommentsSection: FC = ({ beerPost }) => { const { user } = useContext(UserContext); const router = useRouter(); const { id } = beerPost; const pageNum = parseInt(router.query.comments_page as string, 10) || 1; const PAGE_SIZE = 4; const { comments, isLoading, mutate, setSize, size, isLoadingMore, isAtEnd } = useBeerPostComments({ id, pageNum, pageSize: PAGE_SIZE, }); const { ref: lastCommentRef } = useInView({ /** * When the last comment comes into view, call setSize from useBeerPostComments to * load more comments. */ onChange: (visible) => { if (!visible || isAtEnd) return; setSize(size + 1); }, }); const sectionRef: MutableRefObject = useRef(null); return (
{user ? ( ) : (
Log in to leave a comment.
)}
{ /** * If the comments are loading, show a loading component. Otherwise, show the * comments. */ isLoading ? (
) : ( <> {!!comments.length && (
{comments.map((comment, index) => { const isLastComment = index === comments.length - 1; /** * Attach a ref to the last comment in the list. When it comes into * view, the component will call setSize to load more comments. */ return (
); })} { /** * If there are more comments to load, show a loading component with a * skeleton loader and a loading spinner. */ !!isLoadingMore && ( ) } { /** * If the user has scrolled to the end of the comments, show a button * that will scroll them back to the top of the comments section. */ !!isAtEnd && (
) }
)} {!comments.length && } ) }
); }; export default BeerPostCommentsSection;