"use client"; import useSWRInfinite from "swr/infinite"; import { ReleaseSection } from "@/app/components/ReleaseSection/ReleaseSection"; import { Spinner } from "@/app/components/Spinner/Spinner"; import { useState, useEffect } from "react"; import { useScrollPosition } from "@/app/hooks/useScrollPosition"; import { useUserStore } from "../store/auth"; import { Dropdown } from "flowbite-react"; const fetcher = async (url) => { const res = await fetch(url); if (!res.ok) { const error = new Error("An error occurred while fetching the data."); error.info = await res.json(); error.status = res.status; throw error; } return res.json(); }; const sort = { descendingIcon: "material-symbols--sort", ascendingIcon: "[transform:rotateX(180deg)] material-symbols--sort", values: [ { name: "Сначала новые", value: "adding_descending", }, { name: "Сначала старые", value: "adding_ascending", }, { name: "А-Я", value: "alphabet_descending", }, { name: "Я-А", value: "alphabet_ascending", }, ], }; export function BookmarksCategoryPage(props) { const token = useUserStore((state) => state.token); const [selectedSort, setSelectedSort] = useState(0); const getKey = (pageIndex, previousPageData) => { if (previousPageData && !previousPageData.content.length) return null; return `/api/bookmarks?list=${props.slug}&page=${pageIndex}&token=${token}&sort=${sort.values[selectedSort].value}`; }; const { data, error, isLoading, size, setSize } = useSWRInfinite( getKey, fetcher, { initialSize: 2, revalidateFirstPage: false } ); 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); } }, [scrollPosition]); if (error) return