"use client"; import useSWRInfinite from "swr/infinite"; import { ReleaseSection } from "#/components/ReleaseSection/ReleaseSection"; import { RelatedSection } from "#/components/RelatedSection/RelatedSection"; import { Spinner } from "#/components/Spinner/Spinner"; import { useState, useEffect } from "react"; import { useScrollPosition } from "#/hooks/useScrollPosition"; import { useRouter } from "next/navigation"; import { useSearchParams } from "next/navigation"; import { useUserStore } from "../store/auth"; 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(); }; export function SearchPage() { const router = useRouter(); const searchParams = useSearchParams(); const [query, setQuery] = useState(searchParams.get("q") || null); const where = searchParams.get("where") || null const searchBy = searchParams.get("searchBy") || null const list = searchParams.get("list") || null const token = useUserStore((state) => state.token); const getKey = (pageIndex: number, previousPageData: any) => { if (previousPageData && !previousPageData.releases.length) return null; const url = new URL("/api/search", window.location.origin); url.searchParams.set("page", pageIndex.toString()); if (token) { url.searchParams.set("token", token); } if (where) { url.searchParams.set("where", where); } if (searchBy) { url.searchParams.set("searchBy", searchBy); } if (list) { url.searchParams.set("list", list); } if (query) { url.searchParams.set("q", query); return url.toString(); } return; }; 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].releases); } setContent(allReleases); } }, [data]); const scrollPosition = useScrollPosition(); useEffect(() => { if (scrollPosition >= 98 && scrollPosition <= 99) { setSize(size + 1); } }, [scrollPosition]); if (error) return
failed to load
; return (
{ e.preventDefault(); setContent(null) setQuery(e.target[0].value.trim()); router.push(`/search?q=${e.target[0].value.trim()}`); }} >
{data && data[0].related && } {content ? ( content.length > 0 ? ( ) : (

Странно, аниме не найдено, попробуйте другой запрос...

) ) : ( isLoading && (
) )} {!content && !isLoading && !query && (

Введите ваш запрос что-бы найти любимый тайтл

)}
{data && data[data.length - 1].releases.length == 25 && ( )}
); }