"use client"; import useSWRInfinite from "swr/infinite"; import useSWR from "swr"; import { Spinner } from "#/components/Spinner/Spinner"; import { useState, useEffect } from "react"; import { useScrollPosition } from "#/hooks/useScrollPosition"; import { useUserStore } from "../store/auth"; import { ENDPOINTS } from "#/api/config"; import { ReleaseSection } from "#/components/ReleaseSection/ReleaseSection"; import { CollectionInfoBasics } from "#/components/CollectionInfo/CollectionInfo.Basics"; import { InfoLists } from "#/components/InfoLists/InfoLists"; import { CollectionInfoControls } from "#/components/CollectionInfo/CollectionInfoControls"; import { CommentsMain } from "#/components/Comments/Comments.Main"; import { useSWRfetcher } from "#/api/utils"; export const ViewCollectionPage = (props: { id: number }) => { const userStore = useUserStore(); function useFetchCollectionInfo(type: "info" | "comments") { let url: string; if (type == "info") { url = `${ENDPOINTS.collection.base}/${props.id}`; } else if (type == "comments") { url = `${ENDPOINTS.collection.base}/comment/all/${props.id}/0?sort=3`; } if (userStore.token) { url += `${type != "info" ? "&" : "?"}token=${userStore.token}`; } const { data, error, isLoading } = useSWR(url, useSWRfetcher); return [data, error, isLoading]; } const getKey = (pageIndex: number, previousPageData: any) => { if (previousPageData && !previousPageData.content.length) return null; let url: string = `${ENDPOINTS.collection.base}/${props.id}/releases/${pageIndex}`; if (userStore.token) { url += `?token=${userStore.token}`; } return url; }; const [collectionInfo, collectionInfoError, collectionInfoIsLoading] = useFetchCollectionInfo("info"); const [ collectionComments, collectionCommentsError, collectionCommentsIsLoading, ] = useFetchCollectionInfo("comments"); const { data, error, isLoading, size, setSize } = useSWRInfinite( getKey, useSWRfetcher, { 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); } }, [data]); const scrollPosition = useScrollPosition(); useEffect(() => { if (scrollPosition >= 98 && scrollPosition <= 99) { setSize(size + 1); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [scrollPosition]); if (isLoading) { return (
); } if (error || collectionInfoError) { return (

Ошибка

Произошла ошибка при загрузке коллекции. Попробуйте обновить страницу или зайдите позже.

); } return ( <> {collectionInfoIsLoading ?
: collectionInfo && ( <>
{collectionComments && !collectionCommentsIsLoading && ( )} {userStore.token && !isLoading && ( <> )}
{content && ( )} ) } ); };