'use client' import { useState, useRef, useEffect } from 'react' import { useChat } from 'ai/react' type MessageWithLoading = { content: string; role: string; isStreaming?: boolean; } const EXAMPLE_PROMPTS = [ { title: "Personalized Learning", description: "Get tailored explanations", prompt: "Can you help me understand photosynthesis in a simple way?" }, { title: "24/7 Availability", description: "Learn at your own pace", prompt: "I need help solving this quadratic equation: x² + 5x + 6 = 0" }, { title: "Multiple Subjects", description: "From math to literature", prompt: "What are the main themes in Shakespeare's Macbeth?" } ] export default function LandingPageChatBot() { const { messages: rawMessages, input, handleInputChange, handleSubmit, isLoading, append } = useChat({ api: '/api/landing_page_chat', streamProtocol: 'data', onError: (error) => { console.error('Chat error:', error); }, onFinish: (message) => { console.log('Chat finished:', message); setMessages(prev => prev.map(msg => ({...msg, isStreaming: false}))) }, keepLastMessageOnError: true, }) const chatContainerRef = useRef(null) const [hasInteracted, setHasInteracted] = useState(false) const [messages, setMessages] = useState([]) useEffect(() => { setMessages(rawMessages.map((msg, index) => ({ ...msg, isStreaming: isLoading && index === rawMessages.length - 1 && msg.role === 'assistant' }))) }, [rawMessages, isLoading]) const scrollToBottom = () => { if (chatContainerRef.current) { const { scrollHeight, clientHeight } = chatContainerRef.current chatContainerRef.current.scrollTop = scrollHeight - clientHeight } } useEffect(() => { scrollToBottom() }, [messages]) const sendMessage = async (text: string) => { setHasInteracted(true) await append({ content: text, role: 'user', }) } const onSubmit = async (e: React.FormEvent) => { setHasInteracted(true) handleSubmit(e) } return (
{!hasInteracted && messages.length === 0 ? (

AI Learning Assistant

Explore new topics, get homework help, or discover learning resources

Try these examples:

{EXAMPLE_PROMPTS.map((prompt, index) => ( ))}
) : (

AI Learning Assistant

{messages.map((message, index) => (
{message.content} {message.isStreaming && ( )}
))}
)}
) }