|
| 1 | +import React, { useState, useRef } from "react"; |
| 2 | +import { Avatar } from "@/components/ui/avatar"; |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import AuditProgressAnimation from "./audit-progress-animation"; |
| 5 | + |
| 6 | +interface Assertion { |
| 7 | + index: number; |
| 8 | + content: string; |
| 9 | + type: string; |
| 10 | + winner: number; // It should be the winner's candidate id |
| 11 | + name: string; // candidate name |
| 12 | +} |
| 13 | + |
| 14 | +interface AssertionTableProps { |
| 15 | + assertions: Assertion[]; |
| 16 | + winnerName: string; |
| 17 | + isValid: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +const pageSize = 4; |
| 21 | + |
| 22 | +const AssertionTable: React.FC<AssertionTableProps> = ({ |
| 23 | + assertions, |
| 24 | + winnerName, |
| 25 | + isValid, |
| 26 | +}) => { |
| 27 | + const [currentPage, setCurrentPage] = useState(1); |
| 28 | + const totalPages = Math.ceil(assertions.length / pageSize); |
| 29 | + const tableRef = useRef<HTMLDivElement>(null); |
| 30 | + const paginationRef = useRef<HTMLDivElement>(null); |
| 31 | + |
| 32 | + const animationSpacerRef = useRef<HTMLDivElement>(null); |
| 33 | + |
| 34 | + const paged = assertions.slice( |
| 35 | + (currentPage - 1) * pageSize, |
| 36 | + currentPage * pageSize, |
| 37 | + ); |
| 38 | + |
| 39 | + const handlePageChange = (page: number) => { |
| 40 | + if (page >= 1 && page <= totalPages) { |
| 41 | + setCurrentPage(page); |
| 42 | + tableRef.current?.scrollIntoView({ behavior: "smooth", block: "start" }); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + const [showAnimation, setShowAnimation] = useState(true); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className="flex flex-col h-full"> |
| 50 | + <div ref={tableRef} className="flex-1 overflow-y-auto border rounded-lg"> |
| 51 | + <table className="min-w-full table-auto"> |
| 52 | + <thead className="sticky top-0 bg-background z-10"> |
| 53 | + <tr> |
| 54 | + <th className="px-4 py-2 border-b">Index</th> |
| 55 | + <th className="px-4 py-2 border-b">Content</th> |
| 56 | + <th className="px-4 py-2 border-b">Type</th> |
| 57 | + </tr> |
| 58 | + </thead> |
| 59 | + <tbody> |
| 60 | + {paged.map((a) => ( |
| 61 | + <tr key={a.index} className="h-[48px]"> |
| 62 | + <td className="px-4 py-2 text-center border-b">{a.index}</td> |
| 63 | + <td className="px-4 py-2 text-left border-b"> |
| 64 | + <div className="flex items-center"> |
| 65 | + <Avatar candidateId={a.winner} className="mr-2" /> |
| 66 | + <span>{a.content}</span> |
| 67 | + </div> |
| 68 | + </td> |
| 69 | + <td className="px-4 py-2 text-center border-b">{a.type}</td> |
| 70 | + </tr> |
| 71 | + ))} |
| 72 | + </tbody> |
| 73 | + </table> |
| 74 | + </div> |
| 75 | + |
| 76 | + <div ref={paginationRef} className="mt-2 pt-2 border-t"> |
| 77 | + <div className="flex justify-center items-center gap-2 flex-wrap"> |
| 78 | + <Button |
| 79 | + size="sm" |
| 80 | + variant="outline" |
| 81 | + disabled={currentPage === 1} |
| 82 | + onClick={() => handlePageChange(currentPage - 1)} |
| 83 | + > |
| 84 | + Prev |
| 85 | + </Button> |
| 86 | + |
| 87 | + <div className="flex items-center gap-1 text-sm text-gray-700"> |
| 88 | + Page |
| 89 | + <select |
| 90 | + value={currentPage} |
| 91 | + onChange={(e) => handlePageChange(Number(e.target.value))} |
| 92 | + className="border border-gray-300 rounded px-2 py-1 text-sm" |
| 93 | + > |
| 94 | + {(() => { |
| 95 | + const rangeSize = 5; |
| 96 | + const half = Math.floor(rangeSize / 2); |
| 97 | + let start = Math.max(1, currentPage - half); |
| 98 | + let end = Math.min(totalPages, start + rangeSize - 1); |
| 99 | + |
| 100 | + if (end - start + 1 < rangeSize && start > 1) { |
| 101 | + start = Math.max(1, end - rangeSize + 1); |
| 102 | + } |
| 103 | + |
| 104 | + return Array.from( |
| 105 | + { length: end - start + 1 }, |
| 106 | + (_, i) => start + i, |
| 107 | + ).map((page) => ( |
| 108 | + <option key={page} value={page}> |
| 109 | + {page} |
| 110 | + </option> |
| 111 | + )); |
| 112 | + })()} |
| 113 | + </select> |
| 114 | + of {totalPages} |
| 115 | + </div> |
| 116 | + |
| 117 | + <Button |
| 118 | + size="sm" |
| 119 | + variant="outline" |
| 120 | + disabled={currentPage === totalPages} |
| 121 | + onClick={() => handlePageChange(currentPage + 1)} |
| 122 | + > |
| 123 | + Next |
| 124 | + </Button> |
| 125 | + </div> |
| 126 | + |
| 127 | + {showAnimation && ( |
| 128 | + <div className="mt-2"> |
| 129 | + <AuditProgressAnimation |
| 130 | + championName={winnerName} |
| 131 | + isValid={isValid} |
| 132 | + onClose={() => { |
| 133 | + setShowAnimation(false); |
| 134 | + setTimeout(() => { |
| 135 | + animationSpacerRef.current?.scrollIntoView({ |
| 136 | + behavior: "smooth", |
| 137 | + block: "start", |
| 138 | + }); |
| 139 | + }, 100); |
| 140 | + }} |
| 141 | + /> |
| 142 | + </div> |
| 143 | + )} |
| 144 | + |
| 145 | + <div ref={animationSpacerRef} className="h-1" /> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ); |
| 149 | +}; |
| 150 | + |
| 151 | +export default AssertionTable; |
0 commit comments