@blocklet/discuss-kit-ux 2.4.35 → 2.4.37

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 (222) hide show
  1. package/dist/axios.d.ts +8 -0
  2. package/dist/blocklets.d.ts +1 -1
  3. package/dist/blocklets.mjs +17 -0
  4. package/dist/components/api-error-handler/api-error-handler.d.ts +1 -1
  5. package/dist/components/api-error-handler/api-error-handler.mjs +25 -0
  6. package/dist/components/api-error-handler/default-handler.mjs +69 -0
  7. package/dist/components/api-error-handler/index.mjs +2 -0
  8. package/dist/components/api-error-handler/json-validation-interceptor.d.ts +1 -1
  9. package/dist/components/api-error-handler/json-validation-interceptor.mjs +9 -0
  10. package/dist/components/arcsphere/index.mjs +51 -0
  11. package/dist/components/authz/access-control.d.ts +15 -1
  12. package/dist/components/authz/access-control.mjs +15 -0
  13. package/dist/components/authz/context.d.ts +2 -1
  14. package/dist/components/authz/context.mjs +30 -0
  15. package/dist/components/authz/index.d.ts +1 -1
  16. package/dist/components/authz/index.mjs +2 -0
  17. package/dist/components/auto-translate/api.d.ts +1 -1
  18. package/dist/components/auto-translate/api.mjs +23 -0
  19. package/dist/components/auto-translate/auto-translate-button.d.ts +2 -1
  20. package/dist/components/auto-translate/auto-translate-button.mjs +47 -0
  21. package/dist/components/auto-translate/editor-store-adaptor.mjs +14 -0
  22. package/dist/components/auto-translate/index.mjs +6 -0
  23. package/dist/components/auto-translate/languages.mjs +61 -0
  24. package/dist/components/auto-translate/post-auto-translate-plugin.d.ts +4 -3
  25. package/dist/components/auto-translate/post-auto-translate-plugin.mjs +32 -0
  26. package/dist/components/auto-translate/store.d.ts +3 -3
  27. package/dist/components/auto-translate/store.mjs +23 -0
  28. package/dist/components/auto-translate/translate.d.ts +3 -2
  29. package/dist/components/auto-translate/translate.mjs +103 -0
  30. package/dist/components/auto-translate/utils.d.ts +1 -1
  31. package/dist/components/auto-translate/utils.mjs +14 -0
  32. package/dist/components/auto-translate/with-availibility-check.d.ts +1 -1
  33. package/dist/components/auto-translate/with-availibility-check.mjs +10 -0
  34. package/dist/components/avatars/author-info.d.ts +3 -2
  35. package/dist/components/avatars/author-info.mjs +184 -0
  36. package/dist/components/avatars/avatar.d.ts +2 -1
  37. package/dist/components/avatars/avatar.mjs +9 -0
  38. package/dist/components/avatars/avatars.d.ts +4 -3
  39. package/dist/components/avatars/avatars.mjs +49 -0
  40. package/dist/components/avatars/badge.d.ts +2 -1
  41. package/dist/components/avatars/badge.mjs +178 -0
  42. package/dist/components/avatars/index.mjs +5 -0
  43. package/dist/components/avatars/system-user.d.ts +1 -1
  44. package/dist/components/avatars/system-user.mjs +58 -0
  45. package/dist/components/back.d.ts +2 -1
  46. package/dist/components/back.mjs +28 -0
  47. package/dist/components/blog/blog-link.mjs +16 -0
  48. package/dist/components/blog/blog-list.d.ts +4 -3
  49. package/dist/components/blog/blog-list.mjs +441 -0
  50. package/dist/components/blog/cover-image.d.ts +4 -3
  51. package/dist/components/blog/cover-image.mjs +160 -0
  52. package/dist/components/blog/index.mjs +4 -0
  53. package/dist/components/blog/permalink.d.ts +3 -2
  54. package/dist/components/blog/permalink.mjs +78 -0
  55. package/dist/components/blog/types.d.ts +1 -1
  56. package/dist/components/blog/types.mjs +0 -0
  57. package/dist/components/button-group/button-group.d.ts +2 -1
  58. package/dist/components/button-group/button-group.mjs +195 -0
  59. package/dist/components/button-group/index.mjs +1 -0
  60. package/dist/components/chat/channel-group.d.ts +3 -2
  61. package/dist/components/chat/channel-group.mjs +41 -0
  62. package/dist/components/chat/chat-client.d.ts +2 -2
  63. package/dist/components/chat/chat-client.mjs +33 -0
  64. package/dist/components/chat/chat-header-addon.d.ts +2 -1
  65. package/dist/components/chat/chat-header-addon.mjs +26 -0
  66. package/dist/components/chat/chat-in-wallet.d.ts +2 -1
  67. package/dist/components/chat/chat-in-wallet.mjs +67 -0
  68. package/dist/components/chat/chat-input.d.ts +3 -2
  69. package/dist/components/chat/chat-input.mjs +295 -0
  70. package/dist/components/chat/chat-list-in-wallet.d.ts +2 -1
  71. package/dist/components/chat/chat-list-in-wallet.mjs +113 -0
  72. package/dist/components/chat/chat-list.d.ts +2 -1
  73. package/dist/components/chat/chat-list.mjs +109 -0
  74. package/dist/components/chat/chat-room.d.ts +3 -2
  75. package/dist/components/chat/chat-room.mjs +225 -0
  76. package/dist/components/chat/chat.d.ts +2 -1
  77. package/dist/components/chat/chat.mjs +163 -0
  78. package/dist/components/chat/context.d.ts +4 -3
  79. package/dist/components/chat/context.mjs +303 -0
  80. package/dist/components/chat/hooks.d.ts +1 -1
  81. package/dist/components/chat/hooks.mjs +59 -0
  82. package/dist/components/chat/index.mjs +7 -0
  83. package/dist/components/chat/message-list.d.ts +3 -2
  84. package/dist/components/chat/message-list.mjs +151 -0
  85. package/dist/components/chat/message.d.ts +4 -3
  86. package/dist/components/chat/message.mjs +312 -0
  87. package/dist/components/chat/participants.d.ts +3 -2
  88. package/dist/components/chat/participants.mjs +60 -0
  89. package/dist/components/chat/time.d.ts +1 -1
  90. package/dist/components/chat/time.mjs +16 -0
  91. package/dist/components/chat/types.d.ts +1 -1
  92. package/dist/components/chat/types.mjs +0 -0
  93. package/dist/components/chat/unread-notification.d.ts +2 -2
  94. package/dist/components/chat/unread-notification.mjs +43 -0
  95. package/dist/components/chat/user-search.d.ts +2 -1
  96. package/dist/components/chat/user-search.mjs +143 -0
  97. package/dist/components/check-mark.d.ts +2 -1
  98. package/dist/components/check-mark.mjs +98 -0
  99. package/dist/components/confirm.d.ts +4 -4
  100. package/dist/components/confirm.mjs +103 -0
  101. package/dist/components/dayjs.d.ts +1 -1
  102. package/dist/components/dayjs.mjs +4 -0
  103. package/dist/components/default-editor-config-provider.d.ts +3 -2
  104. package/dist/components/default-editor-config-provider.mjs +107 -0
  105. package/dist/components/dirty-prompt.d.ts +3 -2
  106. package/dist/components/dirty-prompt.mjs +117 -0
  107. package/dist/components/editor/editor.d.ts +4 -3
  108. package/dist/{editor-B3z0rXVh.mjs → components/editor/editor.mjs} +7 -8
  109. package/dist/components/editor/index.mjs +2 -0
  110. package/dist/components/editor/lazy-editor.d.ts +3 -2
  111. package/dist/components/editor/lazy-editor.mjs +14 -0
  112. package/dist/components/editor/plugins/video-path-fixer-plugin.mjs +29 -0
  113. package/dist/components/editor/preview.d.ts +4 -3
  114. package/dist/components/editor/preview.mjs +32 -0
  115. package/dist/components/emoji-icon.d.ts +2 -1
  116. package/dist/components/emoji-icon.mjs +62 -0
  117. package/dist/components/empty-status/empty-status.d.ts +2 -1
  118. package/dist/components/empty-status/empty-status.mjs +24 -0
  119. package/dist/components/empty-status/index.mjs +1 -0
  120. package/dist/components/hooks/changed.mjs +34 -0
  121. package/dist/components/hooks/index.mjs +6 -0
  122. package/dist/components/hooks/interval.mjs +16 -0
  123. package/dist/components/hooks/locale-context.mjs +12 -0
  124. package/dist/components/hooks/measure.mjs +8 -0
  125. package/dist/components/hooks/now.mjs +10 -0
  126. package/dist/components/hooks/responsive.d.ts +1 -1
  127. package/dist/components/hooks/responsive.mjs +25 -0
  128. package/dist/components/hooks/session.d.ts +1 -0
  129. package/dist/components/hooks/session.mjs +41 -0
  130. package/dist/components/hooks/use-event-callback.mjs +14 -0
  131. package/dist/components/icon-button.d.ts +4 -3
  132. package/dist/components/icon-button.mjs +37 -0
  133. package/dist/components/input/auto-clear-plugin.mjs +26 -0
  134. package/dist/components/input/comment-input.d.ts +2 -1
  135. package/dist/components/input/comment-input.mjs +107 -0
  136. package/dist/components/input/image-path-fixer-plugin.mjs +29 -0
  137. package/dist/components/input/index.mjs +4 -0
  138. package/dist/components/input/input.d.ts +4 -4
  139. package/dist/components/input/input.mjs +152 -0
  140. package/dist/components/input/post-edit.d.ts +2 -1
  141. package/dist/components/input/post-edit.mjs +49 -0
  142. package/dist/components/input/scrollable-editor-wrapper.d.ts +2 -1
  143. package/dist/components/input/scrollable-editor-wrapper.mjs +18 -0
  144. package/dist/components/input/shortcut-plugin.mjs +28 -0
  145. package/dist/components/label-picker.d.ts +2 -1
  146. package/dist/components/label-picker.mjs +41 -0
  147. package/dist/components/lexical.mjs +69 -0
  148. package/dist/components/locale/en.mjs +92 -0
  149. package/dist/components/locale/index.mjs +3 -0
  150. package/dist/components/locale/zh.mjs +94 -0
  151. package/dist/components/pagination.d.ts +3 -2
  152. package/dist/components/pagination.mjs +44 -0
  153. package/dist/components/paywall.d.ts +2 -2
  154. package/dist/components/paywall.mjs +110 -0
  155. package/dist/components/point-up/{box-animation.json.d.ts → box-animation.json} +1 -4
  156. package/dist/components/point-up/index.d.ts +3 -2
  157. package/dist/components/point-up/index.mjs +153 -0
  158. package/dist/components/point-up/lottie-component.d.ts +2 -1
  159. package/dist/components/point-up/lottie-component.mjs +7 -0
  160. package/dist/components/posts/comment-list/comment-list.d.ts +2 -1
  161. package/dist/components/posts/comment-list/comment-list.mjs +163 -0
  162. package/dist/components/posts/comment-list/context.d.ts +4 -4
  163. package/dist/components/posts/comment-list/context.mjs +318 -0
  164. package/dist/components/posts/comment.d.ts +3 -2
  165. package/dist/components/posts/comment.mjs +184 -0
  166. package/dist/components/posts/index.mjs +6 -0
  167. package/dist/components/posts/menu.d.ts +3 -2
  168. package/dist/components/posts/menu.mjs +83 -0
  169. package/dist/components/posts/post-content.d.ts +2 -1
  170. package/dist/components/posts/post-content.mjs +86 -0
  171. package/dist/components/posts/post.d.ts +4 -4
  172. package/dist/components/posts/post.mjs +198 -0
  173. package/dist/components/profile-card/index.mjs +1 -0
  174. package/dist/components/profile-card/profile-card.d.ts +4 -3
  175. package/dist/components/profile-card/profile-card.mjs +162 -0
  176. package/dist/components/rating/binary-thumb.d.ts +4 -3
  177. package/dist/components/rating/binary-thumb.mjs +157 -0
  178. package/dist/components/rating/github-reaction-container.d.ts +2 -1
  179. package/dist/components/rating/github-reaction-container.mjs +46 -0
  180. package/dist/components/rating/github-reaction.d.ts +3 -2
  181. package/dist/components/rating/github-reaction.mjs +173 -0
  182. package/dist/components/rating/index.mjs +3 -0
  183. package/dist/components/rating/rater-list.d.ts +3 -2
  184. package/dist/components/rating/rater-list.mjs +33 -0
  185. package/dist/components/rating/rating.d.ts +1 -1
  186. package/dist/components/rating/rating.mjs +50 -0
  187. package/dist/components/routes.d.ts +1 -1
  188. package/dist/components/routes.mjs +27 -0
  189. package/dist/components/segmented-control.d.ts +3 -2
  190. package/dist/components/segmented-control.mjs +55 -0
  191. package/dist/components/shared/relative-time.d.ts +2 -1
  192. package/dist/components/shared/relative-time.mjs +21 -0
  193. package/dist/components/toast.d.ts +1 -1
  194. package/dist/components/toast.mjs +44 -0
  195. package/dist/components/uploader/index.d.ts +3 -3
  196. package/dist/components/uploader/index.mjs +70 -0
  197. package/dist/components/uploader/utils.mjs +16 -0
  198. package/dist/components/utils.d.ts +4 -4
  199. package/dist/components/utils.mjs +139 -0
  200. package/dist/components/view-more.d.ts +1 -1
  201. package/dist/components/view-more.mjs +50 -0
  202. package/dist/constants.mjs +14 -0
  203. package/dist/global.d.ts +1 -0
  204. package/dist/index.mjs +37 -0
  205. package/dist/preferences.mjs +9 -0
  206. package/dist/theme/index.d.ts +3 -3
  207. package/dist/theme/index.mjs +96 -0
  208. package/dist/theme/typography.d.ts +1 -1
  209. package/dist/theme/typography.mjs +66 -0
  210. package/dist/type-override.d.ts +7 -0
  211. package/dist/types.mjs +0 -0
  212. package/dist/vite-env.d.ts +1 -0
  213. package/dist/ws.d.ts +2 -2
  214. package/dist/ws.mjs +39 -0
  215. package/package.json +18 -25
  216. package/dist/components/avatars/Avatars.stories.d.ts +0 -5
  217. package/dist/components/posts/Post.stories.d.ts +0 -7
  218. package/dist/index-BlqRGWgQ.mjs +0 -7631
  219. package/dist/index.es.js +0 -118
  220. package/dist/index.umd.js +0 -7589
  221. package/dist/test/fixtures/index.d.ts +0 -4
  222. package/dist/test/fixtures/mock-session.d.ts +0 -3
