Skip to content

Commit f278e5b

Browse files
committed
split message.tsx
1 parent c7f731c commit f278e5b

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

src/chatbox.tsx

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState } from "preact/hooks";
22
import type { ChatStore } from "./app";
33
import ChatGPT, { ChunkMessage } from "./chatgpt";
4+
import Message from "./message";
45
import Settings from "./settings";
56

67
export default function ChatBOX(props: {
@@ -184,51 +185,13 @@ export default function ChatBOX(props: {
184185
暂无历史对话记录
185186
</p>
186187
)}
187-
{chatStore.history.map((chat, i) => {
188-
const pClassName =
189-
chat.role === "assistant"
190-
? "p-2 rounded relative bg-white my-2 text-left dark:bg-gray-700 dark:text-white"
191-
: "p-2 rounded relative bg-green-400 my-2 text-right";
192-
const iconClassName =
193-
chat.role === "user"
194-
? "absolute bottom-0 left-0"
195-
: "absolute bottom-0 right-0";
196-
const DeleteIcon = () => (
197-
<button
198-
className={iconClassName}
199-
onClick={() => {
200-
if (
201-
confirm(
202-
`Are you sure to delete this message?\n${chat.content.slice(
203-
0,
204-
39
205-
)}...`
206-
)
207-
) {
208-
chatStore.history.splice(i, 1);
209-
chatStore.postBeginIndex = Math.max(
210-
chatStore.postBeginIndex - 1,
211-
0
212-
);
213-
setChatStore({ ...chatStore });
214-
}
215-
}}
216-
>
217-
🗑️
218-
</button>
219-
);
220-
return (
221-
<p className={pClassName}>
222-
{chat.content
223-
.split("\n")
224-
.filter((line) => line)
225-
.map((line) => (
226-
<p className="my-1">{line}</p>
227-
))}
228-
<DeleteIcon />
229-
</p>
230-
);
231-
})}
188+
{chatStore.history.map((_, messageIndex) => (
189+
<Message
190+
chatStore={chatStore}
191+
setChatStore={setChatStore}
192+
messageIndex={messageIndex}
193+
/>
194+
))}
232195
{showGenerating && (
233196
<p className="p-2 my-2 animate-pulse dark:text-white">
234197
{generatingMessage

src/message.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ChatStore } from "./app";
2+
3+
interface Props {
4+
messageIndex: number;
5+
chatStore: ChatStore;
6+
setChatStore: (cs: ChatStore) => void;
7+
}
8+
export default function Message(props: Props) {
9+
const { chatStore, messageIndex, setChatStore } = props;
10+
const chat = chatStore.history[messageIndex];
11+
const pClassName =
12+
chat.role === "assistant"
13+
? "p-2 rounded relative bg-white my-2 text-left dark:bg-gray-700 dark:text-white"
14+
: "p-2 rounded relative bg-green-400 my-2 text-right";
15+
const iconClassName =
16+
chat.role === "user"
17+
? "absolute bottom-0 left-0"
18+
: "absolute bottom-0 right-0";
19+
const DeleteIcon = () => (
20+
<button
21+
className={iconClassName}
22+
onClick={() => {
23+
if (
24+
confirm(
25+
`Are you sure to delete this message?\n${chat.content.slice(
26+
0,
27+
39
28+
)}...`
29+
)
30+
) {
31+
chatStore.history.splice(messageIndex, 1);
32+
chatStore.postBeginIndex = Math.max(chatStore.postBeginIndex - 1, 0);
33+
setChatStore({ ...chatStore });
34+
}
35+
}}
36+
>
37+
🗑️
38+
</button>
39+
);
40+
return (
41+
<p className={pClassName}>
42+
{chat.content
43+
.split("\n")
44+
.filter((line) => line)
45+
.map((line) => (
46+
<p className="my-1">{line}</p>
47+
))}
48+
<DeleteIcon />
49+
</p>
50+
);
51+
}

0 commit comments

Comments
 (0)