Skip to content
Merged
Changes from 2 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
19 changes: 18 additions & 1 deletion frontend/app/routes/books.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SortSelect } from "~/components/books/SortSelect/SortSelect";
import { PageBreadcrumb } from "~/components/PageBreadcrumb/PageBreadcrumb";
import { useBookSearchParams } from "~/hooks/useBookSearchParams";
import { useSuggestedBooks } from "~/hooks/useSuggestedBooks";
import { useSearchParams } from "react-router";
import { Navigate, useSearchParams } from "react-router";
import { useDataIsDelayed } from "~/hooks/useDataIsDelayed";
import type { ReactNode } from "react";
import { BookFilters } from "~/components/books/BookFilters/BookFilters";
Expand Down Expand Up @@ -128,6 +128,13 @@ export default function BooksPage() {
const suggestionParams = new URLSearchParams(searchParams);
suggestionParams.set("origin", "suggestions");

// Redirect to page 1, keeping the search query, filters and sort
function redirectToFirstPage() {
const firstPageParams = new URLSearchParams(searchParams);
firstPageParams.delete("page");
return <Navigate to={{ search: firstPageParams.toString() }} replace />;
}

// Later loads keep showing the previous books until the new ones arrive,
// unless it takes a while
if ((loading && !books) || slowLoading) {
Expand All @@ -152,7 +159,17 @@ export default function BooksPage() {
);
}

// The page is beyond the last page of the search results
if (page > 1 && totalCount > 0 && books?.length === 0) {
return redirectToFirstPage();
}

if (!books || books.length === 0) {
// The page is beyond the last page of the suggestions
if (page > 1 && suggestions?.length === 0 && suggestionCount > 0) {
return redirectToFirstPage();
}

if (suggestions && suggestions.length > 0) {
return (
<BooksLayout resultCount={0}>
Expand Down