File size: 18,431 Bytes
66de9c8
89250ee
 
66de9c8
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66de9c8
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66de9c8
0649f1b
 
66de9c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0649f1b
66de9c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0649f1b
 
 
 
 
89250ee
0649f1b
89250ee
 
 
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66de9c8
0649f1b
 
66de9c8
 
 
 
 
 
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66de9c8
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89250ee
0649f1b
 
 
 
 
 
 
 
 
 
 
 
89250ee
0649f1b
 
 
 
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
import { useMemo, useState, type ReactNode } from "react";
import type { GenerateBody } from "../api/endpoints";
import type { Artifact, ChatSummary, QuizQuestion, QuizQuestionType } from "../api/types";
import { ArtifactFeedback } from "./ArtifactFeedback";
import { CheckIcon, ChevronIcon } from "./icons/Icons";
import { Pills, SourceFocusFields, sourceSummary, type SourceSelection } from "./SourceFocusFields";
import { StudyOverlay } from "./StudyOverlay";
import { Modal } from "./ui";

const LEVELS = [
  { id: "easy", label: "Easy" },
  { id: "medium", label: "Medium (Default)" },
  { id: "hard", label: "Hard" },
] as const;
const TYPES: { id: QuizQuestionType; label: string }[] = [
  { id: "multiple_choice", label: "Multiple Choice" },
  { id: "fill_blank", label: "Fill in the Blank" },
  { id: "short_answer", label: "Short Answer" },
];

export type QuizRequest = Omit<GenerateBody, "type">;

/** The quiz settings, laid out like the video wizard's first step: what it is made from, what it should focus on, then
 * how it is asked. */
export function QuizDialog({ notebookId, notebookTitle, chats, currentChatId, busy, error, onClose, onCreate }: {
  notebookId: string;
  notebookTitle?: string;
  chats: ChatSummary[];
  /** The chat open beside the dialog, ticked to start with. */
  currentChatId: string | null;
  busy: boolean;
  error: string | null;
  onClose: () => void;
  onCreate: (body: QuizRequest) => void;
}) {
  const [sel, setSel] = useState<SourceSelection | null>(null);
  const [level, setLevel] = useState<(typeof LEVELS)[number]["id"]>("medium");
  const [types, setTypes] = useState<Set<QuizQuestionType>>(new Set(["multiple_choice"]));

  const toggleType = (t: QuizQuestionType) =>
    setTypes((cur) => {
      const next = new Set(cur);
      if (next.has(t)) next.delete(t);
      else next.add(t);
      return next.size ? next : cur; // at least one stays on
    });

  const submit = () =>
    sel && onCreate({
      ...sel.request,
      difficulty: level,
      question_types: TYPES.map((t) => t.id).filter((t) => types.has(t)),
    });

  return (
    <Modal title="Quiz" onClose={onClose} wide>
      <div className="stack vw-in-modal">
        <section className="stack vw qz-setup">
          <div className="field">
            <span className="vw-label">Level of difficulty</span>
            <Pills label="Level of difficulty" value={level} options={LEVELS} onChange={setLevel} />
          </div>
          <div className="field">
            <span className="vw-label">Question types</span>
            <div className="vw-chips">
              {TYPES.map((t) => (
                <button key={t.id} type="button" className={`vw-chip${types.has(t.id) ? " on" : ""}`}
                        aria-pressed={types.has(t.id)} onClick={() => toggleType(t.id)}>
                  {types.has(t.id) && <CheckIcon size={14} />} {t.label}
                </button>
              ))}
            </div>
          </div>
          <SourceFocusFields notebookId={notebookId} notebookTitle={notebookTitle} chats={chats} currentChatId={currentChatId}
                             preselectChat what="quiz" largeSelection onChange={setSel} />
          <div className="qz-foot">
            <div>
              {!sel?.ready && <p className="muted small">Select at least one post or chat to generate a quiz.</p>}
              {error && <p className="error-text">{error}</p>}
            </div>
            {/* Waits for the focus cards (shown, or none to show), so they are never skipped past */}
            <button type="button" className="btn btn-primary vw-next" disabled={busy || !sel?.ready || sel.loading} onClick={submit}>
              {busy ? "Starting..." : "Generate Quiz"}
            </button>
          </div>
        </section>
      </div>
    </Modal>
  );
}

/* ---------- Taking a quiz ---------- */

const norm = (s: string) => s.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, "").replace(/\s+/g, " ").trim();

type Answer = { choice: number[]; text: string; checked: boolean; correct: boolean | null };
const blank = (): Answer => ({ choice: [], text: "", checked: false, correct: null });

