All files / src/components/features/admin/AdminPagination index.tsx

0% Statements 0/155
100% Branches 0/0
0% Functions 0/1
0% Lines 0/155

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                                                                                                                                                                                                                                                                                       
"use client";

import { useMemo } from "react";
import { Button } from "@/components/ui";
import { Icon } from "@/components/ui/icons";

interface AdminPaginationProps {
  page: number;
  pageSize: number;
  total: number;
  onPageChange: (page: number) => void;
  onPageSizeChange: (size: number) => void;
  pageSizeOptions?: number[];
  className?: string;
}

export const AdminPagination: React.FC<AdminPaginationProps> = ({
  page,
  pageSize,
  total,
  onPageChange,
  onPageSizeChange,
  pageSizeOptions = [10, 20, 50, 100],
  className = "",
}) => {
  const totalPages = Math.ceil(total / pageSize);
  const startItem = total === 0 ? 0 : (page - 1) * pageSize + 1;
  const endItem = Math.min(page * pageSize, total);

  // Generate page numbers with ellipsis
  const getPaginationRange = useMemo(() => {
    const delta = 1; // Pages to show on each side of current
    const range: (number | string)[] = [];

    // Always show first page
    range.push(1);

    // Calculate start and end of middle range
    const rangeStart = Math.max(2, page - delta);
    const rangeEnd = Math.min(totalPages - 1, page + delta);

    // Add ellipsis after first page if needed
    if (rangeStart > 2) {
      range.push("...");
    }

    // Add middle pages
    for (let i = rangeStart; i <= rangeEnd; i++) {
      range.push(i);
    }

    // Add ellipsis before last page if needed
    if (rangeEnd < totalPages - 1) {
      range.push("...");
    }

    // Always show last page if more than 1 page
    if (totalPages > 1) {
      range.push(totalPages);
    }

    return range;
  }, [page, totalPages]);

  if (total === 0) {
    return null;
  }

  return (
    <div className={`flex flex-col sm:flex-row items-center justify-between gap-4 ${className}`}>
      {/* Results info and page size selector */}
      <div className="flex items-center gap-4 text-sm text-gray-600 dark:text-gray-400">
        <span>
          Showing {startItem}-{endItem} of {total} items
        </span>
        <div className="flex items-center gap-2">
          <label htmlFor="adminPageSize" className="text-gray-600 dark:text-gray-400">
            Show:
          </label>
          <select
            id="adminPageSize"
            value={pageSize}
            onChange={(e) => onPageSizeChange(Number(e.target.value))}
            className="bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md px-2 py-1 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 dark:text-gray-200"
          >
            {pageSizeOptions.map((size) => (
              <option key={size} value={size}>
                {size}
              </option>
            ))}
          </select>
        </div>
      </div>

      {/* Pagination controls */}
      {totalPages > 1 && (
        <nav aria-label="Pagination">
          <ul className="flex items-center gap-1" role="list">
            {/* Previous button */}
            <li>
              <Button
                onClick={() => onPageChange(Math.max(1, page - 1))}
                disabled={page === 1}
                aria-label="Go to previous page"
                type="button"
                variant="ghost"
                size="sm"
                className="w-9 h-9 rounded-md"
              >
                <Icon name="chevron-left" className="fill-current" size={18} aria-hidden="true" />
              </Button>
            </li>

            {/* Page numbers with ellipsis */}
            {getPaginationRange.map((item, index) => (
              <li key={index}>
                {item === "..." ? (
                  <span className="px-2 py-1 text-gray-500 dark:text-gray-400">...</span>
                ) : (
                  <Button
                    onClick={() => onPageChange(item as number)}
                    variant={page === item ? "primary" : "ghost"}
                    size="sm"
                    className={`w-9 h-9 rounded-md ${page === item ? "" : "hover:bg-gray-100 dark:hover:bg-gray-700"}`}
                    aria-label={`Page ${item}${page === item ? ", current page" : ""}`}
                    aria-current={page === item ? "page" : undefined}
                  >
                    {item}
                  </Button>
                )}
              </li>
            ))}

            {/* Next button */}
            <li>
              <Button
                onClick={() => onPageChange(Math.min(totalPages, page + 1))}
                disabled={page === totalPages}
                aria-label="Go to next page"
                type="button"
                variant="ghost"
                size="sm"
                className="w-9 h-9 rounded-md"
              >
                <Icon name="chevron-right" className="fill-current" size={18} aria-hidden="true" />
              </Button>
            </li>
          </ul>
        </nav>
      )}
    </div>
  );
};

export default AdminPagination;