"use client"; import { useCallback, useState } from "react"; import { useRouter } from "next/navigation"; import { Conversation, ConversationContent, ConversationEmptyState } from "@/components/ai-elements/conversation"; import { Composer } from "@/components/composer"; import { GraphContextControl, effectiveContextTokens } from "@/components/graph-context-control"; import { PhaseChip } from "@/components/phase-divider"; import { SidebarToggle } from "@/components/sidebar"; import { StatsBar } from "@/components/stats-bar"; import type { ChatSettings } from "@/src/core"; /** * The sessionless draft view at /chat/new (owner, 2026-09-13): "+ New * session" opens THIS, never a created record — a session comes to exist * only when the learner sends the first message, and that message becomes * the session's title (the exact create-then-redirect contract the home * composer established). Visually identical to a fresh chat: same header * (title + probe chip), the same empty state, the same composer blob + * stats dock. The model picker's empty-chatId branch defers its save and * lifts the selection here, so the pre-chat model/thinking choice rides * the creation. */ const DOCK = "mx-auto w-full max-w-[46rem] px-4"; /** * The draft's whole behavior as one pure seam: create the session (title = * the first message), persist the lifted pre-chat settings, return the push * target — /chat/?prompt= — whose initialPrompt path delivers the * message as turn 1. Injected fetch/router so tests pin the contract. * Returns null on empty text; rethrows on transport failure (the caller's * busy flag resets). */ export async function startDraftSession( text: string, settings: ChatSettings | undefined, fetchLike: typeof fetch, ): Promise { const t = text.trim(); if (!t) return null; const res = await fetchLike("/api/chats", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: t }), }); const { id } = (await res.json()) as { id: string }; if (settings) { // Best-effort persist — the selection was made pre-chat. await fetchLike(`/api/chats/${id}/settings`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(settings), }).catch(() => {}); } return `/chat/${id}?prompt=${encodeURIComponent(t)}`; } /** The pure shell (no router) — the render-test seam: the draft view's * visible surface, assembled from the same pieces a fresh chat shows. */ export function DraftShell({ busy, text, onChange, onSend, settings, onSettingsSaved, }: { busy: boolean; text: string; onChange: (v: string) => void; onSend: () => void; settings: ChatSettings | undefined; onSettingsSaved: (s: ChatSettings) => void; }) { const canSend = text.trim().length > 0; return (
{/* h-15 (60px) matches the chat header and the sidebar's LH wordmark band (owner, round 6). */}
New session
{/* The pointer-events pattern (953b22b): the sticky wrapper is pointer-events-none so nothing here blocks the empty-state area; the blob and the stats row re-enable it themselves. */}
{}} canSend={canSend} autoFocus />
onSettingsSaved({ ...settings, graphContextTokens: tokens }) } /> } />
); } export function NewSession() { const router = useRouter(); const [text, setText] = useState(""); const [busy, setBusy] = useState(false); // The pre-chat model+thinking selection, lifted from the StatsBar's // ModelControl (empty chatId → it never PUTs; it hands the choice up). const [settings, setSettings] = useState(undefined); const send = useCallback(() => { if (!text.trim() || busy) return; setBusy(true); void startDraftSession(text, settings, fetch) .then((target) => { if (target) router.push(target); else setBusy(false); }) .catch(() => setBusy(false)); }, [text, busy, settings, router]); return ( ); }