mirror of
https://github.com/morpheus65535/bazarr
synced 2025-02-23 14:31:06 +00:00
Persist pages on url and hydrate on page load (#2826)
This commit is contained in:
parent
d572ab7b05
commit
dbcbf4ecde
2 changed files with 25 additions and 9 deletions
|
@ -1,4 +1,5 @@
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
import { useSearchParams } from "react-router";
|
||||||
import {
|
import {
|
||||||
QueryKey,
|
QueryKey,
|
||||||
useQuery,
|
useQuery,
|
||||||
|
@ -34,7 +35,12 @@ export function usePaginationQuery<
|
||||||
): UsePaginationQueryResult<TObject> {
|
): UsePaginationQueryResult<TObject> {
|
||||||
const client = useQueryClient();
|
const client = useQueryClient();
|
||||||
|
|
||||||
const [page, setIndex] = useState(0);
|
const [searchParams] = useSearchParams();
|
||||||
|
|
||||||
|
const [page, setIndex] = useState(
|
||||||
|
searchParams.get("page") ? Number(searchParams.get("page")) - 1 : 0,
|
||||||
|
);
|
||||||
|
|
||||||
const pageSize = usePageSize();
|
const pageSize = usePageSize();
|
||||||
|
|
||||||
const start = page * pageSize;
|
const start = page * pageSize;
|
||||||
|
@ -62,7 +68,14 @@ export function usePaginationQuery<
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [results.isSuccess, results.data, client, cacheIndividual, queryKey]);
|
}, [
|
||||||
|
results.isSuccess,
|
||||||
|
results.data,
|
||||||
|
client,
|
||||||
|
cacheIndividual,
|
||||||
|
queryKey,
|
||||||
|
page,
|
||||||
|
]);
|
||||||
|
|
||||||
const totalCount = data?.total ?? 0;
|
const totalCount = data?.total ?? 0;
|
||||||
const pageCount = Math.ceil(totalCount / pageSize);
|
const pageCount = Math.ceil(totalCount / pageSize);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { FunctionComponent, useEffect } from "react";
|
import { FunctionComponent } from "react";
|
||||||
|
import { useSearchParams } from "react-router";
|
||||||
import { Group, Pagination, Text } from "@mantine/core";
|
import { Group, Pagination, Text } from "@mantine/core";
|
||||||
import { useIsLoading } from "@/contexts";
|
import { useIsLoading } from "@/contexts";
|
||||||
|
|
||||||
|
@ -22,11 +23,7 @@ const PageControl: FunctionComponent<Props> = ({
|
||||||
const end = Math.min(size * (index + 1), total);
|
const end = Math.min(size * (index + 1), total);
|
||||||
|
|
||||||
const isLoading = useIsLoading();
|
const isLoading = useIsLoading();
|
||||||
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
// Jump to first page if total page count changes
|
|
||||||
useEffect(() => {
|
|
||||||
goto(0);
|
|
||||||
}, [total, goto]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Group p={16} justify="space-between">
|
<Group p={16} justify="space-between">
|
||||||
|
@ -37,7 +34,13 @@ const PageControl: FunctionComponent<Props> = ({
|
||||||
size="sm"
|
size="sm"
|
||||||
color={isLoading ? "gray" : "primary"}
|
color={isLoading ? "gray" : "primary"}
|
||||||
value={index + 1}
|
value={index + 1}
|
||||||
onChange={(page) => goto(page - 1)}
|
onChange={(page) => {
|
||||||
|
searchParams.set("page", page.toString());
|
||||||
|
|
||||||
|
setSearchParams(searchParams, { replace: true });
|
||||||
|
|
||||||
|
return goto(page - 1);
|
||||||
|
}}
|
||||||
hidden={count <= 1}
|
hidden={count <= 1}
|
||||||
total={count}
|
total={count}
|
||||||
></Pagination>
|
></Pagination>
|
||||||
|
|
Loading…
Reference in a new issue