fix delete error
This commit is contained in:
		
							parent
							
								
									dcfd3d956e
								
							
						
					
					
						commit
						742efea50d
					
				|  | @ -3,20 +3,22 @@ import { IconMessage, IconTrash } from "@tabler/icons-react"; | ||||||
| import { FC } from "react"; | import { FC } from "react"; | ||||||
| 
 | 
 | ||||||
| interface Props { | interface Props { | ||||||
|  |   loading: boolean; | ||||||
|   conversations: Conversation[]; |   conversations: Conversation[]; | ||||||
|   selectedConversation: Conversation; |   selectedConversation: Conversation; | ||||||
|   onSelectConversation: (conversation: Conversation) => void; |   onSelectConversation: (conversation: Conversation) => void; | ||||||
|   onDeleteConversation: (conversation: Conversation) => void; |   onDeleteConversation: (conversation: Conversation) => void; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const Conversations: FC<Props> = ({ conversations, selectedConversation, onSelectConversation, onDeleteConversation }) => { | export const Conversations: FC<Props> = ({ loading, conversations, selectedConversation, onSelectConversation, onDeleteConversation }) => { | ||||||
|   return ( |   return ( | ||||||
|     <div className="flex flex-col space-y-2"> |     <div className="flex flex-col space-y-2"> | ||||||
|       {conversations.map((conversation, index) => ( |       {conversations.map((conversation, index) => ( | ||||||
|         <div |         <button | ||||||
|           key={index} |           key={index} | ||||||
|           className={`flex items-center justify-start w-[240px] h-[40px] px-2 text-sm rounded-lg hover:bg-neutral-700 cursor-pointer ${selectedConversation.id === conversation.id ? "bg-slate-600" : ""}`} |           className={`flex items-center justify-start w-[240px] h-[40px] px-2 text-sm rounded-lg hover:bg-neutral-700 cursor-pointer ${loading ? "disabled:cursor-not-allowed" : ""} ${selectedConversation.id === conversation.id ? "bg-slate-600" : ""}`} | ||||||
|           onClick={() => onSelectConversation(conversation)} |           onClick={() => onSelectConversation(conversation)} | ||||||
|  |           disabled={loading} | ||||||
|         > |         > | ||||||
|           <IconMessage |           <IconMessage | ||||||
|             className="mr-2 min-w-[20px]" |             className="mr-2 min-w-[20px]" | ||||||
|  | @ -32,7 +34,7 @@ export const Conversations: FC<Props> = ({ conversations, selectedConversation, | ||||||
|               onDeleteConversation(conversation); |               onDeleteConversation(conversation); | ||||||
|             }} |             }} | ||||||
|           /> |           /> | ||||||
|         </div> |         </button> | ||||||
|       ))} |       ))} | ||||||
|     </div> |     </div> | ||||||
|   ); |   ); | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ import { Conversations } from "./Conversations"; | ||||||
| import { SidebarSettings } from "./SidebarSettings"; | import { SidebarSettings } from "./SidebarSettings"; | ||||||
| 
 | 
 | ||||||
| interface Props { | interface Props { | ||||||
|  |   loading: boolean; | ||||||
|   conversations: Conversation[]; |   conversations: Conversation[]; | ||||||
|   lightMode: "light" | "dark"; |   lightMode: "light" | "dark"; | ||||||
|   selectedConversation: Conversation; |   selectedConversation: Conversation; | ||||||
|  | @ -14,7 +15,7 @@ interface Props { | ||||||
|   onDeleteConversation: (conversation: Conversation) => void; |   onDeleteConversation: (conversation: Conversation) => void; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const Sidebar: FC<Props> = ({ conversations, lightMode, selectedConversation, onNewConversation, onToggleLightMode, onSelectConversation, onDeleteConversation }) => { | export const Sidebar: FC<Props> = ({ loading, conversations, lightMode, selectedConversation, onNewConversation, onToggleLightMode, onSelectConversation, onDeleteConversation }) => { | ||||||
|   return ( |   return ( | ||||||
|     <div className="flex flex-col bg-[#202123] min-w-[260px]"> |     <div className="flex flex-col bg-[#202123] min-w-[260px]"> | ||||||
|       <div className="flex items-center justify-center h-[60px]"> |       <div className="flex items-center justify-center h-[60px]"> | ||||||
|  | @ -32,6 +33,7 @@ export const Sidebar: FC<Props> = ({ conversations, lightMode, selectedConversat | ||||||
| 
 | 
 | ||||||
|       <div className="flex-1 mx-auto pb-2 overflow-auto"> |       <div className="flex-1 mx-auto pb-2 overflow-auto"> | ||||||
|         <Conversations |         <Conversations | ||||||
|  |           loading={loading} | ||||||
|           conversations={conversations} |           conversations={conversations} | ||||||
|           selectedConversation={selectedConversation} |           selectedConversation={selectedConversation} | ||||||
|           onSelectConversation={onSelectConversation} |           onSelectConversation={onSelectConversation} | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ export default function Home() { | ||||||
|   const [loading, setLoading] = useState<boolean>(false); |   const [loading, setLoading] = useState<boolean>(false); | ||||||
|   const [model, setModel] = useState<OpenAIModel>(OpenAIModel.GPT_3_5); |   const [model, setModel] = useState<OpenAIModel>(OpenAIModel.GPT_3_5); | ||||||
|   const [lightMode, setLightMode] = useState<"dark" | "light">("dark"); |   const [lightMode, setLightMode] = useState<"dark" | "light">("dark"); | ||||||
|  |   const [disabled, setDisabled] = useState<boolean>(false); | ||||||
| 
 | 
 | ||||||
|   const handleSend = async (message: Message) => { |   const handleSend = async (message: Message) => { | ||||||
|     if (selectedConversation) { |     if (selectedConversation) { | ||||||
|  | @ -20,6 +21,7 @@ export default function Home() { | ||||||
| 
 | 
 | ||||||
|       setSelectedConversation(updatedConversation); |       setSelectedConversation(updatedConversation); | ||||||
|       setLoading(true); |       setLoading(true); | ||||||
|  |       setDisabled(true); | ||||||
| 
 | 
 | ||||||
|       const response = await fetch("/api/chat", { |       const response = await fetch("/api/chat", { | ||||||
|         method: "POST", |         method: "POST", | ||||||
|  | @ -106,6 +108,8 @@ export default function Home() { | ||||||
|       setConversations(updatedConversations); |       setConversations(updatedConversations); | ||||||
| 
 | 
 | ||||||
|       localStorage.setItem("conversationHistory", JSON.stringify(updatedConversations)); |       localStorage.setItem("conversationHistory", JSON.stringify(updatedConversations)); | ||||||
|  | 
 | ||||||
|  |       setDisabled(false); | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
| 
 | 
 | ||||||
|  | @ -142,8 +146,15 @@ export default function Home() { | ||||||
|     setConversations(updatedConversations); |     setConversations(updatedConversations); | ||||||
|     localStorage.setItem("conversationHistory", JSON.stringify(updatedConversations)); |     localStorage.setItem("conversationHistory", JSON.stringify(updatedConversations)); | ||||||
| 
 | 
 | ||||||
|     if (selectedConversation && selectedConversation.id === conversation.id) { |     if (updatedConversations.length > 0) { | ||||||
|       setSelectedConversation(undefined); |       setSelectedConversation(updatedConversations[0]); | ||||||
|  |       localStorage.setItem("selectedConversation", JSON.stringify(updatedConversations[0])); | ||||||
|  |     } else { | ||||||
|  |       setSelectedConversation({ | ||||||
|  |         id: 1, | ||||||
|  |         name: "", | ||||||
|  |         messages: [] | ||||||
|  |       }); | ||||||
|       localStorage.removeItem("selectedConversation"); |       localStorage.removeItem("selectedConversation"); | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
|  | @ -193,6 +204,7 @@ export default function Home() { | ||||||
|       {selectedConversation && ( |       {selectedConversation && ( | ||||||
|         <div className={`flex h-screen text-white ${lightMode}`}> |         <div className={`flex h-screen text-white ${lightMode}`}> | ||||||
|           <Sidebar |           <Sidebar | ||||||
|  |             loading={disabled} | ||||||
|             conversations={conversations} |             conversations={conversations} | ||||||
|             lightMode={lightMode} |             lightMode={lightMode} | ||||||
|             selectedConversation={selectedConversation} |             selectedConversation={selectedConversation} | ||||||
|  |  | ||||||
|  | @ -20,7 +20,7 @@ export const OpenAIStream = async (model: OpenAIModel, messages: Message[]) => { | ||||||
|         }, |         }, | ||||||
|         ...messages |         ...messages | ||||||
|       ], |       ], | ||||||
|       max_tokens: 800, |       max_tokens: 1000, | ||||||
|       temperature: 0.0, |       temperature: 0.0, | ||||||
|       stream: true |       stream: true | ||||||
|     }) |     }) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue