import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { useMemo, useState } from "react"; import { Category } from "@/lib/types"; import Link from "next/link"; import Image from "next/image"; import { Button } from "./ui/button"; import { extractDate } from "@/lib/time"; const ITEMS_PER_PAGE = 3; function LatestScripts({ items }: { items: Category[] }) { const [page, setPage] = useState(1); const latestScripts = useMemo(() => { if (!items) return []; const scripts = items.flatMap((category) => category.expand.items || []); return scripts.sort( (a, b) => new Date(b.created).getTime() - new Date(a.created).getTime(), ); }, [items]); const goToNextPage = () => { setPage((prevPage) => prevPage + 1); }; const goToPreviousPage = () => { setPage((prevPage) => prevPage - 1); }; const startIndex = (page - 1) * ITEMS_PER_PAGE; const endIndex = page * ITEMS_PER_PAGE; if (!items) { return null; } return (
{latestScripts.length > 0 && (

Newest Scripts

{page > 1 && (
Previous
)} {endIndex < latestScripts.length && (
{page === 1 ? "More.." : "Next"}
)}
)}
{latestScripts.slice(startIndex, endIndex).map((item) => (

{item.title} {item.item_type}

Date added: {extractDate(item.created)}

{item.description}
))}
); } export default LatestScripts;