import React, { useState, useEffect } from "react"; import { Plus, Search, Edit, Trash, List, ArrowLeft, ArrowRight, Loader2, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { v4 as uuidv4 } from "uuid"; import { Input } from "@/components/ui/input"; import { useAppDispatch, useAppSelector } from "@/redux/hooks"; import { createChat, setActiveChat, deleteChat, setSearchQuery, renameChat, } from "@/redux/slices/chatsSlice"; import { addMessage, setActiveMessages, setActiveContext, setSidebarVisibility, clearEmbeddings, clearActiveMessages, toggleSidebar, setActiveChatPrompt, } from "@/redux/slices/chatSlice"; import { useToast } from "@/hooks/use-toast"; import { resetSessionId, setSessionId } from "@/redux/slices/sessionSlice"; import { useParams } from "react-router-dom"; import api from "@/api/apiClient"; interface ChatSidebarProps { isMobileSidebar?: boolean; onClose?: () => void; } const ChatSidebar: React.FC = ({ isMobileSidebar = false, onClose, }) => { const dispatch = useAppDispatch(); const { chats, activeChat, searchQuery } = useAppSelector( (state) => state.chats ); const { isListening, uploadingEmbeddings } = useAppSelector( (state) => state.chat ); const [isRenaming, setIsRenaming] = useState(null); const [deleteChatIndex, setDeletingChatIndex] = useState(-1); const [newTitle, setNewTitle] = useState(""); const { toast } = useToast(); if (!chats) { return (
); } const filteredChats = chats.filter((chat) => chat.title.toLowerCase().includes(searchQuery.toLowerCase()) ); const handleCreateNewChat = () => { const newSeesionId = uuidv4(); dispatch(resetSessionId(newSeesionId)); dispatch(createChat(newSeesionId)); dispatch(setActiveMessages([])); dispatch(setActiveContext([])); dispatch(setActiveChat(newSeesionId)); dispatch(setActiveChatPrompt("")); toast({ title: "New chat created", description: "Start a fresh conversation with your AI assistant.", }); if (isMobileSidebar && onClose) { onClose(); } }; const handleSelectChat = async (chatId: string) => { if (isListening) { return; } if (chatId !== activeChat) { dispatch(setActiveChat(chatId)); const activeChats = chats.find((item) => item.id == chatId); dispatch(setActiveMessages(activeChats.chat)); dispatch(setActiveContext(activeChats.context)); dispatch(setActiveChatPrompt(activeChats?.prompt || "")); dispatch(setSessionId(chatId)); if (isMobileSidebar && onClose) { onClose(); } } }; const handleStartRename = ( chatId: string, currentTitle: string, event: React.MouseEvent ) => { event.stopPropagation(); // Prevent chat selection setIsRenaming(chatId); setNewTitle(currentTitle); }; const handleRenameChat = async (chatId: string) => { try { if (newTitle.trim()) { await dispatch( renameChat({ sessionId: chatId, title: newTitle.trim() }) ).unwrap(); toast({ title: "Chat renamed", description: "Your chat has been renamed successfully.", }); } } catch (error) { console.error(error); } finally { setIsRenaming(null); } }; const handleCloseSidebar = () => { if (isMobileSidebar && onClose) { onClose(); } else { dispatch(setSidebarVisibility(false)); } }; const handleClearChat = async (index: number, chatId: string) => { try { setDeletingChatIndex(index); const response = await api.post("/chat/delete-chat", { sessionId: chatId, }); console.log(response.data); if (response.success) { dispatch(deleteChat(chatId)); dispatch(clearEmbeddings()); dispatch(clearActiveMessages()); toast({ title: "Chat deleted", description: "The chat has been removed from your history.", }); } } catch (error) { toast({ title: "Error", description: "failed to delete chat.", variant: "destructive", }); } finally { setDeletingChatIndex(-1); } }; return (
dispatch(setSearchQuery(e.target.value))} />
{filteredChats.length === 0 ? (
No chats found
) : ( filteredChats.map((chat, index) => (
{ handleSelectChat(chat.id); }} > {isRenaming === chat.id ? (
e.stopPropagation()} > setNewTitle(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleRenameChat(chat.id); } else if (e.key === "Escape") { setIsRenaming(null); } }} autoFocus />
) : ( <>
{chat.title}
{new Date(chat.lastUpdatedAt).toLocaleDateString()}
)}
)) )}
); }; export default ChatSidebar;