File size: 17,855 Bytes
8d3e440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import Link from "next/link";
import { useState, useRef, useEffect, useCallback } from "react";
import { useRouter } from "next/navigation";

/* ── 8 PHQ-8 Interview Questions (DAIC-WOZ style) ───────────────────────── */
const QUESTIONS = [
  "Let's start by talking about things you usually enjoy. Lately, have you felt like you've lost interest or pleasure in doing them? Tell me a bit about how you've been feeling about your hobbies or daily activities.",
  "I'd like to hear about your mood recently. Over the past couple of weeks, have you been feeling down, depressed, or perhaps a bit hopeless? Could you share what's been on your mind?",
  "Let's talk about your sleep. How have you been sleeping lately? Tell me if you've had any trouble falling asleep, staying asleep, or maybe even sleeping too much.",
  "How has your energy level been? Do you find yourself feeling tired or exhausted often? Tell me about a typical day and how you feel physically.",
  "What about your eating habits? Have you noticed any changes, like a poor appetite or perhaps overeating? Tell me how your relationship with food has been lately.",
  "How have you been feeling about yourself? Have there been moments where you felt bad, or like you let yourself or your family down? Feel free to share your thoughts openly.",
  "Can you tell me about your focus and concentration? For example, when you read or watch TV, do you find your mind wandering? Tell me how it's been for you.",
  "Have you or others noticed any changes in how you move or speak? Like moving much slower than usual, or maybe the oppositeβ€”feeling restless and unable to sit still? Tell me about it.",
];

const QUICK_OPTIONS = [
  "Not at all",
  "Several days",
  "More than half the days",
  "Nearly every day",
];

const ITEM_SHORT_NAMES = [
  "Loss of Interest",
  "Feeling Depressed",
  "Sleep Issues",
  "Fatigue",
  "Appetite Changes",
  "Self-Criticism",
  "Concentration",
  "Psychomotor",
];

type Message = { role: "bot" | "user"; text: string };