@@ -0,0 +1,303 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useMemo, createContext, useContext, useEffect } from "react";
3
+ import { useNavigate } from "react-router-dom";
4
+ import { useSetState } from "ahooks";
5
+ import uniqBy from "lodash/uniqBy";
6
+ import orderBy from "lodash/orderBy";
7
+ import { useSessionContext } from "../hooks/index.mjs";
8
+ import { useUnreadNotification } from "./unread-notification.mjs";
9
+ import { getExcerptFromLexicalContent } from "../utils.mjs";
10
+ const ChatContext = createContext({});
11
+ export const useChatContext = () => useContext(ChatContext);
12
+ const uniqAndSort = (list, order = "asc") => {
13
+ const unique = uniqBy(list, "id");
14
+ const sorted = orderBy(unique, ["createdAt", "id"], [order, order]);
15
+ return sorted;
16
+ };
17
+ export function ChatProvider({
18
+ client,
19
+ activeChatId,
20
+ children,
21
+ isInWallet,
22
+ renderMessage,
23
+ chatRoomHeaderAddons
24
+ }) {
25
+ const [state, setState] = useSetState({
26
+ initialized: false,
27
+ chats: [],
28
+ activeChatId: void 0,
29
+ editingChannel: null,
30
+ error: void 0
31
+ });
32
+ const { incrementUnreadCount, markChatAsRead } = useUnreadNotification();
33
+ const navigate = useNavigate();
34
+ const { session } = useSessionContext();
35
+ const currentUser = session?.user;
36
+ const orderedChats = useMemo(() => {
37
+ return orderBy(state.chats, (x) => x.lastMessageAt || "", "desc");
38
+ }, [state.chats]);
39
+ const hasUnreadMessages = ({ lastMessageAt, lastAckTime }) => {
40
+ if (lastMessageAt && (!lastAckTime || lastMessageAt > lastAckTime)) {
41
+ return true;
42
+ }
43
+ return false;
44
+ };
45
+ const isActiveChat = (chatId) => state.activeChatId === chatId;
46
+ const updateChat = (chatId, mapper) => {
47
+ setState((prev) => {
48
+ const chats = prev.chats.map((item) => {
49
+ if (item.id === chatId) {
50
+ const mapped = mapper(item);
51
+ return mapped;
52
+ }
53
+ return item;
54
+ });
55
+ return { ...prev, chats };
56
+ });
57
+ };
58
+ const updateLastAckTime = async (chatId, clientOnly) => {
59
+ updateChat(chatId, (chat) => ({ ...chat, lastAckTime: chat.lastMessageAt }));
60
+ markChatAsRead(chatId);
61
+ if (!clientOnly) {
62
+ await client.updateLastAckTime(chatId);
63
+ }
64
+ };
65
+ const addMessage = (chatId, message) => {
66
+ updateChat(chatId, (chat) => ({
67
+ ...chat,
68
+ lastMessageAt: message.createdAt,
69
+ messages: uniqAndSort([...chat.messages || [], message])
70
+ }));
71
+ };
72
+ const addParticipant = (chatId, participant) => {
73
+ updateChat(chatId, (chat) => ({
74
+ ...chat,
75
+ participants: uniqBy([...chat.participants, participant], "did")
76
+ }));
77
+ };
78
+ const removeParticipant = (chatId, participant) => {
79
+ updateChat(chatId, (chat) => ({
80
+ ...chat,
81
+ participants: chat.participants.filter((p) => p.did !== participant.did)
82
+ }));
83
+ };
84
+ const setActiveChat = (chat) => {
85
+ const chatId = chat?.id;
86
+ setState((prev) => ({ ...prev, activeChatId: chatId }));
87
+ if (chatId) {
88
+ if (isInWallet) {
89
+ navigate(`/chat-in-wallet/${chatId}`, { replace: true });
90
+ } else {
91
+ navigate(`/chat/${chatId}`, { replace: true });
92
+ }
93
+ updateChat(chatId, (origin) => ({
94
+ ...origin,
95
+ isActivated: true
96
+ }));
97
+ if ((chat.type !== "channel" || chat.hasJoined) && hasUnreadMessages(chat)) {
98
+ updateLastAckTime(chatId);
99
+ }
100
+ } else {
101
+ navigate("/chat", { replace: true });
102
+ }
103
+ };
104
+ const setActiveChatInWallet = (chat) => {
105
+ const chatId = chat?.id;
106
+ setState((prev) => ({ ...prev, activeChatId: chatId }));
107
+ if (chatId) {
108
+ navigate(`/chat-in-wallet/${chatId}`, { replace: true });
109
+ updateChat(chatId, (origin) => ({
110
+ ...origin,
111
+ isActivated: true
112
+ }));
113
+ if ((chat.type !== "channel" || chat.hasJoined) && hasUnreadMessages(chat)) {
114
+ updateLastAckTime(chatId);
115
+ }
116
+ } else {
117
+ navigate("/chat-in-wallet", { replace: true });
118
+ }
119
+ };
120
+ const addChat = (chat) => {
121
+ setState((prev) => ({ ...prev, chats: uniqAndSort([...state.chats, chat]) }));
122
+ };
123
+ const deleteChat = (chatId) => {
124
+ setState({ chats: state.chats.filter((item) => item.id !== chatId) });
125
+ setActiveChat(null);
126
+ };
127
+ const loadMessages = async (chatId, cursor) => {
128
+ const { messages, nextCursor } = await client.fetchMessages(chatId, cursor);
129
+ updateChat(chatId, (chat) => ({
130
+ ...chat,
131
+ messages: uniqAndSort([...chat.messages || [], ...messages || []]),
132
+ nextCursor
133
+ }));
134
+ };
135
+ const sendMessage = async (chatId, content) => {
136
+ const saved = await client.sendMessage(chatId, content);
137
+ addMessage(chatId, saved);
138
+ updateLastAckTime(chatId, true);
139
+ };
140
+ const getLastMessageText = (chat) => {
141
+ const lastMessage = chat && chat.messages && chat.messages[chat.messages.length - 1];
142
+ const getPrefix = (sender) => {
143
+ const isMyselfMessage = sender.did === currentUser?.did;
144
+ if (isMyselfMessage || !sender?.fullName) {
145
+ return "";
146
+ }
147
+ return `${sender.fullName}: `;
148
+ };
149
+ if (lastMessage) {
150
+ return `${getPrefix(lastMessage?.sender)}${getExcerptFromLexicalContent(lastMessage?.content)}`;
151
+ }
152
+ if (chat.lastMessage) {
153
+ return `${getPrefix(chat.lastMessage.sender)}${chat.lastMessage.excerpt}`;
154
+ }
155
+ return "";
156
+ };
157
+ const initChatRoom = async (chatId) => {
158
+ const {
159
+ chat: { participants },
160
+ messages: { messages, nextCursor }
161
+ } = await client.fetchChat(chatId);
162
+ updateChat(chatId, (chat) => {
163
+ const mapped = {
164
+ ...chat,
165
+ participants,
166
+ messages: uniqAndSort(messages),
167
+ nextCursor,
168
+ ready: true
169
+ };
170
+ return mapped;
171
+ });
172
+ };
173
+ const joinChannel = async (chatId) => {
174
+ await client.joinChannel(chatId);
175
+ updateChat(chatId, (chat) => ({
176
+ ...chat,
177
+ hasJoined: true
178
+ }));
179
+ updateLastAckTime(chatId);
180
+ };
181
+ const leaveChannel = async (chatId) => {
182
+ await client.leaveChannel(chatId);
183
+ updateChat(chatId, (chat) => ({
184
+ ...chat,
185
+ hasJoined: false
186
+ }));
187
+ };
188
+ const deleteChannel = async (chatId) => {
189
+ await client.deleteChannel(chatId);
190
+ };
191
+ const deleteMessage = async (chatId, messageId) => {
192
+ await client.deleteMessage(messageId);
193
+ updateChat(chatId, (chat) => ({
194
+ ...chat,
195
+ messages: chat.messages?.filter((item) => item.id !== messageId) || []
196
+ }));
197
+ };
198
+ const init = async () => {
199
+ try {
200
+ const { chats } = await client.init();
201
+ let activeChat = null;
202
+ if (activeChatId && !chats.find((chat) => chat.id === activeChatId)) {
203
+ try {
204
+ const {
205
+ chat,
206
+ messages: { messages, nextCursor }
207
+ } = await client.fetchChat(activeChatId);
208
+ activeChat = { ...chat, messages, nextCursor, ready: true };
209
+ } catch (e) {
210
+ console.error(e);
211
+ }
212
+ }
213
+ setState({ initialized: true, chats: activeChat ? [activeChat, ...chats] : chats });
214
+ } catch (e) {
215
+ console.error(e);
216
+ setState({ error: e.message });
217
+ }
218
+ };
219
+ const createDM = async (oppositeUserDid) => {
220
+ const chat = await client.createDM(oppositeUserDid);
221
+ addChat(chat);
222
+ return chat;
223
+ };
224
+ useEffect(() => {
225
+ if (currentUser?.did) {
226
+ init();
227
+ } else {
228
+ setState({
229
+ initialized: false,
230
+ chats: [],
231
+ activeChatId: void 0,
232
+ error: void 0
233
+ });
234
+ }
235
+ return () => client.destroy();
236
+ }, [currentUser?.did]);
237
+ useEffect(() => {
238
+ if (activeChatId) {
239
+ if (activeChatId !== state.activeChatId) {
240
+ const matched = state.chats.find((chat) => chat.id === activeChatId);
241
+ if (matched) {
242
+ setActiveChat(matched);
243
+ }
244
+ }
245
+ } else if (state.activeChatId && !activeChatId) {
246
+ setState((prev) => ({ ...prev, activeChatId: void 0 }));
247
+ }
248
+ }, [state, activeChatId]);
249
+ useEffect(() => {
250
+ const cancels = [
251
+ client.onMessage(({ chatId, message }) => {
252
+ addMessage(chatId, message);
253
+ if (message.sender.did === currentUser?.did) {
254
+ updateLastAckTime(chatId, true);
255
+ } else if (isActiveChat(chatId)) {
256
+ updateLastAckTime(chatId);
257
+ } else {
258
+ incrementUnreadCount(chatId, 1);
259
+ }
260
+ }),
261
+ client.onJoinChannel(({ chatId, participant }) => addParticipant(chatId, participant)),
262
+ client.onLeaveChannel(({ chatId, participant }) => removeParticipant(chatId, participant)),
263
+ client.onDeleteChannel(({ chatId }) => deleteChat(chatId)),
264
+ client.onUpdateLastAckTime(({ chatId }) => updateLastAckTime(chatId, true))
265
+ ];
266
+ return () => {
267
+ cancels.forEach((cancel) => cancel?.());
268
+ };
269
+ }, [state]);
270
+ const value = useMemo(() => {
271
+ return {
272
+ ...state,
273
+ client,
274
+ setActiveChat,
275
+ setActiveChatInWallet,
276
+ isActiveChat,
277
+ getOppositeUser: (chat) => {
278
+ if (chat.type === "dm") {
279
+ return chat.participants.filter((item) => item.did !== currentUser?.did)[0];
280
+ }
281
+ return null;
282
+ },
283
+ loadMessages,
284
+ sendMessage,
285
+ addChat,
286
+ updateChat,
287
+ initChatRoom,
288
+ joinChannel,
289
+ leaveChannel,
290
+ deleteChannel,
291
+ deleteMessage,
292
+ hasUnreadMessages,
293
+ orderedChats,
294
+ getLastMessageText,
295
+ isInWallet,
296
+ createDM,
297
+ setEditingChannel: (payload) => setState((prev) => ({ ...prev, editingChannel: payload })),
298
+ renderMessage,
299
+ chatRoomHeaderAddons
300
+ };
301
+ }, [state, client, currentUser?.did]);
302
+ return /* @__PURE__ */ jsx(ChatContext.Provider, { value, children });
303
+ }
@@ -11,5 +11,5 @@ export declare const useChatInWallet: () => {
11
11
  unread: boolean;
12
12
  unreadMessageCount: number;
13
13
  }) => void;
