From f6b3515c9e1bdfccb737e5373a9ba3facbcaba70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs?= <28611065+jgrubiox@users.noreply.github.com> Date: Sun, 18 Jan 2026 12:51:21 +0100 Subject: [PATCH] fix: preserve newest scripts pagination (#10882) --- .../_components/script-info-blocks.tsx | 26 ++++++++++++++----- frontend/src/app/scripts/page.tsx | 19 ++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/scripts/_components/script-info-blocks.tsx b/frontend/src/app/scripts/_components/script-info-blocks.tsx index 9bb0acdcc..a69f2428f 100644 --- a/frontend/src/app/scripts/_components/script-info-blocks.tsx +++ b/frontend/src/app/scripts/_components/script-info-blocks.tsx @@ -1,5 +1,5 @@ import { CalendarPlus } from "lucide-react"; -import { useMemo, useState } from "react"; +import { useEffect, useMemo } from "react"; import Image from "next/image"; import Link from "next/link"; @@ -26,9 +26,15 @@ export function getDisplayValueFromType(type: string) { } } -export function LatestScripts({ items }: { items: Category[] }) { - const [page, setPage] = useState(1); - +export function LatestScripts({ + items, + page, + onPageChange, +}: { + items: Category[]; + page: number; + onPageChange: (page: number) => void; +}) { const latestScripts = useMemo(() => { if (!items) return []; @@ -48,12 +54,20 @@ export function LatestScripts({ items }: { items: Category[] }) { ); }, [items]); + const totalPages = Math.max(1, Math.ceil(latestScripts.length / ITEMS_PER_PAGE)); + + useEffect(() => { + if (page > totalPages) { + onPageChange(totalPages); + } + }, [page, totalPages, onPageChange]); + const goToNextPage = () => { - setPage(prevPage => prevPage + 1); + onPageChange(Math.min(totalPages, page + 1)); }; const goToPreviousPage = () => { - setPage(prevPage => prevPage - 1); + onPageChange(Math.max(1, page - 1)); }; const startIndex = (page - 1) * ITEMS_PER_PAGE; diff --git a/frontend/src/app/scripts/page.tsx b/frontend/src/app/scripts/page.tsx index f646ed932..33d7c1f5e 100644 --- a/frontend/src/app/scripts/page.tsx +++ b/frontend/src/app/scripts/page.tsx @@ -18,6 +18,7 @@ function ScriptContent() { const [selectedCategory, setSelectedCategory] = useQueryState("category"); const [links, setLinks] = useState([]); const [item, setItem] = useState