File size: 9,730 Bytes
02ffd8a
dbeb485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02ffd8a
 
 
dbeb485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34f01ec
 
 
dbeb485
 
34f01ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c932f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbeb485
 
 
 
 
 
 
 
 
 
 
 
 
 
02ffd8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbeb485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b87cbc7
dbeb485
 
 
 
 
 
 
 
 
 
34f01ec
dbeb485
 
34f01ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbeb485
 
 
 
 
34f01ec
dbeb485
 
34f01ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbeb485
 
 
 
0c932f9
dbeb485
0c932f9
dbeb485
0c932f9
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
import { useState, useEffect, useRef, useCallback, useMemo } from "react";
import { Input } from "@labas/ui/components/input";

const MCQ_FORMATS = [
  "multiple_choice",
  "synonym",
  "grammar_in_context",
  "sentence_completion",
  "reference",
  "kanji_reading",
  "particle_choice",
  "article_case",
  "matching_headings",
  "matching_information",
  "summary_completion",
  "cloze",
  "error_recognition",
  "text_insertion",
  "matching_pairs",
];

const TRUE_FALSE_CHOICES = [
  { key: "TRUE", label: "True" },
  { key: "FALSE", label: "False" },
  { key: "NOT_GIVEN", label: "Not Given" },
];

const AUTHOR_VIEW_CHOICES = [
  { key: "YES", label: "Yes" },
  { key: "NO", label: "No" },
  { key: "NOT_GIVEN", label: "Not Given" },
];

const radioClass =
  "flex items-center gap-3 p-3 rounded-[var(--radius-lg)] border-2 border-[var(--oat-border)] bg-[var(--pure-white)] cursor-pointer hover:border-[var(--matcha-300)] transition-all";
const radioSelected =
  "border-[var(--matcha-600)] bg-[#e8f5ed] ring-2 ring-[var(--matcha-600)]/30 shadow-sm";
const radioDisabled = "opacity-60 cursor-not-allowed";

/** Normalize TF / NG style answers from API or loose model output */
function normalizeTriStateKey(
  value: string,
  choices: readonly { key: string }[],
): string {
  const raw = (value ?? "").trim();
  if (!raw) return "";
  const u = raw.toUpperCase().replace(/\s+/g, "_");
  if (choices.some((c) => c.key === u)) return u;
  const compact = u.replace(/_/g, "");
  const alias: Record<string, string> = {
    TRUE: "TRUE",
    FALSE: "FALSE",
    NOTGIVEN: "NOT_GIVEN",
    YES: "YES",
    NO: "NO",
  };
  return alias[compact] ?? u;
}

/** Debounced text input to avoid parent re-render on every keystroke */
function DebouncedTextInput({
  value,
  onChange,
  disabled,
  placeholder,
  className,
}: {
  value: string;
  onChange: (val: string) => void;
  disabled?: boolean;
  placeholder?: string;
  className?: string;
}) {
  const [localValue, setLocalValue] = useState(value);
  const debounceRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);

  // Sync when prop value changes from outside (e.g. restore from server)
  useEffect(() => {
    setLocalValue(value);
  }, [value]);

  const handleChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const val = e.target.value;
      setLocalValue(val);
      clearTimeout(debounceRef.current);
      debounceRef.current = setTimeout(() => {
        onChange(val);
      }, 400);
    },
    [onChange],
  );

  const handleBlur = useCallback(() => {
    clearTimeout(debounceRef.current);
    onChange(localValue);
  }, [localValue, onChange]);

  return (
    <Input
      value={localValue}
      onChange={handleChange}
      onBlur={handleBlur}
      disabled={disabled}
      placeholder={placeholder}
      className={className}
    />
  );
}

