-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathindex.tsx
More file actions
272 lines (235 loc) · 10.7 KB
/
index.tsx
File metadata and controls
272 lines (235 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import "./style.css";
import { useTranslation } from "react-i18next";
import { User, MessageCircle, PlusIcon } from "lucide-react";
import { useCallback, useEffect, useRef, useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { Button } from "@/components/ui/button";
import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { useInstance } from "@/contexts/InstanceContext";
import { useFindChats } from "@/lib/queries/chat/findChats";
import { getToken, TOKEN_ID } from "@/lib/queries/token";
import { Chat as ChatType } from "@/types/evolution.types";
import React from "react";
import { useMediaQuery } from "@/utils/useMediaQuery";
import { connectSocket, disconnectSocket } from "@/services/websocket/socket";
import { Messages } from "./messages";
// Simple utility function
const formatJid = (remoteJid: string): string => {
return remoteJid.split("@")[0];
};
function Chat() {
const { t } = useTranslation();
const isMD = useMediaQuery("(min-width: 768px)");
const lastMessageRef = useRef<HTMLDivElement | null>(null);
const [textareaHeight] = useState("auto");
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const { instance } = useInstance();
// Local state for real-time chats (to supplement React Query data)
const [realtimeChats, setRealtimeChats] = useState<ChatType[]>([]);
const { data: chats, isSuccess } = useFindChats({
instanceName: instance?.name,
});
// Combine React Query chats with real-time updates
const allChats = React.useMemo(() => {
if (!chats) return realtimeChats;
// Merge chats from React Query with real-time updates
const chatMap = new Map();
// First add all chats from React Query
chats.forEach((chat: { remoteJid: any; }) => chatMap.set(chat.remoteJid, chat));
// Then add/update with real-time chats
realtimeChats.forEach((chat) => {
const existing = chatMap.get(chat.remoteJid);
if (existing) {
// Update existing chat with newer data
chatMap.set(chat.remoteJid, { ...existing, ...chat });
} else {
// Add new chat from real-time updates
chatMap.set(chat.remoteJid, chat);
}
});
return Array.from(chatMap.values());
}, [chats, realtimeChats]);
const { instanceId, remoteJid } = useParams<{
instanceId: string;
remoteJid: string;
}>();
const navigate = useNavigate();
// Add websocket functionality for real-time updates
useEffect(() => {
if (!instance?.name) return;
const serverUrl = getToken(TOKEN_ID.API_URL);
if (!serverUrl) {
console.error("API URL not found in localStorage");
return;
}
const socket = connectSocket(serverUrl);
// Function to update chats from websocket events
const updateChatsFromWebsocket = (_eventType: string, data: any) => {
if (!instance) return;
if (data.instance !== instance.name) {
return;
}
const messageRemoteJid = data?.data?.key?.remoteJid;
if (!messageRemoteJid) {
return;
}
setRealtimeChats((prevChats) => {
const existingChatIndex = prevChats.findIndex((chat) => chat.remoteJid === messageRemoteJid);
// Create or update chat object
const chatObject: ChatType = {
id: messageRemoteJid,
remoteJid: messageRemoteJid,
pushName: data?.data?.pushName || formatJid(messageRemoteJid),
profilePicUrl: data?.data?.key?.profilePictureUrl || "",
// Add other required fields
...data?.data,
};
if (existingChatIndex !== -1) {
// Update existing chat
const updatedChats = [...prevChats];
updatedChats[existingChatIndex] = {
...updatedChats[existingChatIndex],
...chatObject,
};
return updatedChats;
} else {
// Add new chat
return [...prevChats, chatObject];
}
});
};
// Set up event listeners
socket.on("messages.upsert", (data: any) => {
updateChatsFromWebsocket("messages.upsert", data);
});
socket.on("send.message", (data: any) => {
updateChatsFromWebsocket("send.message", data);
});
socket.connect();
// Cleanup function
return () => {
socket.off("messages.upsert");
socket.off("send.message");
disconnectSocket(socket);
};
}, [instance?.name]);
const scrollToBottom = useCallback(() => {
if (lastMessageRef.current) {
lastMessageRef.current.scrollIntoView({});
}
}, []);
const handleTextareaChange = () => {
if (textareaRef.current) {
textareaRef.current.style.height = "auto";
const scrollHeight = textareaRef.current.scrollHeight;
const lineHeight = parseInt(getComputedStyle(textareaRef.current).lineHeight);
const maxHeight = lineHeight * 10;
textareaRef.current.style.height = `${Math.min(scrollHeight, maxHeight)}px`;
}
};
useEffect(() => {
if (isSuccess) {
scrollToBottom();
}
}, [isSuccess, scrollToBottom]);
const handleChat = (id: string) => {
navigate(`/manager/instance/${instanceId}/chat/${id}`);
};
return (
<div className="h-[calc(100vh-160px)] overflow-hidden">
<ResizablePanelGroup direction={isMD ? "horizontal" : "vertical"} className="h-full">
<ResizablePanel defaultSize={20}>
<div className="hidden h-full flex-col bg-background text-foreground md:flex">
<div className="flex-shrink-0 p-2">
<Button variant="ghost" className="w-full justify-start gap-2 px-2 text-left">
<div className="flex h-7 w-7 items-center justify-center rounded-full">
<MessageCircle className="h-4 w-4" />
</div>
<div className="grow overflow-hidden text-ellipsis whitespace-nowrap text-sm">{t("chat.title")}</div>
<PlusIcon className="h-4 w-4" />
</Button>
</div>
<Tabs defaultValue="contacts" className="flex flex-col flex-1 min-h-0">
<TabsList className="tabs-chat flex-shrink-0">
<TabsTrigger value="contacts">{t("chat.contacts")}</TabsTrigger>
<TabsTrigger value="groups">{t("chat.groups")}</TabsTrigger>
</TabsList>
<TabsContent value="contacts" className="flex-1 overflow-hidden">
<div className="h-full overflow-auto">
<div className="grid gap-1 p-2 text-foreground">
<div className="px-2 text-xs font-medium text-muted-foreground">{t("chat.contacts")}</div>
{chats?.map(
(chat: ChatType) =>
chat.remoteJid.includes("@s.whatsapp.net") && (
<Link
key={chat.id}
to="#"
onClick={() => handleChat(chat.remoteJid)}
className={`chat-item flex items-center overflow-hidden truncate whitespace-nowrap rounded-md border-b border-gray-600/50 p-2 text-sm transition-colors hover:bg-muted/50 ${
remoteJid === chat.remoteJid ? "active" : ""
}`}>
<span className="chat-avatar mr-2">
<Avatar className="h-8 w-8">
<AvatarImage src={chat.profilePicUrl} alt={chat.pushName || chat.remoteJid.split("@")[0]} />
<AvatarFallback className="bg-slate-700 text-slate-300 border border-slate-600">
<User className="h-5 w-5" />
</AvatarFallback>
</Avatar>
</span>
<div className="min-w-0 flex-1">
<span className="chat-title block font-medium">{chat.pushName || chat.remoteJid.split("@")[0]}</span>
<span className="chat-description block text-xs text-gray-500">{chat.remoteJid.split("@")[0]}</span>
</div>
</Link>
),
)}
</div>
</div>
</TabsContent>
<TabsContent value="groups" className="flex-1 overflow-hidden">
<div className="h-full overflow-auto">
<div className="grid gap-1 p-2 text-foreground">
{allChats?.map(
(chat: ChatType) =>
chat.remoteJid.includes("@g.us") && (
<Link
key={chat.id}
to="#"
onClick={() => handleChat(chat.remoteJid)}
className={`chat-item flex items-center overflow-hidden truncate whitespace-nowrap rounded-md border-b border-gray-600/50 p-2 text-sm transition-colors hover:bg-muted/50 ${
remoteJid === chat.remoteJid ? "active" : ""
}`}>
<span className="chat-avatar mr-2">
<Avatar className="h-8 w-8">
<AvatarImage src={chat.profilePicUrl} alt={chat.pushName || chat.remoteJid.split("@")[0]} />
<AvatarFallback className="bg-slate-700 text-slate-300 border border-slate-600">
<User className="h-5 w-5" />
</AvatarFallback>
</Avatar>
</span>
<div className="min-w-0 flex-1">
<span className="chat-title block font-medium">{chat.pushName || chat.remoteJid.split("@")[0]}</span>
<span className="chat-description block text-xs text-gray-500">{chat.remoteJid}</span>
</div>
</Link>
),
)}
</div>
</div>
</TabsContent>
</Tabs>
</div>
</ResizablePanel>
<ResizableHandle withHandle className="border border-black" />
<ResizablePanel>
{remoteJid && (
<Messages textareaRef={textareaRef} handleTextareaChange={handleTextareaChange} textareaHeight={textareaHeight} lastMessageRef={lastMessageRef} scrollToBottom={scrollToBottom} />
)}
</ResizablePanel>
</ResizablePanelGroup>
</div>
);
}
export { Chat };