"use client"; import { useEffect } from 'react'; import { ChatWindow } from '@/components/features/chat/chat-window'; import { ChatInput } from '@/components/features/chat/chat-input'; import { useSessionStore, useActiveSessionId, useTeacherStatus } from '@/store/use-session'; import { ChatService } from '@/services/chat-service'; import { toast } from 'sonner'; import { Button } from "@/components/ui/button"; import { DraftViewSheet } from "@/components/features/draft/DraftViewSheet"; import { useChatStore } from "@/lib/store/chat-store"; import { CheckCircle, Loader2, ArrowLeft, Sparkles } from "lucide-react"; import Link from "next/link"; export default function ChatPage() { const activeSessionId = useActiveSessionId(); const teacherStatus = useTeacherStatus(); const { setActiveSession } = useSessionStore((s) => s.actions); const isDrafting = useChatStore((s) => s.isDrafting); // Initialize Session on Mount useEffect(() => { const initSession = async () => { if (!activeSessionId) { try { const newSessionId = await ChatService.createSession(); setActiveSession(newSessionId); } catch (error) { console.error("Failed to create session:", error); toast.error("Failed to start session. Check your database."); } } }; initSession(); }, [activeSessionId, setActiveSession]); const handleSend = async (message: string) => { if (!activeSessionId) return; try { await ChatService.sendMessage(message, activeSessionId); } catch (error: any) { console.error(error); if (error.message === 'AI Provider not configured') { toast.error("Please configure your AI Provider in Settings", { action: { label: "Go to Settings", onClick: () => window.location.href = '/settings' } }); } else { toast.error("Failed to send message. Please check connection."); } } }; const handleFinishSession = async () => { if (!activeSessionId) return; try { toast.info("Generating your learning summary..."); // Ensure store has latest messages for this session await useChatStore.getState().hydrate(activeSessionId); // Trigger Ghostwriter await useChatStore.getState().generateDraft(activeSessionId); } catch (error) { console.error("Failed to generate draft:", error); toast.error("Failed to generate summary. Please try again."); } }; return (
{/* Session Header */}
Current Session
{/* Chat Messages - Scrollable Area */}
{/* Chat Input - Fixed at Bottom */}
); }