14
- chatInWallet: import('react-router-dom').PathMatch<"chatId"> | null;
14
+ chatInWallet: import("react-router-dom").PathMatch<"chatId"> | null;
15
15
  };
@@ -0,0 +1,59 @@
1
+ import { useMatch, useNavigate } from "react-router-dom";
2
+ import cmp from "semver-compare";
3
+ import bridge from "@arcblock/bridge";
4
+ import { useBrowser } from "@arcblock/react-hooks";
5
+ import { useCallback, useMemo } from "react";
6
+ export const useChatInWallet = () => {
7
+ const navigate = useNavigate();
8
+ const { walletVersion, mobile } = useBrowser();
9
+ const chatInWallet = useMatch("/chat-in-wallet/:chatId?");
10
+ const isInArcSphere = window?.navigator?.userAgent.indexOf("ArcSphere") > -1;
11
+ const isChatInWalletV2 = useMemo(() => {
12
+ try {
13
+ const targetVersion = mobile.apple.device ? "5.3.5" : "5.3.1";
14
+ return isInArcSphere || !!walletVersion && cmp(walletVersion, targetVersion) === 1;
15
+ } catch (e) {
16
+ console.error(e);
17
+ return false;
18
+ }
19
+ }, [walletVersion, mobile, isInArcSphere]);
20
+ const isWebNavbar = useMemo(() => {
21
+ try {
22
+ const targetVersion = mobile.apple.device ? "5.4.6" : "5.4.4";
23
+ return !!walletVersion && cmp(walletVersion, targetVersion) === 1;
24
+ } catch (e) {
25
+ console.error(e);
26
+ return false;
27
+ }
28
+ }, [walletVersion, mobile]);
29
+ const navigateToChat = ({ id, title }) => {
30
+ if (isChatInWalletV2) {
31
+ navigate(`/chat-in-wallet/${id}`);
32
+ } else {
33
+ bridge.call("clickChat", { id, title });
34
+ }
35
+ };
36
+ const navigateToChatList = () => {
37
+ navigate("/chat-in-wallet");
38
+ };
39
+ const updateUnreadState = useCallback(
40
+ ({ unread, unreadMessageCount }) => {
41
+ bridge.call("updateUnreadState", {
42
+ unread,
43
+ unreadMessageCount,
44
+ appId: window.blocklet.appPid,
45
+ timestamp: /* @__PURE__ */ new Date()
46
+ });
47
+ },
48
+ []
49
+ );
50
+ return {
51
+ navigateToChat,
52
+ navigateToChatList,
53
+ isChatInWalletV2,
54
+ isWebNavbar,
55
+ isInArcSphere,
56
+ updateUnreadState,
57
+ chatInWallet
58
+ };
59
+ };
@@ -0,0 +1,7 @@
1
+ export { default as ChatClient } from "./chat-client.mjs";
2
+ export { default as Chat } from "./chat.mjs";
3
+ export { default as ChatInWallet } from "./chat-in-wallet.mjs";
4
+ export { default as ChatListInWallet } from "./chat-list-in-wallet.mjs";
5
+ export { default as ChatHeaderAddon } from "./chat-header-addon.mjs";
6
+ export * from "./context.mjs";
7
+ export * from "./unread-notification.mjs";
@@ -1,4 +1,5 @@
1
- import { Chat } from './types';
1
+ /// <reference types="react" />
2
+ import type { Chat } from './types';
2
3
  interface ChatProps {
3
4
  chat: Chat;
4
5
  }
