From a0056460abd1ccc3a97953188d08f94fce8d75bf Mon Sep 17 00:00:00 2001 From: Mckay Wrigley Date: Sat, 18 Mar 2023 03:57:15 -0600 Subject: [PATCH] add rename and delete improvements --- components/Chat/Chat.tsx | 36 ++++----- components/Chat/ChatLoader.tsx | 6 +- components/Chat/ChatMessage.tsx | 4 +- components/Sidebar/Conversations.tsx | 111 +++++++++++++++++++++++---- components/Sidebar/Sidebar.tsx | 12 +-- pages/index.tsx | 44 ++++++++--- 6 files changed, 160 insertions(+), 53 deletions(-) diff --git a/components/Chat/Chat.tsx b/components/Chat/Chat.tsx index 9f0301c..1a8a97e 100644 --- a/components/Chat/Chat.tsx +++ b/components/Chat/Chat.tsx @@ -26,21 +26,21 @@ export const Chat: FC = ({ model, messages, loading, lightMode, onSend, o }, [messages]); return ( -
- {messages.length === 0 ? ( - <> -
- -
+
+
+ {messages.length === 0 ? ( + <> +
+ +
-
Chatbot UI Pro
- - ) : ( - <> -
+
Chatbot UI Pro
+ + ) : ( + <>
Model: {OpenAIModelNames[model]}
{messages.map((message, index) => ( @@ -53,11 +53,11 @@ export const Chat: FC = ({ model, messages, loading, lightMode, onSend, o ))} {loading && }
-
- - )} + + )} +
-
+
diff --git a/components/Chat/ChatLoader.tsx b/components/Chat/ChatLoader.tsx index b81fbd7..af16558 100644 --- a/components/Chat/ChatLoader.tsx +++ b/components/Chat/ChatLoader.tsx @@ -6,11 +6,11 @@ interface Props {} export const ChatLoader: FC = () => { return (
-
-
AI:
+
+
AI:
diff --git a/components/Chat/ChatMessage.tsx b/components/Chat/ChatMessage.tsx index 4441264..f9948f8 100644 --- a/components/Chat/ChatMessage.tsx +++ b/components/Chat/ChatMessage.tsx @@ -11,10 +11,10 @@ interface Props { export const ChatMessage: FC = ({ message, lightMode }) => { return (
-
+
{message.role === "assistant" ? "AI:" : "You:"}
diff --git a/components/Sidebar/Conversations.tsx b/components/Sidebar/Conversations.tsx index ec97bda..321ff5b 100644 --- a/components/Sidebar/Conversations.tsx +++ b/components/Sidebar/Conversations.tsx @@ -1,6 +1,6 @@ import { Conversation } from "@/types"; -import { IconMessage, IconTrash } from "@tabler/icons-react"; -import { FC } from "react"; +import { IconCheck, IconMessage, IconPencil, IconTrash, IconX } from "@tabler/icons-react"; +import { FC, KeyboardEvent, useEffect, useState } from "react"; interface Props { loading: boolean; @@ -8,15 +8,41 @@ interface Props { selectedConversation: Conversation; onSelectConversation: (conversation: Conversation) => void; onDeleteConversation: (conversation: Conversation) => void; + onRenameConversation: (conversation: Conversation, name: string) => void; } -export const Conversations: FC = ({ loading, conversations, selectedConversation, onSelectConversation, onDeleteConversation }) => { +export const Conversations: FC = ({ loading, conversations, selectedConversation, onSelectConversation, onDeleteConversation, onRenameConversation }) => { + const [isDeleting, setIsDeleting] = useState(false); + const [isRenaming, setIsRenaming] = useState(false); + const [renameValue, setRenameValue] = useState(""); + + const handleEnterDown = (e: KeyboardEvent) => { + if (e.key === "Enter") { + e.preventDefault(); + handleRename(selectedConversation); + } + }; + + const handleRename = (conversation: Conversation) => { + onRenameConversation(conversation, renameValue); + setRenameValue(""); + setIsRenaming(false); + }; + + useEffect(() => { + if (isRenaming) { + setIsDeleting(false); + } else if (isDeleting) { + setIsRenaming(false); + } + }, [isRenaming, isDeleting]); + return ( -
+
{conversations.map((conversation, index) => ( ))}
diff --git a/components/Sidebar/Sidebar.tsx b/components/Sidebar/Sidebar.tsx index ff8451c..ed9ef4c 100644 --- a/components/Sidebar/Sidebar.tsx +++ b/components/Sidebar/Sidebar.tsx @@ -14,14 +14,15 @@ interface Props { onSelectConversation: (conversation: Conversation) => void; onDeleteConversation: (conversation: Conversation) => void; onToggleSidebar: () => void; + onRenameConversation: (conversation: Conversation, name: string) => void; } -export const Sidebar: FC = ({ loading, conversations, lightMode, selectedConversation, onNewConversation, onToggleLightMode, onSelectConversation, onDeleteConversation, onToggleSidebar }) => { +export const Sidebar: FC = ({ loading, conversations, lightMode, selectedConversation, onNewConversation, onToggleLightMode, onSelectConversation, onDeleteConversation, onToggleSidebar, onRenameConversation }) => { return (
-
+
-
+
diff --git a/pages/index.tsx b/pages/index.tsx index 2896ab8..2adad71 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -68,6 +68,7 @@ export default function Home() { updatedConversation = { ...updatedConversation, + name: message.content, messages: updatedMessages }; @@ -120,12 +121,34 @@ export default function Home() { localStorage.setItem("theme", mode); }; + const handleRenameConversation = (conversation: Conversation, name: string) => { + const updatedConversations = conversations.map((c) => { + if (c.id === conversation.id) { + return { + ...c, + name + }; + } + + return c; + }); + + setConversations(updatedConversations); + localStorage.setItem("conversationHistory", JSON.stringify(updatedConversations)); + + setSelectedConversation({ + ...conversation, + name + }); + localStorage.setItem("selectedConversation", JSON.stringify(selectedConversation)); + }; + const handleNewConversation = () => { const lastConversation = conversations[conversations.length - 1]; const newConversation: Conversation = { id: lastConversation ? lastConversation.id + 1 : 1, - name: "", + name: "New conversation", messages: [] }; @@ -218,6 +241,7 @@ export default function Home() { onSelectConversation={handleSelectConversation} onDeleteConversation={handleDeleteConversation} onToggleSidebar={() => setShowSidebar(!showSidebar)} + onRenameConversation={handleRenameConversation} /> ) : ( )} -
- -
+
)}