import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Card, CardContent } from "@/components/ui/card"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Loader2, Download } from "lucide-react"; import AiTipBanner from "@/components/ai/AiTipBanner"; import AiReportNarrative from "@/components/ai/AiReportNarrative"; import { reportsService, RecordRow } from "@/services/reports.service"; type Period = "all" | "day" | "week" | "month"; export default function RecordPage() { const [entityId, setEntityId] = useState("all"); const [userId, setUserId] = useState("all"); const [period, setPeriod] = useState("all"); const { data: filters } = useQuery({ queryKey: ["reports-filters"], queryFn: () => reportsService.filters(), }); const { data, isLoading } = useQuery({ queryKey: ["record", entityId, userId, period], queryFn: () => reportsService.record({ entity_id: entityId === "all" ? undefined : Number(entityId), user_id: userId === "all" ? undefined : Number(userId), period: period === "all" ? undefined : period, size: 100, }), }); const records: RecordRow[] = data?.items ?? []; const exportCsv = () => { const header = [ "Student", "Assignment", "Exam", "Date", "Score", "Duration", "Status", ]; const lines = records.map((r) => [ r.student_name, r.assignment, r.exam_code || r.exam, r.date ?? "", r.score ?? "", r.duration, r.status_label, ] .map((v) => `"${String(v).replace(/"/g, '""')}"`) .join(","), ); const blob = new Blob([[header.join(","), ...lines].join("\n")], { type: "text/csv", }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `records-${new Date().toISOString().slice(0, 10)}.csv`; a.click(); URL.revokeObjectURL(url); }; const statusVariant = (status: string) => { if (status === "completed" || status === "released" || status === "scored") return "default"; if (status === "in_progress" || status === "scoring") return "secondary"; return "outline"; }; return (

Record

Browse assignment and exam attempt history. {data && ( ({data.total} attempts) )}

{records.length > 0 && ( )}
{(["all", "day", "week", "month"] as Period[]).map((p) => ( ))}
{isLoading ? (
Loading attempts...
) : records.length === 0 ? (
No attempts match these filters.
) : ( Student Assignment Exam Date Score Duration Status {records.map((r) => ( {r.student_name || "—"} {r.assignment || "—"} {r.exam_code} {r.date || "—"} {r.score ? r.score.toFixed(1) : "—"} {r.duration} {r.status_label} ))}
)}
); }