@@ -7,5 +8,5 @@ export interface MessageListHandle {
7
8
  }
8
9
  export default function MessageList({ ref, chat, ...rest }: ChatProps & {
9
10
  ref?: React.RefObject<MessageListHandle | null>;
10
- }): import("react/jsx-runtime").JSX.Element;
11
+ }): import("react").JSX.Element;
11
12
  export {};
@@ -0,0 +1,151 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { useState, useEffect, useRef, useImperativeHandle } from "react";
3
+ import Box from "@mui/material/Box";
4
+ import LoadingButton from "@mui/lab/LoadingButton";
5
+ import Fab from "@mui/material/Fab";
6
+ import { ArrowUpward as ArrowUpwardIcon, ArrowDownward as ArrowDownwardIcon } from "@mui/icons-material";
7
+ import debounce from "lodash/debounce";
8
+ import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
9
+ import { useChatContext } from "./context.mjs";
10
+ import { Message, NotificationMessage } from "./message.mjs";
11
+ import EmptyStatus from "../empty-status/empty-status.mjs";
12
+ const checkIsAtBottom = (element) => {
13
+ return element && (element.scrollHeight === 0 || element.scrollHeight - element.scrollTop < element.clientHeight + 60);
14
+ };
15
+ export default function MessageList({
16
+ ref = void 0,
17
+ chat,
18
+ ...rest
19
+ }) {
20
+ const { isActiveChat, loadMessages } = useChatContext();
21
+ const { t } = useLocaleContext();
22
+ const isActive = isActiveChat(chat.id);
23
+ const containerRef = useRef(null);
24
+ const [isAtBottom, setIsAtBottom] = useState(true);
25
+ const [isLoadingMore, setIsLoadingMore] = useState(false);
26
+ const scrollToBottom = () => {
27
+ containerRef.current?.scrollTo({ top: containerRef.current?.scrollHeight || 0 });
28
+ setIsAtBottom(true);
29
+ };
30
+ useImperativeHandle(ref, () => ({
31
+ scrollToBottom
32
+ }));
33
+ useEffect(() => {
34
+ const handleScroll = debounce(() => {
35
+ setIsAtBottom(checkIsAtBottom(containerRef.current));
36
+ }, 300);
37
+ if (containerRef.current) {
38
+ containerRef.current.addEventListener("scroll", handleScroll);
39
+ }
40
+ }, []);
41
+ useEffect(() => {
42
+ setTimeout(() => {
43
+ if (isAtBottom) {
44
+ scrollToBottom();
45
+ }
46
+ }, 100);
47
+ });
48
+ useEffect(() => {
49
+ if (isActive) {
50
+ setIsAtBottom(checkIsAtBottom(containerRef.current));
51
+ }
52
+ }, [isActive]);
53
+ return /* @__PURE__ */ jsxs(Box, { ...rest, sx: { position: "relative", height: "100%" }, children: [
54
+ /* @__PURE__ */ jsxs(
55
+ Box,
56
+ {
57
+ sx: {
58
+ height: "100%",
59
+ overflowY: "auto",
60
+ display: "flex",
61
+ flexDirection: "column",
62
+ py: 2.5,
63
+ px: 1.25,
64
+ gap: 1,
65
+ // 闪烁
66
+ "@keyframes blinking": {
67
+ "0%": { opacity: 0 },
68
+ "50%": { opacity: 1 },
69
+ "100%": { opacity: 0 }
70
+ },
71
+ // 渐变显示
72
+ "@keyframes fade-in": {
73
+ "0%": { opacity: 0 },
74
+ "100%": { opacity: 1 }
75
+ },
76
+ // 用于弱化消息渲染闪烁
77
+ ".message-item": {
78
+ animation: "fade-in 0.2s linear",
79
+ animationIterationCount: 1
80
+ }
81
+ },
82
+ ref: containerRef,
83
+ children: [
84
+ chat.nextCursor && /* @__PURE__ */ jsx(
85
+ Box,
86
+ {
87
+ id: "next-cursor",
88
+ sx: {
89
+ display: "flex",
90
+ justifyContent: "center",
91
+ py: 0.5,
92
+ height: 40
93
+ },
94
+ children: /* @__PURE__ */ jsx(
95
+ LoadingButton,
96
+ {
97
+ variant: "text",
98
+ color: "inherit",
99
+ loadingPosition: "start",
100
+ loading: isLoadingMore,
101
+ onClick: async () => {
102
+ try {
103
+ setIsLoadingMore(true);
104
+ const firstChatId = chat.messages?.[0]?.id;
105
+ const firstChatMessage = document.getElementById(firstChatId);
106
+ const divider = document.createElement("div");
107
+ divider.style.animation = "blinking 1s linear";
108
+ divider.style.animationIterationCount = "5";
109
+ divider.style.borderTop = "1px solid #e5e5e5";
110
+ divider.style.opacity = "0";
111
+ divider.style.height = "0";
112
+ await loadMessages(chat.id, chat.nextCursor);
113
+ setTimeout(() => {
114
+ containerRef.current?.insertBefore(divider, firstChatMessage);
115
+ containerRef.current?.scrollTo({ top: divider.offsetTop - 40 - 16 - 1 });
116
+ }, 20);
117
+ } catch (error) {
118
+ } finally {
119
+ setIsLoadingMore(false);
120
+ }
121
+ },
122
+ startIcon: /* @__PURE__ */ jsx(ArrowUpwardIcon, {}),
123
+ children: t("chat.loadMore")
124
+ }
125
+ )
126
+ }
127
+ ),
128
+ chat.type !== "notification" && chat.messages?.map((message, index) => {
129
+ const prev = chat.messages?.[index - 1];
130
+ return /* @__PURE__ */ jsx(Box, { className: "message-item", id: message.id, children: /* @__PURE__ */ jsx(Message, { message, prevMessage: prev }) }, message.id);
131
+ }),
132
+ chat.type === "notification" && /* @__PURE__ */ jsxs(Fragment, { children: [
133
+ chat.messages?.length === 0 && /* @__PURE__ */ jsx(EmptyStatus, { sx: { height: "100%" } }),
134
+ chat.messages?.map((message, index) => {
135
+ const prev = chat.messages?.[index - 1];
136
+ return /* @__PURE__ */ jsx(Box, { className: "message-item", id: message.id, children: /* @__PURE__ */ jsx(
137
+ NotificationMessage,
138
+ {
139
+ chat,
140
+ message,
141
+ prevMessage: prev
142
+ }
143
+ ) }, message.id);
144
+ })
145
+ ] })
146
+ ]
147
+ }
148
+ ),
149
+ !isAtBottom && /* @__PURE__ */ jsx(Box, { sx: { position: "absolute", bottom: 16, right: 24 }, onClick: scrollToBottom, children: /* @__PURE__ */ jsx(Fab, { color: "inherit", sx: { width: { xs: 36, sm: 44 }, height: { xs: 36, sm: 44 } }, children: /* @__PURE__ */ jsx(ArrowDownwardIcon, {}) }) })
150
+ ] });
151
+ }
@@ -1,14 +1,15 @@
1
- import { Chat, Message as MessageType, NotificationMessage as NotificationMessageType } from './types';
1
+ /// <reference types="react" />
2
+ import type { Chat, Message as MessageType, NotificationMessage as NotificationMessageType } from './types';
2
3
  interface MessageProps {
3
4
  message: MessageType;
4
5
  prevMessage?: MessageType;
5
6
  showTime?: boolean;
6
7
  }
7
- export declare function Message({ message, prevMessage, showTime, ...rest }: MessageProps): import("react/jsx-runtime").JSX.Element;
8
+ export declare function Message({ message, prevMessage, showTime, ...rest }: MessageProps): import("react").JSX.Element;
8
9
  interface NotificationMessageProps {
9
10
  chat: Chat;
10
11
  message: NotificationMessageType;
11
12
  prevMessage?: NotificationMessageType;
12
13
  }
13
- export declare function NotificationMessage({ chat, message, prevMessage }: NotificationMessageProps): import("react/jsx-runtime").JSX.Element;
14
+ export declare function NotificationMessage({ chat, message, prevMessage }: NotificationMessageProps): import("react").JSX.Element;
14
15
  export {};