export function QuestionInput({
  question,
  value,
  onChange,
  disabled,
}: {
  question: any;
  value: string;
  onChange: (val: string) => void;
  disabled?: boolean;
}) {
  const format = question.format;
  const options = question.options as Array<{ key: string; text: string }> | undefined;

  if (format === "matching_pairs") {
    if (!options || options.length === 0) {
      return (
        <div className="text-sm text-[var(--warm-silver)] italic">
          Tidak ada opsi tersedia untuk soal ini.
        </div>
      );
    }
    const parseCurrentMapping = (val: string): Map<string, string> => {
      const map = new Map<string, string>();
      val.split(",").forEach((pair) => {
        const [k, v] = pair.split(":").map((s) => s.trim());
        if (k && v) map.set(k, v);
      });
      return map;
    };
    const currentMap = value ? parseCurrentMapping(value) : new Map();
    const updateMapping = (key: string, val: string) => {
      currentMap.set(key, val);
      const serialized = Array.from(currentMap.entries())
        .map(([k, v]) => `${k}:${v}`)
        .join(",");
      onChange(serialized);
    };
    return (
      <div className="space-y-3">
        {options.map((opt) => {
          const matched = currentMap.get(opt.key) || "";
          return (
            <div key={opt.key} className="flex items-center gap-3">
              <span className="w-8 h-8 rounded-full text-sm font-bold flex items-center justify-center shrink-0 bg-[var(--oat-light)] text-[var(--clay-black)]">
                {opt.key}
              </span>
              <span className="flex-1 text-sm text-[var(--clay-black)]">{opt.text}</span>
              <span className="text-[var(--warm-silver)]"></span>
              <input
                type="text"
                value={matched}
                onChange={(e) => updateMapping(opt.key, e.target.value)}
                disabled={disabled}
                placeholder="Padanan..."
                className="w-24 px-3 py-2 text-sm rounded-[var(--radius-md)] border-2 border-[var(--oat-border)] bg-[var(--pure-white)] focus:outline-none focus:border-[var(--matcha-600)]"
              />
            </div>
          );
        })}
      </div>
    );
  }

  if (MCQ_FORMATS.includes(format)) {
    if (!options || options.length === 0) {
      return (
        <div className="text-sm text-[var(--warm-silver)] italic">
          Tidak ada opsi tersedia untuk soal ini.
        </div>
      );
    }
    return (
      <div className="space-y-2">
        {options.map((opt) => (
          <label
            key={opt.key}
            className={`${radioClass} ${value === opt.key ? radioSelected : ""} ${disabled ? radioDisabled : ""}`}
          >
            <input
              type="radio"
              name={question.id}
              value={opt.key}
              checked={value === opt.key}
              onChange={() => onChange(opt.key)}
              disabled={disabled}
              className="hidden"
            />
            <span className={`w-8 h-8 rounded-full text-sm font-bold flex items-center justify-center shrink-0 transition-colors ${value === opt.key ? "bg-[var(--matcha-600)] text-[var(--pure-white)]" : "bg-[var(--oat-light)] text-[var(--clay-black)]"}`}>
              {opt.key}
            </span>
            <span className="text-sm text-[var(--clay-black)]">{opt.text}</span>
          </label>
        ))}
      </div>
    );
  }

  if (format === "true_false_not_given") {
    const normalizedValue = normalizeTriStateKey(value, TRUE_FALSE_CHOICES);
    return (
      <div className="space-y-2">
        {TRUE_FALSE_CHOICES.map((c) => {
          const selected = normalizedValue === c.key;
          return (
            <label
              key={c.key}
              className={`${radioClass} ${selected ? radioSelected : ""} ${disabled ? radioDisabled : ""}`}
            >
              <input
                type="radio"
                name={question.id}
                value={c.key}
                checked={selected}
                onChange={() => onChange(c.key)}
                disabled={disabled}
                className="sr-only"
              />
              <span
                aria-hidden
                className={`w-5 h-5 rounded-full border-2 shrink-0 flex items-center justify-center transition-colors ${
                  selected
                    ? "border-[var(--matcha-600)] bg-[var(--matcha-600)]"
                    : "border-[var(--oat-border)] bg-[var(--pure-white)]"
                }`}
              >
                {selected ? <span className="w-2 h-2 rounded-full bg-[var(--pure-white)]" /> : null}
              </span>
              <span
                className={`text-sm font-semibold ${
                  selected ? "text-[var(--matcha-800)]" : "text-[var(--clay-black)]"
                }`}
              >
                {c.label}
              </span>
            </label>
          );
        })}
      </div>
    );
  }

  if (format === "author_view") {
    const normalizedValue = normalizeTriStateKey(value, AUTHOR_VIEW_CHOICES);
    return (
      <div className="space-y-2">
        {AUTHOR_VIEW_CHOICES.map((c) => {
          const selected = normalizedValue === c.key;
          return (
            <label
              key={c.key}
              className={`${radioClass} ${selected ? radioSelected : ""} ${disabled ? radioDisabled : ""}`}
            >
              <input
                type="radio"
                name={question.id}
                value={c.key}
                checked={selected}
                onChange={() => onChange(c.key)}
                disabled={disabled}
                className="sr-only"
              />
              <span
                aria-hidden
                className={`w-5 h-5 rounded-full border-2 shrink-0 flex items-center justify-center transition-colors ${
                  selected
                    ? "border-[var(--matcha-600)] bg-[var(--matcha-600)]"
                    : "border-[var(--oat-border)] bg-[var(--pure-white)]"
                }`}
              >
                {selected ? <span className="w-2 h-2 rounded-full bg-[var(--pure-white)]" /> : null}
              </span>
              <span
                className={`text-sm font-semibold ${selected ? "text-[var(--matcha-800)]" : "text-[var(--clay-black)]"}`}
              >
                {c.label}
              </span>
            </label>
          );
        })}
      </div>
    );
  }

  // Fallback: debounced text input for fill_blank and other free-text formats
  return (
    <DebouncedTextInput
      value={value}
      onChange={onChange}
      disabled={disabled}
      placeholder="Ketik jawaban Anda..."
      className="bg-[var(--pure-white)] border-2 border-[var(--oat-border)] rounded-[var(--radius-lg)]"
    />
  );
}