'use client'; import { useChatStore } from '@/store/use-chat'; import { Button } from '@/components/ui/button'; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription, SheetFooter, } from '@/components/ui/sheet'; import { ThumbsUp, ThumbsDown, RefreshCw } from 'lucide-react'; import ReactMarkdown from 'react-markdown'; export function DraftSheet() { const { phase, currentDraft, setPhase, resetSession } = useChatStore(); const isOpen = phase === 'review' && !!currentDraft; const handleKeep = async () => { if (!currentDraft) return; try { // Import dynamically to avoid side-effects during render if possible, // or just import at top. We'll stick to dynamic since DraftService might not be SSR friendly // without checks, but it handles it internally. const { DraftService } = await import('@/lib/db/draft-service'); const { useSessionStore } = await import('@/store/use-session'); const sessionId = useSessionStore.getState().activeSessionId; if (!sessionId) { console.error("No active session ID"); return; } await DraftService.saveDraft({ sessionId, title: currentDraft.title, content: currentDraft.lesson, // Using lesson as content for now, or construct full markdown? // Let's construct a nice markdown // Actually the draft artifact has title, insight, lesson. // We should probably save the raw JSON or a formatted textual representation. // Let's save formatted text. createdAt: Date.now(), updatedAt: Date.now(), status: 'completed', completedAt: Date.now(), tags: [] }); // Redirect to history or show success window.location.href = '/history'; resetSession(); } catch (error) { console.error("Failed to save draft:", error); } }; const handleRefine = () => { // Logic for refinement (Story 3.5) // For now, close sheet and persist state setPhase('drafting'); // Go back or stay? // Actually, refinement usually means going back to chat Elicitation or having a specialized Refinement Mode. // Let's just close for now. setPhase('elicitation'); }; if (!currentDraft) return null; return ( !open && handleRefine()}> {currentDraft.title} " {currentDraft.insight} "

The Lesson

{currentDraft.lesson}

); }