Skip to content

Commit 7b17040

Browse files
committed
save client information for switch between conversations
Signed-off-by: cbh778899 <[email protected]>
1 parent 332c8eb commit 7b17040

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/components/chat/Conversation.jsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { FileImageFill, FileTextFill, Paperclip, Send, StopCircleFill } from 're
44
import useIDB from "../../utils/idb";
55
import { isModelLoaded, loadModel } from '../../utils/workers/worker'
66
import { getCompletionFunctions } from "../../utils/workers";
7-
import { setClient } from "../../utils/workers/aws-worker";
7+
import { setClient as setAwsClient } from "../../utils/workers/aws-worker";
8+
import { setClient as setOpenaiClient } from "../../utils/workers/openai-worker";
89

9-
export default function Conversation({ uid, client }) {
10+
export default function Conversation({ uid, client, updateClient }) {
1011

1112
const [conversation, setConversation] = useState([]);
1213
const [message, setMessage] = useState('');
@@ -130,15 +131,21 @@ export default function Conversation({ uid, client }) {
130131
useEffect(()=>{
131132
if(!chat_functions.current) return;
132133

133-
if(chat_functions.current.platform === 'AWS') {
134+
const platform = chat_functions.current.platform
135+
if(platform) {
134136
(async function() {
135-
if(await setClient(client)) {
136-
await idb.updateOne('chat-history', {client}, [{uid}])
137+
let set_result =
138+
platform === "AWS" ? await setAwsClient(client) :
139+
platform === "OpenAI" ? await setOpenaiClient(client) :
140+
null;
141+
142+
if(set_result) {
143+
updateClient(set_result);
137144
}
138145
})()
139146
}
140147
// eslint-disable-next-line
141-
}, [chat_functions, client])
148+
}, [client])
142149

143150
return (
144151
<div className="conversation-main">

src/components/chat/Tickets.jsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect } from "react";
22
import Ticket from "./Ticket";
33
import useIDB from "../../utils/idb";
44
import { genRandomID } from "../../utils/tools";
55

6-
export default function Tickets({selectChat, current_chat}) {
6+
export default function Tickets({selectChat, current_chat, history, setHistory}) {
77

8-
const [tickets, setTickets] = useState([]);
98
const idb = useIDB();
109

1110
async function syncHistory() {
1211
const history = await idb.getAll('chat-history')
1312
history.sort((a, b)=>b.updatedAt - a.updatedAt)
14-
setTickets(history)
13+
setHistory(history)
1514
}
1615

1716
async function startNewConversation() {
@@ -27,9 +26,9 @@ export default function Tickets({selectChat, current_chat}) {
2726
)
2827
const new_conv_info = await idb.getByID('chat-history', conv_id);
2928
new_conv_info &&
30-
setTickets([
29+
setHistory([
3130
new_conv_info,
32-
...tickets
31+
...history
3332
])
3433
selectChat(new_conv_info)
3534
}
@@ -47,7 +46,7 @@ export default function Tickets({selectChat, current_chat}) {
4746
>
4847
<div>Start New Chat</div>
4948
</div>
50-
{ tickets.map(elem => {
49+
{ history.map(elem => {
5150
const { title, uid } = elem;
5251
return (
5352
<Ticket

src/components/chat/index.jsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
import { useState } from "react";
22
import Tickets from "./Tickets";
33
import Conversation from "./Conversation";
4+
import useIDB from "../../utils/idb";
45

56
export default function Chat() {
67

78
const [chat, selectChat] = useState({});
9+
const [history, setHistory] = useState([]);
10+
const idb = useIDB();
11+
12+
function updateChatClient(client) {
13+
selectChat({
14+
...chat, client
15+
})
16+
17+
let history_cp = [...history];
18+
history_cp[
19+
history_cp.findIndex(e=>e.uid === chat.uid)
20+
].client = client;
21+
setHistory(history_cp);
22+
23+
idb.updateOne('chat-history', {client}, [{uid:chat.uid}])
24+
}
825

926
return (
1027
<div className="chat">
11-
<Tickets selectChat={selectChat} current_chat={chat} />
12-
<Conversation uid={chat.uid} client={chat.client} />
28+
<Tickets selectChat={selectChat} setHistory={setHistory} history={history} current_chat={chat} />
29+
<Conversation uid={chat.uid} client={chat.client} updateClient={updateChatClient} />
1330
</div>
1431
)
1532
}

0 commit comments

Comments
 (0)