refactor: enhance URL parameter handling in PaginatedSeriesGrid and PaginatedBookGrid components by adding replace option for router navigation

This commit is contained in:
Julien Froidefond
2025-04-01 07:31:08 +02:00
parent 4721076dd9
commit 182affb04b
2 changed files with 20 additions and 6 deletions

View File

@@ -39,7 +39,10 @@ export function PaginatedSeriesGrid({
const { isCompact, itemsPerPage } = useDisplayPreferences(); const { isCompact, itemsPerPage } = useDisplayPreferences();
const { t } = useTranslate(); const { t } = useTranslate();
const updateUrlParams = async (updates: Record<string, string | null>) => { const updateUrlParams = async (
updates: Record<string, string | null>,
replace: boolean = false
) => {
setIsChangingPage(true); setIsChangingPage(true);
const params = new URLSearchParams(searchParams.toString()); const params = new URLSearchParams(searchParams.toString());
@@ -51,7 +54,11 @@ export function PaginatedSeriesGrid({
} }
}); });
if (replace) {
await router.replace(`${pathname}?${params.toString()}`);
} else {
await router.push(`${pathname}?${params.toString()}`); await router.push(`${pathname}?${params.toString()}`);
}
}; };
// Reset loading state when series change // Reset loading state when series change
@@ -67,7 +74,7 @@ export function PaginatedSeriesGrid({
// Apply default filter on initial load // Apply default filter on initial load
useEffect(() => { useEffect(() => {
if (defaultShowOnlyUnread && !searchParams.has("unread")) { if (defaultShowOnlyUnread && !searchParams.has("unread")) {
updateUrlParams({ page: "1", unread: "true" }); updateUrlParams({ page: "1", unread: "true" }, true);
} }
}, [defaultShowOnlyUnread, pathname, router, searchParams]); }, [defaultShowOnlyUnread, pathname, router, searchParams]);

View File

@@ -38,7 +38,10 @@ export function PaginatedBookGrid({
const { isCompact, itemsPerPage } = useDisplayPreferences(); const { isCompact, itemsPerPage } = useDisplayPreferences();
const { t } = useTranslate(); const { t } = useTranslate();
const updateUrlParams = async (updates: Record<string, string | null>) => { const updateUrlParams = async (
updates: Record<string, string | null>,
replace: boolean = false
) => {
setIsChangingPage(true); setIsChangingPage(true);
const params = new URLSearchParams(searchParams.toString()); const params = new URLSearchParams(searchParams.toString());
@@ -50,7 +53,11 @@ export function PaginatedBookGrid({
} }
}); });
if (replace) {
await router.replace(`${pathname}?${params.toString()}`);
} else {
await router.push(`${pathname}?${params.toString()}`); await router.push(`${pathname}?${params.toString()}`);
}
}; };
// Reset loading state when books change // Reset loading state when books change
@@ -66,7 +73,7 @@ export function PaginatedBookGrid({
// Apply default filter on initial load // Apply default filter on initial load
useEffect(() => { useEffect(() => {
if (defaultShowOnlyUnread && !searchParams.has("unread")) { if (defaultShowOnlyUnread && !searchParams.has("unread")) {
updateUrlParams({ page: "1", unread: "true" }); updateUrlParams({ page: "1", unread: "true" }, true);
} }
}, [defaultShowOnlyUnread, pathname, router, searchParams]); }, [defaultShowOnlyUnread, pathname, router, searchParams]);