@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.
- package/dist/axios.d.ts +8 -0
- package/dist/blocklets.d.ts +1 -1
- package/dist/blocklets.mjs +17 -0
- package/dist/components/api-error-handler/api-error-handler.d.ts +1 -1
- package/dist/components/api-error-handler/api-error-handler.mjs +25 -0
- package/dist/components/api-error-handler/default-handler.mjs +69 -0
- package/dist/components/api-error-handler/index.mjs +2 -0
- package/dist/components/api-error-handler/json-validation-interceptor.d.ts +1 -1
- package/dist/components/api-error-handler/json-validation-interceptor.mjs +9 -0
- package/dist/components/arcsphere/index.mjs +51 -0
- package/dist/components/authz/access-control.d.ts +15 -1
- package/dist/components/authz/access-control.mjs +15 -0
- package/dist/components/authz/context.d.ts +2 -1
- package/dist/components/authz/context.mjs +30 -0
- package/dist/components/authz/index.d.ts +1 -1
- package/dist/components/authz/index.mjs +2 -0
- package/dist/components/auto-translate/api.d.ts +1 -1
- package/dist/components/auto-translate/api.mjs +23 -0
- package/dist/components/auto-translate/auto-translate-button.d.ts +2 -1
- package/dist/components/auto-translate/auto-translate-button.mjs +47 -0
- package/dist/components/auto-translate/editor-store-adaptor.mjs +14 -0
- package/dist/components/auto-translate/index.mjs +6 -0
- package/dist/components/auto-translate/languages.mjs +61 -0
- package/dist/components/auto-translate/post-auto-translate-plugin.d.ts +4 -3
- package/dist/components/auto-translate/post-auto-translate-plugin.mjs +32 -0
- package/dist/components/auto-translate/store.d.ts +3 -3
- package/dist/components/auto-translate/store.mjs +23 -0
- package/dist/components/auto-translate/translate.d.ts +3 -2
- package/dist/components/auto-translate/translate.mjs +103 -0
- package/dist/components/auto-translate/utils.d.ts +1 -1
- package/dist/components/auto-translate/utils.mjs +14 -0
- package/dist/components/auto-translate/with-availibility-check.d.ts +1 -1
- package/dist/components/auto-translate/with-availibility-check.mjs +10 -0
- package/dist/components/avatars/author-info.d.ts +3 -2
- package/dist/components/avatars/author-info.mjs +184 -0
- package/dist/components/avatars/avatar.d.ts +2 -1
- package/dist/components/avatars/avatar.mjs +9 -0
- package/dist/components/avatars/avatars.d.ts +4 -3
- package/dist/components/avatars/avatars.mjs +49 -0
- package/dist/components/avatars/badge.d.ts +2 -1
- package/dist/components/avatars/badge.mjs +178 -0
- package/dist/components/avatars/index.mjs +5 -0
- package/dist/components/avatars/system-user.d.ts +1 -1
- package/dist/components/avatars/system-user.mjs +58 -0
- package/dist/components/back.d.ts +2 -1
- package/dist/components/back.mjs +28 -0
- package/dist/components/blog/blog-link.mjs +16 -0
- package/dist/components/blog/blog-list.d.ts +4 -3
- package/dist/components/blog/blog-list.mjs +441 -0
- package/dist/components/blog/cover-image.d.ts +4 -3
- package/dist/components/blog/cover-image.mjs +160 -0
- package/dist/components/blog/index.mjs +4 -0
- package/dist/components/blog/permalink.d.ts +3 -2
- package/dist/components/blog/permalink.mjs +78 -0
- package/dist/components/blog/types.d.ts +1 -1
- package/dist/components/blog/types.mjs +0 -0
- package/dist/components/button-group/button-group.d.ts +2 -1
- package/dist/components/button-group/button-group.mjs +195 -0
- package/dist/components/button-group/index.mjs +1 -0
- package/dist/components/chat/channel-group.d.ts +3 -2
- package/dist/components/chat/channel-group.mjs +41 -0
- package/dist/components/chat/chat-client.d.ts +2 -2
- package/dist/components/chat/chat-client.mjs +33 -0
- package/dist/components/chat/chat-header-addon.d.ts +2 -1
- package/dist/components/chat/chat-header-addon.mjs +26 -0
- package/dist/components/chat/chat-in-wallet.d.ts +2 -1
- package/dist/components/chat/chat-in-wallet.mjs +67 -0
- package/dist/components/chat/chat-input.d.ts +3 -2
- package/dist/components/chat/chat-input.mjs +295 -0
- package/dist/components/chat/chat-list-in-wallet.d.ts +2 -1
- package/dist/components/chat/chat-list-in-wallet.mjs +113 -0
- package/dist/components/chat/chat-list.d.ts +2 -1
- package/dist/components/chat/chat-list.mjs +109 -0
- package/dist/components/chat/chat-room.d.ts +3 -2
- package/dist/components/chat/chat-room.mjs +225 -0
- package/dist/components/chat/chat.d.ts +2 -1
- package/dist/components/chat/chat.mjs +163 -0
- package/dist/components/chat/context.d.ts +4 -3
- package/dist/components/chat/context.mjs +303 -0
- package/dist/components/chat/hooks.d.ts +1 -1
- package/dist/components/chat/hooks.mjs +59 -0
- package/dist/components/chat/index.mjs +7 -0
- package/dist/components/chat/message-list.d.ts +3 -2
- package/dist/components/chat/message-list.mjs +151 -0
- package/dist/components/chat/message.d.ts +4 -3
- package/dist/components/chat/message.mjs +312 -0
- package/dist/components/chat/participants.d.ts +3 -2
- package/dist/components/chat/participants.mjs +60 -0
- package/dist/components/chat/time.d.ts +1 -1
- package/dist/components/chat/time.mjs +16 -0
- package/dist/components/chat/types.d.ts +1 -1
- package/dist/components/chat/types.mjs +0 -0
- package/dist/components/chat/unread-notification.d.ts +2 -2
- package/dist/components/chat/unread-notification.mjs +43 -0
- package/dist/components/chat/user-search.d.ts +2 -1
- package/dist/components/chat/user-search.mjs +143 -0
- package/dist/components/check-mark.d.ts +2 -1
- package/dist/components/check-mark.mjs +98 -0
- package/dist/components/confirm.d.ts +4 -4
- package/dist/components/confirm.mjs +103 -0
- package/dist/components/dayjs.d.ts +1 -1
- package/dist/components/dayjs.mjs +4 -0
- package/dist/components/default-editor-config-provider.d.ts +3 -2
- package/dist/components/default-editor-config-provider.mjs +107 -0
- package/dist/components/dirty-prompt.d.ts +3 -2
- package/dist/components/dirty-prompt.mjs +117 -0
- package/dist/components/editor/editor.d.ts +4 -3
- package/dist/{editor-B3z0rXVh.mjs → components/editor/editor.mjs} +7 -8
- package/dist/components/editor/index.mjs +2 -0
- package/dist/components/editor/lazy-editor.d.ts +3 -2
- package/dist/components/editor/lazy-editor.mjs +14 -0
- package/dist/components/editor/plugins/video-path-fixer-plugin.mjs +29 -0
- package/dist/components/editor/preview.d.ts +4 -3
- package/dist/components/editor/preview.mjs +32 -0
- package/dist/components/emoji-icon.d.ts +2 -1
- package/dist/components/emoji-icon.mjs +62 -0
- package/dist/components/empty-status/empty-status.d.ts +2 -1
- package/dist/components/empty-status/empty-status.mjs +24 -0
- package/dist/components/empty-status/index.mjs +1 -0
- package/dist/components/hooks/changed.mjs +34 -0
- package/dist/components/hooks/index.mjs +6 -0
- package/dist/components/hooks/interval.mjs +16 -0
- package/dist/components/hooks/locale-context.mjs +12 -0
- package/dist/components/hooks/measure.mjs +8 -0
- package/dist/components/hooks/now.mjs +10 -0
- package/dist/components/hooks/responsive.d.ts +1 -1
- package/dist/components/hooks/responsive.mjs +25 -0
- package/dist/components/hooks/session.d.ts +1 -0
- package/dist/components/hooks/session.mjs +41 -0
- package/dist/components/hooks/use-event-callback.mjs +14 -0
- package/dist/components/icon-button.d.ts +4 -3
- package/dist/components/icon-button.mjs +37 -0
- package/dist/components/input/auto-clear-plugin.mjs +26 -0
- package/dist/components/input/comment-input.d.ts +2 -1
- package/dist/components/input/comment-input.mjs +107 -0
- package/dist/components/input/image-path-fixer-plugin.mjs +29 -0
- package/dist/components/input/index.mjs +4 -0
- package/dist/components/input/input.d.ts +4 -4
- package/dist/components/input/input.mjs +152 -0
- package/dist/components/input/post-edit.d.ts +2 -1
- package/dist/components/input/post-edit.mjs +49 -0
- package/dist/components/input/scrollable-editor-wrapper.d.ts +2 -1
- package/dist/components/input/scrollable-editor-wrapper.mjs +18 -0
- package/dist/components/input/shortcut-plugin.mjs +28 -0
- package/dist/components/label-picker.d.ts +2 -1
- package/dist/components/label-picker.mjs +41 -0
- package/dist/components/lexical.mjs +69 -0
- package/dist/components/locale/en.mjs +92 -0
- package/dist/components/locale/index.mjs +3 -0
- package/dist/components/locale/zh.mjs +94 -0
- package/dist/components/pagination.d.ts +3 -2
- package/dist/components/pagination.mjs +44 -0
- package/dist/components/paywall.d.ts +2 -2
- package/dist/components/paywall.mjs +110 -0
- package/dist/components/point-up/{box-animation.json.d.ts → box-animation.json} +1 -4
- package/dist/components/point-up/index.d.ts +3 -2
- package/dist/components/point-up/index.mjs +153 -0
- package/dist/components/point-up/lottie-component.d.ts +2 -1
- package/dist/components/point-up/lottie-component.mjs +7 -0
- package/dist/components/posts/comment-list/comment-list.d.ts +2 -1
- package/dist/components/posts/comment-list/comment-list.mjs +163 -0
- package/dist/components/posts/comment-list/context.d.ts +4 -4
- package/dist/components/posts/comment-list/context.mjs +318 -0
- package/dist/components/posts/comment.d.ts +3 -2
- package/dist/components/posts/comment.mjs +184 -0
- package/dist/components/posts/index.mjs +6 -0
- package/dist/components/posts/menu.d.ts +3 -2
- package/dist/components/posts/menu.mjs +83 -0
- package/dist/components/posts/post-content.d.ts +2 -1
- package/dist/components/posts/post-content.mjs +86 -0
- package/dist/components/posts/post.d.ts +4 -4
- package/dist/components/posts/post.mjs +198 -0
- package/dist/components/profile-card/index.mjs +1 -0
- package/dist/components/profile-card/profile-card.d.ts +4 -3
- package/dist/components/profile-card/profile-card.mjs +162 -0
- package/dist/components/rating/binary-thumb.d.ts +4 -3
- package/dist/components/rating/binary-thumb.mjs +157 -0
- package/dist/components/rating/github-reaction-container.d.ts +2 -1
- package/dist/components/rating/github-reaction-container.mjs +46 -0
- package/dist/components/rating/github-reaction.d.ts +3 -2
- package/dist/components/rating/github-reaction.mjs +173 -0
- package/dist/components/rating/index.mjs +3 -0
- package/dist/components/rating/rater-list.d.ts +3 -2
- package/dist/components/rating/rater-list.mjs +33 -0
- package/dist/components/rating/rating.d.ts +1 -1
- package/dist/components/rating/rating.mjs +50 -0
- package/dist/components/routes.d.ts +1 -1
- package/dist/components/routes.mjs +27 -0
- package/dist/components/segmented-control.d.ts +3 -2
- package/dist/components/segmented-control.mjs +55 -0
- package/dist/components/shared/relative-time.d.ts +2 -1
- package/dist/components/shared/relative-time.mjs +21 -0
- package/dist/components/toast.d.ts +1 -1
- package/dist/components/toast.mjs +44 -0
- package/dist/components/uploader/index.d.ts +3 -3
- package/dist/components/uploader/index.mjs +70 -0
- package/dist/components/uploader/utils.mjs +16 -0
- package/dist/components/utils.d.ts +4 -4
- package/dist/components/utils.mjs +139 -0
- package/dist/components/view-more.d.ts +1 -1
- package/dist/components/view-more.mjs +50 -0
- package/dist/constants.mjs +14 -0
- package/dist/global.d.ts +1 -0
- package/dist/index.mjs +37 -0
- package/dist/preferences.mjs +9 -0
- package/dist/theme/index.d.ts +3 -3
- package/dist/theme/index.mjs +96 -0
- package/dist/theme/typography.d.ts +1 -1
- package/dist/theme/typography.mjs +66 -0
- package/dist/type-override.d.ts +7 -0
- package/dist/types.mjs +0 -0
- package/dist/vite-env.d.ts +1 -0
- package/dist/ws.d.ts +2 -2
- package/dist/ws.mjs +39 -0
- package/package.json +18 -25
- package/dist/components/avatars/Avatars.stories.d.ts +0 -5
- package/dist/components/posts/Post.stories.d.ts +0 -7
- package/dist/index-BlqRGWgQ.mjs +0 -7631
- package/dist/index.es.js +0 -118
- package/dist/index.umd.js +0 -7589
- package/dist/test/fixtures/index.d.ts +0 -4
- 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
|
+
}
|
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
14
|
+
export declare function NotificationMessage({ chat, message, prevMessage }: NotificationMessageProps): import("react").JSX.Element;
|
|
14
15
|
export {};
|