@webinex/chatify 2.0.0-build5 → 2.0.0-build8

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.
Files changed (117) hide show
  1. package/dist/esm/index.js +2 -2
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/esm/types/core/api/accountApi.d.ts +3 -2
  4. package/dist/esm/types/core/api/api.d.ts +1592 -1752
  5. package/dist/esm/types/core/api/baseApi.d.ts +5 -1
  6. package/dist/esm/types/core/api/chatApi.d.ts +2 -0
  7. package/dist/esm/types/core/api/chatListApi.d.ts +3 -3
  8. package/dist/esm/types/core/api/chatMemberApi.d.ts +3 -2
  9. package/dist/esm/types/core/api/chatMessageApi.d.ts +3 -22
  10. package/dist/esm/types/core/api/threadApi.d.ts +1 -87
  11. package/dist/esm/types/core/api/threadMessageApi.d.ts +2 -15
  12. package/dist/index.d.ts +2987 -3115
  13. package/package.json +2 -3
  14. package/src/core/api/accountApi.ts +5 -5
  15. package/src/core/api/api.ts +642 -42
  16. package/src/core/api/baseApi.ts +4 -1
  17. package/src/core/api/chatApi.ts +5 -1
  18. package/src/core/api/chatListApi.ts +5 -18
  19. package/src/core/api/chatMemberApi.ts +5 -5
  20. package/src/core/api/chatMessageApi.ts +4 -139
  21. package/src/core/api/threadApi.ts +1 -117
  22. package/src/core/api/threadMessageApi.ts +1 -99
  23. package/src/types/core/api/accountApi.d.ts +5 -0
  24. package/src/types/core/api/api.d.ts +2380 -0
  25. package/src/types/core/api/baseApi.d.ts +15 -0
  26. package/src/types/core/api/chatApi.d.ts +16 -0
  27. package/src/types/core/api/chatListApi.d.ts +5 -0
  28. package/src/types/core/api/chatMemberApi.d.ts +6 -0
  29. package/src/types/core/api/chatMessageApi.d.ts +3 -0
  30. package/src/types/core/api/index.d.ts +1 -0
  31. package/src/types/core/api/threadApi.d.ts +1 -0
  32. package/src/types/core/api/threadMessageApi.d.ts +6 -0
  33. package/src/types/core/client.d.ts +83 -0
  34. package/src/types/core/constants.d.ts +1 -0
  35. package/src/types/core/debounce.d.ts +27 -0
  36. package/src/types/core/index.d.ts +4 -0
  37. package/src/types/core/types.d.ts +105 -0
  38. package/src/types/index.d.ts +2 -0
  39. package/src/types/ui/Chat/ChatContext.d.ts +10 -0
  40. package/src/types/ui/Chat/ChatEditableHeaderName.d.ts +2 -0
  41. package/src/types/ui/Chat/ChatHeaderActions.d.ts +2 -0
  42. package/src/types/ui/Chat/ChatPanel.d.ts +13 -0
  43. package/src/types/ui/Chat/ChatView.d.ts +17 -0
  44. package/src/types/ui/Chat/Members/ChatMembersButton.d.ts +2 -0
  45. package/src/types/ui/Chat/Members/ChatMembersPanel.d.ts +2 -0
  46. package/src/types/ui/Chat/Members/ChatMembersPreview.d.ts +2 -0
  47. package/src/types/ui/Chat/Members/index.d.ts +11 -0
  48. package/src/types/ui/Chat/index.d.ts +6 -0
  49. package/src/types/ui/ChatGroup/ChatGroupContext.d.ts +12 -0
  50. package/src/types/ui/ChatGroup/ChatGroupPanel.d.ts +15 -0
  51. package/src/types/ui/ChatGroup/Create/CreateChatButton.d.ts +2 -0
  52. package/src/types/ui/ChatGroup/Create/CreateChatForm.d.ts +11 -0
  53. package/src/types/ui/ChatGroup/Create/CreateChatMemberInput.d.ts +5 -0
  54. package/src/types/ui/ChatGroup/Create/CreateChatNameInput.d.ts +5 -0
  55. package/src/types/ui/ChatGroup/Create/CreateChatPanel.d.ts +2 -0
  56. package/src/types/ui/ChatGroup/Create/index.d.ts +17 -0
  57. package/src/types/ui/ChatGroup/Layout/ChatGroupAside.d.ts +2 -0
  58. package/src/types/ui/ChatGroup/Layout/ChatGroupBody.d.ts +2 -0
  59. package/src/types/ui/ChatGroup/Layout/ChatGroupFooter.d.ts +2 -0
  60. package/src/types/ui/ChatGroup/Layout/ChatGroupHeader.d.ts +2 -0
  61. package/src/types/ui/ChatGroup/Layout/ChatGroupMain.d.ts +2 -0
  62. package/src/types/ui/ChatGroup/Layout/index.d.ts +17 -0
  63. package/src/types/ui/ChatGroup/List/ChatList.d.ts +2 -0
  64. package/src/types/ui/ChatGroup/List/ChatListItemAvatar.d.ts +3 -0
  65. package/src/types/ui/ChatGroup/List/ChatListItemBox.d.ts +6 -0
  66. package/src/types/ui/ChatGroup/List/ChatListItemLastMessage.d.ts +3 -0
  67. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageAuthor.d.ts +3 -0
  68. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageContent.d.ts +3 -0
  69. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageSentAt.d.ts +3 -0
  70. package/src/types/ui/ChatGroup/List/ChatListItemName.d.ts +3 -0
  71. package/src/types/ui/ChatGroup/List/ChatListItemUnreadCount.d.ts +3 -0
  72. package/src/types/ui/ChatGroup/List/index.d.ts +29 -0
  73. package/src/types/ui/ChatGroup/index.d.ts +5 -0
  74. package/src/types/ui/Chatify.d.ts +8 -0
  75. package/src/types/ui/Conversation/Conversation.d.ts +21 -0
  76. package/src/types/ui/Conversation/ConversationActions.d.ts +2 -0
  77. package/src/types/ui/Conversation/ConversationBody.d.ts +2 -0
  78. package/src/types/ui/Conversation/ConversationContext.d.ts +20 -0
  79. package/src/types/ui/Conversation/ConversationHeader.d.ts +2 -0
  80. package/src/types/ui/Conversation/ConversationName.d.ts +2 -0
  81. package/src/types/ui/Conversation/InputBox/InputBox.d.ts +2 -0
  82. package/src/types/ui/Conversation/InputBox/InputFilesBox.d.ts +2 -0
  83. package/src/types/ui/Conversation/InputBox/InputSubmitButtonBox.d.ts +5 -0
  84. package/src/types/ui/Conversation/InputBox/InputTextBox.d.ts +8 -0
  85. package/src/types/ui/Conversation/InputBox/index.d.ts +14 -0
  86. package/src/types/ui/Conversation/Message/MessageAuthor.d.ts +3 -0
  87. package/src/types/ui/Conversation/Message/MessageBox.d.ts +6 -0
  88. package/src/types/ui/Conversation/Message/MessageContent.d.ts +3 -0
  89. package/src/types/ui/Conversation/Message/MessageInfoBox.d.ts +3 -0
  90. package/src/types/ui/Conversation/Message/MessageRow.d.ts +6 -0
  91. package/src/types/ui/Conversation/Message/MessageText.d.ts +3 -0
  92. package/src/types/ui/Conversation/Message/index.d.ts +20 -0
  93. package/src/types/ui/Conversation/NextObserver/NextMessagesObserver.d.ts +5 -0
  94. package/src/types/ui/Conversation/NextObserver/index.d.ts +1 -0
  95. package/src/types/ui/Conversation/ReadObserver/MessageReadObserver.d.ts +6 -0
  96. package/src/types/ui/Conversation/ReadObserver/index.d.ts +1 -0
  97. package/src/types/ui/Conversation/SendingMessage/SendingMessageBox.d.ts +7 -0
  98. package/src/types/ui/Conversation/SendingMessage/SendingMessageContent.d.ts +3 -0
  99. package/src/types/ui/Conversation/SendingMessage/SendingMessageInfoBox.d.ts +3 -0
  100. package/src/types/ui/Conversation/SendingMessage/SendingMessageText.d.ts +3 -0
  101. package/src/types/ui/Conversation/SendingMessage/index.d.ts +14 -0
  102. package/src/types/ui/Conversation/SystemMessage/SystemMessageBox.d.ts +6 -0
  103. package/src/types/ui/Conversation/SystemMessage/SystemMessageRow.d.ts +9 -0
  104. package/src/types/ui/Conversation/SystemMessage/index.d.ts +8 -0
  105. package/src/types/ui/Conversation/index.d.ts +12 -0
  106. package/src/types/ui/Thread/ThreadActions.d.ts +2 -0
  107. package/src/types/ui/Thread/ThreadContext.d.ts +8 -0
  108. package/src/types/ui/Thread/ThreadPanel.d.ts +14 -0
  109. package/src/types/ui/Thread/index.d.ts +3 -0
  110. package/src/types/ui/color.d.ts +6 -0
  111. package/src/types/ui/common/Avatar.d.ts +6 -0
  112. package/src/types/ui/common/Icon.d.ts +20 -0
  113. package/src/types/ui/common/MessageSkeleton.d.ts +5 -0
  114. package/src/types/ui/common/index.d.ts +11 -0
  115. package/src/types/ui/customize.d.ts +9 -0
  116. package/src/types/ui/index.d.ts +11 -0
  117. package/src/types/ui/localizer.d.ts +35 -0
