Ошибка
Произошла ошибка поиска. Попробуйте обновить страницу или зайдите позже.
"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";
import { Button, Dropdown, DropdownItem, Modal, ModalBody, ModalFooter, ModalHeader } from "flowbite-react";
import { CollectionsSection } from "#/components/CollectionsSection/CollectionsSection";
import { UserSection } from "#/components/UserSection/UserSection";
import { useSWRfetcher } from "#/api/utils";
const ListsMapping = {
watching: {
name: "Смотрю",
id: 1,
},
planned: {
name: "В планах",
id: 2,
},
watched: {
name: "Просмотрено",
id: 3,
},
delayed: {
name: "Отложено",
id: 4,
},
abandoned: {
name: "Заброшено",
id: 5,
},
};
const TagMapping = {
name: {
name: "Названию",
id: 0,
},
studio: {
name: "Студии",
id: 1,
},
director: {
name: "Режиссёру",
id: 2,
},
author: {
name: "Автору",
id: 3,
},
tag: {
name: "Тегу",
id: 4,
},
};
const WhereMapping = {
releases: "Релизах",
list: "Списках",
history: "Истории",
favorites: "Избранном",
collections: "Коллекциях",
profiles: "Профилях",
};
export function SearchPage() {
const router = useRouter();
const searchParams = useSearchParams();
const [query, setQuery] = useState(searchParams.get("q") || "");
const [searchVal, setSearchVal] = useState(searchParams.get("q") || "");
const [where, setWhere] = useState(searchParams.get("where") || "releases");
const [searchBy, setSearchBy] = useState(
searchParams.get("searchBy") || "name"
);
const [list, setList] = useState(searchParams.get("list") || "watching");
const [filtersModalOpen, setFiltersModalOpen] = useState(false);
const userStore = useUserStore();
const getKey = (pageIndex: number, previousPageData: any) => {
if (where == "releases") {
if (previousPageData && !previousPageData.releases.length) return null;
} else {
if (previousPageData && !previousPageData.content.length) return null;
}
const url = new URL("/api/search", window.location.origin);
url.searchParams.set("page", pageIndex.toString());
if (userStore.token) {
url.searchParams.set("token", userStore.token);
}
if (where) {
url.searchParams.set("where", where);
}
if (where == "list" && list && ListsMapping.hasOwnProperty(list)) {
url.searchParams.set("list", ListsMapping[list].id);
}
url.searchParams.set("searchBy", TagMapping[searchBy].id);
if (query) {
url.searchParams.set("q", query);
return url.toString();
}
return;
};
const { data, error, isLoading, size, setSize } = useSWRInfinite(
getKey,
useSWRfetcher,
{ initialSize: 2, revalidateFirstPage: false }
);
const [content, setContent] = useState(null);
useEffect(() => {
if (data) {
let allReleases = [];
if (where == "releases") {
for (let i = 0; i < data.length; i++) {
allReleases.push(...data[i].releases);
}
} else {
for (let i = 0; i < data.length; i++) {
allReleases.push(...data[i].content);
}
}
setContent(allReleases);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data]);
const scrollPosition = useScrollPosition();
useEffect(() => {
if (scrollPosition >= 98 && scrollPosition <= 99) {
setSize(size + 1);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [scrollPosition]);
function _executeSearch(value: string) {
const Params = new URLSearchParams(window.location.search);
Params.set("q", value);
const url = new URL(`/search?${Params.toString()}`, window.location.origin);
setContent(null);
setQuery(value);
router.push(url.toString());
}
useEffect(() => {
if (searchVal && searchVal.length % 4 == 1) {
_executeSearch(searchVal.trim());
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchVal]);
if (error)
return (
Произошла ошибка поиска. Попробуйте обновить страницу или зайдите
позже.
Ошибка
Странно, аниме не найдено, попробуйте другой запрос...
Введите ваш запрос что-бы найти любимый тайтл
Искать в
Список
Искать по