import { Card, Button, Modal, Spinner } from "flowbite-react"; import { CommentsComment } from "./Comments.Comment"; import { useState, useEffect, useCallback } from "react"; import { ENDPOINTS } from "#/api/config"; import useSWRInfinite from "swr/infinite"; import { CommentsAddModal } from "./Comments.Add"; export const CommentsMain = (props: { release_id: number; token: string | null; comments: any; }) => { const [isAllCommentsOpen, setIsAllCommentsOpen] = useState(false); const [isAddCommentsOpen, setIsAddCommentsOpen] = useState(false); return ( <>

Комментарии

Популярные и актуальные

{props.token && ( )}
{props.comments.map((comment: any) => ( ))}
); }; const fetcher = async (url: string) => { const res = await fetch(url); if (!res.ok) { const error = new Error( `An error occurred while fetching the data. status: ${res.status}` ); error.message = await res.json(); throw error; } return res.json(); }; const CommentsAllModal = (props: { isOpen: boolean; setIsOpen: any; release_id: number; token: string | null; }) => { const [isLoadingEnd, setIsLoadingEnd] = useState(false); const [currentRef, setCurrentRef] = useState(null); const modalRef = useCallback((ref) => { setCurrentRef(ref); }, []); const getKey = (pageIndex: number, previousPageData: any) => { if (previousPageData && !previousPageData.content.length) return null; let url = `${ENDPOINTS.release.info}/comment/all/${props.release_id}/${pageIndex}?sort=1`; if (props.token) { url += `&token=${props.token}`; } return url; }; const { data, error, isLoading, size, setSize } = useSWRInfinite( getKey, fetcher, { initialSize: 2 } ); const [content, setContent] = useState(null); useEffect(() => { if (data) { let allReleases = []; for (let i = 0; i < data.length; i++) { allReleases.push(...data[i].content); } setContent(allReleases); setIsLoadingEnd(true); } }, [data]); const [scrollPosition, setScrollPosition] = useState(0); function handleScroll() { const height = currentRef.scrollHeight - currentRef.clientHeight; const windowScroll = currentRef.scrollTop; const scrolled = (windowScroll / height) * 100; setScrollPosition(Math.floor(scrolled)); } useEffect(() => { if (scrollPosition >= 95 && scrollPosition <= 96) { setSize(size + 1); } }, [scrollPosition]); return ( props.setIsOpen(false)} >

Все комментарии

всего: {!isLoadingEnd ? "загрузка..." : data[0].total_count}

{!isLoadingEnd ? ( ) : content ? ( content.map((comment: any) => ( )) ) : (

Комментариев нет

)}
); };