@webinex/chatify 2.0.0-alpha1 → 2.0.0-alpha4

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 (234) hide show
  1. package/dist/css/chatify.css +117 -13
  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 +10703 -387
  5. package/dist/esm/types/core/api/baseApi.d.ts +1 -1
  6. package/dist/esm/types/core/client.d.ts +2 -2
  7. package/dist/esm/types/core/types.d.ts +3 -6
  8. package/dist/esm/types/ui/Conversation/Conversation.d.ts +2 -0
  9. package/dist/esm/types/ui/Conversation/ConversationContext.d.ts +2 -0
  10. package/dist/esm/types/ui/Conversation/InputBox/InputBoxFile.d.ts +8 -0
  11. package/dist/esm/types/ui/Conversation/InputBox/InputBoxFileList.d.ts +8 -0
  12. package/dist/esm/types/ui/Conversation/InputBox/InputFilesBox.d.ts +11 -1
  13. package/dist/esm/types/ui/Conversation/InputBox/InputSubmitButtonBox.d.ts +2 -0
  14. package/dist/esm/types/ui/Conversation/InputBox/InputTextBox.d.ts +1 -0
  15. package/dist/esm/types/ui/Conversation/InputBox/index.d.ts +8 -1
  16. package/dist/esm/types/ui/Conversation/Message/MessageFile.d.ts +6 -0
  17. package/dist/esm/types/ui/Conversation/Message/MessageFileList.d.ts +3 -0
  18. package/dist/esm/types/ui/Conversation/Message/index.d.ts +6 -0
  19. package/dist/esm/types/ui/common/Icon.d.ts +14 -12
  20. package/dist/esm/types/ui/customize.d.ts +2 -2
  21. package/dist/esm/types/ui/localizer.d.ts +5 -0
  22. package/dist/esm/types/ui/useFormatter.d.ts +3 -0
  23. package/dist/index.d.ts +10772 -415
  24. package/package.json +94 -93
  25. package/src/core/api/api.ts +639 -642
  26. package/src/core/api/baseApi.ts +38 -38
  27. package/src/core/api/index.ts +1 -1
  28. package/src/core/client.ts +280 -280
  29. package/src/core/constants.ts +1 -1
  30. package/src/core/debounce.ts +90 -90
  31. package/src/core/index.ts +4 -4
  32. package/src/core/types.tsx +149 -152
  33. package/src/index.ts +2 -2
  34. package/src/styles/aside.scss +162 -161
  35. package/src/styles/chat.scss +209 -208
  36. package/src/styles/index.scss +159 -110
  37. package/src/styles/input.scss +148 -74
  38. package/src/styles/keyframes.scss +14 -14
  39. package/src/styles/members.scss +13 -13
  40. package/src/styles/mixins.scss +25 -20
  41. package/src/styles/variables.scss +13 -10
  42. package/src/ui/Chat/ChatContext.tsx +30 -30
  43. package/src/ui/Chat/ChatEditableHeaderName.tsx +63 -63
  44. package/src/ui/Chat/ChatHeaderActions.tsx +19 -19
  45. package/src/ui/Chat/ChatPanel.tsx +30 -30
  46. package/src/ui/Chat/ChatView.tsx +115 -115
  47. package/src/ui/Chat/Members/ChatMembersButton.tsx +9 -9
  48. package/src/ui/Chat/Members/ChatMembersPanel.tsx +112 -112
  49. package/src/ui/Chat/Members/ChatMembersPreview.tsx +25 -25
  50. package/src/ui/Chat/Members/index.ts +13 -13
  51. package/src/ui/Chat/index.ts +6 -6
  52. package/src/ui/ChatGroup/ChatGroupContext.tsx +50 -50
  53. package/src/ui/ChatGroup/ChatGroupPanel.tsx +73 -73
  54. package/src/ui/ChatGroup/Create/CreateChatButton.tsx +15 -15
  55. package/src/ui/ChatGroup/Create/CreateChatForm.tsx +36 -36
  56. package/src/ui/ChatGroup/Create/CreateChatMemberInput.tsx +65 -65
  57. package/src/ui/ChatGroup/Create/CreateChatNameInput.tsx +25 -25
  58. package/src/ui/ChatGroup/Create/CreateChatPanel.tsx +45 -45
  59. package/src/ui/ChatGroup/Create/index.ts +19 -19
  60. package/src/ui/ChatGroup/Layout/ChatGroupAside.tsx +12 -12
  61. package/src/ui/ChatGroup/Layout/ChatGroupBody.tsx +10 -10
  62. package/src/ui/ChatGroup/Layout/ChatGroupFooter.tsx +3 -3
  63. package/src/ui/ChatGroup/Layout/ChatGroupHeader.tsx +5 -5
  64. package/src/ui/ChatGroup/Layout/ChatGroupMain.tsx +17 -17
  65. package/src/ui/ChatGroup/Layout/index.ts +19 -19
  66. package/src/ui/ChatGroup/List/ChatList.tsx +41 -42
  67. package/src/ui/ChatGroup/List/ChatListItemAvatar.tsx +22 -22
  68. package/src/ui/ChatGroup/List/ChatListItemBox.tsx +26 -26
  69. package/src/ui/ChatGroup/List/ChatListItemLastMessage.tsx +15 -15
  70. package/src/ui/ChatGroup/List/ChatListItemLastMessageAuthor.tsx +15 -15
  71. package/src/ui/ChatGroup/List/ChatListItemLastMessageContent.tsx +21 -21
  72. package/src/ui/ChatGroup/List/ChatListItemLastMessageSentAt.tsx +22 -22
  73. package/src/ui/ChatGroup/List/ChatListItemName.tsx +8 -8
  74. package/src/ui/ChatGroup/List/ChatListItemUnreadCount.tsx +12 -12
  75. package/src/ui/ChatGroup/List/index.ts +31 -31
  76. package/src/ui/ChatGroup/index.ts +5 -5
  77. package/src/ui/Chatify.tsx +9 -9
  78. package/src/ui/Conversation/Conversation.tsx +78 -76
  79. package/src/ui/Conversation/ConversationActions.tsx +5 -5
  80. package/src/ui/Conversation/ConversationBody.tsx +28 -28
  81. package/src/ui/Conversation/ConversationContext.ts +29 -27
  82. package/src/ui/Conversation/ConversationHeader.tsx +23 -23
  83. package/src/ui/Conversation/ConversationName.tsx +7 -7
  84. package/src/ui/Conversation/InputBox/InputBox.tsx +122 -40
  85. package/src/ui/Conversation/InputBox/InputBoxFile.tsx +38 -0
  86. package/src/ui/Conversation/InputBox/InputBoxFileList.tsx +21 -0
  87. package/src/ui/Conversation/InputBox/InputFilesBox.tsx +78 -9
  88. package/src/ui/Conversation/InputBox/InputSubmitButtonBox.tsx +26 -17
  89. package/src/ui/Conversation/InputBox/InputTextBox.tsx +61 -53
  90. package/src/ui/Conversation/InputBox/index.ts +23 -16
  91. package/src/ui/Conversation/Message/MessageAuthor.tsx +13 -13
  92. package/src/ui/Conversation/Message/MessageBox.tsx +20 -20
  93. package/src/ui/Conversation/Message/MessageContent.tsx +15 -11
  94. package/src/ui/Conversation/Message/MessageFile.tsx +54 -0
  95. package/src/ui/Conversation/Message/MessageFileList.tsx +14 -0
  96. package/src/ui/Conversation/Message/MessageInfoBox.tsx +28 -28
  97. package/src/ui/Conversation/Message/MessageRow.tsx +28 -28
  98. package/src/ui/Conversation/Message/MessageText.tsx +7 -7
  99. package/src/ui/Conversation/Message/index.ts +28 -22
  100. package/src/ui/Conversation/NextObserver/NextMessagesObserver.tsx +47 -47
  101. package/src/ui/Conversation/NextObserver/index.ts +1 -1
  102. package/src/ui/Conversation/ReadObserver/MessageReadObserver.tsx +49 -49
  103. package/src/ui/Conversation/ReadObserver/index.ts +1 -1
  104. package/src/ui/Conversation/SendingMessage/SendingMessageBox.tsx +16 -16
  105. package/src/ui/Conversation/SendingMessage/SendingMessageContent.tsx +13 -13
  106. package/src/ui/Conversation/SendingMessage/SendingMessageInfoBox.tsx +13 -13
  107. package/src/ui/Conversation/SendingMessage/SendingMessageText.tsx +7 -7
  108. package/src/ui/Conversation/SendingMessage/index.ts +16 -16
  109. package/src/ui/Conversation/SystemMessage/SystemMessageBox.tsx +20 -20
  110. package/src/ui/Conversation/SystemMessage/SystemMessageRow.tsx +28 -28
  111. package/src/ui/Conversation/SystemMessage/index.ts +10 -10
  112. package/src/ui/Conversation/index.ts +13 -13
  113. package/src/ui/Thread/ThreadActions.tsx +22 -22
  114. package/src/ui/Thread/ThreadContext.ts +13 -13
  115. package/src/ui/Thread/ThreadPanel.tsx +127 -127
  116. package/src/ui/Thread/index.tsx +3 -3
  117. package/src/ui/color.ts +20 -20
  118. package/src/ui/common/Avatar.tsx +20 -20
  119. package/src/ui/common/Icon.tsx +44 -40
  120. package/src/ui/common/MessageSkeleton.tsx +20 -20
  121. package/src/ui/common/index.ts +13 -13
  122. package/src/ui/customize.tsx +51 -53
  123. package/src/ui/index.ts +11 -11
  124. package/src/ui/localizer.tsx +96 -81
  125. package/src/ui/useFormatter.tsx +17 -0
  126. package/dist/esm/types/core/api/accountApi.d.ts +0 -5
  127. package/dist/esm/types/core/api/chatApi.d.ts +0 -16
  128. package/dist/esm/types/core/api/chatListApi.d.ts +0 -5
  129. package/dist/esm/types/core/api/chatMemberApi.d.ts +0 -6
  130. package/dist/esm/types/core/api/chatMessageApi.d.ts +0 -3
  131. package/dist/esm/types/core/api/threadApi.d.ts +0 -1
  132. package/dist/esm/types/core/api/threadMessageApi.d.ts +0 -6
  133. package/src/core/api/accountApi.ts +0 -11
  134. package/src/core/api/chatApi.ts +0 -87
  135. package/src/core/api/chatListApi.ts +0 -134
  136. package/src/core/api/chatMemberApi.ts +0 -20
  137. package/src/core/api/chatMessageApi.ts +0 -12
  138. package/src/core/api/threadApi.ts +0 -8
  139. package/src/core/api/threadMessageApi.ts +0 -20
  140. package/src/types/core/api/accountApi.d.ts +0 -5
  141. package/src/types/core/api/api.d.ts +0 -2380
  142. package/src/types/core/api/baseApi.d.ts +0 -15
  143. package/src/types/core/api/chatApi.d.ts +0 -16
  144. package/src/types/core/api/chatListApi.d.ts +0 -5
  145. package/src/types/core/api/chatMemberApi.d.ts +0 -6
  146. package/src/types/core/api/chatMessageApi.d.ts +0 -3
  147. package/src/types/core/api/index.d.ts +0 -1
  148. package/src/types/core/api/threadApi.d.ts +0 -1
  149. package/src/types/core/api/threadMessageApi.d.ts +0 -6
  150. package/src/types/core/client.d.ts +0 -83
  151. package/src/types/core/constants.d.ts +0 -1
  152. package/src/types/core/debounce.d.ts +0 -27
  153. package/src/types/core/index.d.ts +0 -4
  154. package/src/types/core/types.d.ts +0 -105
  155. package/src/types/index.d.ts +0 -2
  156. package/src/types/ui/Chat/ChatContext.d.ts +0 -10
  157. package/src/types/ui/Chat/ChatEditableHeaderName.d.ts +0 -2
  158. package/src/types/ui/Chat/ChatHeaderActions.d.ts +0 -2
  159. package/src/types/ui/Chat/ChatPanel.d.ts +0 -13
  160. package/src/types/ui/Chat/ChatView.d.ts +0 -17
  161. package/src/types/ui/Chat/Members/ChatMembersButton.d.ts +0 -2
  162. package/src/types/ui/Chat/Members/ChatMembersPanel.d.ts +0 -2
  163. package/src/types/ui/Chat/Members/ChatMembersPreview.d.ts +0 -2
  164. package/src/types/ui/Chat/Members/index.d.ts +0 -11
  165. package/src/types/ui/Chat/index.d.ts +0 -6
  166. package/src/types/ui/ChatGroup/ChatGroupContext.d.ts +0 -12
  167. package/src/types/ui/ChatGroup/ChatGroupPanel.d.ts +0 -15
  168. package/src/types/ui/ChatGroup/Create/CreateChatButton.d.ts +0 -2
  169. package/src/types/ui/ChatGroup/Create/CreateChatForm.d.ts +0 -11
  170. package/src/types/ui/ChatGroup/Create/CreateChatMemberInput.d.ts +0 -5
  171. package/src/types/ui/ChatGroup/Create/CreateChatNameInput.d.ts +0 -5
  172. package/src/types/ui/ChatGroup/Create/CreateChatPanel.d.ts +0 -2
  173. package/src/types/ui/ChatGroup/Create/index.d.ts +0 -17
  174. package/src/types/ui/ChatGroup/Layout/ChatGroupAside.d.ts +0 -2
  175. package/src/types/ui/ChatGroup/Layout/ChatGroupBody.d.ts +0 -2
  176. package/src/types/ui/ChatGroup/Layout/ChatGroupFooter.d.ts +0 -2
  177. package/src/types/ui/ChatGroup/Layout/ChatGroupHeader.d.ts +0 -2
  178. package/src/types/ui/ChatGroup/Layout/ChatGroupMain.d.ts +0 -2
  179. package/src/types/ui/ChatGroup/Layout/index.d.ts +0 -17
  180. package/src/types/ui/ChatGroup/List/ChatList.d.ts +0 -2
  181. package/src/types/ui/ChatGroup/List/ChatListItemAvatar.d.ts +0 -3
  182. package/src/types/ui/ChatGroup/List/ChatListItemBox.d.ts +0 -6
  183. package/src/types/ui/ChatGroup/List/ChatListItemLastMessage.d.ts +0 -3
  184. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageAuthor.d.ts +0 -3
  185. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageContent.d.ts +0 -3
  186. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageSentAt.d.ts +0 -3
  187. package/src/types/ui/ChatGroup/List/ChatListItemName.d.ts +0 -3
  188. package/src/types/ui/ChatGroup/List/ChatListItemUnreadCount.d.ts +0 -3
  189. package/src/types/ui/ChatGroup/List/index.d.ts +0 -29
  190. package/src/types/ui/ChatGroup/index.d.ts +0 -5
  191. package/src/types/ui/Chatify.d.ts +0 -8
  192. package/src/types/ui/Conversation/Conversation.d.ts +0 -21
  193. package/src/types/ui/Conversation/ConversationActions.d.ts +0 -2
  194. package/src/types/ui/Conversation/ConversationBody.d.ts +0 -2
  195. package/src/types/ui/Conversation/ConversationContext.d.ts +0 -20
  196. package/src/types/ui/Conversation/ConversationHeader.d.ts +0 -2
  197. package/src/types/ui/Conversation/ConversationName.d.ts +0 -2
  198. package/src/types/ui/Conversation/InputBox/InputBox.d.ts +0 -2
  199. package/src/types/ui/Conversation/InputBox/InputFilesBox.d.ts +0 -2
  200. package/src/types/ui/Conversation/InputBox/InputSubmitButtonBox.d.ts +0 -5
  201. package/src/types/ui/Conversation/InputBox/InputTextBox.d.ts +0 -8
  202. package/src/types/ui/Conversation/InputBox/index.d.ts +0 -14
  203. package/src/types/ui/Conversation/Message/MessageAuthor.d.ts +0 -3
  204. package/src/types/ui/Conversation/Message/MessageBox.d.ts +0 -6
  205. package/src/types/ui/Conversation/Message/MessageContent.d.ts +0 -3
  206. package/src/types/ui/Conversation/Message/MessageInfoBox.d.ts +0 -3
  207. package/src/types/ui/Conversation/Message/MessageRow.d.ts +0 -6
  208. package/src/types/ui/Conversation/Message/MessageText.d.ts +0 -3
  209. package/src/types/ui/Conversation/Message/index.d.ts +0 -20
  210. package/src/types/ui/Conversation/NextObserver/NextMessagesObserver.d.ts +0 -5
  211. package/src/types/ui/Conversation/NextObserver/index.d.ts +0 -1
  212. package/src/types/ui/Conversation/ReadObserver/MessageReadObserver.d.ts +0 -6
  213. package/src/types/ui/Conversation/ReadObserver/index.d.ts +0 -1
  214. package/src/types/ui/Conversation/SendingMessage/SendingMessageBox.d.ts +0 -7
  215. package/src/types/ui/Conversation/SendingMessage/SendingMessageContent.d.ts +0 -3
  216. package/src/types/ui/Conversation/SendingMessage/SendingMessageInfoBox.d.ts +0 -3
  217. package/src/types/ui/Conversation/SendingMessage/SendingMessageText.d.ts +0 -3
  218. package/src/types/ui/Conversation/SendingMessage/index.d.ts +0 -14
  219. package/src/types/ui/Conversation/SystemMessage/SystemMessageBox.d.ts +0 -6
  220. package/src/types/ui/Conversation/SystemMessage/SystemMessageRow.d.ts +0 -9
  221. package/src/types/ui/Conversation/SystemMessage/index.d.ts +0 -8
  222. package/src/types/ui/Conversation/index.d.ts +0 -12
  223. package/src/types/ui/Thread/ThreadActions.d.ts +0 -2
  224. package/src/types/ui/Thread/ThreadContext.d.ts +0 -8
  225. package/src/types/ui/Thread/ThreadPanel.d.ts +0 -14
  226. package/src/types/ui/Thread/index.d.ts +0 -3
  227. package/src/types/ui/color.d.ts +0 -6
  228. package/src/types/ui/common/Avatar.d.ts +0 -6
  229. package/src/types/ui/common/Icon.d.ts +0 -20
  230. package/src/types/ui/common/MessageSkeleton.d.ts +0 -5
  231. package/src/types/ui/common/index.d.ts +0 -11
  232. package/src/types/ui/customize.d.ts +0 -9
  233. package/src/types/ui/index.d.ts +0 -11
  234. package/src/types/ui/localizer.d.ts +0 -35
