@webinex/chatify 2.0.0-alpha1 → 2.0.0-alpha10

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