/** Whether a choice or fill-in answer is right. A short answer has no automatic grade: the reader marks it. */
function grade(q: QuizQuestion, a: Answer): boolean | null {
  if (q.type === "short_answer") return null;
  if (q.type === "fill_blank") {
    const given = norm(a.text);
    return !!given && [q.answer ?? "", ...(q.accepted ?? [])].some((x) => norm(x) === given);
  }
  const right = [...(q.correct ?? [])].sort().join(",");
  return [...a.choice].sort().join(",") === right;
}

const TYPE_LABEL: Record<QuizQuestionType, string> = {
  multiple_choice: "Multiple choice",
  multiple_select: "Select all that apply",
  fill_blank: "Fill in the blank",
  short_answer: "Short answer",
};

/** How the score reads: a medal for a strong result, plain encouragement for a weak one. */
function resultTier(pct: number): { tone: "gold" | "silver" | "bronze" | "low"; title: string; note: string } {
  if (pct >= 90) return { tone: "gold", title: "Outstanding!", note: "You know this material inside out." };
  if (pct >= 70) return { tone: "silver", title: "Great job!", note: "A solid grasp. Review the ones you missed." };
  if (pct >= 50) return { tone: "bronze", title: "Good effort", note: "You are getting there. Another pass will help." };
  return { tone: "low", title: "Back to the launchpad", note: "Read the material again, then relaunch." };
}

const STAR_PATH = "m0-9 2.6 5.6 6.1.7-4.5 4.2 1.2 6L0 4.2-5.4 7.5l1.2-6-4.5-4.2 6.1-.7z";
const ROCKET_PATH = "M0-11c4 2.700 6 7 6 11.500v6H-6v-6c0-4.500 2-8.800 6-11.500zM-6 1l-3.500 4v4L-6 7M6 1l3.500 4v4L6 7";

/** The result as a small galaxy: a medal-planet with a ring and a moon, one star per question on a wide orbit around it
 * (lit for each right answer), and a field of stars behind. The colours come from the tone (see .quiz-hero in the CSS). */
export function ResultScene({ score, total, tone }: { score: number; total: number; tone: string }) {
  const n = Math.min(Math.max(total, 1), 14);
  const lit = Math.round((score / Math.max(total, 1)) * n);
  const orbit = Array.from({ length: n }, (_, i) => {
    const a = Math.PI + (i / n) * Math.PI * 2;
    return { i, x: 200 + Math.cos(a) * 168, y: 90 + Math.sin(a) * 40, front: Math.sin(a) > 0 };
  });
  const field = (() => {
    let seed = 7;
    const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
    return Array.from({ length: 46 }, (_, k) => ({ x: rnd() * 400, y: rnd() * 180, r: 0.5 + rnd() * 1.1, d: (rnd() * 4).toFixed(2), big: k % 11 === 0 }));
  })();
  const dot = (p: (typeof orbit)[number]) => (
    <g key={p.i} className={p.i < lit ? "orb lit" : "orb"} style={{ animationDelay: `${p.i * 0.15}s` }}>
      {p.i < lit && <circle className="orb-halo" cx={p.x} cy={p.y} r="8" />}
      <circle cx={p.x} cy={p.y} r={p.i < lit ? 3 : 1.8} />
    </g>
  );
  return (
    <svg className="quiz-scene" viewBox="0 0 400 180" aria-hidden="true">
      <defs>
        <radialGradient id="qs-planet" cx="34%" cy="28%" r="80%">
          <stop offset="0" style={{ stopColor: "var(--c1)" }} />
          <stop offset="0.5" style={{ stopColor: "var(--c2)" }} />
          <stop offset="1" style={{ stopColor: "var(--c3)" }} />
        </radialGradient>
        <radialGradient id="qs-glow" cx="50%" cy="50%" r="50%">
          <stop offset="0" style={{ stopColor: "var(--c2)", stopOpacity: 0.45 }} />
          <stop offset="1" style={{ stopColor: "var(--c2)", stopOpacity: 0 }} />
        </radialGradient>
        <clipPath id="qs-clip"><circle cx="200" cy="90" r="40" /></clipPath>
      </defs>
      {field.map((f, k) => f.big ? (
        <path key={k} className="qs-spark" d="M0-5 1-1 5 0 1 1 0 5-1 1-5 0-1-1z" transform={`translate(${f.x} ${f.y})`} style={{ animationDelay: `${f.d}s` }} />
      ) : (
        <circle key={k} className="qs-star" cx={f.x} cy={f.y} r={f.r} style={{ animationDelay: `${f.d}s` }} />
      ))}
      <circle cx="200" cy="90" r="84" fill="url(#qs-glow)" />
      <g transform="rotate(-12 200 90)">
        <ellipse className="qs-orbit" cx="200" cy="90" rx="168" ry="40" />
        {orbit.filter((p) => !p.front).map(dot)}
        <path className="qs-ring back" d="M118 90a82 19 0 0 1 164 0" />
        <circle cx="200" cy="90" r="40" fill="url(#qs-planet)" />
        <g clipPath="url(#qs-clip)" className="qs-bands">
          <path d="M150 74q50 -9 100 0M150 104q50 10 100 0" />
          <ellipse cx="228" cy="112" rx="34" ry="30" className="qs-shade" />
        </g>
        <g transform="translate(200 90) rotate(12)" className="qs-emblem">
          {tone === "low" ? <path d={ROCKET_PATH} transform="scale(1.15)" /> : <path d={STAR_PATH} transform="scale(1.35)" />}
        </g>
        <path className="qs-ring front" d="M118 90a82 19 0 0 0 164 0" />
        {orbit.filter((p) => p.front).map(dot)}
        <circle className="qs-moon" r="5">
          <animateMotion dur="11s" repeatCount="indefinite" path="M368 90a168 40 0 1 0 -336 0a168 40 0 1 0 336 0" />
        </circle>
      </g>
    </svg>
  );
}

