@cometchat/chat-uikit-react-native 5.4.1 → 5.5.1
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/android/src/main/java/com/reactnativecometchatuikit/CometChatClipboardModule.kt +34 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +562 -5
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +28 -1
- package/ios/CometChatClipboardModule.m +5 -0
- package/ios/CometChatClipboardModule.swift +22 -0
- package/ios/RichTextEditorView.swift +636 -31
- package/ios/RichTextEditorViewManager.m +3 -0
- package/ios/RichTextEditorViewManager.swift +24 -0
- package/package.json +5 -2
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +283 -64
- package/src/CometChatCompactMessageComposer/index.ts +2 -0
- package/src/CometChatConversations/CometChatConversations.tsx +299 -15
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +2 -2
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +15 -3
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +181 -12
- package/src/CometChatMessageInformation/CometChatMessageInformation.tsx +10 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +572 -16
- package/src/CometChatMessageList/components/MessageModals.tsx +30 -0
- package/src/CometChatMessageList/components/MessageOptionsSheet.tsx +21 -9
- package/src/CometChatMessageList/components/MessageQuickActions.tsx +162 -0
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +964 -0
- package/src/CometChatPinnedMessages/index.ts +5 -0
- package/src/CometChatPinnedMessages/style.ts +216 -0
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +5 -0
- package/src/CometChatRichTextEditor/index.tsx +31 -1
- package/src/CometChatRichTextEditor/types.ts +21 -1
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +871 -0
- package/src/CometChatSavedMessages/index.ts +5 -0
- package/src/CometChatSavedMessages/style.ts +159 -0
- package/src/CometChatSearch/CometChatSearch.tsx +71 -341
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +22 -2
- package/src/index.ts +60 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +18 -1
- package/src/shared/base/Types.ts +5 -0
- package/src/shared/constants/UIKitConstants.ts +16 -17
- package/src/shared/constants/textPatterns.ts +27 -0
- package/src/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.ts +25 -225
- package/src/shared/events/CometChatUIEventHandler/Listener.ts +37 -0
- package/src/shared/events/ListenerInitializer.ts +20 -1
- package/src/shared/events/messages.ts +18 -0
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +123 -25
- package/src/shared/formatters/CometChatTextFormatter.ts +22 -0
- package/src/shared/formatters/applyRawFormatters.ts +31 -0
- package/src/shared/formatters/index.ts +14 -0
- package/src/shared/formatters/richTextWireFormat.ts +165 -0
- package/src/shared/framework/MessageDataSource.tsx +309 -10
- package/src/shared/icons/components/thread-follow.tsx +13 -0
- package/src/shared/icons/components/thread-unfollow.tsx +13 -0
- package/src/shared/icons/components/unsave.tsx +13 -0
- package/src/shared/icons/icon-mapping.tsx +6 -0
- package/src/shared/index.ts +37 -0
- package/src/shared/resources/CometChatLocalizeNew/CometChatI18nProvider.tsx +15 -30
- package/src/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.ts +138 -19
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +74 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +54 -1
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +11 -0
- package/src/shared/utils/MarkdownUtils.ts +57 -6
- package/src/shared/utils/MessageUtils.tsx +80 -5
- package/src/shared/utils/PinConversationHelper.ts +245 -0
- package/src/shared/utils/PinSaveFeatureGates.ts +178 -0
- package/src/shared/utils/PinSaveHelper.ts +549 -0
- package/src/shared/utils/PinnedMessagesHelper.ts +65 -0
- package/src/shared/utils/SavedMessagesHelper.ts +267 -0
- package/src/shared/utils/ThreadSubscriptionHelper.ts +427 -0
- package/src/shared/utils/__testUtils__/messagesRequestBuilderMock.ts +37 -0
- package/src/shared/utils/cloneRequestBuilder.ts +25 -0
- package/src/shared/utils/conversationUtils.ts +8 -1
- package/src/shared/utils/stripRichText.ts +7 -0
- package/src/shared/views/ClipboardPasteHandler/ClipboardPasteHandler.ts +32 -0
- package/src/shared/views/ClipboardPasteHandler/NativeCometChatClipboardModule.ts +6 -0
- package/src/shared/views/CometChatActionSheet/CometChatActionSheet.tsx +4 -0
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +5 -0
- package/src/shared/views/CometChatAvatar/CometChatAvatar.tsx +17 -5
- package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +5 -1
- package/src/shared/views/CometChatCaptionText/CometChatCaptionText.tsx +8 -1
- package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +2 -0
- package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +91 -20
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +5 -0
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +5 -0
- package/src/shared/views/CometChatList/CometChatList.tsx +6 -1
- package/src/shared/views/CometChatList/Header.tsx +8 -2
- package/src/shared/views/CometChatListItem/CometChatListItem.tsx +4 -0
- package/src/shared/views/CometChatRetryButton/CometChatRetryButton.tsx +1 -0
- package/src/shared/views/CometChatSuggestionList/CometChatSuggestionList.tsx +4 -0
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +5 -0
- package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +5 -0
- package/src/shared/views/ErrorEmptyView/ErrorEmptyView.tsx +4 -1
- package/src/theme/default/default.ts +6 -0
- package/src/theme/type.ts +4 -0
|
@@ -0,0 +1,964 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CometChatPinnedMessages — the per-conversation pinned list (§6.3).
|
|
3
|
+
*
|
|
4
|
+
* Opened from the chat-header ⋮. Conversation-scoped and PUBLIC: every member sees
|
|
5
|
+
* the same list regardless of who pinned what, and role-gating applies to unpinning,
|
|
6
|
+
* not to reading. Ordered `pinnedAt DESC`. Full-screen route on RN (§6.9).
|
|
7
|
+
*
|
|
8
|
+
* Deliberately shaped like CometChatSavedMessages without sharing an abstraction:
|
|
9
|
+
* the data source, the row action, the title and the empty copy all differ, and a
|
|
10
|
+
* config-driven single component would be fought by both callers. The design doc
|
|
11
|
+
* also specifies two public components, which the other four platforms mirror.
|
|
12
|
+
*
|
|
13
|
+
* Read-only w.r.t. unread state (§7.7): no markAsRead, no receipts, no counts.
|
|
14
|
+
*
|
|
15
|
+
* Two interactions per row:
|
|
16
|
+
* tap → `onItemPress`, which the host turns into a jump-to-message
|
|
17
|
+
* long-press → MessageOptionsSheet — the SAME sheet the message list opens, over
|
|
18
|
+
* an allow-list of options (see PANEL_OPTIONS)
|
|
19
|
+
*
|
|
20
|
+
* @module CometChatPinnedMessages
|
|
21
|
+
*/
|
|
22
|
+
import { CometChat } from "@cometchat/chat-sdk-react-native";
|
|
23
|
+
import { ClipboardPasteHandler } from "../shared/views/ClipboardPasteHandler/ClipboardPasteHandler";
|
|
24
|
+
import { stripRichText } from "../shared/utils/stripRichText";
|
|
25
|
+
import React, { JSX, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
26
|
+
import { FlatList, NativeModules, Pressable, Text, View } from "react-native";
|
|
27
|
+
import { ChatConfigurator } from "../shared";
|
|
28
|
+
import { MessageOptionsSheet } from "../CometChatMessageList/components/MessageOptionsSheet";
|
|
29
|
+
import { MessageOptionConstants } from "../shared/constants/UIKitConstants";
|
|
30
|
+
import { CometChatMessageOption } from "../shared/modals/CometChatMessageOption";
|
|
31
|
+
import { CometChatMessageTemplate } from "../shared/modals/CometChatMessageTemplate";
|
|
32
|
+
import { MessageUtils } from "../shared/utils/MessageUtils";
|
|
33
|
+
import { useCompTheme, useTheme } from "../theme/hook";
|
|
34
|
+
import { deepMerge } from "../shared/helper/helperFunctions";
|
|
35
|
+
import { DeepPartial } from "../shared/helper/types";
|
|
36
|
+
import { PinnedMessagesStyle } from "./style";
|
|
37
|
+
import { CometChatTheme } from "../theme/type";
|
|
38
|
+
import { Icon } from "../shared/icons/Icon";
|
|
39
|
+
import { CometChatAvatar } from "../shared/views/CometChatAvatar";
|
|
40
|
+
import { CometChatDate } from "../shared/views/CometChatDate";
|
|
41
|
+
import { ErrorEmptyView } from "../shared/views/ErrorEmptyView/ErrorEmptyView";
|
|
42
|
+
import { CometChatConfirmDialog } from "../shared/views/CometChatConfirmDialog";
|
|
43
|
+
import { CometChatRetryButton } from "../shared/views/CometChatRetryButton/CometChatRetryButton";
|
|
44
|
+
import { useCometChatTranslation } from "../shared/resources/CometChatLocalizeNew";
|
|
45
|
+
import { useToast } from "../shared/helper/useToast";
|
|
46
|
+
import {
|
|
47
|
+
applyPinSaveAnswer,
|
|
48
|
+
canPin,
|
|
49
|
+
carryPinSaveAttrs,
|
|
50
|
+
isMessageGoneError,
|
|
51
|
+
isSaved,
|
|
52
|
+
isSystemPin,
|
|
53
|
+
saveMessage,
|
|
54
|
+
unpinMessage,
|
|
55
|
+
unsaveMessage,
|
|
56
|
+
sameMessageId,
|
|
57
|
+
} from "../shared/utils/PinSaveHelper";
|
|
58
|
+
import { CometChatUIEventHandler } from "../shared/events/CometChatUIEventHandler/CometChatUIEventHandler";
|
|
59
|
+
import { MessageEvents } from "../shared/events/messages";
|
|
60
|
+
import { CometChatTextFormatter } from "../shared/formatters/CometChatTextFormatter";
|
|
61
|
+
import { applyRawFormatters } from "../shared/formatters/applyRawFormatters";
|
|
62
|
+
import { buildPinnedMessagesRequest } from "../shared/utils/PinnedMessagesHelper";
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The options this panel is willing to show, as an ALLOW-LIST.
|
|
66
|
+
*
|
|
67
|
+
* A deny-list would be the obvious choice and is the wrong one. Options arrive from
|
|
68
|
+
* the DataSource as pure descriptors — `{id, title, icon}` with NO `onPress`; the
|
|
69
|
+
* message list binds every handler itself. So an option this panel does not
|
|
70
|
+
* explicitly wire renders as a dead menu row. A deny-list silently acquires those
|
|
71
|
+
* every time someone adds an option upstream; an allow-list fails closed.
|
|
72
|
+
*
|
|
73
|
+
* Excluded on purpose:
|
|
74
|
+
* - delete / edit / reply / replyInThread — product decision, this is a reference
|
|
75
|
+
* surface, not a place to author from.
|
|
76
|
+
* - replyPrivately / sendPrivately — same class as reply; they need a composer,
|
|
77
|
+
* which does not exist here.
|
|
78
|
+
* - markAsUnread — §7.7 makes this panel read-only w.r.t. unread state.
|
|
79
|
+
* - reactToMessage — needs the reaction sheet plus live row rebinding.
|
|
80
|
+
* - translateMessage — per-row in-place render state.
|
|
81
|
+
* - reportMessage — needs the report dialog; add it when it is asked for.
|
|
82
|
+
* - threadSubscription — NOT a product decision, a correctness one. `threadSubscribed`
|
|
83
|
+
* is only populated on responses to requests that asked for it, and a pinned thread
|
|
84
|
+
* message can be appended to this list in realtime off a websocket frame, which
|
|
85
|
+
* carries no flag. The option would render "Subscribe" on a thread the user already
|
|
86
|
+
* follows, and tapping it would unfollow them. Absent is the honest state here.
|
|
87
|
+
*/
|
|
88
|
+
const PANEL_OPTIONS: string[] = [
|
|
89
|
+
MessageOptionConstants.messageInformation,
|
|
90
|
+
MessageOptionConstants.copyMessage,
|
|
91
|
+
MessageOptionConstants.shareMessage,
|
|
92
|
+
MessageOptionConstants.unpinMessage,
|
|
93
|
+
MessageOptionConstants.saveMessage,
|
|
94
|
+
MessageOptionConstants.unsaveMessage,
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
const templateKey = (category: string, type: string) => `${category}_${type}`;
|
|
98
|
+
|
|
99
|
+
/** Plain text for copy/share. Empty for anything without a text body. */
|
|
100
|
+
const textOf = (message: CometChat.BaseMessage): string => {
|
|
101
|
+
if (message instanceof CometChat.TextMessage) return message.getText() ?? "";
|
|
102
|
+
if (message instanceof CometChat.MediaMessage) return message.getCaption() ?? "";
|
|
103
|
+
return "";
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export interface CometChatPinnedMessagesInterface {
|
|
107
|
+
/** 1-1 conversation. Mutually exclusive with `group`. */
|
|
108
|
+
user?: CometChat.User;
|
|
109
|
+
/** Group conversation. Mutually exclusive with `user`. */
|
|
110
|
+
group?: CometChat.Group;
|
|
111
|
+
/** Page size. Defaults to 30; the server caps a conversation at 100. */
|
|
112
|
+
limit?: number;
|
|
113
|
+
/**
|
|
114
|
+
* Your own request builder, for anything `limit` alone cannot express — a search
|
|
115
|
+
* term, a category or type filter, a different page size.
|
|
116
|
+
*
|
|
117
|
+
* `setPinnedOnly(true)` and the `user`/`group` conversation scope are re-asserted
|
|
118
|
+
* on whatever you pass, so a builder that forgets them still returns this
|
|
119
|
+
* conversation's pinned messages rather than ordinary history. Set them anyway to
|
|
120
|
+
* keep the intent readable.
|
|
121
|
+
*
|
|
122
|
+
* The `limit` prop wins: `limit={10}` with a builder that calls `setLimit(50)` pages
|
|
123
|
+
* in tens. Without the prop the builder's own limit is used, and with neither, 30.
|
|
124
|
+
*/
|
|
125
|
+
messagesRequestBuilder?: CometChat.MessagesRequestBuilder;
|
|
126
|
+
/** Closes the panel — rendered as the ✕ in the header. */
|
|
127
|
+
onBack?: () => void;
|
|
128
|
+
/** Tapping a row — the host jumps its message list to this message. */
|
|
129
|
+
onItemPress?: (message: CometChat.BaseMessage) => void;
|
|
130
|
+
/**
|
|
131
|
+
* Hide the unpin option in the long-press sheet. Named per design doc §6.7,
|
|
132
|
+
* which lists the same four flags for the action sheet — the panel mirrors that
|
|
133
|
+
* vocabulary rather than inventing a shorter one.
|
|
134
|
+
*/
|
|
135
|
+
hideUnpinMessageOption?: boolean;
|
|
136
|
+
/** Hide Save in the long-press sheet (§6.7). */
|
|
137
|
+
hideSaveMessageOption?: boolean;
|
|
138
|
+
/** Hide Copy. Named to match CometChatMessageList's flag of the same purpose. */
|
|
139
|
+
hideCopyMessageOption?: boolean;
|
|
140
|
+
/** Hide Share. Named to match CometChatMessageList's flag of the same purpose. */
|
|
141
|
+
hideShareMessageOption?: boolean;
|
|
142
|
+
/** Hide Info. Named to match CometChatMessageList's flag of the same purpose. */
|
|
143
|
+
hideMessageInfoOption?: boolean;
|
|
144
|
+
/** Hide Unsave in the long-press sheet (§6.7). */
|
|
145
|
+
hideUnsaveMessageOption?: boolean;
|
|
146
|
+
ItemView?: (message: CometChat.BaseMessage) => JSX.Element;
|
|
147
|
+
/**
|
|
148
|
+
* Text formatters, same array the host passes to CometChatMessageList. The panel renders real
|
|
149
|
+
* bubbles, so without this a consumer's own token reaches the template unrecognised and the
|
|
150
|
+
* pinned copy of a message reads differently from the same message in the list.
|
|
151
|
+
*/
|
|
152
|
+
textFormatters?: Array<CometChatTextFormatter>;
|
|
153
|
+
title?: string;
|
|
154
|
+
/** Per-instance style overrides, merged over the theme's pinnedMessagesStyles. */
|
|
155
|
+
style?: DeepPartial<PinnedMessagesStyle>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
type ListState = "loading" | "loaded" | "error" | "empty";
|
|
159
|
+
|
|
160
|
+
export const CometChatPinnedMessages = (props: CometChatPinnedMessagesInterface) => {
|
|
161
|
+
const {
|
|
162
|
+
user,
|
|
163
|
+
group,
|
|
164
|
+
limit,
|
|
165
|
+
messagesRequestBuilder,
|
|
166
|
+
onBack,
|
|
167
|
+
onItemPress,
|
|
168
|
+
hideUnpinMessageOption = false,
|
|
169
|
+
hideSaveMessageOption = false,
|
|
170
|
+
hideUnsaveMessageOption = false,
|
|
171
|
+
hideCopyMessageOption = false,
|
|
172
|
+
hideShareMessageOption = false,
|
|
173
|
+
hideMessageInfoOption = false,
|
|
174
|
+
ItemView,
|
|
175
|
+
textFormatters,
|
|
176
|
+
title,
|
|
177
|
+
style,
|
|
178
|
+
} = props;
|
|
179
|
+
|
|
180
|
+
const theme = useTheme() as CometChatTheme;
|
|
181
|
+
const compTheme = useCompTheme();
|
|
182
|
+
const { t } = useCometChatTranslation();
|
|
183
|
+
|
|
184
|
+
/** theme → component-level override → per-instance prop, the kit's usual order. */
|
|
185
|
+
const styles = useMemo(
|
|
186
|
+
() => deepMerge(theme.pinnedMessagesStyles, compTheme.pinnedMessagesStyles ?? {}, style ?? {}),
|
|
187
|
+
[theme.pinnedMessagesStyles, compTheme.pinnedMessagesStyles, style]
|
|
188
|
+
);
|
|
189
|
+
const { showToast, ToastElement } = useToast();
|
|
190
|
+
|
|
191
|
+
const [messages, setMessagesRaw] = useState<CometChat.BaseMessage[]>([]);
|
|
192
|
+
const [state, setStateRaw] = useState<ListState>("loading");
|
|
193
|
+
const loggedInUser = useRef<CometChat.User | null>(null);
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* These panels are full-screen routes, so popping one while a fetch or a post-confirm
|
|
197
|
+
* write is still in flight lands a setState on an unmounted component — the React Native
|
|
198
|
+
* warning, plus work nobody will see. Guarding at the SETTER rather than at each of the
|
|
199
|
+
* async resolvers keeps every existing call site unchanged and cannot be forgotten at a
|
|
200
|
+
* new one.
|
|
201
|
+
*/
|
|
202
|
+
const mounted = useRef(true);
|
|
203
|
+
useEffect(() => () => { mounted.current = false; }, []);
|
|
204
|
+
const setMessages: typeof setMessagesRaw = useCallback(
|
|
205
|
+
value => { if (mounted.current) setMessagesRaw(value); },
|
|
206
|
+
[]
|
|
207
|
+
);
|
|
208
|
+
const setState: typeof setStateRaw = useCallback(
|
|
209
|
+
value => { if (mounted.current) setStateRaw(value); },
|
|
210
|
+
[]
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
/** Long-press sheet state, mirroring what CometChatMessageList keeps. */
|
|
214
|
+
const [sheetOptions, setSheetOptions] = useState<CometChatMessageOption[]>([]);
|
|
215
|
+
const [selectedMessage, setSelectedMessage] = useState<CometChat.BaseMessage | null>(null);
|
|
216
|
+
const [showInfo, setShowInfo] = useState(false);
|
|
217
|
+
/** The row awaiting unpin/unsave confirmation, or null. */
|
|
218
|
+
const [confirm, setConfirm] = useState<{
|
|
219
|
+
message: CometChat.BaseMessage;
|
|
220
|
+
action: "unpin" | "unsave";
|
|
221
|
+
} | null>(null);
|
|
222
|
+
const infoObject = useRef<CometChat.BaseMessage | null | undefined>(null);
|
|
223
|
+
const sheetRef = useRef<{ togglePanel: () => void } | null>(null);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* MessageOptionsSheet takes these because the message list drives delete and
|
|
227
|
+
* report through them. This panel offers neither, so they stay inert — they exist
|
|
228
|
+
* to satisfy the contract, not to be read.
|
|
229
|
+
*/
|
|
230
|
+
const deleteItem = useRef<CometChat.BaseMessage | undefined>(undefined);
|
|
231
|
+
const pendingReportRef = useRef(false);
|
|
232
|
+
|
|
233
|
+
/** Single-use and accumulates its own paging state, so it lives for the screen. */
|
|
234
|
+
const request = useRef<CometChat.MessagesRequest | null>(null);
|
|
235
|
+
const fetching = useRef(false);
|
|
236
|
+
/**
|
|
237
|
+
* The end of a cursor-paged list is an empty page — there is nothing to ask the request
|
|
238
|
+
* for beforehand. Latch it here, or every further scroll-to-end fires a round trip that
|
|
239
|
+
* can only come back empty again.
|
|
240
|
+
*/
|
|
241
|
+
const exhausted = useRef(false);
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The ordinary messages request, filtered to pinned rows. Paging is the ordinary
|
|
245
|
+
* cursor walk — setPinnedOnly() only changes the field the cursor rides on, from
|
|
246
|
+
* sentAt to pinnedAt, because that is the order the server returns pins in.
|
|
247
|
+
*
|
|
248
|
+
* A factory rather than an inline build: a request is single-use, so retrying after
|
|
249
|
+
* an error needs a fresh one.
|
|
250
|
+
*/
|
|
251
|
+
const buildRequest = useCallback(
|
|
252
|
+
() => buildPinnedMessagesRequest({ user, group, limit, messagesRequestBuilder }),
|
|
253
|
+
[limit, group, user, messagesRequestBuilder]
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
if (request.current === null) request.current = buildRequest();
|
|
257
|
+
|
|
258
|
+
const loadNext = useCallback(async () => {
|
|
259
|
+
if (fetching.current || exhausted.current || !request.current) return;
|
|
260
|
+
fetching.current = true;
|
|
261
|
+
try {
|
|
262
|
+
// fetchPrevious(), not fetchNext() — "previous" is older, which is the direction
|
|
263
|
+
// this list scrolls. Same call CometChatMessageList makes to load history.
|
|
264
|
+
const rows = await request.current.fetchPrevious();
|
|
265
|
+
if (rows.length > 0) {
|
|
266
|
+
// Dedupe on append (ENG-38891). A realtime pin/save PREPENDS its row the moment the
|
|
267
|
+
// event lands; the same message then comes back inside a later fetched page, and a
|
|
268
|
+
// plain concat lists it twice — which is how a five-pin cap rendered six rows.
|
|
269
|
+
setMessages(prev => {
|
|
270
|
+
const seen = new Set(prev.map(m => String(m.getId())));
|
|
271
|
+
return [...prev, ...rows.filter(r => !seen.has(String(r.getId())))];
|
|
272
|
+
});
|
|
273
|
+
setState("loaded");
|
|
274
|
+
} else {
|
|
275
|
+
exhausted.current = true;
|
|
276
|
+
setState(prev => (prev === "loading" ? "empty" : prev));
|
|
277
|
+
}
|
|
278
|
+
} catch (e) {
|
|
279
|
+
// Log it — a bare `catch {}` hides client-side rejections that never reach the
|
|
280
|
+
// network, leaving a generic error screen and an empty network log to debug from.
|
|
281
|
+
console.error("CometChatPinnedMessages: failed to fetch pinned messages", e);
|
|
282
|
+
setState(prev => (prev === "loading" ? "error" : prev));
|
|
283
|
+
} finally {
|
|
284
|
+
fetching.current = false;
|
|
285
|
+
}
|
|
286
|
+
}, []);
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Keep loading while the rows do not fill the viewport.
|
|
290
|
+
*
|
|
291
|
+
* onEndReached is the only paging trigger, and it needs a scroll to fire. When the first page
|
|
292
|
+
* is shorter than the screen there is nothing to scroll, so the list stops at page one and the
|
|
293
|
+
* rest is unreachable — verified with limit=2 against 4 pinned messages: `loadNext` ran once,
|
|
294
|
+
* returned 2 rows, and was never called again while the panel sat half empty.
|
|
295
|
+
*
|
|
296
|
+
* The default page size fills a phone screen, which is why this hid; `limit` is a public prop,
|
|
297
|
+
* so any integrator who lowers it hits a permanently incomplete list. loadNext already guards
|
|
298
|
+
* on `fetching`/`exhausted`, so this tops up at most one request at a time and stops on its own.
|
|
299
|
+
*/
|
|
300
|
+
const viewportHeight = useRef(0);
|
|
301
|
+
const contentHeight = useRef(0);
|
|
302
|
+
const fillViewport = useCallback(() => {
|
|
303
|
+
if (exhausted.current || fetching.current) return;
|
|
304
|
+
if (viewportHeight.current <= 0 || contentHeight.current <= 0) return;
|
|
305
|
+
if (contentHeight.current < viewportHeight.current) loadNext();
|
|
306
|
+
}, [loadNext]);
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Retry after an error. The request is single-use and has already consumed whatever
|
|
310
|
+
* paging state it got to, so this builds a fresh one rather than re-calling the old —
|
|
311
|
+
* a retried request must start from the top of the list, not from a half-walked cursor.
|
|
312
|
+
*/
|
|
313
|
+
const reload = useCallback(() => {
|
|
314
|
+
request.current = buildRequest();
|
|
315
|
+
exhausted.current = false;
|
|
316
|
+
fetching.current = false;
|
|
317
|
+
setMessages([]);
|
|
318
|
+
setState("loading");
|
|
319
|
+
loadNext();
|
|
320
|
+
}, [buildRequest, loadNext]);
|
|
321
|
+
|
|
322
|
+
useEffect(() => {
|
|
323
|
+
CometChat.getLoggedinUser()
|
|
324
|
+
.then(u => {
|
|
325
|
+
loggedInUser.current = u ?? null;
|
|
326
|
+
})
|
|
327
|
+
.catch(() => {
|
|
328
|
+
loggedInUser.current = null;
|
|
329
|
+
})
|
|
330
|
+
.finally(() => {
|
|
331
|
+
loadNext();
|
|
332
|
+
});
|
|
333
|
+
}, [loadNext]);
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Keep the panel in step with pins made anywhere else — the message list next to it,
|
|
337
|
+
* this user's other device, or another participant over the socket.
|
|
338
|
+
*
|
|
339
|
+
* Without this the list is a snapshot taken on mount: unpin a message from the
|
|
340
|
+
* conversation and its row sits here until the screen is reopened.
|
|
341
|
+
*
|
|
342
|
+
* Two event families, deliberately both:
|
|
343
|
+
* - cc* — emitted by the device that PERFORMED the action. Covers the local case,
|
|
344
|
+
* and 1-1 conversations, where the server emits no socket frame at all.
|
|
345
|
+
* - on* — the socket echo, i.e. somebody else's pin. Groups AND 1-1: the DM
|
|
346
|
+
* case was confirmed at the handler on 1 Sep (receiverType='user'),
|
|
347
|
+
* so this panel accepts BOTH directions of a 1-1 frame.
|
|
348
|
+
* The payload shapes differ (cc* wraps in {message}), hence the unwrap.
|
|
349
|
+
*/
|
|
350
|
+
useEffect(() => {
|
|
351
|
+
const id = "pinned-messages-panel";
|
|
352
|
+
|
|
353
|
+
const belongsHere = (message: CometChat.BaseMessage): boolean => {
|
|
354
|
+
const receiver = message.getReceiverId?.();
|
|
355
|
+
if (group) return receiver === group.getGuid();
|
|
356
|
+
if (!user) return false;
|
|
357
|
+
|
|
358
|
+
// A 1-1 frame is NOT symmetric: on an INCOMING pin — the counterparty pinned it —
|
|
359
|
+
// getReceiverId() is the logged-in user's own uid, not theirs. Matching on the
|
|
360
|
+
// receiver alone therefore accepted only the pins this device sent and silently
|
|
361
|
+
// dropped the other participant's, which is the case this listener exists for.
|
|
362
|
+
// Match either direction, and require receiverType 'user' so a group message from
|
|
363
|
+
// that same person cannot leak into a 1-1 panel.
|
|
364
|
+
if (message.getReceiverType?.() !== CometChat.RECEIVER_TYPE.USER) return false;
|
|
365
|
+
const uid = user.getUid();
|
|
366
|
+
return receiver === uid || message.getSender?.()?.getUid?.() === uid;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const unwrap = (payload: any): CometChat.BaseMessage | null =>
|
|
370
|
+
payload?.message ?? payload ?? null;
|
|
371
|
+
|
|
372
|
+
const added = (payload: any) => {
|
|
373
|
+
const message = unwrap(payload);
|
|
374
|
+
if (!message || !belongsHere(message)) return;
|
|
375
|
+
setMessages(prev => {
|
|
376
|
+
if (prev.some(m => sameMessageId(m.getId(), message.getId()))) return prev;
|
|
377
|
+
// Newest pin first — the list is ordered by pinnedAt, and this one is now the
|
|
378
|
+
// most recent.
|
|
379
|
+
return [message, ...prev];
|
|
380
|
+
});
|
|
381
|
+
setState("loaded");
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
const removed = (payload: any) => {
|
|
385
|
+
const message = unwrap(payload);
|
|
386
|
+
if (!message) return;
|
|
387
|
+
setMessages(prev => {
|
|
388
|
+
const next = prev.filter(m => !sameMessageId(m.getId(), message.getId()));
|
|
389
|
+
if (next.length === prev.length) return prev;
|
|
390
|
+
// Only claim empty once the first page has actually been read; an event that
|
|
391
|
+
// arrives mid-load would otherwise replace the spinner with "no pinned messages".
|
|
392
|
+
if (next.length === 0 && exhausted.current) setState("empty");
|
|
393
|
+
return next;
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* ENG-38177 — an edit rewrites the row in place.
|
|
399
|
+
*
|
|
400
|
+
* A pinned message stays pinned when it is edited, so the row must survive and show the
|
|
401
|
+
* new content. Without this the panel serves whatever text was pinned, indefinitely.
|
|
402
|
+
*
|
|
403
|
+
* The edit response carries no pinnedAt (verified on staging 2026-08-14), so replacing
|
|
404
|
+
* the row wholesale would strip the very attribute this list is built on — hence
|
|
405
|
+
* carryPinSaveAttrs. Rows not already in the list are ignored: an edit is not a pin.
|
|
406
|
+
*/
|
|
407
|
+
const edited = (payload: any) => {
|
|
408
|
+
const message = unwrap(payload);
|
|
409
|
+
if (!message) return;
|
|
410
|
+
setMessages(prev => {
|
|
411
|
+
const index = prev.findIndex(m => sameMessageId(m.getId(), message.getId()));
|
|
412
|
+
if (index === -1) return prev;
|
|
413
|
+
const next = [...prev];
|
|
414
|
+
next[index] = carryPinSaveAttrs(message, prev[index]);
|
|
415
|
+
return next;
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* ENG-38181 — a deleted message cannot stay in the pinned list.
|
|
421
|
+
*
|
|
422
|
+
* The server drops it from the pinned set, so a row left behind is one the user can tap
|
|
423
|
+
* into nothing. Reuses `removed`, which already handles the last-row empty state.
|
|
424
|
+
*/
|
|
425
|
+
const deleted = (payload: any) => removed(payload);
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* A save or unsave made ANYWHERE rewrites the bookmark on the row in place.
|
|
429
|
+
*
|
|
430
|
+
* The row stays either way — `savedAt` is a private per-viewer flag with no bearing on
|
|
431
|
+
* whether the message is pinned — so this is not `added`/`removed`, it is an update.
|
|
432
|
+
*
|
|
433
|
+
* applyPinSaveAnswer, NOT carryPinSaveAttrs (which `edited` above uses): on this path the
|
|
434
|
+
* response IS the answer, and an ABSENT savedAt means "no longer saved". Carrying the old
|
|
435
|
+
* value across would leave the bookmark on screen for ever, which is the exact mistake
|
|
436
|
+
* the edit path has to make in the opposite direction.
|
|
437
|
+
*/
|
|
438
|
+
const savedChanged = (payload: any) => {
|
|
439
|
+
const message = unwrap(payload);
|
|
440
|
+
if (!message) return;
|
|
441
|
+
setMessages(prev => {
|
|
442
|
+
const index = prev.findIndex(m => sameMessageId(m.getId(), message.getId()));
|
|
443
|
+
if (index === -1) return prev;
|
|
444
|
+
const next = [...prev];
|
|
445
|
+
next[index] = applyPinSaveAnswer(prev[index], message);
|
|
446
|
+
return next;
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// The keys must be written out literally. emitMessageEvent dispatches by matching
|
|
451
|
+
// the event name against each callback's inferred function NAME, which an anonymous
|
|
452
|
+
// arrow gets from its property key — passing `added` by reference would register a
|
|
453
|
+
// function named "added" and never match anything.
|
|
454
|
+
CometChatUIEventHandler.addMessageListener(id, {
|
|
455
|
+
ccMessagePinned: (payload: any) => added(payload),
|
|
456
|
+
onMessagePinned: (payload: any) => added(payload),
|
|
457
|
+
ccMessageUnpinned: (payload: any) => removed(payload),
|
|
458
|
+
onMessageUnpinned: (payload: any) => removed(payload),
|
|
459
|
+
ccMessageEdited: (payload: any) => edited(payload),
|
|
460
|
+
onMessageEdited: (payload: any) => edited(payload),
|
|
461
|
+
ccMessageSaved: (payload: any) => savedChanged(payload),
|
|
462
|
+
onMessageSaved: (payload: any) => savedChanged(payload),
|
|
463
|
+
ccMessageUnsaved: (payload: any) => savedChanged(payload),
|
|
464
|
+
onMessageUnsaved: (payload: any) => savedChanged(payload),
|
|
465
|
+
ccMessageDeleted: (payload: any) => deleted(payload),
|
|
466
|
+
onMessageDeleted: (payload: any) => deleted(payload),
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
return () => CometChatUIEventHandler.removeMessageListener(id);
|
|
470
|
+
}, [group, user]);
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Clearing a conversation deletes its messages FOR THIS VIEWER, pinned ones included.
|
|
474
|
+
* The server is consistent about it — the pinned list comes back empty and every id
|
|
475
|
+
* behind the old rows returns ERR_MESSAGE_ID_NOT_FOUND — but this panel had no idea it
|
|
476
|
+
* had happened, so it kept rendering rows for messages that no longer existed and every
|
|
477
|
+
* unpin from them 404'd.
|
|
478
|
+
*
|
|
479
|
+
* Emptying rather than refetching: there is nothing left to fetch, and a request here
|
|
480
|
+
* would race the delete and could repopulate from a stale read.
|
|
481
|
+
*/
|
|
482
|
+
useEffect(() => {
|
|
483
|
+
const id = "pinned-messages-panel-conversation";
|
|
484
|
+
|
|
485
|
+
CometChatUIEventHandler.addConversationListener(id, {
|
|
486
|
+
ccConversationDeleted: ({ conversation }: { conversation: CometChat.Conversation }) => {
|
|
487
|
+
const cleared = conversation?.getConversationWith?.();
|
|
488
|
+
if (!cleared) return;
|
|
489
|
+
const clearedId =
|
|
490
|
+
cleared instanceof CometChat.Group ? cleared.getGuid() : (cleared as CometChat.User).getUid();
|
|
491
|
+
const mine = group ? group.getGuid() : user ? user.getUid() : null;
|
|
492
|
+
if (!mine || clearedId !== mine) return;
|
|
493
|
+
|
|
494
|
+
setMessages([]);
|
|
495
|
+
exhausted.current = true;
|
|
496
|
+
setState("empty");
|
|
497
|
+
},
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
return () => CometChatUIEventHandler.removeConversationListener(id);
|
|
501
|
+
}, [group, user]);
|
|
502
|
+
|
|
503
|
+
const templates: CometChatMessageTemplate[] = useMemo(
|
|
504
|
+
() => ChatConfigurator.dataSource.getAllMessageTemplates(theme, { textFormatters }),
|
|
505
|
+
[theme, textFormatters]
|
|
506
|
+
);
|
|
507
|
+
|
|
508
|
+
const templatesMap = useMemo(() => {
|
|
509
|
+
const map = new Map<string, CometChatMessageTemplate>();
|
|
510
|
+
templates.forEach(tpl => map.set(templateKey(tpl.category, tpl.type), tpl));
|
|
511
|
+
return map;
|
|
512
|
+
}, [templates]);
|
|
513
|
+
|
|
514
|
+
const closeSheet = useCallback(() => {
|
|
515
|
+
setSheetOptions([]);
|
|
516
|
+
setSelectedMessage(null);
|
|
517
|
+
infoObject.current = null;
|
|
518
|
+
setShowInfo(false);
|
|
519
|
+
}, []);
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Unpin and unsave ASK first (Jitvar, 2026-08-04) — the removing half of each pair
|
|
523
|
+
* confirms, on every surface it appears. Unpin especially: the pin may not be yours,
|
|
524
|
+
* and nothing on this panel offers it back.
|
|
525
|
+
*/
|
|
526
|
+
const onUnpin = (message: CometChat.BaseMessage) => setConfirm({ message, action: "unpin" });
|
|
527
|
+
|
|
528
|
+
const runUnpin = (message: CometChat.BaseMessage) => {
|
|
529
|
+
const id = message.getId();
|
|
530
|
+
setMessages(prev => {
|
|
531
|
+
const next = prev.filter(m => !sameMessageId(m.getId(), id));
|
|
532
|
+
if (next.length === 0) setState("empty");
|
|
533
|
+
return next;
|
|
534
|
+
});
|
|
535
|
+
unpinMessage(id)
|
|
536
|
+
.then((updated) => {
|
|
537
|
+
// Tell the other surfaces (ENG-38884). This panel sits ON TOP of the message list,
|
|
538
|
+
// which holds its own copy of the message — without this the bubble behind kept its
|
|
539
|
+
// pin glyph until a fetch rebuilt the list, so the message read as still pinned.
|
|
540
|
+
// The server's response is the authoritative answer; the row we held is the fallback
|
|
541
|
+
// for the case where it comes back empty.
|
|
542
|
+
CometChatUIEventHandler.emitMessageEvent(MessageEvents.ccMessageUnpinned, {
|
|
543
|
+
message: updated ?? message,
|
|
544
|
+
});
|
|
545
|
+
showToast(t("MESSAGE_UNPINNED") ?? "Message unpinned");
|
|
546
|
+
})
|
|
547
|
+
.catch((error: any) => {
|
|
548
|
+
// A message that no longer exists must STAY removed. Reinstating the row — which is
|
|
549
|
+
// right when the write merely failed — leaves the user tapping Unpin forever on
|
|
550
|
+
// something the server will never accept. Clearing a chat with pinned messages does
|
|
551
|
+
// exactly this: the rows survive in the open panel, but every id behind them is gone.
|
|
552
|
+
if (isMessageGoneError(error)) {
|
|
553
|
+
setMessages(prev => {
|
|
554
|
+
if (prev.length === 0 && exhausted.current) setState("empty");
|
|
555
|
+
return prev;
|
|
556
|
+
});
|
|
557
|
+
showToast(t("MESSAGE_NO_LONGER_AVAILABLE") ?? "This message is no longer available.");
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
setMessages(prev => [message, ...prev]);
|
|
561
|
+
setState("loaded");
|
|
562
|
+
showToast(t("PIN_SAVE_GENERIC_ERROR") ?? "Something went wrong. Please try again.");
|
|
563
|
+
});
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Save/unsave from inside the panel. Unlike unpin, the row STAYS — `savedAt` is a
|
|
568
|
+
* private per-viewer flag and has no bearing on whether a message is pinned.
|
|
569
|
+
* Swaps in the settled server row so the bookmark indicator re-renders off it.
|
|
570
|
+
*/
|
|
571
|
+
const onToggleSave = (message: CometChat.BaseMessage, save: boolean) => {
|
|
572
|
+
// Saving is additive and runs straight away; unsaving asks.
|
|
573
|
+
if (!save) {
|
|
574
|
+
setConfirm({ message, action: "unsave" });
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
runToggleSave(message, true);
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
const runToggleSave = (message: CometChat.BaseMessage, save: boolean) => {
|
|
581
|
+
const id = message.getId();
|
|
582
|
+
const call = save ? saveMessage : unsaveMessage;
|
|
583
|
+
call(id)
|
|
584
|
+
.then(updated => {
|
|
585
|
+
setMessages(prev => prev.map(m => (sameMessageId(m.getId(), id) ? (updated ?? m) : m)));
|
|
586
|
+
// Same reason as the unpin above — the bookmark on the bubble behind this panel is a
|
|
587
|
+
// second copy of the same state and has to be told.
|
|
588
|
+
CometChatUIEventHandler.emitMessageEvent(
|
|
589
|
+
save ? MessageEvents.ccMessageSaved : MessageEvents.ccMessageUnsaved,
|
|
590
|
+
{ message: updated ?? message }
|
|
591
|
+
);
|
|
592
|
+
showToast(
|
|
593
|
+
save
|
|
594
|
+
? (t("MESSAGE_SAVED") ?? "Message saved")
|
|
595
|
+
: (t("MESSAGE_UNSAVED") ?? "Message unsaved")
|
|
596
|
+
);
|
|
597
|
+
})
|
|
598
|
+
.catch(() =>
|
|
599
|
+
showToast(t("PIN_SAVE_GENERIC_ERROR") ?? "Something went wrong. Please try again.")
|
|
600
|
+
);
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
const onCopy = (message: CometChat.BaseMessage) => {
|
|
604
|
+
// Via the shared handler, NOT Clipboard directly: it strips the rich-text wire
|
|
605
|
+
// format for the plain clipboard while keeping the rich version for a paste back
|
|
606
|
+
// into our own composer. Copying raw put `<color=#e11d48>` into Notes and mail.
|
|
607
|
+
//
|
|
608
|
+
// ponytail: still the raw text rather than the mention-resolved string the message
|
|
609
|
+
// list copies via its local getPlainString (not exported). Copying "@uid" instead
|
|
610
|
+
// of "@Name" is the known ceiling; lift it by extracting that helper into
|
|
611
|
+
// shared/utils.
|
|
612
|
+
ClipboardPasteHandler.copyMessageText(textOf(message), textFormatters);
|
|
613
|
+
// Deferred like the message list does: unmounting the Modal while Clipboard is
|
|
614
|
+
// still touching views crashes Fabric on iOS.
|
|
615
|
+
requestAnimationFrame(closeSheet);
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
const onShare = (message: CometChat.BaseMessage) => {
|
|
619
|
+
const attachment =
|
|
620
|
+
message instanceof CometChat.MediaMessage ? message.getAttachment() : undefined;
|
|
621
|
+
const fileUrl = attachment?.getUrl() ?? "";
|
|
622
|
+
const shareObj = {
|
|
623
|
+
// Plain text: the share target is another app, which cannot read our wire format.
|
|
624
|
+
message: stripRichText(applyRawFormatters(textOf(message), textFormatters)),
|
|
625
|
+
type: message.getType(),
|
|
626
|
+
mediaName: attachment?.getName() ?? "",
|
|
627
|
+
fileUrl,
|
|
628
|
+
mimeType: attachment?.getMimeType() ?? "",
|
|
629
|
+
};
|
|
630
|
+
closeSheet();
|
|
631
|
+
// Same 600ms as the message list: presenting the native share sheet before the
|
|
632
|
+
// modal finishes dismissing freezes the screen on iOS/Fabric.
|
|
633
|
+
setTimeout(() => {
|
|
634
|
+
NativeModules?.FileManager?.shareMessage?.(shareObj, () => {});
|
|
635
|
+
}, 600);
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Unpin is offered only to Admin/Moderator/Owner — the same gate as the
|
|
640
|
+
* action-sheet option, so the panel cannot become a back door to an action the
|
|
641
|
+
* message list refuses. A system pin (`app_system`) is never user-unpinnable.
|
|
642
|
+
*/
|
|
643
|
+
const mayUnpin = (message: CometChat.BaseMessage) =>
|
|
644
|
+
!hideUnpinMessageOption && !isSystemPin(message) && canPin(loggedInUser.current, group ?? null);
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Takes the DataSource's options for this message, keeps only the allow-listed
|
|
648
|
+
* ones, and binds a handler to each. Anything left unbound is dropped rather than
|
|
649
|
+
* shown inert — the switch's default is the safety net for PANEL_OPTIONS drifting
|
|
650
|
+
* ahead of the handlers below.
|
|
651
|
+
*/
|
|
652
|
+
const buildOptions = (message: CometChat.BaseMessage): CometChatMessageOption[] => {
|
|
653
|
+
const template = templatesMap.get(templateKey(message.getCategory(), message.getType()));
|
|
654
|
+
// No logged-in user means no options at all rather than an unauthenticated menu.
|
|
655
|
+
const raw =
|
|
656
|
+
template?.options && loggedInUser.current
|
|
657
|
+
? (template.options(loggedInUser.current, message, theme, group) ?? [])
|
|
658
|
+
: [];
|
|
659
|
+
|
|
660
|
+
const bound: CometChatMessageOption[] = [];
|
|
661
|
+
raw
|
|
662
|
+
.filter(option => PANEL_OPTIONS.includes(option.id))
|
|
663
|
+
.forEach(option => {
|
|
664
|
+
switch (option.id) {
|
|
665
|
+
case MessageOptionConstants.messageInformation:
|
|
666
|
+
if (hideMessageInfoOption) break;
|
|
667
|
+
bound.push({
|
|
668
|
+
...option,
|
|
669
|
+
onPress: () => {
|
|
670
|
+
infoObject.current = message;
|
|
671
|
+
setShowInfo(true);
|
|
672
|
+
},
|
|
673
|
+
});
|
|
674
|
+
break;
|
|
675
|
+
case MessageOptionConstants.copyMessage:
|
|
676
|
+
if (hideCopyMessageOption) break;
|
|
677
|
+
bound.push({ ...option, onPress: () => onCopy(message) });
|
|
678
|
+
break;
|
|
679
|
+
case MessageOptionConstants.shareMessage:
|
|
680
|
+
if (hideShareMessageOption) break;
|
|
681
|
+
bound.push({ ...option, onPress: () => onShare(message) });
|
|
682
|
+
break;
|
|
683
|
+
case MessageOptionConstants.unpinMessage:
|
|
684
|
+
if (!mayUnpin(message)) break;
|
|
685
|
+
bound.push({
|
|
686
|
+
...option,
|
|
687
|
+
onPress: () => {
|
|
688
|
+
closeSheet();
|
|
689
|
+
onUnpin(message);
|
|
690
|
+
},
|
|
691
|
+
});
|
|
692
|
+
break;
|
|
693
|
+
case MessageOptionConstants.saveMessage:
|
|
694
|
+
if (hideSaveMessageOption) break;
|
|
695
|
+
bound.push({
|
|
696
|
+
...option,
|
|
697
|
+
onPress: () => {
|
|
698
|
+
closeSheet();
|
|
699
|
+
onToggleSave(message, true);
|
|
700
|
+
},
|
|
701
|
+
});
|
|
702
|
+
break;
|
|
703
|
+
case MessageOptionConstants.unsaveMessage:
|
|
704
|
+
if (hideUnsaveMessageOption) break;
|
|
705
|
+
bound.push({
|
|
706
|
+
...option,
|
|
707
|
+
onPress: () => {
|
|
708
|
+
closeSheet();
|
|
709
|
+
onToggleSave(message, false);
|
|
710
|
+
},
|
|
711
|
+
});
|
|
712
|
+
break;
|
|
713
|
+
default:
|
|
714
|
+
break;
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
return bound;
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
const onLongPressRow = (message: CometChat.BaseMessage) => {
|
|
721
|
+
const options = buildOptions(message);
|
|
722
|
+
if (options.length === 0) return;
|
|
723
|
+
setSelectedMessage(message);
|
|
724
|
+
setSheetOptions(options);
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
const renderRow = ({ item, index }: { item: CometChat.BaseMessage; index: number }) => {
|
|
728
|
+
const sender = item.getSender();
|
|
729
|
+
const avatar = sender?.getAvatar();
|
|
730
|
+
const isSelf = sender?.getUid() === loggedInUser.current?.getUid();
|
|
731
|
+
|
|
732
|
+
return (
|
|
733
|
+
<Pressable
|
|
734
|
+
testID={`PinnedMessages.row-${index}`}
|
|
735
|
+
onPress={() => onItemPress?.(item)}
|
|
736
|
+
onLongPress={() => onLongPressRow(item)}
|
|
737
|
+
accessibilityRole='button'
|
|
738
|
+
style={styles.itemStyle?.containerStyle}
|
|
739
|
+
>
|
|
740
|
+
<View style={styles.itemStyle?.headerContainerStyle}>
|
|
741
|
+
<CometChatAvatar
|
|
742
|
+
// getAvatar() is a URL string; the avatar wraps bare strings into {uri}
|
|
743
|
+
// at runtime but types the prop as ImageSourcePropType, so wrap it here.
|
|
744
|
+
image={avatar ? { uri: avatar } : undefined}
|
|
745
|
+
name={sender?.getName() ?? ""}
|
|
746
|
+
style={styles.itemStyle?.avatarStyle}
|
|
747
|
+
/>
|
|
748
|
+
{/* No source label here, unlike the saved list — every row is from THIS
|
|
749
|
+
conversation, so it would be identical on every row and pure noise. */}
|
|
750
|
+
<Text
|
|
751
|
+
numberOfLines={1}
|
|
752
|
+
style={styles.itemStyle?.senderNameStyle}
|
|
753
|
+
>
|
|
754
|
+
{isSelf ? (t("YOU") ?? "You") : (sender?.getName() ?? "")}
|
|
755
|
+
</Text>
|
|
756
|
+
<Text
|
|
757
|
+
style={styles.itemStyle?.separatorStyle}
|
|
758
|
+
>
|
|
759
|
+
{"•"}
|
|
760
|
+
</Text>
|
|
761
|
+
<CometChatDate
|
|
762
|
+
timeStamp={item.getSentAt() * 1000}
|
|
763
|
+
pattern='dayDateFormat'
|
|
764
|
+
style={styles.itemStyle?.dateStyle}
|
|
765
|
+
/>
|
|
766
|
+
|
|
767
|
+
{/* A saved-too row shows the bookmark, so state reads consistently with the
|
|
768
|
+
message list. The pin glyph lives in the bubble's own meta row. */}
|
|
769
|
+
{isSaved(item) ? (
|
|
770
|
+
<View
|
|
771
|
+
accessible={true}
|
|
772
|
+
accessibilityLabel={t("SAVED") ?? "Saved"}
|
|
773
|
+
style={styles.itemStyle?.savedIndicatorContainerStyle}
|
|
774
|
+
>
|
|
775
|
+
<Icon
|
|
776
|
+
name='bookmark-fill'
|
|
777
|
+
color={styles.itemStyle?.savedIndicatorStyle?.tintColor}
|
|
778
|
+
height={styles.itemStyle?.savedIndicatorStyle?.height}
|
|
779
|
+
width={styles.itemStyle?.savedIndicatorStyle?.width}
|
|
780
|
+
/>
|
|
781
|
+
</View>
|
|
782
|
+
) : null}
|
|
783
|
+
</View>
|
|
784
|
+
|
|
785
|
+
{/* pointerEvents none so the bubble never swallows the row's tap/long-press. */}
|
|
786
|
+
<View pointerEvents='none' style={styles.itemStyle?.bubbleContainerStyle}>
|
|
787
|
+
{ItemView
|
|
788
|
+
? ItemView(item)
|
|
789
|
+
: MessageUtils.getMessageView({
|
|
790
|
+
message: item,
|
|
791
|
+
templates,
|
|
792
|
+
alignment: "left",
|
|
793
|
+
theme,
|
|
794
|
+
// The row header above already carries the avatar and the sender
|
|
795
|
+
// name. Both of the bubble's own copies are group-only, so leaving
|
|
796
|
+
// these on printed each of them twice in a group.
|
|
797
|
+
avatarVisibility: false,
|
|
798
|
+
headerVisibility: false,
|
|
799
|
+
})}
|
|
800
|
+
</View>
|
|
801
|
+
</Pressable>
|
|
802
|
+
);
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
return (
|
|
806
|
+
<View style={styles.containerStyle}>
|
|
807
|
+
{/* Back arrow LEADING the title, matching Group Info / User Info. This is a pushed
|
|
808
|
+
full-screen route, not a modal, so a leading back is the right affordance — a
|
|
809
|
+
trailing ✕ read as "dismiss a sheet". */}
|
|
810
|
+
<View style={styles.headerContainerStyle}>
|
|
811
|
+
{onBack ? (
|
|
812
|
+
<Pressable
|
|
813
|
+
testID='PinnedMessages.back'
|
|
814
|
+
onPress={onBack}
|
|
815
|
+
accessibilityRole='button'
|
|
816
|
+
accessibilityLabel={t("BACK") ?? "Back"}
|
|
817
|
+
hitSlop={8}
|
|
818
|
+
>
|
|
819
|
+
<Icon
|
|
820
|
+
name='arrow-back'
|
|
821
|
+
icon={styles.closeButtonIcon}
|
|
822
|
+
color={styles.closeButtonIconStyle?.tintColor}
|
|
823
|
+
height={styles.closeButtonIconStyle?.height}
|
|
824
|
+
width={styles.closeButtonIconStyle?.width}
|
|
825
|
+
/>
|
|
826
|
+
</Pressable>
|
|
827
|
+
) : null}
|
|
828
|
+
<Text style={styles.titleStyle}>
|
|
829
|
+
{/* Static, matching the mobile mock. The web spec puts the count inline
|
|
830
|
+
(§6.0), but there is no count field on the server — it would be a
|
|
831
|
+
fetch-all-and-count that ticks upward as pages load, so the title would
|
|
832
|
+
visibly change under the user while they scroll. */}
|
|
833
|
+
{title ?? (t("PINNED_MESSAGES") ?? "Pinned Messages")}
|
|
834
|
+
</Text>
|
|
835
|
+
</View>
|
|
836
|
+
|
|
837
|
+
{state === "empty" ? (
|
|
838
|
+
<ErrorEmptyView
|
|
839
|
+
testID='PinnedMessages.empty'
|
|
840
|
+
// ErrorEmptyView carries no layout of its own — everything comes from these props.
|
|
841
|
+
containerStyle={styles.emptyStateStyle?.containerStyle}
|
|
842
|
+
title={t("NO_PINNED_MESSAGES") ?? "No pinned messages yet"}
|
|
843
|
+
titleStyle={styles.emptyStateStyle?.titleStyle}
|
|
844
|
+
subTitle={
|
|
845
|
+
t("NO_PINNED_MESSAGES_SUBTITLE") ??
|
|
846
|
+
"Pin important messages to keep them easy to find."
|
|
847
|
+
}
|
|
848
|
+
subTitleStyle={styles.emptyStateStyle?.subTitleStyle}
|
|
849
|
+
Icon={
|
|
850
|
+
// Filled, matching the design and the saved screen's bookmark-fill — an empty state
|
|
851
|
+
// is an illustration, not the outline glyph used inline as metadata.
|
|
852
|
+
<Icon
|
|
853
|
+
name='keep-fill'
|
|
854
|
+
color={styles.emptyStateStyle?.iconStyle?.tintColor}
|
|
855
|
+
height={styles.emptyStateStyle?.iconStyle?.height}
|
|
856
|
+
width={styles.emptyStateStyle?.iconStyle?.width}
|
|
857
|
+
/>
|
|
858
|
+
}
|
|
859
|
+
/>
|
|
860
|
+
) : state === "error" ? (
|
|
861
|
+
// The house error state, same as the conversation list: error-state illustration,
|
|
862
|
+
// Oops! / went wrong / try again, and a Retry button. A bare sentence in the
|
|
863
|
+
// default text style was the only thing here before, which read as a broken
|
|
864
|
+
// screen rather than a recoverable one.
|
|
865
|
+
<ErrorEmptyView
|
|
866
|
+
testID='PinnedMessages.error'
|
|
867
|
+
containerStyle={styles.errorStateStyle?.containerStyle}
|
|
868
|
+
title={t("OOPS") ?? "Oops!"}
|
|
869
|
+
titleStyle={styles.errorStateStyle?.titleStyle}
|
|
870
|
+
subTitle={t("SOMETHING_WENT_WRONG") ?? "Looks like something went wrong."}
|
|
871
|
+
tertiaryTitle={t("WRONG_TEXT_TRY_AGAIN") ?? "Please try again."}
|
|
872
|
+
subTitleStyle={styles.errorStateStyle?.subTitleStyle}
|
|
873
|
+
Icon={
|
|
874
|
+
<Icon
|
|
875
|
+
name='error-state'
|
|
876
|
+
height={styles.errorStateStyle?.iconStyle?.height}
|
|
877
|
+
width={styles.errorStateStyle?.iconStyle?.width}
|
|
878
|
+
containerStyle={{ marginBottom: styles.errorStateStyle?.iconStyle?.marginBottom }}
|
|
879
|
+
/>
|
|
880
|
+
}
|
|
881
|
+
RetryView={<CometChatRetryButton onPress={reload} />}
|
|
882
|
+
/>
|
|
883
|
+
) : (
|
|
884
|
+
<FlatList
|
|
885
|
+
testID='PinnedMessages.list'
|
|
886
|
+
data={messages}
|
|
887
|
+
keyExtractor={item => String(item.getId())}
|
|
888
|
+
renderItem={renderRow}
|
|
889
|
+
onEndReachedThreshold={0.5}
|
|
890
|
+
onEndReached={loadNext}
|
|
891
|
+
onLayout={event => {
|
|
892
|
+
viewportHeight.current = event.nativeEvent.layout.height;
|
|
893
|
+
fillViewport();
|
|
894
|
+
}}
|
|
895
|
+
onContentSizeChange={(_width, height) => {
|
|
896
|
+
contentHeight.current = height;
|
|
897
|
+
fillViewport();
|
|
898
|
+
}}
|
|
899
|
+
/>
|
|
900
|
+
)}
|
|
901
|
+
|
|
902
|
+
{/* The message list's own sheet, so the panel's long-press looks and behaves
|
|
903
|
+
identically. Reactions are off — this panel does not wire them. */}
|
|
904
|
+
<MessageOptionsSheet
|
|
905
|
+
bottomSheetRef={sheetRef}
|
|
906
|
+
isOpen={sheetOptions.length > 0 || showInfo}
|
|
907
|
+
showMessageOptions={sheetOptions}
|
|
908
|
+
ExtensionsComponent={null}
|
|
909
|
+
messageInfo={showInfo}
|
|
910
|
+
infoObject={infoObject}
|
|
911
|
+
selectedMessage={selectedMessage}
|
|
912
|
+
hideReactionOption={true}
|
|
913
|
+
templatesMap={templatesMap}
|
|
914
|
+
mergedTheme={theme}
|
|
915
|
+
deleteItem={deleteItem}
|
|
916
|
+
pendingReportRef={pendingReportRef}
|
|
917
|
+
onClose={closeSheet}
|
|
918
|
+
onReactionPress={() => {}}
|
|
919
|
+
onAddReactionPress={() => {}}
|
|
920
|
+
setShowDeleteModal={() => {}}
|
|
921
|
+
setShowReportDialog={() => {}}
|
|
922
|
+
setShowMessageOptions={setSheetOptions}
|
|
923
|
+
setExtensionsComponent={() => {}}
|
|
924
|
+
setMessageInfo={setShowInfo}
|
|
925
|
+
setShowEmojiKeyboard={() => {}}
|
|
926
|
+
/>
|
|
927
|
+
|
|
928
|
+
<CometChatConfirmDialog
|
|
929
|
+
titleText={
|
|
930
|
+
confirm?.action === "unpin"
|
|
931
|
+
? (t("UNPIN_MESSAGE_TITLE") ?? "Unpin Message")
|
|
932
|
+
: (t("UNSAVE_MESSAGE_TITLE") ?? "Unsave Message")
|
|
933
|
+
}
|
|
934
|
+
icon={
|
|
935
|
+
<Icon
|
|
936
|
+
name={confirm?.action === "unpin" ? "keep-off" : "unsave"}
|
|
937
|
+
size={theme.spacing.spacing.s12}
|
|
938
|
+
color={theme.color.error}
|
|
939
|
+
/>
|
|
940
|
+
}
|
|
941
|
+
cancelButtonText={t("CANCEL") ?? "Cancel"}
|
|
942
|
+
confirmButtonText={
|
|
943
|
+
confirm?.action === "unpin" ? (t("UNPIN") ?? "Unpin") : (t("UNSAVE") ?? "Unsave")
|
|
944
|
+
}
|
|
945
|
+
messageText={
|
|
946
|
+
confirm?.action === "unpin"
|
|
947
|
+
? (t("UNPIN_MESSAGE_CONFIRM") ??
|
|
948
|
+
"Do you want to unpin this message from this conversation?")
|
|
949
|
+
: (t("UNSAVE_MESSAGE_CONFIRM") ??
|
|
950
|
+
"Do you want to remove this message from your saved messages?")
|
|
951
|
+
}
|
|
952
|
+
isOpen={confirm != null}
|
|
953
|
+
onCancel={() => setConfirm(null)}
|
|
954
|
+
onConfirm={() => {
|
|
955
|
+
if (confirm?.action === "unpin") runUnpin(confirm.message);
|
|
956
|
+
else if (confirm) runToggleSave(confirm.message, false);
|
|
957
|
+
setConfirm(null);
|
|
958
|
+
}}
|
|
959
|
+
/>
|
|
960
|
+
|
|
961
|
+
{ToastElement}
|
|
962
|
+
</View>
|
|
963
|
+
);
|
|
964
|
+
};
|