"use client"; import { CATEGORIES } from "@/lib/constants"; import { Question, TaskCategory } from "@/types"; import { AnimatePresence, motion } from "framer-motion"; import { useEffect, useRef, useState } from "react"; interface QuestionnaireProps { questions: Question[]; mode: "normal" | "crisis"; onComplete: ( answers: Array<{ questionIndex: number; category: string; question: string; answer: string; }> ) => void; onBack: () => void; onAddTask?: (text: string, category: TaskCategory) => Promise; } interface KanbanSelection { [questionIndex: number]: TaskCategory[]; } export default function Questionnaire({ questions, mode, onComplete, onBack, onAddTask, }: QuestionnaireProps) { const [currentQ, setCurrentQ] = useState(0); const [answers, setAnswers] = useState( new Array(questions.length).fill("") ); const [direction, setDirection] = useState(1); const [kanbanSelections, setKanbanSelections] = useState( {} ); const [addingToKanban, setAddingToKanban] = useState<{ [key: string]: boolean; }>({}); const textareaRef = useRef(null); useEffect(() => { textareaRef.current?.focus(); }, [currentQ]); const handleNext = () => { if (currentQ < questions.length - 1) { setDirection(1); setCurrentQ(currentQ + 1); } else { const formattedAnswers = answers .map((answer, index) => ({ questionIndex: index, category: questions[index].category, question: questions[index].question, answer, })) .filter((a) => a.answer.trim()); onComplete(formattedAnswers); } }; const handlePrev = () => { if (currentQ > 0) { setDirection(-1); setCurrentQ(currentQ - 1); } }; const handleAnswer = (value: string) => { const newAnswers = [...answers]; newAnswers[currentQ] = value; setAnswers(newAnswers); }; const toggleKanbanCategory = async (category: TaskCategory) => { if (!answers[currentQ].trim()) return; const currentSelections = kanbanSelections[currentQ] || []; const isSelected = currentSelections.includes(category); if (isSelected) { setKanbanSelections({ ...kanbanSelections, [currentQ]: currentSelections.filter((c) => c !== category), }); } else { const key = `${currentQ}-${category}`; setAddingToKanban({ ...addingToKanban, [key]: true }); try { if (onAddTask) { await onAddTask(answers[currentQ], category); } setKanbanSelections({ ...kanbanSelections, [currentQ]: [...currentSelections, category], }); } catch (error) { console.error("Error adding task:", error); } finally { setAddingToKanban({ ...addingToKanban, [key]: false }); } } }; const isKanbanSelected = (category: TaskCategory) => { return (kanbanSelections[currentQ] || []).includes(category); }; const progress = ((currentQ + 1) / questions.length) * 100; const currentQuestion = questions[currentQ]; const hasAnswer = answers[currentQ].trim().length > 0; return (
Question {currentQ + 1} / {questions.length} {Math.round(progress)}%
{currentQuestion.category}

{currentQuestion.question}