|
1 | 1 | import { useState, useCallback } from "react"; |
2 | | -import { generateId } from "../utils/helpers"; |
| 2 | +import { useChats } from "./useChats"; |
| 3 | +import { useChatUpdate } from "./useChatUpdate"; |
| 4 | +import { useChatCreate } from "./useChatCreate"; |
| 5 | +import { useChatDelete } from "./useChatDelete"; |
3 | 6 |
|
4 | 7 | export default function useChat() { |
5 | | - const [conversations, setConversations] = useState<Conversation[]>([ |
6 | | - { |
7 | | - id: generateId(), |
8 | | - title: "New conversation", |
9 | | - messages: [], |
10 | | - createdAt: new Date(), |
11 | | - }, |
12 | | - ]); |
13 | | - const [activeConversationId, setActiveConversationId] = useState<string>( |
14 | | - conversations[0].id |
15 | | - ); |
| 8 | + const { chats } = useChats(); |
| 9 | + const [activeConversationId, setActiveConversationId] = useState< |
| 10 | + string | null |
| 11 | + >(null); |
| 12 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
16 | 13 | const [isTyping, setIsTyping] = useState(false); |
| 14 | + const { addMessageToChat } = useChatUpdate({}); |
| 15 | + const { createChat } = useChatCreate({}); |
| 16 | + const { deleteChat } = useChatDelete({}); |
17 | 17 |
|
18 | 18 | const activeConversation = |
19 | | - conversations.find((c) => c.id === activeConversationId) || |
20 | | - conversations[0]; |
| 19 | + chats.find((c) => c.id === activeConversationId) || null; |
21 | 20 |
|
22 | 21 | const addMessage = useCallback( |
23 | | - (content: string, role: "user" | "assistant") => { |
24 | | - const newMessage: Message = { |
25 | | - id: generateId(), |
26 | | - content, |
27 | | - role, |
28 | | - timestamp: new Date(), |
29 | | - }; |
30 | | - |
31 | | - setConversations((prevConversations) => |
32 | | - prevConversations.map((conversation) => |
33 | | - conversation.id === activeConversationId |
34 | | - ? { |
35 | | - ...conversation, |
36 | | - messages: [...conversation.messages, newMessage], |
37 | | - title: |
38 | | - conversation.messages.length === 0 && role === "user" |
39 | | - ? content.substring(0, 30) + |
40 | | - (content.length > 30 ? "..." : "") |
41 | | - : conversation.title, |
42 | | - } |
43 | | - : conversation |
44 | | - ) |
45 | | - ); |
46 | | - |
47 | | - if (role === "user") { |
48 | | - // Simulate bot typing |
49 | | - setIsTyping(true); |
50 | | - const typingTimeout = setTimeout(() => { |
51 | | - const responses = [ |
52 | | - "I'm an AI assistant here to help you with your questions.", |
53 | | - "That's an interesting question. Let me think about it.", |
54 | | - "I can provide information on a wide range of topics.", |
55 | | - "I'm designed to be helpful, harmless, and honest in my responses.", |
56 | | - "I'm still learning, but I'll do my best to assist you.", |
57 | | - ]; |
58 | | - const randomResponse = |
59 | | - responses[Math.floor(Math.random() * responses.length)]; |
60 | | - addMessage(randomResponse, "assistant"); |
61 | | - setIsTyping(false); |
62 | | - }, 1500); |
63 | | - |
64 | | - return () => clearTimeout(typingTimeout); |
65 | | - } |
| 22 | + (content: string) => { |
| 23 | + addMessageToChat({ |
| 24 | + message: content, |
| 25 | + conversationId: activeConversationId || "", |
| 26 | + }); |
66 | 27 | }, |
67 | | - [activeConversationId] |
| 28 | + [activeConversationId, addMessageToChat] |
68 | 29 | ); |
69 | 30 |
|
70 | | - const createNewConversation = useCallback(() => { |
71 | | - const newConversation: Conversation = { |
72 | | - id: generateId(), |
73 | | - title: "New conversation", |
74 | | - messages: [], |
75 | | - createdAt: new Date(), |
76 | | - }; |
77 | | - |
78 | | - setConversations((prev) => [...prev, newConversation]); |
79 | | - setActiveConversationId(newConversation.id); |
80 | | - return newConversation; |
81 | | - }, []); |
| 31 | + const createNewConversation = useCallback( |
| 32 | + async (title?: string) => await createChat(title ?? "New Conversation"), |
| 33 | + [createChat] |
| 34 | + ); |
82 | 35 |
|
83 | 36 | const deleteConversation = useCallback( |
84 | | - (id: string) => { |
85 | | - setConversations((prev) => |
86 | | - prev.filter((conversation) => conversation.id !== id) |
87 | | - ); |
88 | | - |
89 | | - if (activeConversationId === id && conversations.length > 1) { |
90 | | - // Set the active conversation to the most recent one that's not the deleted one |
91 | | - const remainingConversations = conversations.filter((c) => c.id !== id); |
92 | | - setActiveConversationId(remainingConversations[0].id); |
93 | | - } |
| 37 | + (conversationId: string) => { |
| 38 | + deleteChat({ conversationId }); |
94 | 39 | }, |
95 | | - [activeConversationId, conversations] |
| 40 | + [deleteChat] |
96 | 41 | ); |
97 | 42 |
|
98 | 43 | return { |
99 | | - conversations, |
| 44 | + conversations: chats, |
100 | 45 | activeConversation, |
101 | 46 | activeConversationId, |
102 | 47 | setActiveConversationId, |
|
0 commit comments