export default function ChatScreening() {
  const router = useRouter();
  const [messages, setMessages] = useState<Message[]>([
    { role: "bot", text: "Hello! I'm going to ask you 8 short questions based on the PHQ-8 screening. Please answer honestly β€” all data stays anonymous and is not stored on any server.\n\nLet's begin." },
    { role: "bot", text: QUESTIONS[0] },
  ]);
  const [currentStep, setCurrentStep] = useState(0);
  const [answers, setAnswers] = useState<string[]>([]);
  const [inputValue, setInputValue] = useState("");
  const [isTyping, setIsTyping] = useState(false);
  const [isSubmitting, setIsSubmitting] = useState(false);
  const chatEndRef = useRef<HTMLDivElement>(null);
  const inputRef = useRef<HTMLInputElement>(null);

  /* auto-scroll on new messages */
  useEffect(() => {
    chatEndRef.current?.scrollIntoView({ behavior: "smooth" });
  }, [messages, isTyping]);

  /* focus input */
  useEffect(() => {
    if (!isSubmitting) inputRef.current?.focus();
  }, [currentStep, isSubmitting]);

  /* ── Submit all answers to backend ──────────────────────────────────── */
  const submitToBackend = useCallback(
    async (allAnswers: string[]) => {
      setIsSubmitting(true);
      setIsTyping(true);
      setMessages((prev) => [
        ...prev,
        { role: "bot", text: "Thank you for completing all 8 questions! I'm now analyzing your responses..." },
      ]);

      try {
        const res = await fetch("/api/predict", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ answers: allAnswers }),
        });

        if (!res.ok) {
          console.warn(`Backend returned ${res.status}, falling back to local scoring`);
          throw "Fallback";
        }
        const data = await res.json();

        /* Store result in localStorage */
        const result = {
          ...data,
          answers: allAnswers,
          questions: QUESTIONS,
          itemShortNames: ITEM_SHORT_NAMES,
          date: new Date().toISOString(),
        };

        localStorage.setItem("mindcheck_latest_result", JSON.stringify(result));

        setIsTyping(false);
        setMessages((prev) => [
          ...prev,
          { role: "bot", text: "Analysis complete! Redirecting you to your results..." },
        ]);

        setTimeout(() => router.push("/result"), 1200);
      } catch (err) {
        console.warn("Prediction error:", err);
        setIsTyping(false);

        /* ── Fallback: keyword-based scoring ────────────────── */
        const fallbackScores: number[] = allAnswers.map((ans) => {
          const a = ans.toLowerCase().trim();
          if (/\b(not at all|never|no|none|tidak|ngga|ga|tidak pernah)\b/.test(a)) return 0;
          if (/\b(several days|sometimes|a little|occasionally|kadang|jarang)\b/.test(a)) return 1;
          if (/\b(more than half|often|frequently|most days|sering|lumayan sering)\b/.test(a)) return 2;
          if (/\b(nearly every day|always|every day|constantly|everyday|selalu|tiap hari|hampir tiap hari)\b/.test(a)) return 3;
          
          /* Sentiment-based fallback */
          const negWords = ["hopeless","worthless","empty","tired","sad","lonely","fail","useless","burden","numb","anxious","hate","depressed","miserable","terrible","awful","struggling","suffering","exhausted","overwhelmed","stressed","worried", "sedih", "capek", "lelah", "hampa", "kesepian", "gagal", "beban", "benci", "stres", "bingung", "hancur", "buruk"];
          const words = a.split(/\s+/);
          const negCount = words.filter((w) => negWords.includes(w)).length;
          if (negCount >= 3) return 3;
          if (negCount >= 2) return 2;
          if (negCount >= 1) return 1;
          return 1; // default middle score for ambiguous text

        });

        const totalScore = fallbackScores.reduce((s, v) => s + v, 0);
        let severity;
        if (totalScore <= 4) severity = { label: "Minimal", color: "green", description: "Minimal depression β€” no treatment typically needed." };
        else if (totalScore <= 9) severity = { label: "Mild", color: "amber", description: "Mild depression β€” watchful waiting recommended." };
        else if (totalScore <= 14) severity = { label: "Moderate", color: "orange", description: "Moderate depression β€” consultation recommended." };
        else if (totalScore <= 19) severity = { label: "Moderately Severe", color: "red-orange", description: "Moderately severe depression β€” active treatment recommended." };
        else severity = { label: "Severe", color: "red", description: "Severe depression β€” immediate referral recommended." };

        const combined = allAnswers.join(" ").toLowerCase();
        const negWordsList = ["hopeless","worthless","empty","tired","sad","lonely","fail","useless","burden","numb","anxious","hate","depressed","miserable","terrible","awful","struggling","suffering","exhausted","overwhelmed","stressed","worried", "sedih", "capek", "lelah", "hampa", "kesepian", "gagal", "beban", "benci", "stres", "bingung", "hancur", "buruk"];
        const foundNeg = negWordsList.filter((w) => combined.includes(w));
        const fillerPattern = /\b(um|uh|uhm|hmm|hm|like|you know)\b/gi;
        const fillerCount = (combined.match(fillerPattern) || []).length;

        const result = {
          scores: fallbackScores,
          total_score: totalScore,
          severity,
          filler_count: fillerCount,
          negative_words: foundNeg,
          positive_words: [],
          item_names: ITEM_SHORT_NAMES.map((n) => n.replace(/ /g, "")),
          answers: allAnswers,
          questions: QUESTIONS,
          itemShortNames: ITEM_SHORT_NAMES,
          date: new Date().toISOString(),
          fallback: true,
        };

        localStorage.setItem("mindcheck_latest_result", JSON.stringify(result));

        setMessages((prev) => [
          ...prev,
          { role: "bot", text: "Analysis complete (using local scoring as the model server is unavailable). Redirecting to your results..." },
        ]);
        setTimeout(() => router.push("/result"), 1200);
      }
    },
    [router],
  );

  /* ── Handle user answer ─────────────────────────────────────────────── */
  const handleAnswer = useCallback(
    (text: string) => {
      if (isSubmitting || !text.trim()) return;

      const newAnswers = [...answers, text.trim()];
      setAnswers(newAnswers);
      setMessages((prev) => [...prev, { role: "user", text: text.trim() }]);
      setInputValue("");

      const nextStep = currentStep + 1;

      if (nextStep >= QUESTIONS.length) {
        /* All questions answered β†’ submit */
        setCurrentStep(nextStep);
        submitToBackend(newAnswers);
      } else {
        /* Show typing indicator, then next question */
        setIsTyping(true);
        setCurrentStep(nextStep);
        setTimeout(() => {
          setIsTyping(false);
          setMessages((prev) => [
            ...prev,
            { role: "bot", text: QUESTIONS[nextStep] },
          ]);
        }, 800);
      }
    },
    [answers, currentStep, isSubmitting, submitToBackend],
  );

  const handleKeyDown = (e: React.KeyboardEvent) => {
    if (e.key === "Enter" && !e.shiftKey) {
      e.preventDefault();
      handleAnswer(inputValue);
    }
  };

  const progress = Math.min(((currentStep) / QUESTIONS.length) * 100, 100);
  const isDone = currentStep >= QUESTIONS.length;

  return (
    <>
      {/* TopAppBar */}
      <header className="bg-surface-bright dark:bg-surface-dim font-section-heading text-section-heading w-full sticky top-0 border-b border-outline-variant flex justify-between items-center h-14 px-gutter z-50">
        <div className="flex items-center gap-2">
          <span className="material-symbols-outlined text-primary dark:text-primary-fixed" data-icon="psychology">psychology</span>
          <span className="font-section-heading text-section-heading text-primary font-bold">MindCheck</span>
        </div>
        <div className="text-primary dark:text-primary-fixed text-sm">
          {isDone ? "Complete" : `Question ${currentStep + 1} of ${QUESTIONS.length}`}
        </div>
        <Link href="/" className="text-secondary hover:opacity-80 transition-opacity active:opacity-70">
          Exit
        </Link>
      </header>

      <div className="flex-1 flex max-w-container-max mx-auto w-full h-[calc(100vh-3.5rem)]">
        {/* LEFT SIDEBAR */}
        <aside className="hidden md:flex flex-col w-[280px] border-r border-outline-variant bg-surface-container-lowest p-gutter justify-between h-full sticky top-14 overflow-y-auto">
          <div>
            <h2 className="font-section-heading text-section-heading text-on-surface mb-stack-md">Questions</h2>
            <ul className="space-y-stack-sm flex flex-col">
              {QUESTIONS.map((_, i) => {
                const isCompleted = i < currentStep;
                const isCurrent = i === currentStep && !isDone;
                return (
                  <li
                    key={i}
                    className={`flex items-center gap-3 p-2 rounded transition-all duration-300 ${
                      isCurrent
                        ? "text-primary bg-surface-container-low border border-outline-variant"
                        : isCompleted
                        ? "text-secondary"
                        : "text-secondary"
                    }`}
                  >
                    <span className="material-symbols-outlined" data-icon={
                      isCompleted ? "check_circle" : isCurrent ? "radio_button_checked" : "radio_button_unchecked"
                    }>
                      {isCompleted ? "check_circle" : isCurrent ? "radio_button_checked" : "radio_button_unchecked"}
                    </span>
                    <span className={`font-caption-sm text-caption-sm ${
                      isCompleted ? "text-on-surface-variant" : isCurrent ? "font-bold" : "text-outline-variant"
                    }`}>
                      {ITEM_SHORT_NAMES[i]}
                    </span>
                  </li>
                );
              })}
            </ul>
          </div>
          <div className="mt-stack-lg bg-surface-container-low border border-outline-variant rounded p-stack-md flex items-start gap-2 flex-col">
            <span className="material-symbols-outlined text-primary text-[20px]" data-icon="info">info</span>
            <p className="font-caption-sm text-caption-sm text-on-surface-variant">Answer honestly, all data is anonymous and processed locally.</p>
            <div className="w-full border-t border-outline-variant mt-4 pt-4 text-center">
              <p className="text-[11px] text-on-surface-variant opacity-60">Β© 2023 MindCheck. All rights reserved.</p>
            </div>
          </div>
        </aside>

        {/* RIGHT MAIN */}
        <main className="flex-1 flex flex-col relative h-full bg-surface-container-lowest">
          {/* Progress Bar */}
          <div className="w-full h-1 bg-surface-variant absolute top-0 left-0 z-10">
            <div
              className="h-full bg-primary transition-all duration-700 ease-out"
              style={{ width: `${progress}%` }}
            />
          </div>

          {/* Chat Area */}
          <div className="flex-1 overflow-y-auto p-gutter pt-[40px] space-y-stack-lg pb-56">
            {messages.map((msg, i) => (
              <div
                key={i}
                className={`flex gap-4 items-end ${msg.role === "user" ? "justify-end w-full" : "max-w-[80%]"}`}
                style={{
                  animation: "fadeSlideIn 0.4s ease-out",
                }}
              >
                {msg.role === "bot" && (
                  <div className="w-8 h-8 rounded-full bg-surface-container-high border border-outline-variant flex items-center justify-center shrink-0">
                    <span className="material-symbols-outlined text-primary text-[18px]" data-icon="psychology">psychology</span>
                  </div>
                )}
                <div
                  className={`${
                    msg.role === "bot"
                      ? "bg-surface-container-lowest border border-outline-variant rounded-t-lg rounded-br-lg"
                      : "bg-[#EEEDFE] border border-outline-variant rounded-t-lg rounded-bl-lg max-w-[80%]"
                  } p-4`}
                >
                  <p className="font-body-md text-body-md text-on-surface whitespace-pre-line">{msg.text}</p>
                </div>
              </div>
            ))}

            {/* Typing Indicator */}
            {isTyping && (
              <div className="flex gap-4 items-end max-w-[80%]">
                <div className="w-8 h-8 rounded-full bg-surface-container-high border border-outline-variant flex items-center justify-center shrink-0">
                  <span className="material-symbols-outlined text-primary text-[18px]" data-icon="psychology">psychology</span>
                </div>
                <div className="bg-surface-container-lowest border border-outline-variant rounded-t-lg rounded-br-lg p-4 flex gap-1.5 items-center">
                  <span className="w-2 h-2 bg-primary rounded-full animate-bounce" style={{ animationDelay: "0ms" }} />
                  <span className="w-2 h-2 bg-primary rounded-full animate-bounce" style={{ animationDelay: "150ms" }} />
                  <span className="w-2 h-2 bg-primary rounded-full animate-bounce" style={{ animationDelay: "300ms" }} />
                </div>
              </div>
            )}

            <div ref={chatEndRef} />
          </div>

          {/* Input Area Fixed Bottom */}
          {!isDone && (
            <div className="absolute bottom-0 left-0 w-full bg-surface-container-lowest border-t border-outline-variant p-gutter">
              {/* Text Input */}
              <div className="relative flex items-center">
                <input
                  ref={inputRef}
                  className="w-full h-12 pl-4 pr-12 rounded border border-outline-variant focus:border-primary focus:ring-0 focus:outline-none bg-surface-container-lowest text-on-surface placeholder:text-[#888780] font-body-md text-body-md transition-colors"
                  placeholder="Type your answer..."
                  type="text"
                  value={inputValue}
                  onChange={(e) => setInputValue(e.target.value)}
                  onKeyDown={handleKeyDown}
                  disabled={isTyping || isSubmitting}
                />
                <button
                  onClick={() => handleAnswer(inputValue)}
                  disabled={!inputValue.trim() || isTyping || isSubmitting}
                  className="absolute right-2 top-1/2 -translate-y-1/2 w-8 h-8 flex items-center justify-center text-primary hover:text-on-primary-fixed-variant transition-all duration-200 disabled:opacity-30 disabled:cursor-not-allowed hover:scale-110 active:scale-90"
                >
                  <span className="material-symbols-outlined" data-icon="send">send</span>
                </button>
              </div>
            </div>
          )}
        </main>
      </div>

      {/* Inline animation keyframes */}
      <style jsx global>{`
        @keyframes fadeSlideIn {
          from {
            opacity: 0;
            transform: translateY(12px);
          }
          to {
            opacity: 1;
            transform: translateY(0);
          }
        }
      `}</style>
    </>
  );
}