@webinex/chatify 2.0.1-alpha1 → 2.0.2-alpha2

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