-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
121 lines (112 loc) · 3.03 KB
/
App.tsx
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
import { vscode } from "./utilities/vscode";
import {
VSCodeButton,
VSCodeProgressRing,
VSCodeTextArea,
VSCodeTextField,
} from "@vscode/webview-ui-toolkit/react";
import "./App.css";
// import "./codicon.css";
import { ChatMessage } from "./components/ChatMessage";
import { useState } from "react";
import { ChatHelloMessage } from "./components/ChatHelloMessage";
import { useMessageListener } from "./hooks/messageListener";
import { randomMessageId } from "./utilities/messageId";
import TextArea from "./components/TextArea";
// import ProgressDivider from "./components/VsCodeDividerProgress";
export const App = () => {
const [input, setInput] = useState("");
const [chatHistory, setChatHistory] = useState<
{
role: string;
content: string;
chatMessageId: string;
}[]
>([]);
const [isLoading, setIsLoading] = useState(false);
useMessageListener("startNewChat", () => {
setChatHistory([]);
});
const sendMessage = async (chatHistoryLocal: any) => {
const messageId = randomMessageId();
await vscode.postMessageCallback(
{
type: "sendMessage",
data: chatHistoryLocal,
},
(newMessage) => {
setChatHistory((chatHistoryLocal) => {
const messages = chatHistoryLocal.filter(
(message) => message.chatMessageId !== messageId
);
if (newMessage.done) {
setIsLoading(false);
return chatHistoryLocal;
}
return [
...messages,
{
role: "ai",
content: newMessage.data,
chatMessageId: messageId,
} as any,
];
});
}
);
};
const onSubmit = () => {
if (isLoading) {
return;
}
setChatHistory((value) => {
const messageId = randomMessageId();
const newHistory = [
...value,
{
role: "user",
content: input,
chatMessageId: messageId,
},
];
setIsLoading(true);
sendMessage(newHistory);
return newHistory;
});
setInput("");
};
return (
<>
<main>
<div className="chat-history">
<ChatHelloMessage />
{chatHistory.map((item) => (
<ChatMessage role={item.role} content={item.content} />
))}
</div>
<div className="chat-input-block">
<TextArea
value={input}
onChange={(value) => setInput(value || "")}
onSubmit={onSubmit}
buttonEnd={
<VSCodeButton
appearance="icon"
disabled={isLoading}
onClick={onSubmit}
>
<span
className={`codicon ${
isLoading
? "codicon-loading codicon-modifier-spin codicon-modifier-disabled"
: "codicon-send"
}`}
></span>
</VSCodeButton>
}
></TextArea>
</div>
</main>
</>
);
};