/* 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 BeerCommentForm from './BeerCommentForm'; import CommentCardBody from './CommentCardBody'; import NoCommentsCard from './NoCommentsCard'; import CommentLoadingCardBody from './CommentLoadingCardBody'; import Spinner from '../ui/Spinner'; interface BeerPostCommentsSectionProps { beerPost: z.infer; } const LoadingComponent: FC<{ length: number }> = ({ length }) => { return ( <> {Array.from({ length }).map((_, i) => ( ))}
); }; 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 = 6; const { comments, isLoading, mutate, setSize, size, isLoadingMore, isAtEnd } = useBeerPostComments({ id, pageNum, pageSize: PAGE_SIZE, }); const { ref } = useInView({ delay: 3000, onChange: (visible) => { if (!visible || isAtEnd) return; setSize(size + 1); }, }); const sectionRef: MutableRefObject = useRef(null); return (
{user ? ( ) : (
Log in to leave a comment.
)}
{comments && !!comments.length && !isLoading && (
{comments.map((comment, index) => { const isLastComment = index === comments.length - 1; return (
); })} {!!isLoadingMore && (
)} {isAtEnd && (
)}
)} {!comments?.length && !isLoading && } {isLoading && (
)}
); }; export default BeerPostCommentsSection;