/** The quiz itself, without a frame: in a modal (QuizPlayer) or inside a report. `onExit` adds a Close button. */
export function QuizRunner({ questions, onExit, feedback }: { questions: QuizQuestion[]; onExit?: () => void; feedback?: ReactNode }) {
  const [i, setI] = useState(0);
  const [answers, setAnswers] = useState<Answer[]>(() => questions.map(blank));
  const [done, setDone] = useState(false);
  const [finished, setFinished] = useState(false); // the results have been reached: a question opened from them leads back to them
  const [detail, setDetail] = useState(false); // the per-question results, under the score

  const q = questions[i];
  const a = answers[i];
  const patch = (p: Partial<Answer>) => setAnswers((all) => all.map((x, k) => (k === i ? { ...x, ...p } : x)));
  const score = answers.filter((x) => x.correct).length;

  const restart = () => {
    setAnswers(questions.map(blank));
    setI(0);
    setDone(false);
    setFinished(false);
  };

  if (!q) return <p className="muted">This quiz has no questions.</p>;

  if (done) {
    const total = questions.length;
    const pct = total ? Math.round((score / total) * 100) : 0;
    const t = resultTier(pct);
    return (
      <>
        <div className="quiz-result">
          <div className={`quiz-hero ${t.tone}`}>
            <ResultScene score={score} total={total} tone={t.tone} />
            <p className="quiz-verdict">{t.title}</p>
            <p className="quiz-score">{score} <span>/ {total}</span></p>
            <p className="quiz-note muted">{t.note}</p>
            <div className="quiz-chips">
              <span className="quiz-chip pct">{pct}%</span>
              <span className="quiz-chip right">{score} correct</span>
              <span className="quiz-chip wrong">{total - score} to review</span>
            </div>
          </div>

          <button type="button" className={`quiz-detail-toggle${detail ? " open" : ""}`} aria-expanded={detail} onClick={() => setDetail(!detail)}>
            View results in depth
            <ChevronIcon size={16} className={`chevron${detail ? " open" : ""}`} />
          </button>
          {detail && (
            <ol className="quiz-review">
              {questions.map((x, k) => (
                <li key={k} className={answers[k].correct ? "right" : "wrong"}>
                  <button type="button" onClick={() => { setI(k); setDone(false); }} title={x.question}>
                    <span className="quiz-review-mark" aria-hidden="true">
                      {answers[k].correct ? <CheckIcon size={14} /> : (
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M6 6l12 12M18 6L6 18" /></svg>
                      )}
                    </span>
                    <span className="quiz-review-n mono">{k + 1}</span>
                    <span className="quiz-review-q">{x.question}</span>
                  </button>
                </li>
              ))}
            </ol>
          )}

          {feedback}
          <div className="row end">
            {onExit && <button type="button" className="btn" onClick={onExit}>Close</button>}
            <button type="button" className="btn btn-primary" onClick={restart}>Retake</button>
          </div>
        </div>
      </>
    );
  }

  const last = i === questions.length - 1;
  const choiceDone = q.type === "multiple_choice" || q.type === "multiple_select";
  const canCheck = choiceDone ? a.choice.length > 0 : a.text.trim().length > 0;

  const check = () => patch({ checked: true, correct: grade(q, a) });
  const next = () => {
    if (!last) return setI(i + 1);
    setFinished(true);
    setDone(true);
  };
  const pick = (k: number) => {
    if (a.checked) return;
    if (q.type === "multiple_choice") patch({ choice: [k] });
    else patch({ choice: a.choice.includes(k) ? a.choice.filter((x) => x !== k) : [...a.choice, k] });
  };

  return (
    <>
      <div className="quiz-play">
        <div className="quiz-progress" aria-label={`Question ${i + 1} of ${questions.length}`}>
          <span style={{ width: `${((i + (a.checked ? 1 : 0)) / questions.length) * 100}%` }} />
        </div>
        <div className="quiz-meta">
          <p className="mono muted small">Question {i + 1} of {questions.length} 路 {TYPE_LABEL[q.type]}</p>
          <p className="quiz-count" aria-label={`${score} correct out of ${questions.length}`} title="Correct so far">
            <CheckIcon size={16} /> <strong>{score}</strong> / {questions.length}
          </p>
        </div>
        <h3 className="quiz-q">{q.question}</h3>

        {choiceDone && (
          <ul className="quiz-options">
            {(q.options ?? []).map((opt, k) => {
              const on = a.choice.includes(k);
              const right = (q.correct ?? []).includes(k);
              const cls = a.checked ? (right ? " right" : on ? " wrong" : "") : on ? " on" : "";
              return (
                <li key={k}>
                  <button type="button" className={`quiz-opt${cls}`} aria-pressed={on} disabled={a.checked} onClick={() => pick(k)}>
                    <span className={`quiz-mark${q.type === "multiple_select" ? " square" : ""}`} aria-hidden="true">{(a.checked ? right : on) && <CheckIcon size={14} />}</span>
                    <span>{opt}</span>
                  </button>
                </li>
              );
            })}
          </ul>
        )}

        {q.type === "fill_blank" && (
          <input className="input" value={a.text} disabled={a.checked} placeholder="Your answer" aria-label="Your answer"
                 onChange={(e) => patch({ text: e.target.value })}
                 onKeyDown={(e) => e.key === "Enter" && canCheck && !a.checked && check()} />
        )}
        {q.type === "short_answer" && (
          <textarea className="input quiz-short" rows={4} value={a.text} disabled={a.checked} placeholder="Write your answer"
                    aria-label="Your answer" onChange={(e) => patch({ text: e.target.value })} />
        )}

        {a.checked && (
          <div className={`quiz-feedback${a.correct ? " right" : a.correct === false ? " wrong" : ""}`}>
            {q.type !== "short_answer" && <strong>{a.correct ? "Correct" : "Not quite"}</strong>}
            {(q.type === "fill_blank" || q.type === "short_answer") && (
              <p><span className="muted">{q.type === "fill_blank" ? "Answer: " : "Model answer: "}</span>{q.answer}</p>
            )}
            {q.explanation && <p className="muted">{q.explanation}</p>}
            {q.sources?.length > 0 && (
              <p className="mono muted small">
                {q.sources.slice(0, 2).map((s) => `${s.title}, lines ${s.line_start}${s.line_end > s.line_start ? ` to ${s.line_end}` : ""}`).join(" 路 ")}
              </p>
            )}
            {q.type === "short_answer" && a.correct === null && (
              <div className="row">
                <span className="muted small">Did you get it?</span>
                <button type="button" className="btn btn-small" onClick={() => patch({ correct: true })}>Yes</button>
                <button type="button" className="btn btn-small" onClick={() => patch({ correct: false })}>Not quite</button>
              </div>
            )}
          </div>
        )}

        <div className="row end">
          {i > 0 && <button type="button" className="btn" onClick={() => setI(i - 1)}>Back</button>}
          {!a.checked ? (
            <button type="button" className="btn btn-primary" disabled={!canCheck} onClick={check}>
              {q.type === "short_answer" ? "Show answer" : "Check answer"}
            </button>
          ) : finished ? (
            <button type="button" className="btn btn-primary" disabled={q.type === "short_answer" && a.correct === null} onClick={() => setDone(true)}>
              Go to results
            </button>
          ) : (
            <button type="button" className="btn btn-primary" disabled={q.type === "short_answer" && a.correct === null} onClick={next}>
              {last ? "See results" : "Next"}
            </button>
          )}
        </div>
      </div>
    </>
  );
}

export function QuizPlayer({ artifact, onClose }: { artifact: Artifact; onClose: () => void }) {
  const questions = useMemo(() => (artifact.content.questions ?? []) as QuizQuestion[], [artifact]);
  return (
    <StudyOverlay title={artifact.title} onClose={onClose}>
      <QuizRunner questions={questions} onExit={onClose} feedback={<ArtifactFeedback artifact={artifact} label="How was this quiz?" />} />
    </StudyOverlay>
  );
}

/** What a finished quiz shows in a card: its size and a way in. */
export function QuizBody({ artifact }: { artifact: Artifact }) {
  const [playing, setPlaying] = useState(false);
  const c = artifact.content;
  const from = sourceSummary(c.source);
  return (
    <div className="artifact-body">
      <p className="muted">{c.question_count ?? 0} questions 路 {c.difficulty ?? "medium"} 路 from {from}</p>
      <button type="button" className="btn btn-small" onClick={() => setPlaying(true)}>Start quiz</button>
      {playing && <QuizPlayer artifact={artifact} onClose={() => setPlaying(false)} />}
    </div>
  );
}