'use client'; import { useState, useRef, useEffect } from 'react'; import type { Message } from '@/types'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import Image from 'next/image'; interface ChatInterfaceProps { messages: Message[]; onSendMessage: (message: string, imageUrl?: string) => void; isGenerating: boolean; isAuthenticated?: boolean; supportsImages?: boolean; } export default function ChatInterface({ messages, onSendMessage, isGenerating, isAuthenticated = false, supportsImages = false }: ChatInterfaceProps) { const [input, setInput] = useState(''); const [uploadedImageUrl, setUploadedImageUrl] = useState(null); const [uploadedImageFile, setUploadedImageFile] = useState(null); const fileInputRef = useRef(null); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (input.trim() && !isGenerating) { onSendMessage(input, uploadedImageUrl || undefined); setInput(''); setUploadedImageUrl(null); setUploadedImageFile(null); } }; const handleImageUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { // Create a data URL for the image const reader = new FileReader(); reader.onload = (event) => { const imageUrl = event.target?.result as string; setUploadedImageUrl(imageUrl); setUploadedImageFile(file); }; reader.readAsDataURL(file); } }; const removeImage = () => { setUploadedImageUrl(null); setUploadedImageFile(null); if (fileInputRef.current) { fileInputRef.current.value = ''; } }; return (
{/* Messages */}
{messages.length === 0 ? (
{isAuthenticated ? ( <>

Start a conversation

Describe what you want to build

) : ( <>

Sign in to get started

Use Dev Login or sign in with Hugging Face

)}
) : ( messages.map((message, index) => ( )) )}
{/* Input */}
{/* Image Preview */} {uploadedImageUrl && (
Upload preview
)}
setInput(e.target.value)} placeholder={isAuthenticated ? "Message AnyCoder..." : "Sign in first..."} disabled={isGenerating || !isAuthenticated} className="flex-1 px-4 py-2.5 bg-[#2d2d2f] text-[#f5f5f7] text-sm border border-[#424245]/50 rounded-full focus:outline-none focus:border-[#424245] disabled:opacity-40 disabled:cursor-not-allowed placeholder-[#86868b]" /> {/* Image Upload Button (only show if model supports images) */} {supportsImages && ( <> )}
); }