Skip to content

Commit 1d0fc2b

Browse files
committed
Add loading
1 parent 2280af5 commit 1d0fc2b

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

client/src/hooks/useChat.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useCallback } from "react";
1+
import { useState, useCallback, useEffect } from "react";
22
import { useChats } from "./useChats";
33
import { useChatUpdate } from "./useChatUpdate";
44
import { useChatCreate } from "./useChatCreate";
@@ -9,23 +9,44 @@ export default function useChat() {
99
const [activeConversationId, setActiveConversationId] = useState<
1010
string | null
1111
>(null);
12-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1312
const [isTyping, setIsTyping] = useState(false);
14-
const { addMessageToChat } = useChatUpdate({});
13+
const { addMessageToChat } = useChatUpdate({
14+
onSuccess: () => setIsTyping(false),
15+
onError: () => setIsTyping(false),
16+
});
1517
const { createChat } = useChatCreate({});
1618
const { deleteChat } = useChatDelete({});
1719

18-
const activeConversation =
19-
chats.find((c) => c.id === activeConversationId) || null;
20+
const [activeConversation, setActiveConversation] = useState(
21+
chats.find((c) => c.id === activeConversationId) || null
22+
);
23+
24+
const optimisticAddMessage = useCallback((content: string) => {
25+
const newMessage: Message = {
26+
id: crypto.randomUUID(),
27+
content,
28+
timestamp: new Date(),
29+
role: "USER",
30+
};
31+
setActiveConversation((prev) => {
32+
if (!prev) return null;
33+
return {
34+
...prev,
35+
messages: [...prev.messages, newMessage],
36+
};
37+
});
38+
}, []);
2039

2140
const addMessage = useCallback(
2241
(content: string) => {
42+
setIsTyping(true);
43+
optimisticAddMessage(content);
2344
addMessageToChat({
2445
message: content,
2546
conversationId: activeConversationId || "",
2647
});
2748
},
28-
[activeConversationId, addMessageToChat]
49+
[activeConversationId, addMessageToChat, optimisticAddMessage]
2950
);
3051

3152
const createNewConversation = useCallback(
@@ -40,6 +61,12 @@ export default function useChat() {
4061
[deleteChat]
4162
);
4263

64+
useEffect(() => {
65+
setActiveConversation(
66+
chats.find((c) => c.id === activeConversationId) || null
67+
);
68+
}, [setActiveConversation, chats, activeConversationId]);
69+
4370
return {
4471
conversations: chats,
4572
activeConversation,

client/src/services/chatService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export async function getConversationsOfUser(
1212
throw new Error("Failed to fetch conversations.");
1313
}
1414
const conversations = await conversationsResponse.json();
15-
console.log("Fetched conversations:", conversations);
1615
return conversations as Conversation[];
1716
} catch (error) {
1817
console.error("Error fetching conversations:", error);

0 commit comments

Comments
 (0)