"use client" import { useMemo } from "react" import { Badge } from "@/shared/components/ui/badge" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardHeader } from "@/shared/components/ui/card" import { Label } from "@/shared/components/ui/label" import { ScrollArea } from "@/shared/components/ui/scroll-area" import { FileText, ChevronLeft } from "lucide-react" import Link from "next/link" import { useTranslations } from "next-intl" import type { StudentHomeworkTakeData } from "../types" import { QuestionRenderer } from "./question-renderer" import { getCorrectnessState, parseSavedAnswer, } from "../lib/question-content-utils" type HomeworkReviewViewProps = { initialData: StudentHomeworkTakeData } export function HomeworkReviewView({ initialData }: HomeworkReviewViewProps) { const t = useTranslations("examHomework") const submissionStatus = initialData.submission?.status ?? "not_started" const isGraded = submissionStatus === "graded" const answersByQuestionId = useMemo(() => { const map = new Map() for (const q of initialData.questions) { map.set(q.questionId, parseSavedAnswer(q.savedAnswer, q.questionType)) } const obj: Record = {} for (const [k, v] of map.entries()) obj[k] = v return obj }, [initialData.questions]) return (

{isGraded ? t("homework.review.gradedReport") : t("homework.review.submissionDetails")}

{submissionStatus} {initialData.questions.length} {t("homework.review.questionsUnit")}
{initialData.questions.map((q, idx) => { const value = answersByQuestionId[q.questionId]?.answer const correctness = isGraded ? getCorrectnessState({ score: q.score ?? null, maxScore: q.maxScore }) : "ungraded" const borderClass = correctness === "correct" ? "border-l-4 border-l-emerald-500" : correctness === "incorrect" ? "border-l-4 border-l-red-500" : correctness === "partial" ? "border-l-4 border-l-yellow-500" : "border-l-4 border-l-primary" return ( {q.score} / {q.maxScore} ) : null } /> {q.knowledgePoints && q.knowledgePoints.length > 0 && (
{q.knowledgePoints.map((kp) => ( {kp.name} ))}
)}
) })}

{t("homework.take.assignmentInfo")}

{submissionStatus}

{initialData.assignment.description || t("homework.review.noDescription")}

{isGraded && (
{initialData.submission?.score ?? 0} / {initialData.questions.reduce((acc, q) => acc + q.maxScore, 0)}
)}
{initialData.questions.map((q, i) => { const answer = answersByQuestionId[q.questionId]?.answer const hasAnswer = answer !== undefined && answer !== "" && (Array.isArray(answer) ? answer.length > 0 : true) const score = q.score ?? 0 const max = q.maxScore let statusClass = "bg-background text-muted-foreground border-input" if (isGraded) { if (score === max && max > 0) statusClass = "bg-emerald-600 text-white border-emerald-600" else if (score > 0) statusClass = "bg-yellow-500 text-white border-yellow-500" else statusClass = "bg-red-500 text-white border-red-500" } else if (hasAnswer) { statusClass = "bg-primary text-primary-foreground border-primary" } return (
{i + 1}
) })}
) }