@thrillee/aegischat 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +452 -0
- package/dist/index.d.ts +452 -0
- package/dist/index.js +1065 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1024 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
- package/src/hooks/index.ts +27 -0
- package/src/hooks/useAutoRead.ts +78 -0
- package/src/hooks/useChannels.ts +99 -0
- package/src/hooks/useChat.ts +731 -0
- package/src/hooks/useFileUpload.ts +25 -0
- package/src/hooks/useMentions.ts +36 -0
- package/src/hooks/useMessages.ts +37 -0
- package/src/hooks/useReactions.ts +28 -0
- package/src/hooks/useTypingIndicator.ts +28 -0
- package/src/index.ts +68 -0
- package/src/services/api.ts +370 -0
- package/src/types/index.ts +373 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1024 @@
|
|
|
1
|
+
// src/hooks/useChat.ts
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
// src/services/api.ts
|
|
5
|
+
var baseUrl = "";
|
|
6
|
+
var getAccessToken = () => "";
|
|
7
|
+
var onUnauthorized;
|
|
8
|
+
function configureApiClient(config) {
|
|
9
|
+
baseUrl = config.baseUrl;
|
|
10
|
+
getAccessToken = config.getAccessToken;
|
|
11
|
+
onUnauthorized = config.onUnauthorized;
|
|
12
|
+
}
|
|
13
|
+
async function fetchWithAuth(path, options = {}) {
|
|
14
|
+
const token = await (typeof getAccessToken === "function" ? getAccessToken() : getAccessToken);
|
|
15
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
16
|
+
...options,
|
|
17
|
+
headers: {
|
|
18
|
+
"Content-Type": "application/json",
|
|
19
|
+
Authorization: `Bearer ${token}`,
|
|
20
|
+
...options.headers
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (response.status === 401) {
|
|
24
|
+
onUnauthorized?.();
|
|
25
|
+
throw new Error("Unauthorized");
|
|
26
|
+
}
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
const error = await response.json().catch(() => ({}));
|
|
29
|
+
throw new Error(error.message || `HTTP ${response.status}`);
|
|
30
|
+
}
|
|
31
|
+
return response.json();
|
|
32
|
+
}
|
|
33
|
+
var chatApi = {
|
|
34
|
+
/**
|
|
35
|
+
* Connect to chat session
|
|
36
|
+
*/
|
|
37
|
+
async connect(params, signal) {
|
|
38
|
+
return fetchWithAuth("/api/v1/chat/connect", {
|
|
39
|
+
method: "POST",
|
|
40
|
+
body: JSON.stringify(params),
|
|
41
|
+
signal
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
/**
|
|
45
|
+
* Refresh access token
|
|
46
|
+
*/
|
|
47
|
+
async refreshToken(refreshToken, signal) {
|
|
48
|
+
return fetchWithAuth("/api/v1/chat/refresh", {
|
|
49
|
+
method: "POST",
|
|
50
|
+
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
51
|
+
signal
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var channelsApi = {
|
|
56
|
+
/**
|
|
57
|
+
* List channels
|
|
58
|
+
*/
|
|
59
|
+
async list(options = {}, signal) {
|
|
60
|
+
const params = new URLSearchParams();
|
|
61
|
+
if (options.type) params.append("type", options.type);
|
|
62
|
+
if (options.limit) params.append("limit", String(options.limit));
|
|
63
|
+
const query = params.toString() ? `?${params.toString()}` : "";
|
|
64
|
+
return fetchWithAuth(`/api/v1/channels${query}`, { signal });
|
|
65
|
+
},
|
|
66
|
+
/**
|
|
67
|
+
* Get channel by ID
|
|
68
|
+
*/
|
|
69
|
+
async get(channelId, signal) {
|
|
70
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}`, { signal });
|
|
71
|
+
},
|
|
72
|
+
/**
|
|
73
|
+
* Get or create DM channel
|
|
74
|
+
*/
|
|
75
|
+
async getOrCreateDM(userId, signal) {
|
|
76
|
+
return fetchWithAuth("/api/v1/channels/dm", {
|
|
77
|
+
method: "POST",
|
|
78
|
+
body: JSON.stringify({ user_id: userId }),
|
|
79
|
+
signal
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
/**
|
|
83
|
+
* Create channel
|
|
84
|
+
*/
|
|
85
|
+
async create(data, signal) {
|
|
86
|
+
return fetchWithAuth("/api/v1/channels", {
|
|
87
|
+
method: "POST",
|
|
88
|
+
body: JSON.stringify(data),
|
|
89
|
+
signal
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
/**
|
|
93
|
+
* Mark channel as read
|
|
94
|
+
*/
|
|
95
|
+
async markAsRead(channelId, signal) {
|
|
96
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/read`, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
signal
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
/**
|
|
102
|
+
* Get channel members
|
|
103
|
+
*/
|
|
104
|
+
async getMembers(channelId, signal) {
|
|
105
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/members`, { signal });
|
|
106
|
+
},
|
|
107
|
+
/**
|
|
108
|
+
* Update channel
|
|
109
|
+
*/
|
|
110
|
+
async update(channelId, data, signal) {
|
|
111
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}`, {
|
|
112
|
+
method: "PATCH",
|
|
113
|
+
body: JSON.stringify(data),
|
|
114
|
+
signal
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var messagesApi = {
|
|
119
|
+
/**
|
|
120
|
+
* List messages in a channel
|
|
121
|
+
*/
|
|
122
|
+
async list(channelId, options = {}, signal) {
|
|
123
|
+
const params = new URLSearchParams();
|
|
124
|
+
if (options.limit) params.append("limit", String(options.limit));
|
|
125
|
+
if (options.before) params.append("before", options.before);
|
|
126
|
+
const query = params.toString() ? `?${params.toString()}` : "";
|
|
127
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages${query}`, { signal });
|
|
128
|
+
},
|
|
129
|
+
/**
|
|
130
|
+
* Send a message
|
|
131
|
+
*/
|
|
132
|
+
async send(channelId, data, signal) {
|
|
133
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages`, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
body: JSON.stringify(data),
|
|
136
|
+
signal
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
/**
|
|
140
|
+
* Update a message
|
|
141
|
+
*/
|
|
142
|
+
async update(channelId, messageId, data, signal) {
|
|
143
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages/${messageId}`, {
|
|
144
|
+
method: "PATCH",
|
|
145
|
+
body: JSON.stringify(data),
|
|
146
|
+
signal
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
/**
|
|
150
|
+
* Delete a message
|
|
151
|
+
*/
|
|
152
|
+
async delete(channelId, messageId, signal) {
|
|
153
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages/${messageId}`, {
|
|
154
|
+
method: "DELETE",
|
|
155
|
+
signal
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
/**
|
|
159
|
+
* Mark messages as delivered
|
|
160
|
+
*/
|
|
161
|
+
async markDelivered(channelId, signal) {
|
|
162
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages/delivered`, {
|
|
163
|
+
method: "POST",
|
|
164
|
+
signal
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
/**
|
|
168
|
+
* Mark messages as read
|
|
169
|
+
*/
|
|
170
|
+
async markRead(channelId, signal) {
|
|
171
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages/read`, {
|
|
172
|
+
method: "POST",
|
|
173
|
+
signal
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
var reactionsApi = {
|
|
178
|
+
/**
|
|
179
|
+
* Add reaction to a message
|
|
180
|
+
*/
|
|
181
|
+
async add(channelId, messageId, emoji, signal) {
|
|
182
|
+
return fetchWithAuth(`/api/v1/channels/${channelId}/messages/${messageId}/reactions`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
body: JSON.stringify({ emoji }),
|
|
185
|
+
signal
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
/**
|
|
189
|
+
* Remove reaction from a message
|
|
190
|
+
*/
|
|
191
|
+
async remove(channelId, messageId, emoji, signal) {
|
|
192
|
+
return fetchWithAuth(
|
|
193
|
+
`/api/v1/channels/${channelId}/messages/${messageId}/reactions/${encodeURIComponent(emoji)}`,
|
|
194
|
+
{ method: "DELETE", signal }
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
var filesApi = {
|
|
199
|
+
/**
|
|
200
|
+
* Get upload URL
|
|
201
|
+
*/
|
|
202
|
+
async getUploadUrl(data, signal) {
|
|
203
|
+
return fetchWithAuth("/api/v1/files/upload-url", {
|
|
204
|
+
method: "POST",
|
|
205
|
+
body: JSON.stringify(data),
|
|
206
|
+
signal
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
/**
|
|
210
|
+
* Confirm file upload
|
|
211
|
+
*/
|
|
212
|
+
async confirm(fileId, signal) {
|
|
213
|
+
return fetchWithAuth("/api/v1/files", {
|
|
214
|
+
method: "POST",
|
|
215
|
+
body: JSON.stringify({ file_id: fileId }),
|
|
216
|
+
signal
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
/**
|
|
220
|
+
* Get download URL
|
|
221
|
+
*/
|
|
222
|
+
async getDownloadUrl(fileId, signal) {
|
|
223
|
+
return fetchWithAuth(`/api/v1/files/${fileId}/download`, { signal });
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var usersApi = {
|
|
227
|
+
/**
|
|
228
|
+
* Search users
|
|
229
|
+
*/
|
|
230
|
+
async search(query, signal) {
|
|
231
|
+
return fetchWithAuth(`/api/v1/users/search?q=${encodeURIComponent(query)}`, { signal });
|
|
232
|
+
},
|
|
233
|
+
/**
|
|
234
|
+
* Get user by ID
|
|
235
|
+
*/
|
|
236
|
+
async get(userId, signal) {
|
|
237
|
+
return fetchWithAuth(`/api/v1/users/${userId}`, { signal });
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// src/hooks/useChat.ts
|
|
242
|
+
var TYPING_TIMEOUT = 3e3;
|
|
243
|
+
var RECONNECT_INTERVAL = 3e3;
|
|
244
|
+
var MAX_RECONNECT_ATTEMPTS = 5;
|
|
245
|
+
var MAX_RECONNECT_DELAY = 3e4;
|
|
246
|
+
var PING_INTERVAL = 3e4;
|
|
247
|
+
var SESSION_STORAGE_KEY = "@aegischat/activeChannel";
|
|
248
|
+
function useChat(options) {
|
|
249
|
+
const { config, role, clientId, autoConnect = true, onMessage, onTyping, onConnectionChange } = options;
|
|
250
|
+
const [session, setSession] = useState(null);
|
|
251
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
252
|
+
const [isConnecting, setIsConnecting] = useState(false);
|
|
253
|
+
const [activeChannelId, setActiveChannelIdState] = useState(null);
|
|
254
|
+
const [channels, setChannels] = useState([]);
|
|
255
|
+
const [messages, setMessages] = useState([]);
|
|
256
|
+
const [typingUsers, setTypingUsers] = useState({});
|
|
257
|
+
const [isLoadingChannels, setIsLoadingChannels] = useState(false);
|
|
258
|
+
const [isLoadingMessages, setIsLoadingMessages] = useState(false);
|
|
259
|
+
const [hasMoreMessages, setHasMoreMessages] = useState(true);
|
|
260
|
+
const [uploadProgress, setUploadProgress] = useState([]);
|
|
261
|
+
const wsRef = useRef(null);
|
|
262
|
+
const reconnectAttempts = useRef(0);
|
|
263
|
+
const reconnectTimeout = useRef(null);
|
|
264
|
+
const pingInterval = useRef(null);
|
|
265
|
+
const typingTimeout = useRef(null);
|
|
266
|
+
const isManualDisconnect = useRef(false);
|
|
267
|
+
const oldestMessageId = useRef(null);
|
|
268
|
+
const activeChannelIdRef = useRef(null);
|
|
269
|
+
const configRef = useRef(config);
|
|
270
|
+
const sessionRef = useRef(null);
|
|
271
|
+
useEffect(() => {
|
|
272
|
+
configRef.current = config;
|
|
273
|
+
}, [config]);
|
|
274
|
+
useEffect(() => {
|
|
275
|
+
activeChannelIdRef.current = activeChannelId;
|
|
276
|
+
}, [activeChannelId]);
|
|
277
|
+
useEffect(() => {
|
|
278
|
+
sessionRef.current = session;
|
|
279
|
+
}, [session]);
|
|
280
|
+
const getActiveChannelId = useCallback(() => {
|
|
281
|
+
if (typeof window === "undefined") return null;
|
|
282
|
+
return sessionStorage.getItem(SESSION_STORAGE_KEY);
|
|
283
|
+
}, []);
|
|
284
|
+
const setActiveChannelId = useCallback((id) => {
|
|
285
|
+
setActiveChannelIdState(id);
|
|
286
|
+
if (typeof window !== "undefined") {
|
|
287
|
+
if (id) {
|
|
288
|
+
sessionStorage.setItem(SESSION_STORAGE_KEY, id);
|
|
289
|
+
} else {
|
|
290
|
+
sessionStorage.removeItem(SESSION_STORAGE_KEY);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}, []);
|
|
294
|
+
const fetchFromComms = useCallback(async (path, fetchOptions = {}) => {
|
|
295
|
+
const currentSession = sessionRef.current;
|
|
296
|
+
if (!currentSession) {
|
|
297
|
+
throw new Error("Chat session not initialized");
|
|
298
|
+
}
|
|
299
|
+
const response = await fetch(`${currentSession.api_url}${path}`, {
|
|
300
|
+
...fetchOptions,
|
|
301
|
+
headers: {
|
|
302
|
+
"Content-Type": "application/json",
|
|
303
|
+
Authorization: `Bearer ${currentSession.access_token}`,
|
|
304
|
+
...fetchOptions.headers
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
if (!response.ok) {
|
|
308
|
+
const error = await response.json().catch(() => ({}));
|
|
309
|
+
throw new Error(error.message || `HTTP ${response.status}`);
|
|
310
|
+
}
|
|
311
|
+
const data = await response.json();
|
|
312
|
+
return data.data || data;
|
|
313
|
+
}, []);
|
|
314
|
+
const clearTimers = useCallback(() => {
|
|
315
|
+
if (reconnectTimeout.current) {
|
|
316
|
+
clearTimeout(reconnectTimeout.current);
|
|
317
|
+
reconnectTimeout.current = null;
|
|
318
|
+
}
|
|
319
|
+
if (pingInterval.current) {
|
|
320
|
+
clearInterval(pingInterval.current);
|
|
321
|
+
pingInterval.current = null;
|
|
322
|
+
}
|
|
323
|
+
}, []);
|
|
324
|
+
const handleWebSocketMessage = useCallback((data) => {
|
|
325
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
326
|
+
console.log("[AegisChat] WebSocket message received:", data.type, data);
|
|
327
|
+
switch (data.type) {
|
|
328
|
+
case "message.new": {
|
|
329
|
+
const newMessage = data.payload;
|
|
330
|
+
if (newMessage.channel_id === currentActiveChannelId) {
|
|
331
|
+
setMessages((prev) => {
|
|
332
|
+
const existingIndex = prev.findIndex(
|
|
333
|
+
(m) => m.tempId && m.content === newMessage.content && m.status === "sending"
|
|
334
|
+
);
|
|
335
|
+
if (existingIndex !== -1) {
|
|
336
|
+
const updated = [...prev];
|
|
337
|
+
updated[existingIndex] = { ...newMessage, status: "sent" };
|
|
338
|
+
return updated;
|
|
339
|
+
}
|
|
340
|
+
if (prev.some((m) => m.id === newMessage.id)) return prev;
|
|
341
|
+
return [...prev, { ...newMessage, status: "delivered" }];
|
|
342
|
+
});
|
|
343
|
+
onMessage?.(newMessage);
|
|
344
|
+
}
|
|
345
|
+
setChannels((prev) => {
|
|
346
|
+
const updated = prev.map(
|
|
347
|
+
(ch) => ch.id === newMessage.channel_id ? {
|
|
348
|
+
...ch,
|
|
349
|
+
last_message: {
|
|
350
|
+
id: newMessage.id,
|
|
351
|
+
content: newMessage.content,
|
|
352
|
+
created_at: newMessage.created_at,
|
|
353
|
+
sender: { id: newMessage.sender_id, display_name: "Unknown", status: "online" }
|
|
354
|
+
},
|
|
355
|
+
unread_count: ch.id === currentActiveChannelId ? 0 : ch.unread_count + 1
|
|
356
|
+
} : ch
|
|
357
|
+
);
|
|
358
|
+
return updated.sort((a, b) => {
|
|
359
|
+
const timeA = a.last_message?.created_at || "";
|
|
360
|
+
const timeB = b.last_message?.created_at || "";
|
|
361
|
+
return timeB.localeCompare(timeA);
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
case "message.updated": {
|
|
367
|
+
const updatedMessage = data.payload;
|
|
368
|
+
setMessages((prev) => prev.map((m) => m.id === updatedMessage.id ? updatedMessage : m));
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
case "message.deleted": {
|
|
372
|
+
const { message_id } = data.payload;
|
|
373
|
+
setMessages((prev) => prev.map((m) => m.id === message_id ? { ...m, deleted: true } : m));
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
case "message.delivered":
|
|
377
|
+
case "message.read": {
|
|
378
|
+
const { message_id, channel_id, status } = data.payload;
|
|
379
|
+
if (channel_id === currentActiveChannelId) {
|
|
380
|
+
setMessages(
|
|
381
|
+
(prev) => prev.map((m) => m.id === message_id ? { ...m, status: status || "delivered" } : m)
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "message.delivered.batch":
|
|
387
|
+
case "message.read.batch": {
|
|
388
|
+
const { channel_id } = data.payload;
|
|
389
|
+
if (channel_id === currentActiveChannelId) {
|
|
390
|
+
setMessages(
|
|
391
|
+
(prev) => prev.map((m) => m.status === "sent" || m.status === "delivered" ? { ...m, status: data.type === "message.delivered.batch" ? "delivered" : "read" } : m)
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
case "typing.start": {
|
|
397
|
+
const { channel_id, user } = data.payload;
|
|
398
|
+
const typingUser = {
|
|
399
|
+
id: user.id,
|
|
400
|
+
displayName: user.display_name,
|
|
401
|
+
avatarUrl: user.avatar_url,
|
|
402
|
+
startedAt: Date.now()
|
|
403
|
+
};
|
|
404
|
+
setTypingUsers((prev) => ({
|
|
405
|
+
...prev,
|
|
406
|
+
[channel_id]: [...(prev[channel_id] || []).filter((u) => u.id !== user.id), typingUser]
|
|
407
|
+
}));
|
|
408
|
+
onTyping?.(channel_id, typingUser);
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
case "typing.stop": {
|
|
412
|
+
const { channel_id, user_id } = data.payload;
|
|
413
|
+
setTypingUsers((prev) => ({
|
|
414
|
+
...prev,
|
|
415
|
+
[channel_id]: (prev[channel_id] || []).filter((u) => u.id !== user_id)
|
|
416
|
+
}));
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
case "pong":
|
|
420
|
+
break;
|
|
421
|
+
default:
|
|
422
|
+
console.log("[AegisChat] Unhandled message type:", data.type);
|
|
423
|
+
}
|
|
424
|
+
}, [onMessage, onTyping]);
|
|
425
|
+
const connectWebSocket = useCallback(() => {
|
|
426
|
+
const currentSession = sessionRef.current;
|
|
427
|
+
if (!currentSession?.websocket_url || !currentSession?.access_token) {
|
|
428
|
+
console.warn("[AegisChat] Cannot connect WebSocket - missing session or token");
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
|
432
|
+
console.log("[AegisChat] WebSocket already open, skipping connection");
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
setIsConnecting(true);
|
|
436
|
+
isManualDisconnect.current = false;
|
|
437
|
+
const wsUrl = `${currentSession.websocket_url}?token=${currentSession.access_token}`;
|
|
438
|
+
console.log("[AegisChat] Creating WebSocket connection to:", wsUrl);
|
|
439
|
+
const ws = new WebSocket(wsUrl);
|
|
440
|
+
ws.onopen = () => {
|
|
441
|
+
console.log("[AegisChat] WebSocket connected");
|
|
442
|
+
setIsConnected(true);
|
|
443
|
+
reconnectAttempts.current = 0;
|
|
444
|
+
onConnectionChange?.(true);
|
|
445
|
+
pingInterval.current = setInterval(() => {
|
|
446
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
447
|
+
ws.send(JSON.stringify({ type: "ping" }));
|
|
448
|
+
}
|
|
449
|
+
}, PING_INTERVAL);
|
|
450
|
+
if (activeChannelIdRef.current) {
|
|
451
|
+
ws.send(JSON.stringify({ type: "channel.join", payload: { channel_id: activeChannelIdRef.current } }));
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
ws.onmessage = (event) => {
|
|
455
|
+
try {
|
|
456
|
+
const data = JSON.parse(event.data);
|
|
457
|
+
handleWebSocketMessage(data);
|
|
458
|
+
} catch (error) {
|
|
459
|
+
console.error("[AegisChat] Failed to parse WebSocket message:", error);
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
ws.onclose = () => {
|
|
463
|
+
console.log("[AegisChat] WebSocket disconnected");
|
|
464
|
+
setIsConnected(false);
|
|
465
|
+
clearTimers();
|
|
466
|
+
onConnectionChange?.(false);
|
|
467
|
+
if (!isManualDisconnect.current && reconnectAttempts.current < MAX_RECONNECT_ATTEMPTS) {
|
|
468
|
+
const delay = Math.min(RECONNECT_INTERVAL * Math.pow(2, reconnectAttempts.current), MAX_RECONNECT_DELAY);
|
|
469
|
+
console.log(`[AegisChat] Reconnecting in ${delay}ms...`);
|
|
470
|
+
reconnectTimeout.current = setTimeout(() => {
|
|
471
|
+
reconnectAttempts.current++;
|
|
472
|
+
connectWebSocket();
|
|
473
|
+
}, delay);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
ws.onerror = (error) => {
|
|
477
|
+
console.error("[AegisChat] WebSocket error:", error);
|
|
478
|
+
};
|
|
479
|
+
wsRef.current = ws;
|
|
480
|
+
}, [clearTimers, handleWebSocketMessage, onConnectionChange]);
|
|
481
|
+
const connect = useCallback(async () => {
|
|
482
|
+
console.log("[AegisChat] connect() called");
|
|
483
|
+
if (sessionRef.current) {
|
|
484
|
+
console.log("[AegisChat] Session exists, calling connectWebSocket directly");
|
|
485
|
+
connectWebSocket();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
setIsConnecting(true);
|
|
490
|
+
console.log("[AegisChat] Fetching chat session...");
|
|
491
|
+
const result = await chatApi.connect({ role, client_id: clientId });
|
|
492
|
+
console.log("[AegisChat] Chat session received:", result);
|
|
493
|
+
setSession(result.data);
|
|
494
|
+
setIsConnecting(false);
|
|
495
|
+
} catch (error) {
|
|
496
|
+
console.error("[AegisChat] Failed to get chat session:", error);
|
|
497
|
+
setIsConnecting(false);
|
|
498
|
+
throw error;
|
|
499
|
+
}
|
|
500
|
+
}, [role, clientId, connectWebSocket]);
|
|
501
|
+
const disconnect = useCallback(() => {
|
|
502
|
+
isManualDisconnect.current = true;
|
|
503
|
+
clearTimers();
|
|
504
|
+
if (wsRef.current) {
|
|
505
|
+
wsRef.current.close();
|
|
506
|
+
wsRef.current = null;
|
|
507
|
+
}
|
|
508
|
+
setIsConnected(false);
|
|
509
|
+
setSession(null);
|
|
510
|
+
setChannels([]);
|
|
511
|
+
setMessages([]);
|
|
512
|
+
}, [clearTimers]);
|
|
513
|
+
const refreshChannels = useCallback(async () => {
|
|
514
|
+
const currentSession = sessionRef.current;
|
|
515
|
+
if (!currentSession) return;
|
|
516
|
+
setIsLoadingChannels(true);
|
|
517
|
+
try {
|
|
518
|
+
const response = await channelsApi.list({});
|
|
519
|
+
setChannels(response.data.channels || []);
|
|
520
|
+
} catch (error) {
|
|
521
|
+
console.error("[AegisChat] Failed to fetch channels:", error);
|
|
522
|
+
} finally {
|
|
523
|
+
setIsLoadingChannels(false);
|
|
524
|
+
}
|
|
525
|
+
}, []);
|
|
526
|
+
const selectChannel = useCallback(async (channelId) => {
|
|
527
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
528
|
+
setActiveChannelId(channelId);
|
|
529
|
+
setMessages([]);
|
|
530
|
+
setHasMoreMessages(true);
|
|
531
|
+
oldestMessageId.current = null;
|
|
532
|
+
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
|
533
|
+
if (currentActiveChannelId) {
|
|
534
|
+
wsRef.current.send(JSON.stringify({ type: "channel.leave", payload: { channel_id: currentActiveChannelId } }));
|
|
535
|
+
}
|
|
536
|
+
wsRef.current.send(JSON.stringify({ type: "channel.join", payload: { channel_id: channelId } }));
|
|
537
|
+
}
|
|
538
|
+
setIsLoadingMessages(true);
|
|
539
|
+
try {
|
|
540
|
+
const response = await fetchFromComms(`/channels/${channelId}/messages?limit=50`);
|
|
541
|
+
setMessages(response.messages || []);
|
|
542
|
+
setHasMoreMessages(response.has_more);
|
|
543
|
+
if (response.oldest_id) {
|
|
544
|
+
oldestMessageId.current = response.oldest_id;
|
|
545
|
+
}
|
|
546
|
+
await markAsRead(channelId);
|
|
547
|
+
setChannels((prev) => prev.map((ch) => ch.id === channelId ? { ...ch, unread_count: 0 } : ch));
|
|
548
|
+
} catch (error) {
|
|
549
|
+
console.error("[AegisChat] Failed to load messages:", error);
|
|
550
|
+
setMessages([]);
|
|
551
|
+
} finally {
|
|
552
|
+
setIsLoadingMessages(false);
|
|
553
|
+
}
|
|
554
|
+
}, [setActiveChannelId, fetchFromComms]);
|
|
555
|
+
const markAsRead = useCallback(async (channelId) => {
|
|
556
|
+
try {
|
|
557
|
+
await fetchFromComms(`/channels/${channelId}/read`, { method: "POST" });
|
|
558
|
+
} catch (error) {
|
|
559
|
+
console.error("[AegisChat] Failed to mark as read:", error);
|
|
560
|
+
}
|
|
561
|
+
}, [fetchFromComms]);
|
|
562
|
+
const loadMoreMessages = useCallback(async () => {
|
|
563
|
+
if (!activeChannelId || !hasMoreMessages || isLoadingMessages) return;
|
|
564
|
+
setIsLoadingMessages(true);
|
|
565
|
+
try {
|
|
566
|
+
const params = oldestMessageId.current ? `?before=${oldestMessageId.current}&limit=50` : "?limit=50";
|
|
567
|
+
const response = await fetchFromComms(`/channels/${activeChannelId}/messages${params}`);
|
|
568
|
+
setMessages((prev) => [...response.messages || [], ...prev]);
|
|
569
|
+
setHasMoreMessages(response.has_more);
|
|
570
|
+
if (response.oldest_id) {
|
|
571
|
+
oldestMessageId.current = response.oldest_id;
|
|
572
|
+
}
|
|
573
|
+
} catch (error) {
|
|
574
|
+
console.error("[AegisChat] Failed to load more messages:", error);
|
|
575
|
+
} finally {
|
|
576
|
+
setIsLoadingMessages(false);
|
|
577
|
+
}
|
|
578
|
+
}, [activeChannelId, hasMoreMessages, isLoadingMessages, fetchFromComms]);
|
|
579
|
+
const sendMessage = useCallback(async (content, msgOptions = {}) => {
|
|
580
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
581
|
+
const currentSession = sessionRef.current;
|
|
582
|
+
if (!currentActiveChannelId || !content.trim() || !currentSession) return;
|
|
583
|
+
const tempId = `temp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
584
|
+
const trimmedContent = content.trim();
|
|
585
|
+
const optimisticMessage = {
|
|
586
|
+
id: tempId,
|
|
587
|
+
tempId,
|
|
588
|
+
channel_id: currentActiveChannelId,
|
|
589
|
+
sender_id: currentSession.comms_user_id,
|
|
590
|
+
content: trimmedContent,
|
|
591
|
+
type: msgOptions.type || "text",
|
|
592
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
593
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
594
|
+
status: "sending",
|
|
595
|
+
metadata: msgOptions.metadata || {}
|
|
596
|
+
};
|
|
597
|
+
setMessages((prev) => [...prev, optimisticMessage]);
|
|
598
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
599
|
+
setChannels((prev) => {
|
|
600
|
+
const updated = prev.map(
|
|
601
|
+
(ch) => ch.id === currentActiveChannelId ? {
|
|
602
|
+
...ch,
|
|
603
|
+
last_message: {
|
|
604
|
+
id: tempId,
|
|
605
|
+
content: trimmedContent,
|
|
606
|
+
created_at: now,
|
|
607
|
+
sender: { id: currentSession.comms_user_id, display_name: "You", status: "online" }
|
|
608
|
+
}
|
|
609
|
+
} : ch
|
|
610
|
+
);
|
|
611
|
+
return updated.sort((a, b) => {
|
|
612
|
+
const timeA = a.last_message?.created_at || "";
|
|
613
|
+
const timeB = b.last_message?.created_at || "";
|
|
614
|
+
return timeB.localeCompare(timeA);
|
|
615
|
+
});
|
|
616
|
+
});
|
|
617
|
+
try {
|
|
618
|
+
await fetchFromComms(`/channels/${currentActiveChannelId}/messages`, {
|
|
619
|
+
method: "POST",
|
|
620
|
+
body: JSON.stringify({ content: trimmedContent, type: msgOptions.type || "text", parent_id: msgOptions.parent_id, metadata: msgOptions.metadata })
|
|
621
|
+
});
|
|
622
|
+
} catch (error) {
|
|
623
|
+
console.error("[AegisChat] Failed to send message:", error);
|
|
624
|
+
setMessages(
|
|
625
|
+
(prev) => prev.map(
|
|
626
|
+
(m) => m.tempId === tempId ? { ...m, status: "failed", errorMessage: error instanceof Error ? error.message : "Failed to send" } : m
|
|
627
|
+
)
|
|
628
|
+
);
|
|
629
|
+
throw error;
|
|
630
|
+
}
|
|
631
|
+
}, [fetchFromComms]);
|
|
632
|
+
const uploadFile = useCallback(async (file) => {
|
|
633
|
+
const currentSession = sessionRef.current;
|
|
634
|
+
if (!currentSession) return null;
|
|
635
|
+
const fileId = `temp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
636
|
+
setUploadProgress((prev) => [...prev, { fileId, fileName: file.name, progress: 0, status: "pending" }]);
|
|
637
|
+
try {
|
|
638
|
+
setUploadProgress((prev) => prev.map((p) => p.fileId === fileId ? { ...p, status: "uploading", progress: 10 } : p));
|
|
639
|
+
const uploadUrlResponse = await fetchFromComms("/files/upload-url", {
|
|
640
|
+
method: "POST",
|
|
641
|
+
body: JSON.stringify({ file_name: file.name, file_type: file.type || "application/octet-stream", file_size: file.size })
|
|
642
|
+
});
|
|
643
|
+
setUploadProgress((prev) => prev.map((p) => p.fileId === fileId ? { ...p, fileId: uploadUrlResponse.file_id, progress: 30 } : p));
|
|
644
|
+
const uploadResponse = await fetch(uploadUrlResponse.upload_url, {
|
|
645
|
+
method: "PUT",
|
|
646
|
+
body: file,
|
|
647
|
+
headers: { "Content-Type": file.type || "application/octet-stream" }
|
|
648
|
+
});
|
|
649
|
+
if (!uploadResponse.ok) throw new Error(`Upload failed: ${uploadResponse.statusText}`);
|
|
650
|
+
setUploadProgress((prev) => prev.map((p) => p.fileId === uploadUrlResponse.file_id ? { ...p, status: "confirming", progress: 70 } : p));
|
|
651
|
+
const confirmResponse = await fetchFromComms("/files", {
|
|
652
|
+
method: "POST",
|
|
653
|
+
body: JSON.stringify({ file_id: uploadUrlResponse.file_id })
|
|
654
|
+
});
|
|
655
|
+
setUploadProgress((prev) => prev.map((p) => p.fileId === uploadUrlResponse.file_id ? { ...p, status: "complete", progress: 100 } : p));
|
|
656
|
+
setTimeout(() => setUploadProgress((prev) => prev.filter((p) => p.fileId !== uploadUrlResponse.file_id)), 2e3);
|
|
657
|
+
return confirmResponse.file;
|
|
658
|
+
} catch (error) {
|
|
659
|
+
console.error("[AegisChat] Failed to upload file:", error);
|
|
660
|
+
setUploadProgress((prev) => prev.map((p) => p.fileId === fileId ? { ...p, status: "error", error: error instanceof Error ? error.message : "Upload failed" } : p));
|
|
661
|
+
setTimeout(() => setUploadProgress((prev) => prev.filter((p) => p.fileId !== fileId)), 5e3);
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
}, [fetchFromComms]);
|
|
665
|
+
const sendMessageWithFiles = useCallback(async (content, files, msgOptions = {}) => {
|
|
666
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
667
|
+
const currentSession = sessionRef.current;
|
|
668
|
+
if (!currentActiveChannelId || !content.trim() && files.length === 0 || !currentSession) return;
|
|
669
|
+
const tempId = `temp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
670
|
+
const trimmedContent = content.trim();
|
|
671
|
+
const optimisticMessage = {
|
|
672
|
+
id: tempId,
|
|
673
|
+
tempId,
|
|
674
|
+
channel_id: currentActiveChannelId,
|
|
675
|
+
sender_id: currentSession.comms_user_id,
|
|
676
|
+
content: trimmedContent || `Uploading ${files.length} file(s)...`,
|
|
677
|
+
type: "file",
|
|
678
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
679
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
680
|
+
status: "sending",
|
|
681
|
+
metadata: { ...msgOptions.metadata, files: files.map((f) => ({ id: `temp-${f.name}`, filename: f.name, mime_type: f.type, size: f.size, url: "" })) }
|
|
682
|
+
};
|
|
683
|
+
setMessages((prev) => [...prev, optimisticMessage]);
|
|
684
|
+
try {
|
|
685
|
+
const uploadedFiles = [];
|
|
686
|
+
for (const file of files) {
|
|
687
|
+
const attachment = await uploadFile(file);
|
|
688
|
+
if (attachment) uploadedFiles.push(attachment);
|
|
689
|
+
}
|
|
690
|
+
const messageType = uploadedFiles.length > 0 && !trimmedContent ? "file" : "text";
|
|
691
|
+
await fetchFromComms(`/channels/${currentActiveChannelId}/messages`, {
|
|
692
|
+
method: "POST",
|
|
693
|
+
body: JSON.stringify({
|
|
694
|
+
content: trimmedContent || (uploadedFiles.length > 0 ? `Shared ${uploadedFiles.length} file(s)` : ""),
|
|
695
|
+
type: msgOptions.type || messageType,
|
|
696
|
+
parent_id: msgOptions.parent_id,
|
|
697
|
+
metadata: { ...msgOptions.metadata, files: uploadedFiles },
|
|
698
|
+
file_ids: uploadedFiles.map((f) => f.id)
|
|
699
|
+
})
|
|
700
|
+
});
|
|
701
|
+
} catch (error) {
|
|
702
|
+
console.error("[AegisChat] Failed to send message with files:", error);
|
|
703
|
+
setMessages((prev) => prev.map((m) => m.tempId === tempId ? { ...m, status: "failed", errorMessage: error instanceof Error ? error.message : "Failed to send" } : m));
|
|
704
|
+
throw error;
|
|
705
|
+
}
|
|
706
|
+
}, [fetchFromComms, uploadFile]);
|
|
707
|
+
const stopTyping = useCallback(() => {
|
|
708
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
709
|
+
if (!currentActiveChannelId || !wsRef.current) return;
|
|
710
|
+
wsRef.current.send(JSON.stringify({ type: "typing.stop", payload: { channel_id: currentActiveChannelId } }));
|
|
711
|
+
if (typingTimeout.current) {
|
|
712
|
+
clearTimeout(typingTimeout.current);
|
|
713
|
+
typingTimeout.current = null;
|
|
714
|
+
}
|
|
715
|
+
}, []);
|
|
716
|
+
const startTyping = useCallback(() => {
|
|
717
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
718
|
+
if (!currentActiveChannelId || !wsRef.current) return;
|
|
719
|
+
wsRef.current.send(JSON.stringify({ type: "typing.start", payload: { channel_id: currentActiveChannelId } }));
|
|
720
|
+
if (typingTimeout.current) clearTimeout(typingTimeout.current);
|
|
721
|
+
typingTimeout.current = setTimeout(stopTyping, TYPING_TIMEOUT);
|
|
722
|
+
}, [stopTyping]);
|
|
723
|
+
const createDMWithUser = useCallback(async (userId) => {
|
|
724
|
+
try {
|
|
725
|
+
const channel = await fetchFromComms("/channels/dm", {
|
|
726
|
+
method: "POST",
|
|
727
|
+
body: JSON.stringify({ user_id: userId })
|
|
728
|
+
});
|
|
729
|
+
await refreshChannels();
|
|
730
|
+
return channel.id;
|
|
731
|
+
} catch (error) {
|
|
732
|
+
console.error("[AegisChat] Failed to create DM:", error);
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
}, [fetchFromComms, refreshChannels]);
|
|
736
|
+
const retryMessage = useCallback(async (tempId) => {
|
|
737
|
+
const failedMessage = messages.find((m) => m.tempId === tempId && m.status === "failed");
|
|
738
|
+
const currentActiveChannelId = activeChannelIdRef.current;
|
|
739
|
+
if (!failedMessage || !currentActiveChannelId) return;
|
|
740
|
+
setMessages((prev) => prev.map((m) => m.tempId === tempId ? { ...m, status: "sending", errorMessage: void 0 } : m));
|
|
741
|
+
try {
|
|
742
|
+
await fetchFromComms(`/channels/${currentActiveChannelId}/messages`, {
|
|
743
|
+
method: "POST",
|
|
744
|
+
body: JSON.stringify({ content: failedMessage.content, type: failedMessage.type, metadata: failedMessage.metadata })
|
|
745
|
+
});
|
|
746
|
+
} catch (error) {
|
|
747
|
+
console.error("[AegisChat] Failed to retry message:", error);
|
|
748
|
+
setMessages((prev) => prev.map((m) => m.tempId === tempId ? { ...m, status: "failed", errorMessage: error instanceof Error ? error.message : "Failed to send" } : m));
|
|
749
|
+
}
|
|
750
|
+
}, [messages, fetchFromComms]);
|
|
751
|
+
const deleteFailedMessage = useCallback((tempId) => {
|
|
752
|
+
setMessages((prev) => prev.filter((m) => m.tempId !== tempId));
|
|
753
|
+
}, []);
|
|
754
|
+
useEffect(() => {
|
|
755
|
+
connect();
|
|
756
|
+
}, []);
|
|
757
|
+
useEffect(() => {
|
|
758
|
+
if (session && !isConnected && !isConnecting && autoConnect) {
|
|
759
|
+
connectWebSocket();
|
|
760
|
+
}
|
|
761
|
+
}, [session, isConnected, isConnecting, autoConnect, connectWebSocket]);
|
|
762
|
+
useEffect(() => {
|
|
763
|
+
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
|
|
764
|
+
wsRef.current.onmessage = (event) => {
|
|
765
|
+
try {
|
|
766
|
+
const data = JSON.parse(event.data);
|
|
767
|
+
handleWebSocketMessage(data);
|
|
768
|
+
} catch (error) {
|
|
769
|
+
console.error("[AegisChat] Failed to parse WebSocket message:", error);
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
}, [handleWebSocketMessage]);
|
|
774
|
+
useEffect(() => {
|
|
775
|
+
if (isConnected && channels.length === 0) {
|
|
776
|
+
refreshChannels();
|
|
777
|
+
}
|
|
778
|
+
}, [isConnected, channels.length, refreshChannels]);
|
|
779
|
+
useEffect(() => {
|
|
780
|
+
const storedActiveChannel = getActiveChannelId();
|
|
781
|
+
if (storedActiveChannel && !activeChannelId) {
|
|
782
|
+
selectChannel(storedActiveChannel);
|
|
783
|
+
}
|
|
784
|
+
}, [getActiveChannelId, activeChannelId, selectChannel]);
|
|
785
|
+
useEffect(() => {
|
|
786
|
+
return () => {
|
|
787
|
+
clearTimers();
|
|
788
|
+
if (typingTimeout.current) clearTimeout(typingTimeout.current);
|
|
789
|
+
if (wsRef.current) {
|
|
790
|
+
isManualDisconnect.current = true;
|
|
791
|
+
wsRef.current.close();
|
|
792
|
+
wsRef.current = null;
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
}, [clearTimers]);
|
|
796
|
+
return {
|
|
797
|
+
session,
|
|
798
|
+
isConnected,
|
|
799
|
+
isConnecting,
|
|
800
|
+
channels,
|
|
801
|
+
messages,
|
|
802
|
+
activeChannelId,
|
|
803
|
+
typingUsers: activeChannelId ? typingUsers[activeChannelId] || [] : [],
|
|
804
|
+
isLoadingChannels,
|
|
805
|
+
isLoadingMessages,
|
|
806
|
+
hasMoreMessages,
|
|
807
|
+
uploadProgress,
|
|
808
|
+
connect,
|
|
809
|
+
disconnect,
|
|
810
|
+
selectChannel,
|
|
811
|
+
sendMessage,
|
|
812
|
+
sendMessageWithFiles,
|
|
813
|
+
uploadFile,
|
|
814
|
+
loadMoreMessages,
|
|
815
|
+
startTyping,
|
|
816
|
+
stopTyping,
|
|
817
|
+
refreshChannels,
|
|
818
|
+
createDMWithUser,
|
|
819
|
+
retryMessage,
|
|
820
|
+
deleteFailedMessage,
|
|
821
|
+
markAsRead
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// src/hooks/useAutoRead.ts
|
|
826
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
|
|
827
|
+
var SESSION_STORAGE_KEY2 = "@aegischat/activeChannel";
|
|
828
|
+
function useAutoRead(options = {}) {
|
|
829
|
+
const [isFocused, setIsFocused] = useState2(false);
|
|
830
|
+
useEffect2(() => {
|
|
831
|
+
setIsFocused(typeof document !== "undefined" && document.hasFocus());
|
|
832
|
+
const handleFocus = () => setIsFocused(true);
|
|
833
|
+
const handleBlur = () => setIsFocused(false);
|
|
834
|
+
window.addEventListener("focus", handleFocus);
|
|
835
|
+
window.addEventListener("blur", handleBlur);
|
|
836
|
+
return () => {
|
|
837
|
+
window.removeEventListener("focus", handleFocus);
|
|
838
|
+
window.removeEventListener("blur", handleBlur);
|
|
839
|
+
};
|
|
840
|
+
}, []);
|
|
841
|
+
useEffect2(() => {
|
|
842
|
+
const handleVisibilityChange = () => {
|
|
843
|
+
if (!document.hidden) {
|
|
844
|
+
const activeChannelId = sessionStorage.getItem(SESSION_STORAGE_KEY2);
|
|
845
|
+
if (activeChannelId) {
|
|
846
|
+
markAsRead(activeChannelId);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
851
|
+
return () => document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
852
|
+
}, []);
|
|
853
|
+
const markAsRead = useCallback2(async (channelId) => {
|
|
854
|
+
if (!isFocused) return;
|
|
855
|
+
try {
|
|
856
|
+
await channelsApi.markAsRead(channelId);
|
|
857
|
+
options.onMarkAsRead?.(channelId);
|
|
858
|
+
} catch (error) {
|
|
859
|
+
console.error("[AegisChat] useAutoRead: Failed to mark as read:", error);
|
|
860
|
+
}
|
|
861
|
+
}, [isFocused, options.onMarkAsRead]);
|
|
862
|
+
const markAllAsRead = useCallback2(async () => {
|
|
863
|
+
if (!isFocused) return;
|
|
864
|
+
try {
|
|
865
|
+
const response = await channelsApi.list({});
|
|
866
|
+
const channels = response.data.channels || [];
|
|
867
|
+
await Promise.all(
|
|
868
|
+
channels.filter((ch) => ch.unread_count > 0).map((ch) => channelsApi.markAsRead(ch.id))
|
|
869
|
+
);
|
|
870
|
+
} catch (error) {
|
|
871
|
+
console.error("[AegisChat] useAutoRead: Failed to mark all as read:", error);
|
|
872
|
+
}
|
|
873
|
+
}, [isFocused]);
|
|
874
|
+
return { markAsRead, markAllAsRead, isFocused };
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// src/hooks/useChannels.ts
|
|
878
|
+
import { useCallback as useCallback3, useEffect as useEffect3, useRef as useRef2, useState as useState3 } from "react";
|
|
879
|
+
function useChannels(options = {}) {
|
|
880
|
+
const { type, limit = 20, autoFetch = true, onChannelCreated, onError } = options;
|
|
881
|
+
const [channels, setChannels] = useState3([]);
|
|
882
|
+
const [isLoading, setIsLoading] = useState3(false);
|
|
883
|
+
const [error, setError] = useState3(null);
|
|
884
|
+
const abortControllerRef = useRef2(null);
|
|
885
|
+
const fetchChannels = useCallback3(async (signal) => {
|
|
886
|
+
setIsLoading(true);
|
|
887
|
+
setError(null);
|
|
888
|
+
try {
|
|
889
|
+
const response = await channelsApi.list({ type, limit }, signal);
|
|
890
|
+
if (signal?.aborted) return;
|
|
891
|
+
setChannels(response.data.channels);
|
|
892
|
+
} catch (err) {
|
|
893
|
+
if (err instanceof Error && err.name === "AbortError") return;
|
|
894
|
+
const error2 = err instanceof Error ? err : new Error("Failed to fetch channels");
|
|
895
|
+
setError(error2);
|
|
896
|
+
onError?.(error2);
|
|
897
|
+
} finally {
|
|
898
|
+
if (!signal?.aborted) setIsLoading(false);
|
|
899
|
+
}
|
|
900
|
+
}, [type, limit, onError]);
|
|
901
|
+
const refetch = useCallback3(async () => {
|
|
902
|
+
if (abortControllerRef.current) abortControllerRef.current.abort();
|
|
903
|
+
const controller = new AbortController();
|
|
904
|
+
abortControllerRef.current = controller;
|
|
905
|
+
await fetchChannels(controller.signal);
|
|
906
|
+
}, [fetchChannels]);
|
|
907
|
+
const getOrCreateDM = useCallback3(async (userId) => {
|
|
908
|
+
try {
|
|
909
|
+
const response = await channelsApi.getOrCreateDM(userId);
|
|
910
|
+
if (abortControllerRef.current) abortControllerRef.current.abort();
|
|
911
|
+
const controller = new AbortController();
|
|
912
|
+
abortControllerRef.current = controller;
|
|
913
|
+
fetchChannels(controller.signal);
|
|
914
|
+
onChannelCreated?.(response.data);
|
|
915
|
+
return response.data;
|
|
916
|
+
} catch (err) {
|
|
917
|
+
const error2 = err instanceof Error ? err : new Error("Failed to create DM");
|
|
918
|
+
onError?.(error2);
|
|
919
|
+
throw error2;
|
|
920
|
+
}
|
|
921
|
+
}, [fetchChannels, onChannelCreated, onError]);
|
|
922
|
+
const markAsRead = useCallback3(async (channelId) => {
|
|
923
|
+
try {
|
|
924
|
+
await channelsApi.markAsRead(channelId);
|
|
925
|
+
setChannels((prev) => prev.map((ch) => ch.id === channelId ? { ...ch, unread_count: 0 } : ch));
|
|
926
|
+
} catch (err) {
|
|
927
|
+
const error2 = err instanceof Error ? err : new Error("Failed to mark as read");
|
|
928
|
+
onError?.(error2);
|
|
929
|
+
throw error2;
|
|
930
|
+
}
|
|
931
|
+
}, [onError]);
|
|
932
|
+
useEffect3(() => {
|
|
933
|
+
if (autoFetch) {
|
|
934
|
+
if (abortControllerRef.current) abortControllerRef.current.abort();
|
|
935
|
+
const controller = new AbortController();
|
|
936
|
+
abortControllerRef.current = controller;
|
|
937
|
+
fetchChannels(controller.signal);
|
|
938
|
+
return () => controller.abort();
|
|
939
|
+
}
|
|
940
|
+
}, [autoFetch, fetchChannels]);
|
|
941
|
+
return { channels, isLoading, error, refetch, getOrCreateDM, markAsRead };
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// src/hooks/useMessages.ts
|
|
945
|
+
import { useCallback as useCallback4, useState as useState4 } from "react";
|
|
946
|
+
function useMessages(_options) {
|
|
947
|
+
const [messages, setMessages] = useState4([]);
|
|
948
|
+
const [isLoading, setIsLoading] = useState4(false);
|
|
949
|
+
const [hasMore, setHasMore] = useState4(true);
|
|
950
|
+
const sendMessage = useCallback4(async (params) => {
|
|
951
|
+
}, []);
|
|
952
|
+
const loadMore = useCallback4(async () => {
|
|
953
|
+
}, []);
|
|
954
|
+
return { messages, isLoading, hasMore, sendMessage, loadMore };
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
// src/hooks/useTypingIndicator.ts
|
|
958
|
+
import { useCallback as useCallback5, useState as useState5 } from "react";
|
|
959
|
+
function useTypingIndicator(_options) {
|
|
960
|
+
const [typingUsers, setTypingUsers] = useState5([]);
|
|
961
|
+
const startTyping = useCallback5(() => {
|
|
962
|
+
}, []);
|
|
963
|
+
const stopTyping = useCallback5(() => {
|
|
964
|
+
}, []);
|
|
965
|
+
return { typingUsers, startTyping, stopTyping };
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// src/hooks/useReactions.ts
|
|
969
|
+
import { useState as useState6 } from "react";
|
|
970
|
+
function useReactions(_options) {
|
|
971
|
+
const [reactions, setReactions] = useState6([]);
|
|
972
|
+
const addReaction = async (_emoji) => {
|
|
973
|
+
};
|
|
974
|
+
const removeReaction = async (_emoji) => {
|
|
975
|
+
};
|
|
976
|
+
return { reactions, addReaction, removeReaction };
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// src/hooks/useFileUpload.ts
|
|
980
|
+
import { useState as useState7 } from "react";
|
|
981
|
+
function useFileUpload(_options) {
|
|
982
|
+
const [uploadProgress, setUploadProgress] = useState7([]);
|
|
983
|
+
const upload = async (_file) => null;
|
|
984
|
+
return { uploadProgress, upload };
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
// src/hooks/useMentions.ts
|
|
988
|
+
function useMentions(_options = {}) {
|
|
989
|
+
const parseMentions = (content) => {
|
|
990
|
+
const mentionRegex = /@\[([^\]]+)\]\(([^)]+)\)/g;
|
|
991
|
+
const mentions = [];
|
|
992
|
+
let match;
|
|
993
|
+
while ((match = mentionRegex.exec(content)) !== null) {
|
|
994
|
+
mentions.push(match[2]);
|
|
995
|
+
}
|
|
996
|
+
return mentions;
|
|
997
|
+
};
|
|
998
|
+
const highlightMentions = (content) => {
|
|
999
|
+
return content.replace(/@\[([^\]]+)\]\(([^)]+)\)/g, '<span class="mention">@$1</span>');
|
|
1000
|
+
};
|
|
1001
|
+
const isUserMentioned = (content, userId) => {
|
|
1002
|
+
const mentions = parseMentions(content);
|
|
1003
|
+
return mentions.includes(userId);
|
|
1004
|
+
};
|
|
1005
|
+
return { parseMentions, highlightMentions, isUserMentioned };
|
|
1006
|
+
}
|
|
1007
|
+
export {
|
|
1008
|
+
channelsApi,
|
|
1009
|
+
chatApi,
|
|
1010
|
+
configureApiClient,
|
|
1011
|
+
filesApi,
|
|
1012
|
+
messagesApi,
|
|
1013
|
+
reactionsApi,
|
|
1014
|
+
useAutoRead,
|
|
1015
|
+
useChannels,
|
|
1016
|
+
useChat,
|
|
1017
|
+
useFileUpload,
|
|
1018
|
+
useMentions,
|
|
1019
|
+
useMessages,
|
|
1020
|
+
useReactions,
|
|
1021
|
+
useTypingIndicator,
|
|
1022
|
+
usersApi
|
|
1023
|
+
};
|
|
1024
|
+
//# sourceMappingURL=index.mjs.map
|