Skip to content

Commit 875fe99

Browse files
author
James
committed
fix: add back last message
Signed-off-by: James <[email protected]>
1 parent f48f552 commit 875fe99

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

web/helpers/EventHandler.tsx

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import { addNewMessageAtom, updateMessageAtom } from './atoms/ChatMessage.atom'
22
import { toChatMessage } from '@models/ChatMessage'
3-
import { events, EventName, NewMessageResponse } from '@janhq/core'
3+
import { events, EventName, NewMessageResponse, DataService } from '@janhq/core'
44
import { useSetAtom } from 'jotai'
55
import { ReactNode, useEffect } from 'react'
66
import useGetBots from '@hooks/useGetBots'
77
import useGetUserConversations from '@hooks/useGetUserConversations'
8+
import { updateConversationAtom } from './atoms/Conversation.atom'
9+
import { executeSerial } from '../../electron/core/plugin-manager/execution/extension-manager'
10+
import { debounce } from 'lodash'
11+
12+
let currentConversation: Conversation | undefined = undefined
13+
14+
const debouncedUpdateConversation = debounce(
15+
async (updatedConv: Conversation) => {
16+
await executeSerial(DataService.UpdateConversation, updatedConv)
17+
},
18+
1000
19+
)
820

921
export default function EventHandler({ children }: { children: ReactNode }) {
1022
const addNewMessage = useSetAtom(addNewMessageAtom)
1123
const updateMessage = useSetAtom(updateMessageAtom)
24+
const updateConversation = useSetAtom(updateConversationAtom)
1225
const { getBotById } = useGetBots()
1326
const { getConversationById } = useGetUserConversations()
1427

@@ -34,12 +47,32 @@ export default function EventHandler({ children }: { children: ReactNode }) {
3447
messageResponse.conversationId &&
3548
messageResponse._id &&
3649
messageResponse.message
37-
)
50+
) {
3851
updateMessage(
3952
messageResponse._id,
4053
messageResponse.conversationId,
4154
messageResponse.message
4255
)
56+
}
57+
58+
if (messageResponse.conversationId) {
59+
if (
60+
!currentConversation ||
61+
currentConversation._id !== messageResponse.conversationId
62+
) {
63+
currentConversation = await getConversationById(
64+
messageResponse.conversationId
65+
)
66+
}
67+
68+
const updatedConv: Conversation = {
69+
...currentConversation,
70+
lastMessage: messageResponse.message,
71+
}
72+
73+
updateConversation(updatedConv)
74+
debouncedUpdateConversation(updatedConv)
75+
}
4376
}
4477

4578
useEffect(() => {
@@ -57,3 +90,6 @@ export default function EventHandler({ children }: { children: ReactNode }) {
5790
}, [])
5891
return <>{children}</>
5992
}
93+
function useDebouncedCallback(arg0: () => void, arg1: number) {
94+
throw new Error('Function not implemented.')
95+
}

web/hooks/useSendChatMessage.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,20 @@ export default function useSendChatMessage() {
7777
events.emit(EventName.OnNewMessageRequest, newMessage)
7878

7979
if (!currentConvo?.summary && currentConvo) {
80-
const updatedConv = {
80+
const updatedConv: Conversation = {
8181
...currentConvo,
82+
lastMessage: prompt,
8283
summary: `Prompt: ${prompt}`,
8384
}
85+
86+
updateConversation(updatedConv)
87+
await executeSerial(DataService.UpdateConversation, updatedConv)
88+
} else {
89+
const updatedConv: Conversation = {
90+
...currentConvo,
91+
lastMessage: prompt,
92+
}
93+
8494
updateConversation(updatedConv)
8595
await executeSerial(DataService.UpdateConversation, updatedConv)
8696
}

web/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
"eslint-config-next": "13.4.10",
2929
"framer-motion": "^10.16.4",
3030
"highlight.js": "^11.9.0",
31-
"react-intersection-observer": "^9.5.2",
3231
"jotai": "^2.4.0",
3332
"jotai-optics": "^0.3.1",
3433
"jwt-decode": "^3.1.2",
34+
"lodash": "^4.17.21",
3535
"lucide-react": "^0.288.0",
3636
"marked": "^9.1.2",
3737
"marked-highlight": "^2.0.6",
@@ -42,6 +42,7 @@
4242
"react": "18.2.0",
4343
"react-dom": "18.2.0",
4444
"react-hook-form": "^7.45.4",
45+
"react-intersection-observer": "^9.5.2",
4546
"sass": "^1.69.4",
4647
"tailwind-merge": "^1.14.0",
4748
"tailwindcss": "3.3.3",
@@ -50,6 +51,7 @@
5051
},
5152
"devDependencies": {
5253
"@tailwindcss/forms": "^0.5.4",
54+
"@types/lodash": "^4.14.200",
5355
"@types/node": "20.6.5",
5456
"@types/uuid": "^9.0.6",
5557
"encoding": "^0.1.13",

0 commit comments

Comments
 (0)