import { Fragment, useState } from "react"; import { useTranslate } from "ra-core"; import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, } from "@/components/ds/ui/pagination"; import { Separator } from "@/components/ds/ui/separator"; import { COMPANY_CREATED, COMPANY_NOTE_CREATED, CONTACT_CREATED, CONTACT_NOTE_CREATED, DEAL_CREATED, DEAL_NOTE_CREATED, } from "../consts"; import type { Activity } from "../types"; import { ActivityLogCompanyCreated } from "./ActivityLogCompanyCreated"; import { ActivityLogCompanyNoteCreated } from "./ActivityLogCompanyNoteCreated"; import { ActivityLogContactCreated } from "./ActivityLogContactCreated"; import { ActivityLogContactNoteCreated } from "./ActivityLogContactNoteCreated"; import { ActivityLogDealCreated } from "./ActivityLogDealCreated"; import { ActivityLogDealNoteCreated } from "./ActivityLogDealNoteCreated"; type ActivityLogIteratorProps = { activities: Activity[]; pageSize: number; }; export function ActivityLogIterator({ activities, pageSize, }: ActivityLogIteratorProps) { const translate = useTranslate(); const [currentPage, setCurrentPage] = useState(1); const totalPages = Math.ceil(activities.length / pageSize); const startIndex = (currentPage - 1) * pageSize; const endIndex = startIndex + pageSize; const currentActivities = activities.slice(startIndex, endIndex); // Generate page numbers to display const getPageNumbers = () => { const pages: (number | "ellipsis")[] = []; const maxVisiblePages = 5; if (totalPages <= maxVisiblePages) { // Show all pages if total is small for (let i = 1; i <= totalPages; i++) { pages.push(i); } } else { // Always show first page pages.push(1); if (currentPage > 3) { pages.push("ellipsis"); } // Show pages around current page const startPage = Math.max(2, currentPage - 1); const endPage = Math.min(totalPages - 1, currentPage + 1); for (let i = startPage; i <= endPage; i++) { pages.push(i); } if (currentPage < totalPages - 2) { pages.push("ellipsis"); } // Always show last page if (totalPages > 1) { pages.push(totalPages); } } return pages; }; const handlePageChange = (page: number) => { setCurrentPage(page); // Scroll to top of activity log window.scrollTo({ top: 0, behavior: "smooth" }); }; return (
{currentActivities.map((activity, index) => ( ))} {totalPages > 1 && (
{translate("crm.activity.pagination", { from: startIndex + 1, to: Math.min(endIndex, activities.length), total: activities.length, })}
{ e.preventDefault(); if (currentPage > 1) handlePageChange(currentPage - 1); }} aria-disabled={currentPage === 1} className={ currentPage === 1 ? "pointer-events-none opacity-50" : "" } /> {getPageNumbers().map((page, index) => page === "ellipsis" ? ( ) : ( { e.preventDefault(); handlePageChange(page); }} isActive={currentPage === page} > {page} ), )} { e.preventDefault(); if (currentPage < totalPages) handlePageChange(currentPage + 1); }} aria-disabled={currentPage === totalPages} className={ currentPage === totalPages ? "pointer-events-none opacity-50" : "" } />
)}
); } function ActivityItem({ activity }: { activity: Activity }) { if (activity.type === COMPANY_CREATED) { return ; } if (activity.type === CONTACT_CREATED) { return ; } if (activity.type === CONTACT_NOTE_CREATED) { return ; } if (activity.type === DEAL_CREATED) { return ; } if (activity.type === DEAL_NOTE_CREATED) { return ; } if (activity.type === COMPANY_NOTE_CREATED) { return ; } return null; }