@@ -1,42 +1,642 @@
1
- import { Api } from '@reduxjs/toolkit/query/react';
2
- import { __accountApi } from './accountApi';
3
- import { __chatApi } from './chatApi';
4
- import { __baseApi, __settings } from './baseApi';
5
- import { __chatMessageApi } from './chatMessageApi';
6
- import { __chatMemberApi } from './chatMemberApi';
7
- import { __chatListApi, useChatListItem } from './chatListApi';
8
- import { __threadApi } from './threadApi';
9
- import { __threadMessageApi } from './threadMessageApi';
10
-
11
- type GetApiTypes<T> = T extends Api<
12
- infer TBaseQuery,
13
- infer TEndpoints,
14
- infer TTagTypes,
15
- infer TEnchancers,
16
- infer TModule
17
- >
18
- ? [TBaseQuery, TEndpoints, TTagTypes, TEnchancers, TModule]
19
- : never;
20
-
21
- const api: Api<
22
- GetApiTypes<typeof __baseApi>[0],
23
- GetApiTypes<typeof __chatApi>[1] &
24
- GetApiTypes<typeof __chatListApi>[1] &
25
- GetApiTypes<typeof __accountApi>[1] &
26
- GetApiTypes<typeof __chatMessageApi>[1] &
27
- GetApiTypes<typeof __chatMemberApi>[1] &
28
- GetApiTypes<typeof __threadApi>[1] &
29
- GetApiTypes<typeof __threadMessageApi>[1],
30
- GetApiTypes<typeof __baseApi>[2],
31
- GetApiTypes<typeof __baseApi>[3] &
32
- GetApiTypes<typeof __chatApi>[3] &
33
- GetApiTypes<typeof __chatListApi>[3] &
34
- GetApiTypes<typeof __accountApi>[3] &
35
- GetApiTypes<typeof __chatMessageApi>[3] &
36
- GetApiTypes<typeof __chatMemberApi>[3] &
37
- GetApiTypes<typeof __threadApi>[3] &
38
- GetApiTypes<typeof __threadMessageApi>[3],
39
- GetApiTypes<typeof __baseApi>[4]
40
- > = __baseApi as any;
41
-
42
- export const chatifyApi = Object.assign(api, { settings: __settings, useChatListItem });
1
+ import { DEFAULT_MESSAGE_LIST_TAKE, __baseApi, __settings } from './baseApi';
2
+ import { useSelector } from 'react-redux';
3
+ import {
4
+ Account,
5
+ AddChatMemberRequest,
6
+ Chat,
7
+ ChatListItem,
8
+ ChatMessage,
9
+ RemoveChatMemberRequest,
10
+ SendThreadMessageRequest,
11
+ Thread,
12
+ ThreadMessage,
13
+ ThreadWatchListItem,
14
+ calcThreadListUnreadCount,
15
+ chatId,
16
+ parseThreadMessageId,
17
+ } from '../types';
18
+ import { unsubscribe } from '../client';
19
+ import { Debounce, DebounceValue } from '../debounce';
20
+ import { useMemo } from 'react';
21
+
22
+ export interface ChatMessageListResult {
23
+ messages: ChatMessage[];
24
+ hasMore: boolean;
25
+ }
26
+
27
+ export interface ThreadMessageList {
28
+ messages: ThreadMessage[];
29
+ hasMore: boolean;
30
+ }
31
+
32
+ const THREAD_MESSAGE_READ_QUEUE = new Debounce<string>({
33
+ callback: (id) => __settings.client.readThreadMessage(id),
34
+ compare: (a, b) => -a.localeCompare(b),
35
+ groupBy: (id) => parseThreadMessageId(id)[0],
36
+ timeout: 500,
37
+ });
38
+
39
+ const CHAT_MESSAGE_READ_QUEUE = new Debounce<string>({
40
+ callback: (id) => __settings.client.readChatMessage({ id }),
41
+ compare: (a, b) => -a.localeCompare(b),
42
+ groupBy: (id) => chatId(id),
43
+ timeout: 500,
44
+ });
45
+
46
+ const __chatifyApi = __baseApi.injectEndpoints({
47
+ endpoints: (builder) => ({
48
+ // ====================================================
49
+ // ============== ACCOUNT ======================
50
+ // ====================================================
51
+
52
+ getAccountList: builder.query<Account[], void>({
53
+ queryFn: async () => ({ data: await __settings.client.accounts() }),
54
+ providesTags: ['account'],
55
+ }),
56
+
57
+ // ====================================================
58
+ // ================ CHAT =======================
59
+ // ====================================================
60
+
61
+ getChat: builder.query<Chat, { id: string }>({
62
+ queryFn: async ({ id }) => ({ data: await __settings.client.chat(id) }),
63
+ providesTags: ['chat', 'account'],
64
+ onCacheEntryAdded: async ({ id }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
65
+ await cacheDataLoaded;
66
+
67
+ unsubscribe(
68
+ __settings.client.subscribe(
69
+ 'chatify://chat-member-added',
70
+ ([chatId, _, account, message, , read]) =>
71
+ chatId === id &&
72
+ updateCachedData((chat) => {
73
+ chat.members.push(account);
74
+ chat.lastReadMessageId = read ? message.id : chat.lastReadMessageId;
75
+ }),
76
+ ),
77
+ __settings.client.subscribe(
78
+ 'chatify://chat-member-removed',
79
+ ([chatId, accountId, , message, read]) =>
80
+ id === chatId &&
81
+ updateCachedData((chat) => {
82
+ chat.members = chat.members.filter((x) => x.id !== accountId);
83
+ chat.lastReadMessageId = read ? message.id : chat.lastReadMessageId;
84
+ }),
85
+ ),
86
+ __settings.client.subscribe(
87
+ 'chatify://chat-name-changed',
88
+ ([chatId, newName, message, read]) =>
89
+ id === chatId &&
90
+ updateCachedData((chat) => {
91
+ chat.name = newName;
92
+ chat.lastReadMessageId = read ? message.id : chat.lastReadMessageId;
93
+ }),
94
+ ),
95
+ __settings.client.subscribe(
96
+ 'chatify://chat-new-message',
97
+ ([message]) =>
98
+ id === message.chatId &&
99
+ updateCachedData((chat) => {
100
+ chat.lastReadMessageId =
101
+ message.sentBy.id === __settings.me() ? message.id : chat.lastReadMessageId;
102
+ }),
103
+ ),
104
+ __settings.client.subscribe(
105
+ 'chatify://chat-message-read',
106
+ ([events]) =>
107
+ events.find((x) => x.chatId === id) &&
108
+ updateCachedData((chat) => {
109
+ const event = events.find((x) => x.chatId === id)!;
110
+ chat.lastReadMessageId = event.newLastReadMessageId;
111
+ }),
112
+ ),
113
+ ).when(cacheEntryRemoved);
114
+ },
115
+ }),
116
+
117
+ addChat: builder.mutation<string, { name: string; members: string[] }>({
118
+ queryFn: async ({ name, members }) => {
119
+ const chatId = await __settings.client.addChat({
120
+ name,
121
+ members,
122
+ requestId: new Date().getTime().toString(),
123
+ });
124
+ return { data: chatId };
125
+ },
126
+ }),
127
+
128
+ updateChatName: builder.mutation<void, { id: string; name: string }>({
129
+ queryFn: async ({ id, name }) => {
130
+ await __settings.client.updateChatName({ id, name });
131
+ return { data: null! };
132
+ },
133
+ }),
134
+
135
+ // ====================================================
136
+ // ============= CHAT LIST =====================
137
+ // ====================================================
138
+
139
+ getChatList: builder.query<ChatListItem[], void>({
140
+ queryFn: async () => ({ data: await __settings.client.chats() }),
141
+ providesTags: ['chat', 'account'],
142
+ onCacheEntryAdded: async (_, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
143
+ await cacheDataLoaded;
144
+
145
+ unsubscribe(
146
+ __settings.client.subscribe('chatify://chat-created', ([chat]) =>
147
+ updateCachedData((chatList) => {
148
+ chatList.push(chat);
149
+ }),
150
+ ),
151
+
152
+ __settings.client.subscribe('chatify://chat-new-message', ([message]) =>
153
+ updateCachedData((chatList) => {
154
+ const chat = chatList.find((x) => x.id === message.chatId);
155
+
156
+ if (!chat) {
157
+ return;
158
+ }
159
+
160
+ chat.message = message;
161
+
162
+ if (message.sentBy.id === __settings.me()) {
163
+ chat.lastReadMessageId = message.id;
164
+ chat.totalUnreadCount = 0;
165
+ }
166
+
167
+ if (message.sentBy.id !== __settings.me()) {
168
+ chat.totalUnreadCount++;
169
+ }
170
+ }),
171
+ ),
172
+ __settings.client.subscribe('chatify://chat-message-read', ([events]) =>
173
+ updateCachedData((chatList) => {
174
+ for (const event of events) {
175
+ const chat = chatList.find((x) => x.id === event.chatId);
176
+
177
+ if (!chat) {
178
+ return;
179
+ }
180
+
181
+ chat.lastReadMessageId = event.newLastReadMessageId;
182
+ chat.totalUnreadCount = chat.totalUnreadCount - event.readCount;
183
+ }
184
+ }),
185
+ ),
186
+ __settings.client.subscribe(
187
+ 'chatify://chat-member-added',
188
+ ([chatId, chatName, account, message, , read]) =>
189
+ updateCachedData((chatList) => {
190
+ const chat = chatList.find((x) => x.id === chatId);
191
+
192
+ if (account.id === __settings.me() && !chat) {
193
+ chatList.push({
194
+ id: chatId,
195
+ name: chatName,
196
+ message,
197
+ totalUnreadCount: read ? 0 : 1,
198
+ active: true,
199
+ lastReadMessageId: read ? message.id : null,
200
+ });
201
+ }
202
+
203
+ if (!chat) {
204
+ return;
205
+ }
206
+
207
+ chat.message = message;
208
+ chat.active = true;
209
+
210
+ if (read) {
211
+ chat.totalUnreadCount = 0;
212
+ chat.lastReadMessageId = message.id;
213
+ } else {
214
+ chat.totalUnreadCount++;
215
+ }
216
+ }),
217
+ ),
218
+ __settings.client.subscribe(
219
+ 'chatify://chat-member-removed',
220
+ ([chatId, accountId, deleteHistory, message, read]) =>
221
+ updateCachedData((chatList) => {
222
+ if (accountId === __settings.me() && deleteHistory) {
223
+ return chatList.filter((x) => x.id !== chatId);
224
+ }
225
+
226
+ const chat = chatList.find((x) => x.id === chatId);
227
+
228
+ if (!chat) {
229
+ return;
230
+ }
231
+
232
+ chat.message = message;
233
+
234
+ if (accountId === __settings.me()) {
235
+ chat.active = false;
236
+ }
237
+
238
+ if (!read) {
239
+ chat.totalUnreadCount++;
240
+ } else {
241
+ chat.totalUnreadCount = 0;
242
+ chat.lastReadMessageId = message.id;
243
+ }
244
+ }),
245
+ ),
246
+ __settings.client.subscribe('chatify://chat-name-changed', ([chatId, newName, message, read]) =>
247
+ updateCachedData((chatList) => {
248
+ const chat = chatList.find((x) => x.id === chatId);
249
+ if (!chat) return;
250
+
251
+ chat.name = newName;
252
+ chat.message = message;
253
+
254
+ if (!read) {
255
+ chat.totalUnreadCount++;
256
+ } else {
257
+ chat.totalUnreadCount = 0;
258
+ chat.lastReadMessageId = message.id;
259
+ }
260
+ }),
261
+ ),
262
+ ).when(cacheEntryRemoved);
263
+ },
264
+ }),
265
+
266
+ // ====================================================
267
+ // ============ CHAT MEMBER ====================
268
+ // ====================================================
269
+
270
+ removeChatMember: builder.mutation<void, RemoveChatMemberRequest>({
271
+ queryFn: async (args) => {
272
+ await __settings.client.removeChatMember(args);
273
+ return { data: null! };
274
+ },
275
+ }),
276
+
277
+ addChatMember: builder.mutation<void, AddChatMemberRequest>({
278
+ queryFn: async (args) => {
279
+ await __settings.client.addChatMember(args);
280
+ return { data: null! };
281
+ },
282
+ }),
283
+
284
+ // ====================================================
285
+ // ============ CHAT MESSAGE ===================
286
+ // ====================================================
287
+
288
+ getChatMessageList: builder.query<ChatMessageListResult, { chatId: string }>({
289
+ queryFn: async ({ chatId }) => {
290
+ const messages = await __settings.client.chatMessages({
291
+ chatId,
292
+ skip: 0,
293
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
294
+ });
295
+ return {
296
+ data: {
297
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
298
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
299
+ },
300
+ };
301
+ },
302
+ providesTags: ['message', 'chat', 'account'],
303
+ onCacheEntryAdded: async ({ chatId }, { dispatch, cacheEntryRemoved, cacheDataLoaded }) => {
304
+ await cacheDataLoaded;
305
+
306
+ unsubscribe(
307
+ __settings.client.subscribe('chatify://chat-new-message', ([message]) => {
308
+ message.chatId === chatId &&
309
+ dispatch(
310
+ chatifyApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
311
+ draft.messages.unshift(message);
312
+ }),
313
+ );
314
+ }),
315
+
316
+ __settings.client.subscribe('chatify://chat-member-added', ([eventChatId, , , message]) => {
317
+ eventChatId === chatId &&
318
+ dispatch(
319
+ chatifyApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
320
+ draft.messages.unshift(message);
321
+ }),
322
+ );
323
+ }),
324
+
325
+ __settings.client.subscribe('chatify://chat-member-removed', ([eventChatId, , , message]) => {
326
+ eventChatId === chatId &&
327
+ dispatch(
328
+ chatifyApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
329
+ draft.messages.unshift(message);
330
+ }),
331
+ );
332
+ }),
333
+
334
+ __settings.client.subscribe('chatify://chat-name-changed', ([eventChatId, , message]) => {
335
+ eventChatId === chatId &&
336
+ dispatch(
337
+ chatifyApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
338
+ draft.messages.unshift(message);
339
+ }),
340
+ );
341
+ }),
342
+ ).when(cacheEntryRemoved);
343
+ },
344
+ }),
345
+
346
+ fetchNextChatMessageList: builder.mutation<void, { chatId: string }>({
347
+ queryFn: async ({ chatId }, { getState }) => {
348
+ const current = chatifyApi.endpoints.getChatMessageList.select({ chatId })(getState() as any);
349
+ const messages = await __settings.client.chatMessages({
350
+ chatId,
351
+ skip: current.data!.messages.length,
352
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
353
+ });
354
+ return {
355
+ data: null!,
356
+ meta: {
357
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
358
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
359
+ },
360
+ };
361
+ },
362
+ onQueryStarted: async ({ chatId }, { dispatch, queryFulfilled }) => {
363
+ const result = await queryFulfilled;
364
+ const meta: ChatMessageListResult = result.meta! as any;
365
+
366
+ dispatch(
367
+ chatifyApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
368
+ draft.hasMore = meta.hasMore;
369
+ draft.messages.push(...meta!.messages);
370
+ }),
371
+ );
372
+ },
373
+ }),
374
+
375
+ sendChatMessage: builder.mutation<void, { chatId: string; text: string }>({
376
+ queryFn: async ({ chatId, text }) => {
377
+ const requestId = new Date().getTime().toString();
378
+
379
+ await __settings.client.sendChatMessage({
380
+ chatId,
381
+ text,
382
+ files: [],
383
+ requestId,
384
+ });
385
+
386
+ return { data: null!, meta: { requestId } };
387
+ },
388
+ }),
389
+
390
+ readChatMessage: builder.mutation<void, { id: string }>({
391
+ queryFn: async () => {
392
+ return { data: null! };
393
+ },
394
+ onQueryStarted: async ({ id }, { dispatch }) => {
395
+ CHAT_MESSAGE_READ_QUEUE.push(id);
396
+ dispatch(chatifyApi.endpoints.getChatMessageReadQueue.initiate(undefined, { forceRefetch: true }));
397
+ },
398
+ }),
399
+
400
+ getChatMessageReadQueue: builder.query<DebounceValue<string>[], void>({
401
+ queryFn: async () => ({ data: CHAT_MESSAGE_READ_QUEUE.units }),
402
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved }) => {
403
+ unsubscribe(
404
+ CHAT_MESSAGE_READ_QUEUE.subscribe((value) => {
405
+ dispatch(
406
+ chatifyApi.util.updateQueryData('getChatMessageReadQueue', undefined, () => value.units),
407
+ );
408
+ }),
409
+ ).when(cacheEntryRemoved);
410
+ },
411
+ }),
412
+
413
+ // ====================================================
414
+ // =============== THREAD ======================s
415
+ // ====================================================
416
+ getWatchThreadList: builder.query<ThreadWatchListItem[], void>({
417
+ queryFn: async () => {
418
+ const threads = await __settings.client.watchThreads();
419
+ return { data: threads };
420
+ },
421
+ providesTags: ['account', 'thread'],
422
+ onCacheEntryAdded: async (_, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
423
+ await cacheDataLoaded;
424
+
425
+ unsubscribe(
426
+ __settings.client.subscribe(
427
+ 'chatify://thread-created',
428
+ ([thread, watch]) =>
429
+ watch &&
430
+ updateCachedData((threads) => {
431
+ threads.push(thread);
432
+ }),
433
+ ),
434
+ __settings.client.subscribe('chatify://thread-message-new', ([threadId, message, readForId]) =>
435
+ updateCachedData((threads) => {
436
+ const thread = threads.find((x) => x.id === threadId);
437
+ if (!thread) return;
438
+
439
+ thread.lastMessage = message;
440
+
441
+ if (readForId === __settings.me()) {
442
+ thread.lastReadMessageId = message.id;
443
+ }
444
+ }),
445
+ ),
446
+ __settings.client.subscribe('chatify://thread-message-read', ([threadId, messageId]) =>
447
+ updateCachedData((threads) => {
448
+ const thread = threads.find((x) => x.id === threadId);
449
+ if (!thread) return;
450
+
451
+ thread.lastReadMessageId = messageId;
452
+ }),
453
+ ),
454
+ __settings.client.subscribe('chatify://thread-watch-added', ([thread]) =>
455
+ updateCachedData((threads) => {
456
+ threads.push(thread);
457
+ }),
458
+ ),
459
+ __settings.client.subscribe('chatify://thread-watch-removed', ([id]) =>
460
+ updateCachedData((threads) => {
461
+ const index = threads.findIndex((x) => x.id === id);
462
+ if (index !== -1) threads.splice(index, 1);
463
+ }),
464
+ ),
465
+ ).when(cacheEntryRemoved);
466
+ },
467
+ }),
468
+
469
+ getThread: builder.query<Thread, { id: string }>({
470
+ queryFn: async ({ id }) => {
471
+ const threads = await __settings.client.thread(id);
472
+ return { data: threads };
473
+ },
474
+ providesTags: ['account', 'thread'],
475
+ onCacheEntryAdded: async ({ id }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
476
+ await cacheDataLoaded;
477
+
478
+ unsubscribe(
479
+ __settings.client.subscribe(
480
+ 'chatify://thread-message-read',
481
+ ([threadId, messageId]) =>
482
+ id === threadId &&
483
+ updateCachedData((t) => {
484
+ t.lastReadMessageId = messageId;
485
+ }),
486
+ ),
487
+ __settings.client.subscribe(
488
+ 'chatify://thread-message-new',
489
+ ([threadId, message, readForId]) =>
490
+ id === threadId &&
491
+ updateCachedData((thread) => {
492
+ thread.lastMessageId = message.id;
493
+ thread.lastReadMessageId =
494
+ readForId === __settings.me() ? message.id : thread.lastReadMessageId;
495
+ }),
496
+ ),
497
+ __settings.client.subscribe(
498
+ 'chatify://thread-watch-added',
499
+ ([threadListItem]) =>
500
+ id === threadListItem.id &&
501
+ updateCachedData((thread) => {
502
+ thread.watch = true;
503
+ thread.lastMessageId = threadListItem.lastMessage?.id ?? null;
504
+ thread.lastReadMessageId = threadListItem.lastReadMessageId ?? null;
505
+ }),
506
+ ),
507
+ __settings.client.subscribe(
508
+ 'chatify://thread-watch-removed',
509
+ ([threadId]) =>
510
+ id === threadId &&
511
+ updateCachedData((thread) => {
512
+ thread.watch = false;
513
+ }),
514
+ ),
515
+ ).when(cacheEntryRemoved);
516
+ },
517
+ }),
518
+
519
+ watchThread: builder.mutation<void, { id: string; watch: boolean }>({
520
+ queryFn: async ({ id, watch }) => {
521
+ await __settings.client.watchThread(id, watch);
522
+ return { data: null! };
523
+ },
524
+ }),
525
+
526
+ // ====================================================
527
+ // ============ THREAD MESSAGE =================
528
+ // ====================================================
529
+
530
+ getThreadMessageList: builder.query<ThreadMessageList, { threadId: string }>({
531
+ queryFn: async (args) => {
532
+ const requestArgs = {
533
+ skip: 0,
534
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
535
+ ...args,
536
+ };
537
+ const messages = await __settings.client.threadMessages(requestArgs);
538
+ return {
539
+ data: {
540
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
541
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
542
+ },
543
+ };
544
+ },
545
+ providesTags: ['account', 'thread', 'thread-message'],
546
+ onCacheEntryAdded: async ({ threadId }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
547
+ await cacheDataLoaded;
548
+
549
+ unsubscribe(
550
+ __settings.client.subscribe('chatify://thread-message-new', ([_threadId, message]) => {
551
+ threadId === _threadId &&
552
+ updateCachedData((messageList) => {
553
+ !messageList.messages.some((x) => x.id === message.id) &&
554
+ messageList.messages.unshift(message);
555
+ });
556
+ }),
557
+ ).when(cacheEntryRemoved);
558
+ },
559
+ }),
560
+
561
+ fetchNextThreadMessageList: builder.mutation<void, { threadId: string }>({
562
+ queryFn: async ({ threadId }, { getState }) => {
563
+ const current = chatifyApi.endpoints.getThreadMessageList.select({ threadId })(getState() as any);
564
+ const messages = await __settings.client.threadMessages({
565
+ threadId,
566
+ skip: current.data!.messages.length,
567
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
568
+ });
569
+ return {
570
+ data: null!,
571
+ meta: {
572
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
573
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
574
+ },
575
+ };
576
+ },
577
+ onQueryStarted: async ({ threadId }, { dispatch, queryFulfilled }) => {
578
+ const result = await queryFulfilled;
579
+ const meta: ThreadMessageList = result.meta! as any;
580
+
581
+ dispatch(
582
+ chatifyApi.util.updateQueryData('getThreadMessageList', { threadId }, (draft) => {
583
+ draft.hasMore = meta.hasMore;
584
+ draft.messages.push(...meta!.messages);
585
+ }),
586
+ );
587
+ },
588
+ }),
589
+
590
+ sendThreadMessage: builder.mutation<string, SendThreadMessageRequest>({
591
+ queryFn: async (args) => {
592
+ const id = await __settings.client.sendThreadMessage(args);
593
+ return { data: id };
594
+ },
595
+ }),
596
+
597
+ readThreadMessage: builder.mutation<void, { id: string }>({
598
+ queryFn: async () => {
599
+ return { data: null! };
600
+ },
601
+ onQueryStarted: async ({ id }, { dispatch }) => {
602
+ THREAD_MESSAGE_READ_QUEUE.push(id);
603
+ dispatch(chatifyApi.endpoints.getThreadMessageReadQueue.initiate(undefined, { forceRefetch: true }));
604
+ },
605
+ }),
606
+
607
+ getThreadMessageReadQueue: builder.query<DebounceValue<string>[], void>({
608
+ queryFn: async () => ({ data: THREAD_MESSAGE_READ_QUEUE.units }),
609
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved }) => {
610
+ unsubscribe(
611
+ THREAD_MESSAGE_READ_QUEUE.subscribe((value) => {
612
+ dispatch(
613
+ chatifyApi.util.updateQueryData('getThreadMessageReadQueue', undefined, () => value.units),
614
+ );
615
+ }),
616
+ ).when(cacheEntryRemoved);
617
+ },
618
+ }),
619
+ }),
620
+ });
621
+
622
+ export function useChatListItem(id: string | null) {
623
+ return useSelector((state: any) =>
624
+ id != null
625
+ ? __chatifyApi.endpoints.getChatList
626
+ .select()(state)
627
+ .data?.find((x) => x.id === id)
628
+ : null,
629
+ );
630
+ }
631
+
632
+ export function useUnreadThreadMessageCount() {
633
+ const { data: threads, ...state } = chatifyApi.useGetWatchThreadListQuery();
634
+ const count = useMemo(() => (threads != null ? calcThreadListUnreadCount(threads) : null), [threads]);
635
+ return [count, state] as const;
636
+ }
637
+
638
+ export const chatifyApi = Object.assign(__chatifyApi, {
639
+ settings: __settings,
640
+ useChatListItem,
641
+ useUnreadThreadMessageCount,
642
+ });
@@ -1,4 +1,4 @@
1
- import { BaseQueryFn, createApi, fakeBaseQuery } from '@reduxjs/toolkit/query/react';
1
+ import { BaseQueryFn, EndpointBuilder, createApi, fakeBaseQuery } from '@reduxjs/toolkit/query/react';
2
2
  import { ChatifyClient } from '../client';
3
3
  import { Debounce } from '../debounce';
4
4
  import { chatId } from '../types';
@@ -24,6 +24,9 @@ export const __settings: ChatifyApiSettings = {
24
24
 
25
25
  const TAG_TYPES = ['chat', 'account', 'message', 'thread', 'thread-message'] as const;
26
26
 
27
+ export interface BaseApiEndpointBuilder
28
+ extends EndpointBuilder<BaseQueryFn, (typeof TAG_TYPES)[number], 'chatify'> {}
29
+
27
30
  const baseApi: ReturnType<typeof createApi<BaseQueryFn, {}, 'chatify', (typeof TAG_TYPES)[number]>> =
28
31
  createApi({
29
32
  baseQuery: fakeBaseQuery(),