"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; interface Chat { id: string; title: string; model_alias: string; updated_at: string; } interface User { login: string; } const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || "http://127.0.0.1:18000"; // Global modal state since Sidebar and Topbar both might need logic, but let's keep it simple export default function Layout({ children, chats: initialChats, user }: { children: React.ReactNode, chats: Chat[], user: User }) { const [chats, setChats] = useState(initialChats); const [models, setModels] = useState<{alias: string, name: string}[]>([]); const pathname = usePathname(); const router = useRouter(); const [isModalOpen, setIsModalOpen] = useState(false); const [newChatTitle, setNewChatTitle] = useState(""); const [newChatModel, setNewChatModel] = useState(""); useEffect(() => { fetch(`${API_BASE_URL}/api/models`) .then(res => res.json()) .then(data => { setModels(data); if (data.length > 0) setNewChatModel(data[0].alias); }) .catch(console.error); }, []); const handleLogout = async () => { await fetch(`${API_BASE_URL}/api/auth/logout`, { method: "POST" }); window.location.href = "/"; }; const createChat = async (e: React.FormEvent) => { e.preventDefault(); if (!newChatTitle.trim() || !newChatModel) return; try { const res = await fetch(`${API_BASE_URL}/api/chats`, { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify({ title: newChatTitle, model_alias: newChatModel }), }); if (res.ok) { const chat = await res.json(); setChats([chat, ...chats]); setIsModalOpen(false); setNewChatTitle(""); router.push(`/chat/${chat.id}`); } } catch (err) { console.error("Failed to create chat", err); } }; return (
{/* Top Bar */}
AI Chat MVP
{user.login.charAt(0).toUpperCase()}
{user.login}
{/* Main Container */}
{/* Sidebar */} {/* Main Content Area */}
{children}
{/* New Chat Modal */} {isModalOpen && (

Новый чат

setNewChatTitle(e.target.value)} placeholder="Введите название..." required style={{ width: '100%', padding: '10px 14px', borderRadius: 8, border: '1px solid var(--border-color)', fontSize: 14, outline: 'none' }} />
)}
); }