From ff0dcd5095462ac5a482d9370287b728ad1f9ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aneta=20Jastrz=C4=99bska?= <1544710+anetaj@users.noreply.github.com> Date: Sun, 22 Sep 2024 11:04:28 +0200 Subject: [PATCH] handle show thread history --- extensions/void/src/sidebar/Sidebar.tsx | 34 +++++++++-------- .../src/sidebar/components/ThreadHistory.tsx | 38 ++++++++++++++----- extensions/void/src/sidebar/context.tsx | 13 +++---- extensions/void/src/sidebar/getVscodeApi.ts | 2 + 4 files changed, 56 insertions(+), 31 deletions(-) diff --git a/extensions/void/src/sidebar/Sidebar.tsx b/extensions/void/src/sidebar/Sidebar.tsx index df543a0c..c36d3e88 100644 --- a/extensions/void/src/sidebar/Sidebar.tsx +++ b/extensions/void/src/sidebar/Sidebar.tsx @@ -85,10 +85,9 @@ const useInstantState = (initVal: T) => { const Sidebar = () => { const { - chatMessageHistory, + thread, addMessageToHistory, setPreviousThreads, - previousThreads, startNewChat, } = useChat() @@ -141,10 +140,21 @@ const Sidebar = () => { setPreviousThreads(m.threads) } + // top navigation bar command - new chat + else if (m.type === 'startNewChat') { + setShowThreadsHistory(false) + startNewChat() + } + + // top navigation bar command - new chat + else if (m.type === 'showPreviousChats') { + setShowThreadsHistory(true) + } + } window.addEventListener('message', listener); return () => { window.removeEventListener('message', listener) } - }, [files, selection, setPreviousThreads]) + }, [files, selection, setPreviousThreads, startNewChat]) const formRef = useRef(null) @@ -171,7 +181,7 @@ const Sidebar = () => { // send message to claude let { abort } = sendLLMMessage({ - messages: [...chatMessageHistory.map(m => ({ role: m.role, content: m.content })), { role: 'user', content }], + messages: [...thread.messages.map(m => ({ role: m.role, content: m.content })), { role: 'user', content }], onText: (newText, fullText) => setMessageStream(fullText), onFinalMessage: (content) => { @@ -210,20 +220,14 @@ const Sidebar = () => { return <>
-
-
- {!!chatMessageHistory.length && } - {!!previousThreads.length && ( - - )} + {showThreadsHistory && ( +
+ setShowThreadsHistory(false)} />
- {showThreadsHistory && } -
+ )}
{/* previous messages */} - {chatMessageHistory.map((message, i) => + {thread.messages.map((message, i) => )} {/* message stream */} diff --git a/extensions/void/src/sidebar/components/ThreadHistory.tsx b/extensions/void/src/sidebar/components/ThreadHistory.tsx index 4e0d0300..0994ae22 100644 --- a/extensions/void/src/sidebar/components/ThreadHistory.tsx +++ b/extensions/void/src/sidebar/components/ThreadHistory.tsx @@ -1,19 +1,39 @@ import React from "react" -import { ChatThread } from "../../shared_types" import { useChat } from "../context" +import { classNames } from "../utils" -const ThreadHistory = ({ threads }: { threads: ChatThread[] }) => { - const { selectThread } = useChat() +const ThreadHistory = ({ onClose }: { onClose: () => void }) => { + const { selectThread, previousThreads, thread } = useChat() return ( -
- {threads.map((thread) => ( +
+
+ +
+ {previousThreads.map((prevThread) => ( ))}
diff --git a/extensions/void/src/sidebar/context.tsx b/extensions/void/src/sidebar/context.tsx index 6c2ab653..c294423d 100644 --- a/extensions/void/src/sidebar/context.tsx +++ b/extensions/void/src/sidebar/context.tsx @@ -9,9 +9,9 @@ import { ChatMessage, ChatThread } from "../shared_types" import { getVSCodeAPI } from "./getVscodeApi" const createEmptyThread = () => ({ - id: "", - createdAt: "", - messages: [], + id: "", + createdAt: "", + messages: [], }) const createNewThread = () => ({ @@ -21,7 +21,7 @@ const createNewThread = () => ({ }) interface IChatProviderProps { - chatMessageHistory: ChatMessage[] + thread: ChatThread addMessageToHistory: (message: ChatMessage) => void setPreviousThreads: (threads: any) => void previousThreads: ChatThread[] @@ -30,10 +30,9 @@ interface IChatProviderProps { } const defaults = { - chatMessageHistory: [], addMessageToHistory: () => {}, setPreviousThreads: () => {}, - // placeholder for thread until first message is sent so that createdAt date is accurate + // placeholder for thread until first message is sent so that createdAt date is accurate thread: createEmptyThread(), previousThreads: [], selectThread: () => {}, @@ -78,7 +77,7 @@ function ChatProvider({ children }: { children: ReactNode }) { return ( void)[] } = { "getThreadHistory": [], "threadHistory": [], "updateThread": [], + "startNewChat": [], + "showPreviousChats": [], } // use this function to await responses