import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; import { getQueryParams } from '@/lib/route'; import { cn } from '@/lib/utils'; import { Link, router, usePage } from '@inertiajs/react'; import { ColumnDef } from '@tanstack/react-table'; import { Award, ChevronsUpDown } from 'lucide-react'; const ExamAttemptColumn = (examId: number | string, bestAttemptId?: number | string): ColumnDef[] => { const page = usePage(); const urlParams = getQueryParams(page.url); const statuses = ['all', 'in_progress', 'completed', 'abandoned', 'submitted']; return [ { accessorKey: 'attempt_number', header: ({ column }) => { return (
Attempt
); }, cell: ({ row }) => (

Attempt #{row.getValue('attempt_number')}

{bestAttemptId === row.original.id && ( Best )}

{new Date(row.original.start_time).toLocaleDateString()} • {new Date(row.original.start_time).toLocaleTimeString()}

), }, { accessorKey: 'obtained_marks', header: () =>
Score
, cell: ({ row }) => { const bestScore = row.original && Number(row.original.total_marks) > 0 ? Math.round((Number(row.original.obtained_marks) / Number(row.original.total_marks)) * 100 * 100) / 100 : 0; return (

{row.getValue('obtained_marks')}/{row.original.total_marks}

{bestScore}%

); }, }, { accessorKey: 'status', header: ({ column }) => (
{statuses.map((status) => ( router.get( route('student.exam.show', { id: examId, tab: 'attempts', status: status, }), ) } className={cn('cursor-pointer text-center capitalize', urlParams['status'] === status && 'bg-gray-100')} > {status.replace('_', ' ')} ))}
), cell: ({ row }) => { const status = row.getValue('status') as string; const variant = status === 'completed' ? 'secondary' : status === 'in_progress' ? 'default' : 'destructive'; return (
{status.replace('_', ' ')}
); }, }, { accessorKey: 'correct_answers', header: () =>
Answers
, cell: ({ row }) => (

{row.getValue('correct_answers')} /{' '} {row.original.incorrect_answers}

), }, { accessorKey: 'end_time', header: 'Duration', cell: ({ row }) => { const startTime = row.original.start_time; const endTime = row.getValue('end_time') as string; if (!endTime) { return
; } const start = new Date(startTime); const end = new Date(endTime); const diffMinutes = Math.max(0, Math.round((end.getTime() - start.getTime()) / 60000)); return (

{diffMinutes} min

); }, }, { id: 'actions', header: () =>
Actions
, cell: ({ row }) => { const attempt = row.original; return (
{attempt.status === 'completed' ? ( ) : ( )} {attempt.status === 'in_progress' || (attempt.status === 'abandoned' && ( ))}
); }, }, ]; }; export default ExamAttemptColumn;