fix: preserve newest scripts pagination (#10882)

This commit is contained in:
Jesús
2026-01-18 12:51:21 +01:00
committed by GitHub
parent 67685e62d1
commit f6b3515c9e
2 changed files with 31 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
import { CalendarPlus } from "lucide-react"; import { CalendarPlus } from "lucide-react";
import { useMemo, useState } from "react"; import { useEffect, useMemo } from "react";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
@@ -26,9 +26,15 @@ export function getDisplayValueFromType(type: string) {
} }
} }
export function LatestScripts({ items }: { items: Category[] }) { export function LatestScripts({
const [page, setPage] = useState(1); items,
page,
onPageChange,
}: {
items: Category[];
page: number;
onPageChange: (page: number) => void;
}) {
const latestScripts = useMemo(() => { const latestScripts = useMemo(() => {
if (!items) if (!items)
return []; return [];
@@ -48,12 +54,20 @@ export function LatestScripts({ items }: { items: Category[] }) {
); );
}, [items]); }, [items]);
const totalPages = Math.max(1, Math.ceil(latestScripts.length / ITEMS_PER_PAGE));
useEffect(() => {
if (page > totalPages) {
onPageChange(totalPages);
}
}, [page, totalPages, onPageChange]);
const goToNextPage = () => { const goToNextPage = () => {
setPage(prevPage => prevPage + 1); onPageChange(Math.min(totalPages, page + 1));
}; };
const goToPreviousPage = () => { const goToPreviousPage = () => {
setPage(prevPage => prevPage - 1); onPageChange(Math.max(1, page - 1));
}; };
const startIndex = (page - 1) * ITEMS_PER_PAGE; const startIndex = (page - 1) * ITEMS_PER_PAGE;

View File

@@ -18,6 +18,7 @@ function ScriptContent() {
const [selectedCategory, setSelectedCategory] = useQueryState("category"); const [selectedCategory, setSelectedCategory] = useQueryState("category");
const [links, setLinks] = useState<Category[]>([]); const [links, setLinks] = useState<Category[]>([]);
const [item, setItem] = useState<Script>(); const [item, setItem] = useState<Script>();
const [latestPage, setLatestPage] = useState(1);
useEffect(() => { useEffect(() => {
if (selectedScript && links.length > 0) { if (selectedScript && links.length > 0) {
@@ -50,14 +51,16 @@ function ScriptContent() {
/> />
</div> </div>
<div className="px-4 w-full sm:max-w-[calc(100%-350px-16px)]"> <div className="px-4 w-full sm:max-w-[calc(100%-350px-16px)]">
{selectedScript && item ? ( {selectedScript && item
<ScriptItem item={item} setSelectedScript={setSelectedScript} /> ? (
) : ( <ScriptItem item={item} setSelectedScript={setSelectedScript} />
<div className="flex w-full flex-col gap-5"> )
<LatestScripts items={links} /> : (
<MostViewedScripts items={links} /> <div className="flex w-full flex-col gap-5">
</div> <LatestScripts items={links} page={latestPage} onPageChange={setLatestPage} />
)} <MostViewedScripts items={links} />
</div>
)}
</div> </div>
</div> </div>
</div> </div>