File size: 16,308 Bytes
0c932f9 dbeb485 2b62b0c dbeb485 02ffd8a 9205030 4c7b844 dbeb485 9205030 dbeb485 34f01ec dbeb485 b87cbc7 dbeb485 b87cbc7 dbeb485 0c932f9 9205030 0c932f9 02ffd8a 0c932f9 9205030 0c932f9 4c7b844 0c932f9 02ffd8a 9205030 0c932f9 02ffd8a 0c932f9 dbeb485 b87cbc7 dbeb485 b87cbc7 dbeb485 b87cbc7 b762c97 0c932f9 b87cbc7 dbeb485 9205030 02ffd8a dbeb485 34f01ec 9205030 34f01ec 7faa899 34f01ec b87cbc7 9205030 b87cbc7 0c932f9 b762c97 0c932f9 b762c97 0c932f9 b762c97 9205030 02ffd8a b762c97 9205030 b762c97 02ffd8a b762c97 0c932f9 dbeb485 b87cbc7 4c7b844 9205030 4c7b844 dbeb485 b87cbc7 dbeb485 b87cbc7 02ffd8a b87cbc7 dbeb485 b87cbc7 b762c97 0c932f9 4c7b844 0c932f9 b87cbc7 0c932f9 02ffd8a 0c932f9 02ffd8a 0c932f9 02ffd8a 0c932f9 4c7b844 0c932f9 02ffd8a 0c932f9 dbeb485 0c932f9 4c7b844 dbeb485 b87cbc7 0c932f9 b87cbc7 dbeb485 b87cbc7 dbeb485 b87cbc7 0c932f9 b87cbc7 4c7b844 b87cbc7 4c7b844 b87cbc7 dbeb485 | 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | import { useState, useEffect, useRef, useCallback, memo } from "react";
import { useQuery } from "@tanstack/react-query";
import { Button } from "@pram/ui/components/button";
import { MaterialIcon } from "@/components/ui/MaterialIcon";
import { trpc } from "@/utils/trpc";
import { QuestionInput } from "./QuestionInput";
import { AccentKeyboard } from "./AccentKeyboard";
import { parseFurigana } from "@/lib/furigana";
import type { Question, Package, PackageSection } from "@/lib/types";
import { AttemptHeader } from "./attempt/AttemptHeader";
import { FloatingNav } from "./attempt/FloatingNav";
import { FinishDialog } from "./attempt/FinishDialog";
import { AbandonDialog } from "./attempt/AbandonDialog";
interface AttemptTestViewProps {
attemptId: string;
pkg: Package & { sections: PackageSection[] };
currentSectionIdx: number;
setCurrentSectionIdx: (idx: number) => void;
answers: Record<string, string>;
onAnswerChange: (questionId: string, sectionResultId: string | undefined, value: string) => void;
timeElapsed: number;
answeredCount: number;
totalQuestions: number;
onFinish: () => void;
onAbandon: () => void;
isFinished: boolean;
submittingQId: string | null;
markedQuestions: Set<string>;
toggleMarkQuestion: (questionId: string) => void;
startQuestionTimer: (questionId: string) => void;
}
interface QuestionCardProps {
q: Question & { _examType?: string; _isRtl?: boolean; _useFurigana?: boolean };
globalIdx: number;
answerValue: string;
sectionResultId: string | undefined;
isMarked: boolean;
isFinished: boolean;
isSubmitting: boolean;
onAnswerChange: (questionId: string, sectionResultId: string | undefined, value: string) => void;
toggleMarkQuestion: (questionId: string) => void;
}
const QuestionCard = memo(function QuestionCard({
q,
globalIdx,
answerValue,
sectionResultId,
isMarked,
isFinished,
isSubmitting,
onAnswerChange,
toggleMarkQuestion,
}: QuestionCardProps) {
const handleChange = useCallback(
(val: string) => {
onAnswerChange(q.id, sectionResultId, val);
},
[q.id, sectionResultId, onAnswerChange],
);
const handleAccentInsert = useCallback(
(char: string) => {
onAnswerChange(q.id, sectionResultId, answerValue + char);
},
[q.id, sectionResultId, answerValue, onAnswerChange],
);
const showAccentKeyboard = q.format === "fill_blank" || q.format === "sentence_completion";
return (
<div
id={`question-${q.id}`}
className={`bg-[var(--pure-white)] p-6 md:p-8 rounded-xl shadow-sm border-2 transition-all ${
isMarked ? "border-[var(--pomegranate-400)]" : "border-transparent"
}`}
>
<div className="flex items-center justify-between mb-6">
<div className="flex items-center gap-3">
<span className="bg-[var(--clay-black)] text-[var(--pure-white)] w-8 h-8 flex items-center justify-center rounded-lg font-bold text-sm">
{globalIdx}
</span>
<h2 className="text-xl font-bold text-[var(--clay-black)] capitalize">
{(q.format ?? "").replace(/_/g, " ")}
</h2>
</div>
<button
onClick={() => toggleMarkQuestion(q.id)}
aria-label={isMarked ? "Hapus tanda" : "Tandai untuk review"}
className={`shrink-0 w-10 h-10 flex items-center justify-center rounded-full transition-colors ${
isMarked
? "bg-[var(--pomegranate-400)]/20 text-[var(--pomegranate-400)]"
: "bg-[var(--oat-light)] text-[var(--warm-silver)] hover:text-[var(--pomegranate-400)]"
}`}
title={isMarked ? "Hapus tanda" : "Tandai untuk review"}
>
<MaterialIcon name={isMarked ? "bookmark" : "bookmark_border"} className="text-lg" />
</button>
</div>
<p className="text-[var(--warm-charcoal)] mb-6 font-medium leading-relaxed" dir={q._isRtl ? "rtl" : undefined}>
{q._useFurigana ? parseFurigana(q.questionText ?? "") : (q.questionText ?? "")}
</p>
<div className="pl-0">
{isSubmitting && (
<div className="text-xs text-[var(--matcha-600)] mb-2 flex items-center gap-1">
<MaterialIcon name="sync" className="text-xs animate-spin" />
Menyimpan...
</div>
)}
<QuestionInput
question={q}
value={answerValue}
onChange={handleChange}
disabled={isFinished}
/>
{showAccentKeyboard && (
<div className="mt-3">
<AccentKeyboard
examType={q._examType}
onInsert={handleAccentInsert}
disabled={isFinished}
/>
</div>
)}
</div>
</div>
);
});
export function AttemptTestView({
attemptId,
pkg,
currentSectionIdx,
setCurrentSectionIdx,
answers,
onAnswerChange,
timeElapsed,
answeredCount,
totalQuestions,
onFinish,
onAbandon,
isFinished,
submittingQId,
markedQuestions,
toggleMarkQuestion,
}: AttemptTestViewProps) {
const [showFinishDialog, setShowFinishDialog] = useState(false);
const [showAbandonDialog, setShowAbandonDialog] = useState(false);
const [activeQuestionId, setActiveQuestionId] = useState<string | null>(null);
const questionPanelRef = useRef<HTMLDivElement>(null);
const navSliderRef = useRef<HTMLDivElement>(null);
const hasInitRef = useRef(false);
const attemptQuery = useQuery(
trpc.attempt.getById.queryOptions(
{ id: attemptId },
{ enabled: !!attemptId },
),
);
const attempt = attemptQuery.data;
const currentSection = pkg.sections[currentSectionIdx];
const sectionData = attempt?.sections?.[currentSectionIdx];
const sectionResultId = sectionData?.sectionResultId;
const examType: string = pkg.examTypeName ?? "";
const isRtl = examType === "TOAFL";
const useFurigana = examType === "JLPT" || examType === "TOPIK";
// If user picks an answer before attempt.getById finishes, persist once sectionResultId exists
const flushKeyRef = useRef<string | null>(null);
useEffect(() => {
if (!sectionResultId || isFinished || !currentSection?.questions?.length) return;
const key = `${currentSectionIdx}:${sectionResultId}`;
if (flushKeyRef.current === key) return;
flushKeyRef.current = key;
for (const q of currentSection.questions ?? []) {
const v = answers[q.id];
if (v) void onAnswerChange(q.id, sectionResultId, v);
}
/* flush once per section when ids align; `answers` read from committing render */
}, [sectionResultId, currentSectionIdx, isFinished, currentSection?.questions?.length, onAnswerChange]);
if (!currentSection) {
return (
<div className="min-h-screen flex items-center justify-center bg-[var(--warm-cream)]">
<p className="text-[var(--warm-charcoal)]">Section tidak ditemukan.</p>
</div>
);
}
// Build global question index across all sections
const allQuestions: Array<{ id: string; sectionIdx: number; localIdx: number; passageText?: string }> = [];
pkg.sections.forEach((sec: PackageSection, sIdx: number) => {
sec.questions?.forEach((q: Question, qIdx: number) => {
allQuestions.push({ id: q.id, sectionIdx: sIdx, localIdx: qIdx, passageText: q.passageText ?? undefined });
});
});
// Initialize active question once on mount
useEffect(() => {
if (!hasInitRef.current && allQuestions.length > 0) {
hasInitRef.current = true;
setActiveQuestionId(allQuestions[0].id);
}
}, [allQuestions]);
const addQuestionMeta = (q: Question) => ({
...q,
_examType: examType,
_isRtl: isRtl,
_useFurigana: useFurigana,
});
const activeQuestion = currentSection?.questions?.find(
(q: Question) => q.id === activeQuestionId,
);
const activeQuestionWithMeta = activeQuestion ? addQuestionMeta(activeQuestion) : null;
const passageToShow =
activeQuestion?.passageText ??
currentSection?.questions?.[0]?.passageText ??
"Tidak ada bacaan tambahan untuk section ini.";
const activeGlobalIdx = activeQuestionId
? allQuestions.findIndex((q) => q.id === activeQuestionId) + 1
: 0;
const isFirstQuestion = activeGlobalIdx === 1;
const isLastQuestion = activeGlobalIdx === allQuestions.length;
// Navigation helpers
const goToQuestion = useCallback(
(qId: string | null) => {
if (!qId) return;
const target = allQuestions.find((q) => q.id === qId);
if (!target) return;
setCurrentSectionIdx(target.sectionIdx);
setActiveQuestionId(qId);
// Scroll question panel to top when changing question
setTimeout(() => {
questionPanelRef.current?.scrollTo({ top: 0, behavior: "smooth" });
}, 50);
// Scroll nav slider to keep active button visible
setTimeout(() => {
const btn = navSliderRef.current?.querySelector(`[data-qid="${qId}"]`) as HTMLElement | null;
btn?.scrollIntoView({ behavior: "smooth", inline: "center", block: "nearest" });
}, 100);
},
[allQuestions, setCurrentSectionIdx],
);
const goToPrevQuestion = useCallback(() => {
if (!activeQuestionId) return;
const currentIdx = allQuestions.findIndex((q) => q.id === activeQuestionId);
if (currentIdx > 0) {
goToQuestion(allQuestions[currentIdx - 1].id);
}
}, [activeQuestionId, allQuestions, goToQuestion]);
const goToNextQuestion = useCallback(() => {
if (!activeQuestionId) return;
const currentIdx = allQuestions.findIndex((q) => q.id === activeQuestionId);
if (currentIdx >= 0 && currentIdx < allQuestions.length - 1) {
goToQuestion(allQuestions[currentIdx + 1].id);
}
}, [activeQuestionId, allQuestions, goToQuestion]);
return (
<>
<style>{`
.custom-scrollbar::-webkit-scrollbar { width: 6px; height: 6px; }
.custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: var(--oat-border); border-radius: 10px; }
.hide-scrollbar::-webkit-scrollbar { display: none; }
.hide-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
`}</style>
<div className="h-full flex flex-col bg-[var(--warm-cream)] overflow-hidden">
<AttemptHeader
pkgTitle={pkg.title ?? ""}
currentSectionTitle={currentSection.title ?? ""}
timeElapsed={timeElapsed}
answeredCount={answeredCount}
totalQuestions={totalQuestions}
isFinished={isFinished}
onAbandon={() => setShowAbandonDialog(true)}
onFinish={() => setShowFinishDialog(true)}
/>
{/* Main Exam Workspace */}
<main className="flex-1 flex overflow-hidden flex-col lg:flex-row">
{/* Left Column: Reading Passage */}
<section className="w-full lg:w-1/2 bg-[var(--pure-white)] overflow-y-auto custom-scrollbar p-6 md:p-12 border-b lg:border-b-0 lg:border-r border-[var(--oat-border)] h-1/2 lg:h-full">
<article className="max-w-2xl mx-auto">
<header className="mb-8">
<h1 className="text-3xl font-extrabold text-[var(--clay-black)] mb-4 leading-tight">
{currentSection.title}
</h1>
<div className="flex gap-4 text-sm text-[var(--warm-silver)] italic">
<span>Section {currentSectionIdx + 1} dari {pkg.sections.length}</span>
</div>
</header>
<div className="space-y-6 text-lg leading-relaxed text-[var(--warm-charcoal)] font-body whitespace-pre-wrap" dir={isRtl ? "rtl" : undefined}>
{useFurigana ? parseFurigana(passageToShow) : passageToShow}
</div>
</article>
</section>
{/* Right Column: Question Panel */}
<section ref={questionPanelRef} className="w-full lg:w-1/2 bg-[var(--warm-cream)] overflow-y-auto custom-scrollbar p-6 md:p-10 relative h-1/2 lg:h-full">
<div className="max-w-2xl mx-auto pb-32">
{/* Top bar: question counter + Selesai button */}
<div className="flex items-center justify-between mb-6">
<div className="flex items-center gap-3">
<span className="text-sm font-bold text-[var(--warm-charcoal)] bg-[var(--oat-light)] px-3 py-1.5 rounded-lg">
Soal {activeGlobalIdx} / {totalQuestions}
</span>
{markedQuestions.has(activeQuestionId ?? "") && (
<span className="flex items-center gap-1 text-xs font-semibold text-[var(--pomegranate-400)] bg-[var(--pomegranate-400)]/10 px-2 py-1 rounded-full">
<MaterialIcon name="bookmark" className="text-xs" />
Ditandai
</span>
)}
</div>
<Button
onClick={() => setShowFinishDialog(true)}
disabled={isFinished}
className="flex items-center gap-2 bg-[var(--matcha-600)] text-[var(--pure-white)] hover:bg-[var(--matcha-700)] px-4 py-2 rounded-xl text-sm font-bold shadow-sm transition-all"
>
Selesai
<MaterialIcon name="check_circle" className="text-sm" />
</Button>
</div>
{/* Active question only */}
{activeQuestionWithMeta ? (
<QuestionCard
key={activeQuestionWithMeta.id}
q={activeQuestionWithMeta}
globalIdx={activeGlobalIdx}
answerValue={answers[activeQuestionWithMeta.id] ?? ""}
sectionResultId={sectionResultId}
isMarked={markedQuestions.has(activeQuestionWithMeta.id)}
isFinished={isFinished}
isSubmitting={submittingQId === activeQuestionWithMeta.id}
onAnswerChange={onAnswerChange}
toggleMarkQuestion={toggleMarkQuestion}
/>
) : (
<div className="text-center py-20">
<p className="text-[var(--warm-charcoal)]">Tidak ada soal aktif.</p>
</div>
)}
{/* Prev / Next Navigation */}
<nav className="flex justify-between items-center pt-8" aria-label="Navigasi soal sebelumnya dan berikutnya">
<Button
variant="ghost"
onClick={goToPrevQuestion}
disabled={isFirstQuestion}
className="flex items-center gap-2 text-[var(--clay-black)] font-bold px-6 py-3 rounded-xl hover:bg-[var(--clay-black)]/5 transition-all disabled:opacity-50"
>
<MaterialIcon name="arrow_back" />
Sebelumnya
</Button>
<Button
onClick={goToNextQuestion}
disabled={isLastQuestion}
className="flex items-center gap-2 bg-[var(--clay-black)] text-[var(--pure-white)] font-bold px-8 py-3 rounded-xl shadow-lg hover:bg-[var(--warm-charcoal)] transition-all disabled:opacity-50"
>
Selanjutnya
<MaterialIcon name="arrow_forward" />
</Button>
</nav>
</div>
</section>
</main>
<FloatingNav
questions={allQuestions}
activeQuestionId={activeQuestionId}
answers={answers}
markedQuestions={markedQuestions}
onGoToQuestion={goToQuestion}
onScrollToTop={() => questionPanelRef.current?.scrollTo({ top: 0, behavior: "smooth" })}
navSliderRef={navSliderRef}
/>
</div>
<FinishDialog
open={showFinishDialog}
onClose={() => setShowFinishDialog(false)}
onConfirm={() => {
setShowFinishDialog(false);
onFinish();
}}
answeredCount={answeredCount}
totalQuestions={totalQuestions}
/>
<AbandonDialog
open={showAbandonDialog}
onClose={() => setShowAbandonDialog(false)}
onConfirm={() => {
setShowAbandonDialog(false);
onAbandon();
}}
/>
</>
);
}
|