Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

truncate search #46

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/api/backend/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BaseRequest, client } from "../base";
import { BookItem } from "../types";
import { SearchParams } from "./types";
import { getExternalDownloads } from "../downloads/external";
import { ExternalDownloadResponse } from "../downloads/types";

export const getBooks = (params: SearchParams) => {
return client<BaseRequest<BookItem>>("/books", {
Expand All @@ -22,10 +23,15 @@ export const useGetBooksQueryWithExternalDownloads = (params: SearchParams) => {
queryKey: ["search", params],
queryFn: async () => {
const books = await getBooks(params);
const externalDownloads = await getExternalDownloads(books.results.map((book) => book.md5));
const externalDownloads: ExternalDownloadResponse = [];
for (let i = 0; i < params.limit; i += 10) {
const batch = books.results.slice(i, i + 3).map((book) => book.md5);
const batchExternalDownloads = await getExternalDownloads(batch);
externalDownloads.push(...batchExternalDownloads);
}
return {
...books,
results: books.results.map((book) => ({
results: books.results.slice(0, params.limit).map((book) => ({
...book,
externalDownloads: externalDownloads.find((b) => b.md5 === book.md5)?.external_downloads,
ipfs: externalDownloads.find((b) => b.md5 === book.md5)?.ipfs,
Expand Down
35 changes: 12 additions & 23 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { SkeletonBookItem } from "@/components/books/book-item";
import { useDebounce } from "@/hooks/use-debounce";
import { useSettingsStore } from "@/stores/settings";
import { createFileRoute, useNavigate } from "@tanstack/react-router";
import { useLayoutStore } from "@/stores/layout";
import { cn } from "@/lib/utils";
import { NavLink } from "@/components/ui/nav-link";
import { Input } from "@/components/ui/input";
import { SearchIcon } from "lucide-react";
Expand All @@ -29,7 +27,6 @@ export const Route = createFileRoute("/")({
function Index() {
const navigate = useNavigate({ from: Route.fullPath });
const { q } = Route.useSearch();
const sidebar = useLayoutStore((state) => state.sidebar);

const [filters, setFilters] = useState<FilterProps["filters"]>({
view: "list",
Expand Down Expand Up @@ -64,26 +61,18 @@ function Index() {
<div className="flex flex-col gap-2">
<Filters filters={filters} setFilters={setFilters} />

<div
className={cn("sticky left-[40px] top-[8px] z-50 w-full", {
"left-[200px]": sidebar.isOpen,
})}
>
<div className="relative w-full max-w-full">
<Input
iconLeft={<SearchIcon />}
placeholder="Search for books, comics, or manga..."
value={q}
onChange={(e) =>
navigate({
search: {
q: e.target.value,
},
})
}
/>
</div>
</div>
<Input
iconLeft={<SearchIcon />}
placeholder="Search for books, comics, or manga..."
value={q}
onChange={(e) =>
navigate({
search: {
q: e.target.value,
},
})
}
/>
</div>

{isLoading && (
Expand Down
Loading