import SideBarChatButton from "../buttons/SideBarChatButton/SideBarChatButton"; import React, { useCallback, useEffect, useState } from "react"; import Button from "../buttons/MainButton/Button"; import styles from "./Sidebar.module.css"; import SearchButton from "../buttons/SearchButton/SearchButton"; import ThemeSwitcher from "../ThemeSwitcher/ThemeSwitcher"; import { useNavigate } from "react-router-dom"; import { Chat } from "../Icons/Chat"; import Text from "../Text/Text"; interface ChatButton { id: string; title: string; } interface ChatGroup { title: string; chats: ChatButton[]; } interface SidebarProps { activeChatId?: string; activeChatMessages: any[]; } function Sidebar(props: Readonly) { const [isSearchExpanded, setIsSearchExpanded] = useState(false); const navigate = useNavigate(); const handleSearchToggle = (expanded: boolean) => { setIsSearchExpanded(expanded); }; const [chats, setChats] = useState([]); const baseButtonHeight = "40px"; const isAddChatBlocked = props.activeChatId && props.activeChatMessages.length === 0 && chats.find((chat) => chat.id === props.activeChatId)?.title === "new chat"; const loadChats = useCallback(async () => { try { const res = await fetch("/_api/list_chats"); if (!res.ok) throw new Error(String(res.status)); const data = await res.json(); const flat: ChatButton[] = []; data.chats?.forEach((g: ChatGroup) => flat.push(...g.chats)); setChats(flat); } catch (e) { console.error("Failed to load chats", e); } }, []); useEffect(() => { loadChats(); }, [loadChats]); const handleAddChat = async () => { if (isAddChatBlocked) return; const res = await fetch("/_api/new_chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: "New chat" }), }); if (!res.ok) { console.error("new_chat failed", res.status); return; } await loadChats(); const newChat = await res.json(); navigate(`/chats/${newChat.id}`); }; return ( ); } export default Sidebar;