@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,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kit's seam onto the SDK's conversation-pin surface.
|
|
3
|
+
*
|
|
4
|
+
* Separate from PinSaveHelper on purpose: pinning a CONVERSATION and pinning a
|
|
5
|
+
* MESSAGE share a name and nothing else. Different endpoints, a different cap
|
|
6
|
+
* (5, not 100), a different feature flag, no role gate, and a different failure
|
|
7
|
+
* mode. Folding them together would invite carrying one's rules onto the other.
|
|
8
|
+
*
|
|
9
|
+
* @module PinConversationHelper
|
|
10
|
+
*/
|
|
11
|
+
import { CometChat } from "@cometchat/chat-sdk-react-native";
|
|
12
|
+
|
|
13
|
+
/** Off until an integrator opts in — same shape as PinSaveConfig. */
|
|
14
|
+
export const PinConversationConfig = {
|
|
15
|
+
_enabled: false,
|
|
16
|
+
enable(enabled: boolean = true) {
|
|
17
|
+
this._enabled = enabled;
|
|
18
|
+
},
|
|
19
|
+
isEnabled(): boolean {
|
|
20
|
+
return this._enabled;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** An admin/global conversation pin. Users may not unpin these. */
|
|
25
|
+
export const SYSTEM_PINNER = "app_system";
|
|
26
|
+
|
|
27
|
+
/** Server error codes specific to conversation pinning. */
|
|
28
|
+
export const PinConversationErrors = {
|
|
29
|
+
LIMIT: "ERR_PINNED_CONVERSATIONS_LIMIT_EXCEEDED",
|
|
30
|
+
/** Never-messaged or hidden conversation — cannot be newly pinned. */
|
|
31
|
+
NOT_ACCESSIBLE: "ERR_CONVERSATION_NOT_ACCESSIBLE",
|
|
32
|
+
FEATURE_OFF: "ERR_FEATURE_NOT_ACCESSIBLE",
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* True when the conversation is pinned. PRESENCE of the timestamp is the flag —
|
|
37
|
+
* the SDK deletes rather than zeroes, so never write this as `!!getPinnedAt()`.
|
|
38
|
+
*/
|
|
39
|
+
export const isConversationPinned = (conversation?: CometChat.Conversation | null): boolean => {
|
|
40
|
+
try {
|
|
41
|
+
return (
|
|
42
|
+
conversation?.getPinnedAt?.() !== undefined && conversation?.getPinnedAt?.() !== null
|
|
43
|
+
);
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* True for an admin/global pin. A user cannot unpin one, so the affordance is
|
|
51
|
+
* hidden rather than shown and then refused by the server.
|
|
52
|
+
*/
|
|
53
|
+
export const isSystemPinnedConversation = (
|
|
54
|
+
conversation?: CometChat.Conversation | null
|
|
55
|
+
): boolean => {
|
|
56
|
+
try {
|
|
57
|
+
return isConversationPinned(conversation) && conversation?.getPinnedBy?.() === SYSTEM_PINNER;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The uid/guid and type a pin call needs, or null when the row is unusable. */
|
|
64
|
+
export const targetOf = (
|
|
65
|
+
conversation?: CometChat.Conversation | null
|
|
66
|
+
): { id: string; type: string } | null => {
|
|
67
|
+
try {
|
|
68
|
+
if (!conversation) return null;
|
|
69
|
+
const type = conversation.getConversationType?.();
|
|
70
|
+
const other: any = conversation.getConversationWith?.();
|
|
71
|
+
if (!type || !other) return null;
|
|
72
|
+
const id =
|
|
73
|
+
type === "group"
|
|
74
|
+
? typeof other.getGuid === "function" && other.getGuid()
|
|
75
|
+
: typeof other.getUid === "function" && other.getUid();
|
|
76
|
+
return id ? { id: String(id), type } : null;
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Last-message time for ordering, 0 when the conversation has never had one. */
|
|
83
|
+
const recencyOf = (conversation: any): number => {
|
|
84
|
+
try {
|
|
85
|
+
return conversation?.getLastMessage?.()?.getSentAt?.() ?? 0;
|
|
86
|
+
} catch {
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** Pin time, or -1 when not pinned. Newest pin sorts first within a tier. */
|
|
92
|
+
const pinnedAtOf = (conversation: any): number => {
|
|
93
|
+
try {
|
|
94
|
+
const v = conversation?.getPinnedAt?.();
|
|
95
|
+
return v === undefined || v === null ? -1 : Number(v);
|
|
96
|
+
} catch {
|
|
97
|
+
return -1;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Which of the three ordering tiers a conversation belongs to. Lower wins.
|
|
103
|
+
*
|
|
104
|
+
* 0 SYSTEM pin — set from the dashboard (`pinnedBy: app_system`). Highest
|
|
105
|
+
* priority: it outranks a user's own pin no matter how much
|
|
106
|
+
* more recently the user pinned theirs. A user cannot unpin one.
|
|
107
|
+
* 1 USER pin — this user pinned it themselves.
|
|
108
|
+
* 2 unpinned — ordinary recency ordering.
|
|
109
|
+
*/
|
|
110
|
+
export const TIER = { SYSTEM_PIN: 0, USER_PIN: 1, UNPINNED: 2 };
|
|
111
|
+
|
|
112
|
+
export const tierOf = (conversation: any): number => {
|
|
113
|
+
try {
|
|
114
|
+
if (!isConversationPinned(conversation)) return TIER.UNPINNED;
|
|
115
|
+
return isSystemPinnedConversation(conversation) ? TIER.SYSTEM_PIN : TIER.USER_PIN;
|
|
116
|
+
} catch {
|
|
117
|
+
return TIER.UNPINNED;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Where a conversation belongs in the list — the single ordering rule used by pin,
|
|
123
|
+
* unpin, an incoming message, AND a conversation appearing for the first time.
|
|
124
|
+
*
|
|
125
|
+
* Three blocks, in this order:
|
|
126
|
+
* 1. dashboard/system pins, newest pin first
|
|
127
|
+
* 2. the user's own pins, newest pin first
|
|
128
|
+
* 3. everything else, most recent message first
|
|
129
|
+
*
|
|
130
|
+
* Consequences worth stating, because each was a bug at some point:
|
|
131
|
+
* - a pinned row KEEPS its slot when a message arrives. Pinned order is by when it
|
|
132
|
+
* was pinned, not by who messaged last, so a busy pinned chat must not climb.
|
|
133
|
+
* - a brand-new or previously-deleted conversation goes into tier 3, NOT to index
|
|
134
|
+
* 0. A message in a deleted chat must not surface above the user's pins.
|
|
135
|
+
* - a user pin never outranks a dashboard pin, whatever the timestamps say.
|
|
136
|
+
*
|
|
137
|
+
* @param conversation the row being placed
|
|
138
|
+
* @param all the current list INCLUDING it — filtered out here
|
|
139
|
+
* @returns the index to insert at
|
|
140
|
+
*/
|
|
141
|
+
export const conversationInsertIndex = (conversation: any, all: Array<any>): number => {
|
|
142
|
+
try {
|
|
143
|
+
if (!conversation || !Array.isArray(all)) return 0;
|
|
144
|
+
const id = String(conversation.getConversationId?.() ?? "");
|
|
145
|
+
const others = all.filter(o => String(o?.getConversationId?.() ?? "") !== id);
|
|
146
|
+
|
|
147
|
+
const myTier = tierOf(conversation);
|
|
148
|
+
const myPin = pinnedAtOf(conversation);
|
|
149
|
+
const myRecency = recencyOf(conversation);
|
|
150
|
+
|
|
151
|
+
let index = 0;
|
|
152
|
+
for (let i = 0; i < others.length; i++) {
|
|
153
|
+
const other = others[i];
|
|
154
|
+
const otherTier = tierOf(other);
|
|
155
|
+
|
|
156
|
+
// A higher tier always sits above, regardless of timestamps.
|
|
157
|
+
if (otherTier < myTier) {
|
|
158
|
+
index = i + 1;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (otherTier > myTier) break;
|
|
162
|
+
|
|
163
|
+
// Same tier: pins compare by pinnedAt, unpinned rows by message recency.
|
|
164
|
+
const mine = myTier === TIER.UNPINNED ? myRecency : myPin;
|
|
165
|
+
const theirs = otherTier === TIER.UNPINNED ? recencyOf(other) : pinnedAtOf(other);
|
|
166
|
+
if (theirs > mine) {
|
|
167
|
+
index = i + 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
return index;
|
|
173
|
+
} catch {
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export const isConversationCapError = (error: any): boolean => {
|
|
179
|
+
try {
|
|
180
|
+
return error?.code === PinConversationErrors.LIMIT;
|
|
181
|
+
} catch {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export const isConversationNotPinnable = (error: any): boolean => {
|
|
187
|
+
try {
|
|
188
|
+
return error?.code === PinConversationErrors.NOT_ACCESSIBLE;
|
|
189
|
+
} catch {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const isConversationFeatureOff = (error: any): boolean => {
|
|
195
|
+
try {
|
|
196
|
+
return error?.code === PinConversationErrors.FEATURE_OFF;
|
|
197
|
+
} catch {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The server-owned cap, when the error carries it. Usually absent — the backend
|
|
204
|
+
* documents caps as tenant settings and does not promise them on the payload — so
|
|
205
|
+
* callers must render copy without a number rather than printing a guess.
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The conversation cap, or null when unknown. Same two sources as the message-level
|
|
210
|
+
* resolveCapLimit: `errorParams.limit` first, then app settings via the SDK getter
|
|
211
|
+
* (ENG-37790). This replaced a regex over the server's error prose — see the note there.
|
|
212
|
+
*/
|
|
213
|
+
export const resolveConversationCapLimit = async (error: any): Promise<number | null> => {
|
|
214
|
+
const fromError = readConversationCapLimit(error);
|
|
215
|
+
if (fromError !== null) return fromError;
|
|
216
|
+
try {
|
|
217
|
+
const limit = await CometChat.getPinConversationLimit();
|
|
218
|
+
return typeof limit === "number" && isFinite(limit) && limit > 0 ? limit : null;
|
|
219
|
+
} catch {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/** The cap as carried ON the rejection, or null. See resolveConversationCapLimit. */
|
|
225
|
+
export const readConversationCapLimit = (error: any): number | null => {
|
|
226
|
+
try {
|
|
227
|
+
const raw = error?.errorParams?.limit;
|
|
228
|
+
if (typeof raw !== "number" && typeof raw !== "string") return null;
|
|
229
|
+
const limit = Number(raw);
|
|
230
|
+
return isFinite(limit) && limit > 0 ? limit : null;
|
|
231
|
+
} catch {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Both resolve the FULL updated Conversation, verified on staging 2026-07-30 — not
|
|
238
|
+
* an ack string, which is what these were originally typed as. A caller can apply
|
|
239
|
+
* the resolved row straight into its list instead of trusting its optimistic guess.
|
|
240
|
+
*/
|
|
241
|
+
export const pinConversation = (id: string, type: string): Promise<CometChat.Conversation> =>
|
|
242
|
+
CometChat.pinConversation(id, type);
|
|
243
|
+
|
|
244
|
+
export const unpinConversation = (id: string, type: string): Promise<CometChat.Conversation> =>
|
|
245
|
+
CometChat.unpinConversation(id, type);
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-owned gates for Pin Message, Save Message and Pin Conversation.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS IS IN THE KIT: these three are not integrator preferences, they are app settings —
|
|
5
|
+
* the server decides whether the plan has them and, for pin-conversation, whether ops has mapped
|
|
6
|
+
* the flag for that app at all. Until now the kit shipped the switches (PinSaveConfig,
|
|
7
|
+
* PinConversationConfig) but never asked the server, so every integrator had to know to do it
|
|
8
|
+
* themselves. The sample app did; nobody else would. The two failure modes both look like bugs:
|
|
9
|
+
* leave them off and the features silently never appear, or hardcode them on and the options
|
|
10
|
+
* render on an app whose flag is unmapped, where every tap toasts "this feature isn't available".
|
|
11
|
+
*
|
|
12
|
+
* The kit already owns exactly this kind of state for conversation update settings — fetched on
|
|
13
|
+
* login and re-fetched on every reconnection so a dashboard change lands without a restart. This
|
|
14
|
+
* follows that precedent rather than inventing a second pattern, and mirrors what the React UI Kit
|
|
15
|
+
* does in its own `pinSaveFeatures` module.
|
|
16
|
+
*
|
|
17
|
+
* Thread subscription is deliberately NOT here: no server flag exists for it in the SDK, so it
|
|
18
|
+
* stays a genuine integrator opt-in via ThreadSubscriptionConfig.
|
|
19
|
+
*
|
|
20
|
+
* @module PinSaveFeatureGates
|
|
21
|
+
*/
|
|
22
|
+
import { CometChat } from "@cometchat/chat-sdk-react-native";
|
|
23
|
+
import { PinSaveConfig } from "./PinSaveHelper";
|
|
24
|
+
import { PinConversationConfig } from "./PinConversationHelper";
|
|
25
|
+
|
|
26
|
+
/** Resolved state of the three server flags. */
|
|
27
|
+
export interface PinSaveFeatureFlags {
|
|
28
|
+
pinMessage: boolean;
|
|
29
|
+
saveMessage: boolean;
|
|
30
|
+
pinConversation: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Everything off — the value before resolution completes, and after any failure.
|
|
35
|
+
*
|
|
36
|
+
* Off is the safe direction: a hidden option is a missing feature, a shown one the server refuses
|
|
37
|
+
* is a broken feature. Only the second generates support tickets.
|
|
38
|
+
*/
|
|
39
|
+
export const PIN_SAVE_FEATURES_DISABLED: PinSaveFeatureFlags = Object.freeze({
|
|
40
|
+
pinMessage: false,
|
|
41
|
+
saveMessage: false,
|
|
42
|
+
pinConversation: false,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
let cached: PinSaveFeatureFlags | null = null;
|
|
46
|
+
let inFlight: Promise<PinSaveFeatureFlags> | null = null;
|
|
47
|
+
/**
|
|
48
|
+
* Bumped by reset(). A resolution records the epoch it started under and only commits if that
|
|
49
|
+
* epoch is still current, so a logout→login fast enough to overlap an in-flight read cannot have
|
|
50
|
+
* the older result land last and apply the previous app's flags to the new session.
|
|
51
|
+
*/
|
|
52
|
+
let epoch = 0;
|
|
53
|
+
|
|
54
|
+
type FlagMethod =
|
|
55
|
+
| "isPinMessageEnabled"
|
|
56
|
+
| "isSaveMessageEnabled"
|
|
57
|
+
| "isPinConversationEnabled";
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Ask the SDK for one flag, treating every failure as "off".
|
|
61
|
+
*
|
|
62
|
+
* The typeof check is not defensive noise: these methods ship with the pin/save SDK, and a kit
|
|
63
|
+
* running against an older build would otherwise throw on a missing function rather than simply
|
|
64
|
+
* not offering the feature.
|
|
65
|
+
*/
|
|
66
|
+
const resolveFlag = async (name: FlagMethod): Promise<boolean> => {
|
|
67
|
+
const fn = (CometChat as unknown as Record<string, unknown>)[name];
|
|
68
|
+
if (typeof fn !== "function") return false;
|
|
69
|
+
try {
|
|
70
|
+
return !!(await (fn as () => Promise<boolean>).call(CometChat));
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Push resolved flags into the switches the components actually read. */
|
|
77
|
+
const applyToConfigs = (flags: PinSaveFeatureFlags): void => {
|
|
78
|
+
PinSaveConfig.enablePin(flags.pinMessage);
|
|
79
|
+
PinSaveConfig.enableSave(flags.saveMessage);
|
|
80
|
+
PinConversationConfig.enable(flags.pinConversation);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Resolve all three flags and apply them, asking the SDK at most once per session.
|
|
85
|
+
*
|
|
86
|
+
* Concurrent callers share the in-flight promise — several components mounting at once resolve
|
|
87
|
+
* together rather than firing three reads each.
|
|
88
|
+
*/
|
|
89
|
+
export async function resolvePinSaveFeatures(): Promise<PinSaveFeatureFlags> {
|
|
90
|
+
if (cached) return cached;
|
|
91
|
+
if (inFlight) return inFlight;
|
|
92
|
+
|
|
93
|
+
const startedAt = epoch;
|
|
94
|
+
inFlight = Promise.all([
|
|
95
|
+
resolveFlag("isPinMessageEnabled"),
|
|
96
|
+
resolveFlag("isSaveMessageEnabled"),
|
|
97
|
+
resolveFlag("isPinConversationEnabled"),
|
|
98
|
+
])
|
|
99
|
+
.then(([pinMessage, saveMessage, pinConversation]) => {
|
|
100
|
+
const resolved: PinSaveFeatureFlags = { pinMessage, saveMessage, pinConversation };
|
|
101
|
+
if (startedAt === epoch) {
|
|
102
|
+
cached = resolved;
|
|
103
|
+
applyToConfigs(resolved);
|
|
104
|
+
}
|
|
105
|
+
return cached ?? resolved;
|
|
106
|
+
})
|
|
107
|
+
.catch(() => PIN_SAVE_FEATURES_DISABLED)
|
|
108
|
+
.finally(() => {
|
|
109
|
+
if (startedAt === epoch) inFlight = null;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return inFlight;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Re-read the flags mid-session, for a reconnection.
|
|
117
|
+
*
|
|
118
|
+
* Deliberately does NOT clear the applied values first. Clearing then re-reading would leave a
|
|
119
|
+
* window where the options vanish and come back, which looks like a glitch; the previous answer
|
|
120
|
+
* stays in force until a newer one actually arrives.
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Pull the app settings from the SERVER, replacing the SDK's cached copy.
|
|
124
|
+
*
|
|
125
|
+
* The SDK reads settings through a cache that is populated once and only re-fetched when it
|
|
126
|
+
* is empty, so everything derived from it — the three feature flags AND the pin/save caps —
|
|
127
|
+
* is frozen at whatever the app was configured with when this session logged in. The public
|
|
128
|
+
* `getAppSettings()` is the one call that goes to the network and writes that cache back.
|
|
129
|
+
*
|
|
130
|
+
* Guarded on the method existing: an older SDK build must degrade to the stale-but-working
|
|
131
|
+
* behaviour rather than throwing inside a reconnect handler.
|
|
132
|
+
*/
|
|
133
|
+
const refreshAppSettings = async (): Promise<void> => {
|
|
134
|
+
const fetchSettings = (CometChat as unknown as Record<string, unknown>).getAppSettings;
|
|
135
|
+
if (typeof fetchSettings !== "function") return;
|
|
136
|
+
try {
|
|
137
|
+
await (fetchSettings as () => Promise<unknown>).call(CometChat);
|
|
138
|
+
} catch {
|
|
139
|
+
// Offline, or the endpoint refused. The cached settings stay in place, which is exactly
|
|
140
|
+
// the behaviour this function is improving on — never worse than not having tried.
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Re-ask the server, discarding everything already known.
|
|
146
|
+
*
|
|
147
|
+
* Refreshes the app settings FIRST (ENG-38180). Without that this only re-read the SDK's
|
|
148
|
+
* cache and could never return a different answer than the last resolution — a flag or a cap
|
|
149
|
+
* changed in the dashboard reached the client only after a full logout and login.
|
|
150
|
+
*
|
|
151
|
+
* The caps ride along for free: `getPinMessageLimit()` / `getSaveMessageLimit()` read the
|
|
152
|
+
* same cache, so once it is refreshed the "you can only pin N messages" toast starts quoting
|
|
153
|
+
* the current N. That is why the limit is NOT refreshed on the error path itself — doing it
|
|
154
|
+
* here means the number is already correct by the time anything needs to print it, with no
|
|
155
|
+
* network round trip standing between the user's tap and the toast.
|
|
156
|
+
*/
|
|
157
|
+
export async function refreshPinSaveFeatures(): Promise<PinSaveFeatureFlags> {
|
|
158
|
+
await refreshAppSettings();
|
|
159
|
+
cached = null;
|
|
160
|
+
inFlight = null;
|
|
161
|
+
return resolvePinSaveFeatures();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Last resolved flags. Everything-off until the first resolution lands. */
|
|
165
|
+
export function getPinSaveFeatures(): PinSaveFeatureFlags {
|
|
166
|
+
return cached ?? PIN_SAVE_FEATURES_DISABLED;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Drop the cache and turn the switches off. Called on logout: flags are per-app, and the next
|
|
171
|
+
* login may be a different app entirely, so carrying them over would be worse than re-asking.
|
|
172
|
+
*/
|
|
173
|
+
export function resetPinSaveFeatures(): void {
|
|
174
|
+
cached = null;
|
|
175
|
+
inFlight = null;
|
|
176
|
+
epoch++;
|
|
177
|
+
applyToConfigs(PIN_SAVE_FEATURES_DISABLED);
|
|
178
|
+
}
|