From 29a7fb8c93cffb32c2f89b0775f408ec3fe64789 Mon Sep 17 00:00:00 2001
From: Faisal Amir
Date: Tue, 6 Feb 2024 16:00:29 +0700
Subject: [PATCH] fix: avoid users to create so many threads at the same time
(#1930)
* fix: avoid allow users to create so many threads at the same time
* fix missing last message
* remove console
* update last message metadata thread
* update conditional statement
---
web/containers/Providers/EventHandler.tsx | 8 ++++++++
web/hooks/useCreateNewThread.ts | 12 +++++++++++-
web/screens/Chat/ThreadList/index.tsx | 4 +++-
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/web/containers/Providers/EventHandler.tsx b/web/containers/Providers/EventHandler.tsx
index e9d70d5d29..f22ed1bc7d 100644
--- a/web/containers/Providers/EventHandler.tsx
+++ b/web/containers/Providers/EventHandler.tsx
@@ -33,6 +33,7 @@ import {
updateThreadWaitingForResponseAtom,
threadsAtom,
isGeneratingResponseAtom,
+ updateThreadAtom,
} from '@/helpers/atoms/Thread.atom'
export default function EventHandler({ children }: { children: ReactNode }) {
@@ -49,6 +50,7 @@ export default function EventHandler({ children }: { children: ReactNode }) {
const modelsRef = useRef(downloadedModels)
const threadsRef = useRef(threads)
const setIsGeneratingResponse = useSetAtom(isGeneratingResponseAtom)
+ const updateThread = useSetAtom(updateThreadAtom)
useEffect(() => {
threadsRef.current = threads
@@ -131,6 +133,12 @@ export default function EventHandler({ children }: { children: ReactNode }) {
...thread.metadata,
lastMessage: messageContent,
}
+
+ updateThread({
+ ...thread,
+ metadata,
+ })
+
extensionManager
.get(ExtensionTypeEnum.Conversational)
?.saveThread({
diff --git a/web/hooks/useCreateNewThread.ts b/web/hooks/useCreateNewThread.ts
index ee8df22df6..12a5e04cae 100644
--- a/web/hooks/useCreateNewThread.ts
+++ b/web/hooks/useCreateNewThread.ts
@@ -7,7 +7,7 @@ import {
ThreadState,
Model,
} from '@janhq/core'
-import { atom, useSetAtom } from 'jotai'
+import { atom, useAtomValue, useSetAtom } from 'jotai'
import { selectedModelAtom } from '@/containers/DropdownListSidebar'
import { fileUploadAtom } from '@/containers/Providers/Jotai'
@@ -19,6 +19,7 @@ import useRecommendedModel from './useRecommendedModel'
import useSetActiveThread from './useSetActiveThread'
import { extensionManager } from '@/extension'
+
import {
threadsAtom,
threadStatesAtom,
@@ -53,12 +54,21 @@ export const useCreateNewThread = () => {
const { recommendedModel, downloadedModels } = useRecommendedModel()
+ const threads = useAtomValue(threadsAtom)
+
const requestCreateNewThread = async (
assistant: Assistant,
model?: Model | undefined
) => {
const defaultModel = model ?? recommendedModel ?? downloadedModels[0]
+ // check last thread message, if there empty last message use can not create thread
+ const lastMessage = threads[0]?.metadata?.lastMessage
+
+ if (!lastMessage && threads.length) {
+ return null
+ }
+
const createdAt = Date.now()
const assistantInfo: ThreadAssistantInfo = {
assistant_id: assistant.id,
diff --git a/web/screens/Chat/ThreadList/index.tsx b/web/screens/Chat/ThreadList/index.tsx
index 8f5bfb8f28..2ad9a28c4e 100644
--- a/web/screens/Chat/ThreadList/index.tsx
+++ b/web/screens/Chat/ThreadList/index.tsx
@@ -62,7 +62,9 @@ export default function ThreadList() {
{thread.title}
- {threadStates[thread.id]?.lastMessage ?? 'No new message'}
+ {threadStates[thread.id]?.lastMessage
+ ? threadStates[thread.id]?.lastMessage
+ : 'No new message'}