import { useState, useEffect, useRef } from "react" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { LaIcon } from "./la-icon" interface Answer { id: string author: string content: string timestamp: string replies?: Answer[] } interface QuestionThreadProps { question: { id: string title: string author: string timestamp: string } onClose: () => void } export function QuestionThread({ question, onClose }: QuestionThreadProps) { const [answers, setAnswers] = useState([]) const [newAnswer, setNewAnswer] = useState("") const [replyTo, setReplyTo] = useState(null) const [replyToAuthor, setReplyToAuthor] = useState(null) const inputRef = useRef(null) useEffect(() => { const mockAnswers: Answer[] = [ { id: "1", author: "Noone", content: "Just press Command + Just press Command + Just press Command + Just press Command + Just press Command +", timestamp: "14:40" } ] setAnswers(mockAnswers) }, [question.id]) const sendReply = (answer: Answer) => { setReplyTo(answer) setReplyToAuthor(answer.author) setNewAnswer(`@${answer.author} `) setTimeout(() => { if (inputRef.current) { inputRef.current.focus() const length = inputRef.current.value.length inputRef.current.setSelectionRange(length, length) } }, 0) } const changeInput = (e: React.ChangeEvent) => { const newValue = e.target.value setNewAnswer(newValue) if (replyToAuthor && !newValue.startsWith(`@${replyToAuthor}`)) { setReplyTo(null) setReplyToAuthor(null) } } const sendAnswer = (e: React.FormEvent) => { e.preventDefault() if (newAnswer.trim()) { const newReply: Answer = { id: Date.now().toString(), author: "Me", content: newAnswer, timestamp: new Date().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }) } if (replyTo) { setAnswers(prevAnswers => prevAnswers.map(answer => answer.id === replyTo.id ? { ...answer, replies: [...(answer.replies || []), newReply] } : answer ) ) } else { setAnswers(prevAnswers => [...prevAnswers, newReply]) } setNewAnswer("") setReplyTo(null) setReplyToAuthor(null) } } const renderAnswers = (answers: Answer[], isReply = false) => (
{answers.map(answer => (
{answer.author}
sendReply(answer)}>
Reply
{answer.timestamp}

{answer.content}

{answer.replies && renderAnswers(answer.replies, true)}
))}
) return (

{question.author}

{question.title}

{question.timestamp}

{renderAnswers(answers)}
) }