'use client'; import { useState } from 'react'; import { X, Send, Loader2, MessageCircle } from 'lucide-react'; import clsx from 'clsx'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; interface AskQuestionModalProps { isOpen: boolean; onClose: () => void; context: string; // The original analyzed content } interface Message { role: 'user' | 'assistant'; content: string; } export default function AskQuestionModal({ isOpen, onClose, context }: AskQuestionModalProps) { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || loading) return; const userMessage = input.trim(); setInput(''); setMessages(prev => [...prev, { role: 'user', content: userMessage }]); setLoading(true); try { const formData = new FormData(); formData.append('text', ` Based on this content I just learned: --- ${context.substring(0, 2000)} --- My question: ${userMessage} Please answer my question in a helpful, educational way. Use simple language and examples if needed. `); const res = await fetch('/api/analyze', { method: 'POST', body: formData, }); if (!res.ok) throw new Error('Failed to get response'); const data = await res.json(); const cleanResponse = data.result?.replace(/\[\[TOPICS?:.*?\]\]/gi, '').trim() || 'Sorry, I could not generate a response.'; setMessages(prev => [...prev, { role: 'assistant', content: cleanResponse }]); } catch (error) { setMessages(prev => [...prev, { role: 'assistant', content: 'Sorry, something went wrong. Please try again.' }]); } finally { setLoading(false); } }; if (!isOpen) return null; return (
{/* Header */}

Ask a Question

{/* Messages */}
{messages.length === 0 && (

Ask any question about the content you just analyzed.

)} {messages.map((msg, idx) => (
{msg.role === 'assistant' ? (
{msg.content}
) : (

{msg.content}

)}
))} {loading && (
)}
{/* Input */}
setInput(e.target.value)} placeholder="Type your question..." className="flex-1 px-4 py-3 bg-gray-100 dark:bg-neutral-800 border border-gray-200 dark:border-neutral-700 rounded-xl focus:outline-none focus:ring-2 focus:ring-sky-500 focus:border-transparent text-gray-900 dark:text-white placeholder-gray-400" disabled={loading} />
); }