@webinex/chatify 2.0.0-alpha1 → 2.0.0-alpha12

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