@@ -1,642 +1,639 @@
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
+ RemoveChatMemberRequest,
10
+ SendChatMessageRequest,
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
+ });
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, SendChatMessageRequest>({
376
+ queryFn: async ({ chatId, text, files }) => {
377
+ await __settings.client.sendChatMessage({
378
+ chatId,
379
+ text,
380
+ files,
381
+ });
382
+
383
+ return { data: null! };
384
+ },
385
+ }),
386
+
387
+ readChatMessage: builder.mutation<void, { id: string }>({
388
+ queryFn: async () => {
389
+ return { data: null! };
390
+ },
391
+ onQueryStarted: async ({ id }, { dispatch }) => {
392
+ CHAT_MESSAGE_READ_QUEUE.push(id);
393
+ dispatch(chatifyApi.endpoints.getChatMessageReadQueue.initiate(undefined, { forceRefetch: true }));
394
+ },
395
+ }),
396
+
397
+ getChatMessageReadQueue: builder.query<DebounceValue<string>[], void>({
398
+ queryFn: async () => ({ data: CHAT_MESSAGE_READ_QUEUE.units }),
399
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved }) => {
400
+ unsubscribe(
401
+ CHAT_MESSAGE_READ_QUEUE.subscribe((value) => {
402
+ dispatch(
403
+ chatifyApi.util.updateQueryData('getChatMessageReadQueue', undefined, () => value.units),
404
+ );
405
+ }),
406
+ ).when(cacheEntryRemoved);
407
+ },
408
+ }),
409
+
410
+ // ====================================================
411
+ // =============== THREAD ======================s
412
+ // ====================================================
413
+ getWatchThreadList: builder.query<ThreadWatchListItem[], void>({
414
+ queryFn: async () => {
415
+ const threads = await __settings.client.watchThreads();
416
+ return { data: threads };
417
+ },
418
+ providesTags: ['account', 'thread'],
419
+ onCacheEntryAdded: async (_, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
420
+ await cacheDataLoaded;
421
+
422
+ unsubscribe(
423
+ __settings.client.subscribe(
424
+ 'chatify://thread-created',
425
+ ([thread, watch]) =>
426
+ watch &&
427
+ updateCachedData((threads) => {
428
+ threads.push(thread);
429
+ }),
430
+ ),
431
+ __settings.client.subscribe('chatify://thread-message-new', ([threadId, message, readForId]) =>
432
+ updateCachedData((threads) => {
433
+ const thread = threads.find((x) => x.id === threadId);
434
+ if (!thread) return;
435
+
436
+ thread.lastMessage = message;
437
+
438
+ if (readForId === __settings.me()) {
439
+ thread.lastReadMessageId = message.id;
440
+ }
441
+ }),
442
+ ),
443
+ __settings.client.subscribe('chatify://thread-message-read', ([threadId, messageId]) =>
444
+ updateCachedData((threads) => {
445
+ const thread = threads.find((x) => x.id === threadId);
446
+ if (!thread) return;
447
+
448
+ thread.lastReadMessageId = messageId;
449
+ }),
450
+ ),
451
+ __settings.client.subscribe('chatify://thread-watch-added', ([thread]) =>
452
+ updateCachedData((threads) => {
453
+ threads.push(thread);
454
+ }),
455
+ ),
456
+ __settings.client.subscribe('chatify://thread-watch-removed', ([id]) =>
457
+ updateCachedData((threads) => {
458
+ const index = threads.findIndex((x) => x.id === id);
459
+ if (index !== -1) threads.splice(index, 1);
460
+ }),
461
+ ),
462
+ ).when(cacheEntryRemoved);
463
+ },
464
+ }),
465
+
466
+ getThread: builder.query<Thread, { id: string }>({
467
+ queryFn: async ({ id }) => {
468
+ const threads = await __settings.client.thread(id);
469
+ return { data: threads };
470
+ },
471
+ providesTags: ['account', 'thread'],
472
+ onCacheEntryAdded: async ({ id }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
473
+ await cacheDataLoaded;
474
+
475
+ unsubscribe(
476
+ __settings.client.subscribe(
477
+ 'chatify://thread-message-read',
478
+ ([threadId, messageId]) =>
479
+ id === threadId &&
480
+ updateCachedData((t) => {
481
+ t.lastReadMessageId = messageId;
482
+ }),
483
+ ),
484
+ __settings.client.subscribe(
485
+ 'chatify://thread-message-new',
486
+ ([threadId, message, readForId]) =>
487
+ id === threadId &&
488
+ updateCachedData((thread) => {
489
+ thread.lastMessageId = message.id;
490
+ thread.lastReadMessageId =
491
+ readForId === __settings.me() ? message.id : thread.lastReadMessageId;
492
+ }),
493
+ ),
494
+ __settings.client.subscribe(
495
+ 'chatify://thread-watch-added',
496
+ ([threadListItem]) =>
497
+ id === threadListItem.id &&
498
+ updateCachedData((thread) => {
499
+ thread.watch = true;
500
+ thread.lastMessageId = threadListItem.lastMessage?.id ?? null;
501
+ thread.lastReadMessageId = threadListItem.lastReadMessageId ?? null;
502
+ }),
503
+ ),
504
+ __settings.client.subscribe(
505
+ 'chatify://thread-watch-removed',
506
+ ([threadId]) =>
507
+ id === threadId &&
508
+ updateCachedData((thread) => {
509
+ thread.watch = false;
510
+ }),
511
+ ),
512
+ ).when(cacheEntryRemoved);
513
+ },
514
+ }),
515
+
516
+ watchThread: builder.mutation<void, { id: string; watch: boolean }>({
517
+ queryFn: async ({ id, watch }) => {
518
+ await __settings.client.watchThread(id, watch);
519
+ return { data: null! };
520
+ },
521
+ }),
522
+
523
+ // ====================================================
524
+ // ============ THREAD MESSAGE =================
525
+ // ====================================================
526
+
527
+ getThreadMessageList: builder.query<ThreadMessageList, { threadId: string }>({
528
+ queryFn: async (args) => {
529
+ const requestArgs = {
530
+ skip: 0,
531
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
532
+ ...args,
533
+ };
534
+ const messages = await __settings.client.threadMessages(requestArgs);
535
+ return {
536
+ data: {
537
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
538
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
539
+ },
540
+ };
541
+ },
542
+ providesTags: ['account', 'thread', 'thread-message'],
543
+ onCacheEntryAdded: async ({ threadId }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
544
+ await cacheDataLoaded;
545
+
546
+ unsubscribe(
547
+ __settings.client.subscribe('chatify://thread-message-new', ([_threadId, message]) => {
548
+ threadId === _threadId &&
549
+ updateCachedData((messageList) => {
550
+ !messageList.messages.some((x) => x.id === message.id) &&
551
+ messageList.messages.unshift(message);
552
+ });
553
+ }),
554
+ ).when(cacheEntryRemoved);
555
+ },
556
+ }),
557
+
558
+ fetchNextThreadMessageList: builder.mutation<void, { threadId: string }>({
559
+ queryFn: async ({ threadId }, { getState }) => {
560
+ const current = chatifyApi.endpoints.getThreadMessageList.select({ threadId })(getState() as any);
561
+ const messages = await __settings.client.threadMessages({
562
+ threadId,
563
+ skip: current.data!.messages.length,
564
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
565
+ });
566
+ return {
567
+ data: null!,
568
+ meta: {
569
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
570
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
571
+ },
572
+ };
573
+ },
574
+ onQueryStarted: async ({ threadId }, { dispatch, queryFulfilled }) => {
575
+ const result = await queryFulfilled;
576
+ const meta: ThreadMessageList = result.meta! as any;
577
+
578
+ dispatch(
579
+ chatifyApi.util.updateQueryData('getThreadMessageList', { threadId }, (draft) => {
580
+ draft.hasMore = meta.hasMore;
581
+ draft.messages.push(...meta!.messages);
582
+ }),
583
+ );
584
+ },
585
+ }),
586
+
587
+ sendThreadMessage: builder.mutation<string, SendThreadMessageRequest>({
588
+ queryFn: async (args) => {
589
+ const id = await __settings.client.sendThreadMessage(args);
590
+ return { data: id };
591
+ },
592
+ }),
593
+
594
+ readThreadMessage: builder.mutation<void, { id: string }>({
595
+ queryFn: async () => {
596
+ return { data: null! };
597
+ },
598
+ onQueryStarted: async ({ id }, { dispatch }) => {
599
+ THREAD_MESSAGE_READ_QUEUE.push(id);
600
+ dispatch(chatifyApi.endpoints.getThreadMessageReadQueue.initiate(undefined, { forceRefetch: true }));
601
+ },
602
+ }),
603
+
604
+ getThreadMessageReadQueue: builder.query<DebounceValue<string>[], void>({
605
+ queryFn: async () => ({ data: THREAD_MESSAGE_READ_QUEUE.units }),
606
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved }) => {
607
+ unsubscribe(
608
+ THREAD_MESSAGE_READ_QUEUE.subscribe((value) => {
609
+ dispatch(
610
+ chatifyApi.util.updateQueryData('getThreadMessageReadQueue', undefined, () => value.units),
611
+ );
612
+ }),
613
+ ).when(cacheEntryRemoved);
614
+ },
615
+ }),
616
+ }),
617
+ });
618
+
619
+ export function useChatListItem(id: string | null) {
620
+ return useSelector((state: any) =>
621
+ id != null
622
+ ? __chatifyApi.endpoints.getChatList
623
+ .select()(state)
624
+ .data?.find((x) => x.id === id)
625
+ : null,
626
+ );
627
+ }
628
+
629
+ export function useUnreadThreadMessageCount() {
630
+ const { data: threads, ...state } = chatifyApi.useGetWatchThreadListQuery();
631
+ const count = useMemo(() => (threads != null ? calcThreadListUnreadCount(threads) : null), [threads]);
632
+ return [count, state] as const;
633
+ }
634
+
635
+ export const chatifyApi = Object.assign(__chatifyApi, {
636
+ settings: __settings,
637
+ useChatListItem,
638
+ useUnreadThreadMessageCount,
639
+ });