File size: 9,189 Bytes
5306da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
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<ChatSidebarProps> = ({
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<string | null>(null);
const [deleteChatIndex, setDeletingChatIndex] = useState<number>(-1);
const [newTitle, setNewTitle] = useState("");
const { toast } = useToast();
if (!chats) {
return (
<div>
<Loader2 className="animate-spin text-white" />
</div>
);
}
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 (
<div
className={`flex pt-5 flex-col h-full bg-black border-r border-white/10 ${
isMobileSidebar ? "w-full" : "w-64"
}`}
>
<div className="flex items-center justify-between p-4 border-b border-white/10">
<Button
onClick={handleCreateNewChat}
className="bg-white text-black hover:bg-white/90 flex items-center justify-center gap-2 flex-1 mr-2"
>
<Plus size={16} /> New Chat
</Button>
<Button
variant="ghost"
size="icon"
onClick={handleCloseSidebar}
className="h-9 w-9 text-white/70 hover:text-gray-500"
>
<ArrowLeft size={18} />
</Button>
</div>
<div className="p-4 border-b border-white/10">
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-white/50 h-4 w-4" />
<Input
className="pl-10 bg-white/10 border-white/20 text-white placeholder:text-white/50 focus:border-white/30"
placeholder="Search chats..."
value={searchQuery}
onChange={(e) => dispatch(setSearchQuery(e.target.value))}
/>
</div>
</div>
<div className="flex-1 overflow-y-auto p-2">
{filteredChats.length === 0 ? (
<div className="text-center text-white/50 p-4">No chats found</div>
) : (
filteredChats.map((chat, index) => (
<div
key={chat.id}
className={`flex items-center justify-between p-3 mb-1 rounded-lg cursor-pointer group transition-all ${
chat.id === activeChat
? "bg-white/20 text-white"
: "hover:bg-white/10 text-white/80"
}`}
onClick={() => {
handleSelectChat(chat.id);
}}
>
{isRenaming === chat.id ? (
<div
className="flex-1 flex"
onClick={(e) => e.stopPropagation()}
>
<Input
className="flex-1 bg-black border-white/30 text-white"
value={newTitle}
onChange={(e) => setNewTitle(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
handleRenameChat(chat.id);
} else if (e.key === "Escape") {
setIsRenaming(null);
}
}}
autoFocus
/>
<Button
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation();
handleRenameChat(chat.id);
}}
className="ml-1"
>
<ArrowRight size={16} />
</Button>
</div>
) : (
<>
<div className="flex-1 truncate mr-2">
<div className="flex items-center">
<List className="h-4 w-4 mr-2 opacity-70" />
<span className="truncate">{chat.title}</span>
</div>
<div className="text-xs opacity-50 mt-1">
{new Date(chat.lastUpdatedAt).toLocaleDateString()}
</div>
</div>
<div className="flex opacity-0 group-hover:opacity-100 transition-opacity">
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={(e) => handleStartRename(chat.id, chat.title, e)}
>
<Edit className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
className="relative h-8 w-8 text-red-400 hover:text-red-300"
onClick={(e) => {
e.stopPropagation();
handleClearChat(index, chat.id);
}}
disabled={deleteChatIndex !== -1}
>
{deleteChatIndex !== -1 && deleteChatIndex === index ? (
<Loader2 className="animate-spin h-4 w-4" />
) : (
<Trash className="h-4 w-4" />
)}
</Button>
</div>
</>
)}
</div>
))
)}
</div>
</div>
);
};
export default ChatSidebar;
|