@cometchat/chat-uikit-react-native 5.5.3 → 5.6.0
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/build.gradle +5 -0
- package/android/src/main/java/com/reactnativecometchatuikit/FileManager.java +78 -3
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +47 -4
- package/android/src/main/java/com/reactnativecometchatuikit/SafeFileName.java +78 -0
- package/android/src/main/java/com/reactnativecometchatuikit/SoundPlayer.java +209 -119
- package/ios/CCLocalFileGuard.h +19 -0
- package/ios/CCLocalFileGuard.m +34 -0
- package/ios/FileManager.m +70 -6
- package/ios/RichTextEditorView.swift +96 -1
- package/ios/SoundPlayer.m +152 -6
- package/ios/VideoManager.m +1 -1
- package/package.json +18 -3
- package/react-native-cometchat-ui-kit.podspec +1 -1
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +167 -79
- package/src/CometChatConversations/CometChatConversations.tsx +240 -117
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +4 -0
- package/src/CometChatGroups/CometChatGroups.tsx +61 -52
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +173 -85
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +7 -3
- package/src/CometChatMessageHeader/listners.ts +76 -56
- package/src/CometChatMessageList/CometChatMessageList.tsx +1423 -205
- package/src/CometChatNotificationFeed/CometChatNotificationFeed.tsx +71 -7
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +74 -2
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +69 -4
- package/src/CometChatSearch/CometChatSearch.tsx +353 -101
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +23 -1
- package/src/CometChatUsers/CometChatUsers.tsx +2 -0
- package/src/calls/CometChatCallButtons/CometChatCallButtons.tsx +19 -0
- package/src/extensions/LinkPreview/LinkPreviewBubble.tsx +48 -47
- package/src/extensions/Stickers/StickersExtensionDecorator.tsx +2 -0
- package/src/index.ts +6 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +85 -2
- package/src/shared/CometChatUiKit/UIKitSettings.ts +20 -0
- package/src/shared/events/CometChatUIEventHandler/Listener.ts +10 -0
- package/src/shared/events/index.ts +4 -0
- package/src/shared/events/offline.ts +28 -0
- package/src/shared/formatters/CometChatMentionsFormatter/CometChatMentionsFormatter.tsx +5 -3
- package/src/shared/framework/MessageDataSource.tsx +34 -14
- package/src/shared/index.ts +2 -0
- package/src/shared/offline/OfflineHub.ts +651 -0
- package/src/shared/offline/OfflineMediaBubble.tsx +49 -0
- package/src/shared/offline/OfflineViews.tsx +218 -0
- package/src/shared/offline/conversationPins.ts +137 -0
- package/src/shared/offline/conversations.ts +325 -0
- package/src/shared/offline/frameProbe.ts +24 -0
- package/src/shared/offline/gate.ts +94 -0
- package/src/shared/offline/groupActions.ts +81 -0
- package/src/shared/offline/identity.ts +162 -0
- package/src/shared/offline/index.ts +76 -0
- package/src/shared/offline/media.ts +247 -0
- package/src/shared/offline/notices.ts +90 -0
- package/src/shared/offline/reconcile.ts +296 -0
- package/src/shared/offline/searchOutcome.ts +62 -0
- package/src/shared/offline/send.ts +430 -0
- package/src/shared/offline/stateContract.ts +85 -0
- package/src/shared/offline/threadReplies.ts +125 -0
- package/src/shared/offline/twoPhase.ts +372 -0
- package/src/shared/offline/unsentActions.ts +168 -0
- package/src/shared/offline/useOfflineMediaUri.ts +87 -0
- package/src/shared/offline/useOfflineReconnect.ts +25 -0
- package/src/shared/offline/useOfflineSessionEnded.ts +22 -0
- package/src/shared/offline/useOfflineUploadTray.ts +76 -0
- package/src/shared/offline/usePresenceHidden.ts +33 -0
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +24 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +24 -1
- package/src/shared/services/UploadQueueService.ts +106 -1
- package/src/shared/utils/MessageReceiptUtils.ts +8 -0
- package/src/shared/utils/SavedMessagesHelper.ts +18 -1
- package/src/shared/utils/catchUpCursor.ts +36 -0
- package/src/shared/utils/failedMessageRetry.ts +73 -0
- package/src/shared/utils/messageIdentity.ts +38 -0
- package/src/shared/views/CometChatAttachmentViewer/CometChatAttachmentViewer.tsx +34 -5
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +10 -2
- package/src/shared/views/CometChatFileBubble/CometChatFileBubble.tsx +13 -27
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +60 -11
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +27 -2
- package/src/shared/views/CometChatList/CometChatList.tsx +468 -7
- package/src/shared/views/CometChatMediaViewer/CometChatMediaViewer.tsx +27 -5
- package/src/shared/views/CometChatMessageInput/CometChatMessageInput.tsx +1 -0
- package/src/shared/views/CometChatReactionList/CometChatReactionList.tsx +56 -10
- package/src/shared/views/CometChatStatusIndicator/CometChatStatusIndicator.tsx +4 -0
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +36 -2
- package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +24 -12
- package/src/theme/type.ts +17 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-phase reads for the offline (ON) path (README §3.3): the cached page first, then the
|
|
3
|
+
* server page, then exactly one terminal. Never used with offline off, where every list keeps
|
|
4
|
+
* today's single Promise-form fetch.
|
|
5
|
+
*
|
|
6
|
+
* One listener object carries every per-entity callback name the SDK may call
|
|
7
|
+
* (`onCachedUsers` / `onServerGroups` / …), so the same reader serves messages, conversations,
|
|
8
|
+
* users, groups, group members and banned members. The SDK still returns its Promise alongside
|
|
9
|
+
* the listener and rejects it offline; it is caught and ignored, because the listener is the
|
|
10
|
+
* channel.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
import { isNotConnectedError } from "./reconcile";
|
|
15
|
+
import { isOfflineOn } from "./gate";
|
|
16
|
+
|
|
17
|
+
export interface TwoPhaseHandlers<T> {
|
|
18
|
+
onCached(page: T[]): void;
|
|
19
|
+
onServer(page: T[]): void;
|
|
20
|
+
/** `notConnected` = an offline read past the cache: keep what is shown. */
|
|
21
|
+
onError(error: unknown, notConnected: boolean): void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const ENTITY_SUFFIXES = [
|
|
25
|
+
"Messages",
|
|
26
|
+
"Conversations",
|
|
27
|
+
"Users",
|
|
28
|
+
"Groups",
|
|
29
|
+
"GroupMembers",
|
|
30
|
+
"BannedGroupMembers",
|
|
31
|
+
"Reactions",
|
|
32
|
+
"FeedItems",
|
|
33
|
+
"NotificationFeedItems",
|
|
34
|
+
"Threads",
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Run `request.fetchNext(listener)` (or `fetchPrevious`) two-phase. Resolves once the terminal
|
|
39
|
+
* phase (server page or error) has been handled; never rejects. A `stale()` check, evaluated at
|
|
40
|
+
* each phase, drops callbacks of an operation that a newer one (or a logout) superseded.
|
|
41
|
+
*/
|
|
42
|
+
export function fetchTwoPhase<T>(
|
|
43
|
+
request: any,
|
|
44
|
+
direction: "next" | "previous",
|
|
45
|
+
handlers: TwoPhaseHandlers<T>,
|
|
46
|
+
stale: () => boolean = () => false
|
|
47
|
+
): Promise<void> {
|
|
48
|
+
return new Promise<void>((resolve) => {
|
|
49
|
+
let ended = false;
|
|
50
|
+
const end = () => {
|
|
51
|
+
if (!ended) {
|
|
52
|
+
ended = true;
|
|
53
|
+
resolve();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const onCached = (page: T[]) => {
|
|
57
|
+
if (ended || stale()) return;
|
|
58
|
+
handlers.onCached(Array.isArray(page) ? page : []);
|
|
59
|
+
};
|
|
60
|
+
const onServer = (page: T[]) => {
|
|
61
|
+
if (ended) return;
|
|
62
|
+
if (!stale()) handlers.onServer(Array.isArray(page) ? page : []);
|
|
63
|
+
end();
|
|
64
|
+
};
|
|
65
|
+
const onError = (error: unknown) => {
|
|
66
|
+
if (ended) return;
|
|
67
|
+
if (!stale()) handlers.onError(error, isNotConnectedError(error));
|
|
68
|
+
end();
|
|
69
|
+
};
|
|
70
|
+
const listener: Record<string, Function> = { onError };
|
|
71
|
+
for (const suffix of ENTITY_SUFFIXES) {
|
|
72
|
+
listener[`onCached${suffix}`] = onCached;
|
|
73
|
+
listener[`onServer${suffix}`] = onServer;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const run = direction === "previous" ? request?.fetchPrevious : request?.fetchNext;
|
|
77
|
+
if (typeof run !== "function") {
|
|
78
|
+
onError(new Error("request has no fetch method"));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const result: any = run.call(request, listener);
|
|
82
|
+
if (result && typeof result.then === "function") result.then(undefined, (error: unknown) => onError(error));
|
|
83
|
+
} catch (error) {
|
|
84
|
+
onError(error);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Apply a snapshot list's server page over what its cached page showed: server copies replace
|
|
91
|
+
* cached ones in place, cached rows the server no longer returns are dropped, new rows are
|
|
92
|
+
* appended. Rows of other pages stay.
|
|
93
|
+
*/
|
|
94
|
+
export function mergeSnapshotPage<T>(current: readonly T[], cachedKeys: ReadonlySet<string>, page: readonly T[], keyOf: (item: T) => string): T[] {
|
|
95
|
+
const serverByKey = new Map(page.map((item) => [keyOf(item), item] as const));
|
|
96
|
+
const next: T[] = [];
|
|
97
|
+
const seen = new Set<string>();
|
|
98
|
+
for (const item of current) {
|
|
99
|
+
const key = keyOf(item);
|
|
100
|
+
if (seen.has(key)) continue;
|
|
101
|
+
const fresh = serverByKey.get(key);
|
|
102
|
+
if (fresh !== undefined) {
|
|
103
|
+
next.push(fresh);
|
|
104
|
+
seen.add(key);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (cachedKeys.has(key)) continue;
|
|
108
|
+
next.push(item);
|
|
109
|
+
seen.add(key);
|
|
110
|
+
}
|
|
111
|
+
for (const item of page) {
|
|
112
|
+
const key = keyOf(item);
|
|
113
|
+
if (!seen.has(key)) {
|
|
114
|
+
next.push(item);
|
|
115
|
+
seen.add(key);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return next;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Append `page` to `current`, skipping keys already shown (cached page of a later page). */
|
|
122
|
+
export function appendUnique<T>(current: readonly T[], page: readonly T[], keyOf: (item: T) => string): T[] {
|
|
123
|
+
const seen = new Set(current.map(keyOf));
|
|
124
|
+
const next = current.slice();
|
|
125
|
+
for (const item of page) {
|
|
126
|
+
const key = keyOf(item);
|
|
127
|
+
if (!seen.has(key)) {
|
|
128
|
+
next.push(item);
|
|
129
|
+
seen.add(key);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return next;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface SingleTwoPhaseHandlers<T> {
|
|
136
|
+
/** Fires only on a cache hit. */
|
|
137
|
+
onCached?(item: T): void;
|
|
138
|
+
onServer(item: T): void;
|
|
139
|
+
onError(error: unknown, notConnected: boolean): void;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A two-phase single read (`getMessageDetails(id, listener)`, `getConversation(with, type,
|
|
144
|
+
* listener)`; BEH-READ-28/32/35): the cached copy on a hit, then the server copy, then exactly
|
|
145
|
+
* one terminal. `run` calls the SDK with the listener; its returned Promise is caught and only
|
|
146
|
+
* used as a terminal when the listener gave none. Never rejects.
|
|
147
|
+
*/
|
|
148
|
+
export function getTwoPhase<T>(
|
|
149
|
+
run: (listener: Record<string, Function>) => unknown,
|
|
150
|
+
entity: "Message" | "Conversation",
|
|
151
|
+
handlers: SingleTwoPhaseHandlers<T>,
|
|
152
|
+
stale: () => boolean = () => false
|
|
153
|
+
): Promise<void> {
|
|
154
|
+
return new Promise<void>((resolve) => {
|
|
155
|
+
let ended = false;
|
|
156
|
+
const end = () => {
|
|
157
|
+
if (!ended) {
|
|
158
|
+
ended = true;
|
|
159
|
+
resolve();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const onServer = (item: T) => {
|
|
163
|
+
if (ended) return;
|
|
164
|
+
if (!stale()) handlers.onServer(item);
|
|
165
|
+
end();
|
|
166
|
+
};
|
|
167
|
+
const onError = (error: unknown) => {
|
|
168
|
+
if (ended) return;
|
|
169
|
+
if (!stale()) handlers.onError(error, isNotConnectedError(error));
|
|
170
|
+
end();
|
|
171
|
+
};
|
|
172
|
+
const listener: Record<string, Function> = {
|
|
173
|
+
[`onCached${entity}`]: (item: T | undefined) => {
|
|
174
|
+
if (ended || stale() || item === undefined || item === null) return;
|
|
175
|
+
handlers.onCached?.(item);
|
|
176
|
+
},
|
|
177
|
+
[`onServer${entity}`]: onServer,
|
|
178
|
+
onError,
|
|
179
|
+
};
|
|
180
|
+
try {
|
|
181
|
+
const result: any = run(listener);
|
|
182
|
+
if (result && typeof result.then === "function") {
|
|
183
|
+
result.then(
|
|
184
|
+
(value: T) => {
|
|
185
|
+
// An SDK that ignored the listener still answers through its Promise.
|
|
186
|
+
if (!ended) onServer(value);
|
|
187
|
+
},
|
|
188
|
+
(error: unknown) => onError(error)
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
onError(error);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface AroundReadHandlers<M> {
|
|
198
|
+
/**
|
|
199
|
+
* Show the anchored window: the target and the page after it. `phase` is "cached" for the
|
|
200
|
+
* first paint from the device, "server" for the server's copy (which replaces it).
|
|
201
|
+
*/
|
|
202
|
+
onPaint(target: M, next: M[], phase: "cached" | "server"): void;
|
|
203
|
+
/** Nothing could be shown: `notConnected` = offline with the target not cached. */
|
|
204
|
+
onError(error: unknown, notConnected: boolean): void;
|
|
205
|
+
/**
|
|
206
|
+
* The cached window is on screen (painted) and the server's answer failed: the rows stay, and
|
|
207
|
+
* the caller reports the error once (D83 rule 2).
|
|
208
|
+
*/
|
|
209
|
+
onKeptRowsError?(error: unknown): void;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Jump to a message / start from the first unread on the offline (ON) path (BEH-READ-29,
|
|
214
|
+
* UIT-READ-08/09): the target (`getMessageDetails`) and the page after it (`fetchNext` anchored
|
|
215
|
+
* on it), both two-phase. The cached pair paints first when both are on the device; the server
|
|
216
|
+
* pair replaces it. Offline, a cached pair stays on screen with no error; a target that was never
|
|
217
|
+
* cached ends with `onError(..., true)`. Resolves when both reads ended; never rejects.
|
|
218
|
+
*/
|
|
219
|
+
export function readAroundTwoPhase<M>(
|
|
220
|
+
getTarget: (listener: Record<string, Function>) => unknown,
|
|
221
|
+
nextRequest: any,
|
|
222
|
+
handlers: AroundReadHandlers<M>,
|
|
223
|
+
stale: () => boolean = () => false
|
|
224
|
+
): Promise<void> {
|
|
225
|
+
let cachedTarget: M | undefined;
|
|
226
|
+
let serverTarget: M | undefined;
|
|
227
|
+
let targetError: unknown;
|
|
228
|
+
let nextError: unknown;
|
|
229
|
+
let cachedNext: M[] | undefined;
|
|
230
|
+
let serverNext: M[] | undefined;
|
|
231
|
+
let targetDone = false;
|
|
232
|
+
let nextDone = false;
|
|
233
|
+
let paintedCached = false;
|
|
234
|
+
let finished = false;
|
|
235
|
+
|
|
236
|
+
const step = () => {
|
|
237
|
+
if (finished || stale()) return;
|
|
238
|
+
if (!paintedCached && cachedTarget !== undefined && (cachedNext !== undefined || nextDone)) {
|
|
239
|
+
paintedCached = true;
|
|
240
|
+
handlers.onPaint(cachedTarget, cachedNext ?? serverNext ?? [], "cached");
|
|
241
|
+
}
|
|
242
|
+
if (!targetDone || !nextDone) return;
|
|
243
|
+
finished = true;
|
|
244
|
+
if (serverTarget !== undefined && serverNext !== undefined) {
|
|
245
|
+
handlers.onPaint(serverTarget, serverNext, "server");
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (paintedCached) {
|
|
249
|
+
// Past the server: keep the cached window, and say the server read failed.
|
|
250
|
+
handlers.onKeptRowsError?.(targetError ?? nextError);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const target = serverTarget ?? cachedTarget;
|
|
254
|
+
if (target === undefined) {
|
|
255
|
+
handlers.onError(targetError, isNotConnectedError(targetError));
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
handlers.onPaint(target, serverNext ?? cachedNext ?? [], serverTarget !== undefined && serverNext !== undefined ? "server" : "cached");
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const target = getTwoPhase<M>(
|
|
262
|
+
getTarget,
|
|
263
|
+
"Message",
|
|
264
|
+
{
|
|
265
|
+
onCached: (m) => {
|
|
266
|
+
cachedTarget = m;
|
|
267
|
+
step();
|
|
268
|
+
},
|
|
269
|
+
onServer: (m) => {
|
|
270
|
+
if (m === undefined || m === null) targetError = new Error("message not found");
|
|
271
|
+
else serverTarget = m;
|
|
272
|
+
},
|
|
273
|
+
onError: (error) => {
|
|
274
|
+
targetError = error;
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
stale
|
|
278
|
+
).then(() => {
|
|
279
|
+
targetDone = true;
|
|
280
|
+
step();
|
|
281
|
+
});
|
|
282
|
+
const next = fetchTwoPhase<M>(
|
|
283
|
+
nextRequest,
|
|
284
|
+
"next",
|
|
285
|
+
{
|
|
286
|
+
onCached: (page) => {
|
|
287
|
+
cachedNext = page;
|
|
288
|
+
step();
|
|
289
|
+
},
|
|
290
|
+
onServer: (page) => {
|
|
291
|
+
serverNext = page;
|
|
292
|
+
},
|
|
293
|
+
onError: (error) => {
|
|
294
|
+
// the cached page (if any) stays; the target decides what is shown
|
|
295
|
+
nextError = error;
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
stale
|
|
299
|
+
).then(() => {
|
|
300
|
+
nextDone = true;
|
|
301
|
+
step();
|
|
302
|
+
});
|
|
303
|
+
return Promise.all([target, next]).then(() => undefined);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* A two-phase single read for a caller that needs one value (an anchor id, an unread count): the
|
|
308
|
+
* server copy when the server answers; offline (not-connected), the cached copy when there was a
|
|
309
|
+
* hit; otherwise the error, as the Promise form would give it (UIT-LIFE-11: a chat opened by id
|
|
310
|
+
* offline opens from the cache, or shows today's error when it was never cached).
|
|
311
|
+
*/
|
|
312
|
+
export function resolveTwoPhase<T>(run: (listener: Record<string, Function>) => unknown, entity: "Message" | "Conversation"): Promise<T> {
|
|
313
|
+
return new Promise<T>((resolve, reject) => {
|
|
314
|
+
let cached: T | undefined;
|
|
315
|
+
getTwoPhase<T>(run, entity, {
|
|
316
|
+
onCached: (item) => {
|
|
317
|
+
cached = item;
|
|
318
|
+
},
|
|
319
|
+
onServer: (item) => resolve(item),
|
|
320
|
+
onError: (error, notConnected) => {
|
|
321
|
+
if (notConnected && cached !== undefined) resolve(cached);
|
|
322
|
+
else reject(error);
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export interface ListPage<T> {
|
|
329
|
+
rows: T[];
|
|
330
|
+
/** Offline past the cache: the rows (possibly none) are the device's; more needs a connection. */
|
|
331
|
+
offline: boolean;
|
|
332
|
+
/** With `offline`: the not-connected error the server read ended with (D83: reported to `onError`). */
|
|
333
|
+
error?: unknown;
|
|
334
|
+
/** With `offline`: the SDK gave a cached phase (possibly empty) before the error. */
|
|
335
|
+
cachedDelivered?: boolean;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* One page of a panel list (pinned, saved, reactions, notification feed; README §3.9 U6). The gate
|
|
340
|
+
* is read once for the read. Offline OFF: today's Promise-form fetch, unchanged. Offline ON: the
|
|
341
|
+
* SDK's two-phase read: the server page when it answers, otherwise (not connected) the cached
|
|
342
|
+
* page with `offline: true`, so the panel shows what the device has instead of an error. Any
|
|
343
|
+
* other error rejects as today. `onCached` lets a panel paint the cached page first.
|
|
344
|
+
*/
|
|
345
|
+
export function readListPage<T>(
|
|
346
|
+
request: any,
|
|
347
|
+
direction: "next" | "previous",
|
|
348
|
+
options: { onCached?(rows: T[]): void } = {}
|
|
349
|
+
): Promise<ListPage<T>> {
|
|
350
|
+
if (!isOfflineOn()) {
|
|
351
|
+
const run = direction === "previous" ? request?.fetchPrevious : request?.fetchNext;
|
|
352
|
+
return Promise.resolve(run.call(request)).then((rows: T[]) => ({ rows: Array.isArray(rows) ? rows : [], offline: false }));
|
|
353
|
+
}
|
|
354
|
+
return new Promise<ListPage<T>>((resolve, reject) => {
|
|
355
|
+
let cached: T[] | null = null;
|
|
356
|
+
fetchTwoPhase<T>(request, direction, {
|
|
357
|
+
onCached: (rows) => {
|
|
358
|
+
cached = rows;
|
|
359
|
+
try {
|
|
360
|
+
options.onCached?.(rows);
|
|
361
|
+
} catch {
|
|
362
|
+
/* a paint error must not break the read */
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
onServer: (rows) => resolve({ rows, offline: false }),
|
|
366
|
+
onError: (error, notConnected) => {
|
|
367
|
+
if (notConnected) resolve({ rows: cached ?? [], offline: true, error, cachedDelivered: cached !== null });
|
|
368
|
+
else reject(error);
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an own UNSENT row offers, and what Delete does (BEH-SEND-36; ruling R162 / S73, frozen
|
|
3
|
+
* 2026-11-03, which rewrote BEH-SEND-37 and supersedes UIK-SEND-05's "Copy, no Delete" and the
|
|
4
|
+
* earlier "Delete only after a permanent failure" and "nothing while SENDING" rules).
|
|
5
|
+
*
|
|
6
|
+
* On the sender's own row that has no server id (offline ON path only), a UI kit offers Retry and
|
|
7
|
+
* Delete on every PENDING, SENDING and FAILED (any reason) message, and on a failed row kept in
|
|
8
|
+
* memory that never entered the outbox:
|
|
9
|
+
*
|
|
10
|
+
* | Row | Menu |
|
|
11
|
+
* |-------------------------------------------------------|-----------------|
|
|
12
|
+
* | PENDING | Retry, Delete |
|
|
13
|
+
* | SENDING (on the wire) | Retry, Delete |
|
|
14
|
+
* | FAILED, any reason (TRANSIENT, PERMANENT, unknown, …) | Retry, Delete |
|
|
15
|
+
* | failed (direct), kept in memory, never in the outbox | Retry, Delete |
|
|
16
|
+
*
|
|
17
|
+
* The SDK decides whether it can act: on a message whose send is under way it answers
|
|
18
|
+
* `ERROR_OFFLINE_MESSAGE_IN_FLIGHT` to Retry and to Delete and changes nothing, and the kit shows
|
|
19
|
+
* the short "Sending… try again in a moment" notice (`OfflineHub.raiseSendInFlightNotice`). The
|
|
20
|
+
* kit keeps no in-flight state of its own to hide the menu.
|
|
21
|
+
*
|
|
22
|
+
* Nothing else: no Copy, reply, thread, react, edit, pin, save, mark unread, info, translate,
|
|
23
|
+
* report or forward (they need a server id). With offline off nothing here is reached and the
|
|
24
|
+
* menu is today's.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
import { offlineSdk, sdkConnected } from "./gate";
|
|
29
|
+
import {
|
|
30
|
+
failureReasonOf,
|
|
31
|
+
isOfflineUnsent,
|
|
32
|
+
localIdOf,
|
|
33
|
+
OfflineMessageStatus,
|
|
34
|
+
serverIdOf,
|
|
35
|
+
statusOf,
|
|
36
|
+
} from "./identity";
|
|
37
|
+
import { OfflineHub } from "./OfflineHub";
|
|
38
|
+
|
|
39
|
+
export type UnsentRowState = "pending" | "sending" | "failed";
|
|
40
|
+
export type UnsentAction = "retry" | "delete";
|
|
41
|
+
|
|
42
|
+
export interface UnsentRowInfo {
|
|
43
|
+
state: UnsentRowState;
|
|
44
|
+
/** `MessageFailureReason` of a failed row, `""` when unknown. */
|
|
45
|
+
reason: string;
|
|
46
|
+
/** Failed without entering the outbox: Retry is a fresh send, Delete removes it from the screen. */
|
|
47
|
+
direct: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The SDK's offline error codes a Delete can be refused with (R160). */
|
|
51
|
+
export const DISCARD_ERROR = Object.freeze({
|
|
52
|
+
inFlight: "ERROR_OFFLINE_MESSAGE_IN_FLIGHT",
|
|
53
|
+
notFound: "ERROR_OFFLINE_MESSAGE_NOT_FOUND",
|
|
54
|
+
disabled: "ERROR_OFFLINE_DISABLED",
|
|
55
|
+
/** A queued send ended without being sent: the session ended or the cache was reset (Android `ERROR_DISCARDED`). */
|
|
56
|
+
discarded: "ERROR_OFFLINE_MESSAGE_DISCARDED",
|
|
57
|
+
/** The SDK's error for a send made with nobody logged in (Android `ERROR_NOT_LOGGED_IN`). */
|
|
58
|
+
notLoggedIn: "ERROR_USER_NOT_LOGGED_IN",
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The unsent state of an own row, or `null` when it is not an unsent row (it has a server id, or
|
|
63
|
+
* the offline path never touched it). The hub's last reported state wins over the row's status,
|
|
64
|
+
* because a per-send callback can hand over a message object whose status was not updated.
|
|
65
|
+
*/
|
|
66
|
+
export function unsentRowInfo(message: any, hub: OfflineHub = OfflineHub.getInstance()): UnsentRowInfo | null {
|
|
67
|
+
if (!message || serverIdOf(message) > 0) return null;
|
|
68
|
+
const directReason = hub.directFailureOf(message);
|
|
69
|
+
if (directReason !== undefined) return { state: "failed", reason: directReason, direct: true };
|
|
70
|
+
if (!isOfflineUnsent(message)) return null;
|
|
71
|
+
const localMessageId = localIdOf(message);
|
|
72
|
+
const status = statusOf(message);
|
|
73
|
+
const reported = hub.stateOf(localMessageId);
|
|
74
|
+
const reasonOf = () => failureReasonOf(message) || hub.failureReasonFor(localMessageId);
|
|
75
|
+
if (reported === "failed") return { state: "failed", reason: reasonOf(), direct: false };
|
|
76
|
+
if (reported === "sending") {
|
|
77
|
+
return { state: status === OfflineMessageStatus.sending ? "sending" : "pending", reason: "", direct: false };
|
|
78
|
+
}
|
|
79
|
+
if (status === OfflineMessageStatus.failed) return { state: "failed", reason: reasonOf(), direct: false };
|
|
80
|
+
if (status === OfflineMessageStatus.sending) return { state: "sending", reason: "", direct: false };
|
|
81
|
+
return { state: "pending", reason: "", direct: false };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The menu for an unsent row (R162 / S73): Retry and Delete on every unsent row, whatever its
|
|
86
|
+
* state or failure reason. Empty = not an unsent row, today's menu.
|
|
87
|
+
*/
|
|
88
|
+
export function unsentActionsOf(info: UnsentRowInfo | null): UnsentAction[] {
|
|
89
|
+
return info ? ["retry", "delete"] : [];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Whether the row offers Retry right now. */
|
|
93
|
+
export function canRetryUnsent(message: any, hub: OfflineHub = OfflineHub.getInstance()): boolean {
|
|
94
|
+
return unsentActionsOf(unsentRowInfo(message, hub)).includes("retry");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type DeleteUnsentOutcome =
|
|
98
|
+
/** Gone from the device (or the screen, for a direct failure); the row is removed. */
|
|
99
|
+
| { kind: "removed" }
|
|
100
|
+
/** The send is on the wire: the row stays; the "Sending… try again in a moment" notice is up (R162). */
|
|
101
|
+
| { kind: "in_flight"; error: unknown }
|
|
102
|
+
/** The message was sent: delete it on the server through today's path. */
|
|
103
|
+
| { kind: "sent"; serverId: number }
|
|
104
|
+
/** The message was sent but the device is offline: today's not-connected error. */
|
|
105
|
+
| { kind: "not_connected"; error: unknown }
|
|
106
|
+
/** Offline was switched off (kill switch): the row is failed (direct) now. */
|
|
107
|
+
| { kind: "disabled"; error: unknown }
|
|
108
|
+
/** Any other refusal (store blip): the row stays; show today's error. */
|
|
109
|
+
| { kind: "error"; error: unknown };
|
|
110
|
+
|
|
111
|
+
/** The SDK error code of a refusal (`code`, else `name`), or `""`. */
|
|
112
|
+
export function errorCodeOf(error: unknown): string {
|
|
113
|
+
const code = (error as any)?.code ?? (error as any)?.name;
|
|
114
|
+
return typeof code === "string" ? code : "";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Delete an unsent row (BEH-SEND-36, R162). An outbox row is discarded on the device with
|
|
119
|
+
* `discardLocalMessage(localMessageId)` (works offline, durable; the SDK moves the conversation
|
|
120
|
+
* preview itself and fires no outbox event), then removed from the screen. A direct failure has
|
|
121
|
+
* no outbox row and is removed from the screen only. Never the server delete for a row with no
|
|
122
|
+
* server id. A row whose send is under way is refused by the SDK (`ERROR_OFFLINE_MESSAGE_IN_FLIGHT`,
|
|
123
|
+
* nothing changes) and raises the "Sending… try again in a moment" notice. Never rejects.
|
|
124
|
+
*/
|
|
125
|
+
export async function deleteUnsentMessage(message: any, hub: OfflineHub = OfflineHub.getInstance()): Promise<DeleteUnsentOutcome> {
|
|
126
|
+
const localMessageId = localIdOf(message);
|
|
127
|
+
const info = unsentRowInfo(message, hub);
|
|
128
|
+
if (info?.direct || !localMessageId) {
|
|
129
|
+
hub.reportDiscardedByUser(message);
|
|
130
|
+
return { kind: "removed" };
|
|
131
|
+
}
|
|
132
|
+
const sdk = offlineSdk();
|
|
133
|
+
if (typeof sdk?.discardLocalMessage !== "function") {
|
|
134
|
+
return { kind: "error", error: new Error("discardLocalMessage is not available") };
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
await sdk.discardLocalMessage(localMessageId);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
switch (errorCodeOf(error)) {
|
|
140
|
+
case DISCARD_ERROR.inFlight:
|
|
141
|
+
hub.raiseSendInFlightNotice(message);
|
|
142
|
+
return { kind: "in_flight", error };
|
|
143
|
+
case DISCARD_ERROR.notFound: {
|
|
144
|
+
// The SDK no longer holds it: it was sent, or purged with its chat. A row with a server id
|
|
145
|
+
// is a sent message: today's server delete (offline, today's not-connected error). Any
|
|
146
|
+
// other row is removed at once, online or offline, as on Android; if its sent copy still
|
|
147
|
+
// arrives, it is deleted on the server then.
|
|
148
|
+
const serverId = serverIdOf(message) || hub.sentIdFor(localMessageId);
|
|
149
|
+
if (serverId) return sdkConnected() === false ? { kind: "not_connected", error } : { kind: "sent", serverId };
|
|
150
|
+
hub.reportDiscardedByUser(message);
|
|
151
|
+
hub.deleteWhenSent(localMessageId);
|
|
152
|
+
return { kind: "removed" };
|
|
153
|
+
}
|
|
154
|
+
case DISCARD_ERROR.disabled:
|
|
155
|
+
return { kind: "disabled", error };
|
|
156
|
+
default:
|
|
157
|
+
return { kind: "error", error };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
hub.reportDiscardedByUser(message);
|
|
161
|
+
return { kind: "removed" };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Option ids of the unsent-row menu (stable for integrators' tests and the state contract). */
|
|
165
|
+
export const OFFLINE_OPTION_IDS = Object.freeze({
|
|
166
|
+
retry: "offlineRetryMessage",
|
|
167
|
+
delete: "offlineDeleteMessage",
|
|
168
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React adapter for {@link offlineAttachmentUri} and the playable shim (README §3.8).
|
|
3
|
+
*
|
|
4
|
+
* With offline off it returns `attachment.getUrl()` and does nothing else: its effect returns at
|
|
5
|
+
* once (no lookup, no listener, no timer). On the ON path it starts from the SDK's cached copy (or
|
|
6
|
+
* the URL), swaps in a playable copy when `playable` is set, and re-resolves when the SDK reports
|
|
7
|
+
* that this message's media finished downloading.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
import { useEffect, useRef, useState } from "react";
|
|
12
|
+
import { cachedPathOf, isMediaOutOfReach, offlineAttachmentUri, playableUri, shouldUseCachedMedia } from "./media";
|
|
13
|
+
import { OfflineHub } from "./OfflineHub";
|
|
14
|
+
|
|
15
|
+
function urlKeyOf(url: string): string {
|
|
16
|
+
return String(url ?? "").split("?")[0];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useOfflineMediaUri(attachment: any, options: { playable?: boolean; uid?: string } = {}): string {
|
|
20
|
+
const initial = offlineAttachmentUri(attachment);
|
|
21
|
+
// Keyed by the attachment's URL, never by the object: a receipt, a reaction or an ack clones the
|
|
22
|
+
// message and its attachments, and a clone of the same media must keep its resolved source (a
|
|
23
|
+
// playing video or voice note would restart, an image would reload).
|
|
24
|
+
const [resolved, setResolved] = useState<{ key: string; uri: string } | null>(null);
|
|
25
|
+
const remote = attachment?.getUrl?.();
|
|
26
|
+
const remoteKey = urlKeyOf(remote);
|
|
27
|
+
const attachmentRef = useRef(attachment);
|
|
28
|
+
attachmentRef.current = attachment;
|
|
29
|
+
// The SDK's cached file as of this render: a string, so a clone with the same file changes nothing.
|
|
30
|
+
const cachedNow = shouldUseCachedMedia() ? cachedPathOf(attachment) : "";
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
// Offline off: today's URL, nothing else.
|
|
34
|
+
if (!shouldUseCachedMedia()) return;
|
|
35
|
+
let cancelled = false;
|
|
36
|
+
const apply = (cached: string) => {
|
|
37
|
+
if (!cached) return;
|
|
38
|
+
if (!options.playable) {
|
|
39
|
+
if (!cancelled) setResolved((prev) => (prev && prev.key === remoteKey && prev.uri === cached ? prev : { key: remoteKey, uri: cached }));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
playableUri(cached, attachmentRef.current, options.uid ?? OfflineHub.getInstance().currentUid ?? "").then((uri) => {
|
|
43
|
+
if (!cancelled) setResolved((prev) => (prev && prev.key === remoteKey && prev.uri === uri ? prev : { key: remoteKey, uri }));
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
apply(cachedNow);
|
|
47
|
+
const subscription = OfflineHub.getInstance().mediaReady$.subscribe((event) => {
|
|
48
|
+
if (!event.localPath || urlKeyOf(event.urlKey) !== remoteKey) return;
|
|
49
|
+
apply(event.localPath.startsWith("/") ? `file://${event.localPath}` : event.localPath);
|
|
50
|
+
});
|
|
51
|
+
return () => {
|
|
52
|
+
cancelled = true;
|
|
53
|
+
subscription.unsubscribe();
|
|
54
|
+
};
|
|
55
|
+
}, [remoteKey, cachedNow, options.playable, options.uid]);
|
|
56
|
+
|
|
57
|
+
return resolved && resolved.key === remoteKey ? resolved.uri : initial;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether this attachment is out of reach right now (`isMediaOutOfReach`), kept current across a
|
|
62
|
+
* reconnect and a finished download. Offline off: always `false`, with no subscription.
|
|
63
|
+
*/
|
|
64
|
+
export function useMediaOutOfReach(attachment: any): boolean {
|
|
65
|
+
const [outOfReach, setOutOfReach] = useState(() => isMediaOutOfReach(attachment));
|
|
66
|
+
const remote = attachment?.getUrl?.();
|
|
67
|
+
const remoteKey = urlKeyOf(remote);
|
|
68
|
+
// By URL and cached file, not object identity: a clone of the same media re-subscribes to nothing.
|
|
69
|
+
const attachmentRef = useRef(attachment);
|
|
70
|
+
attachmentRef.current = attachment;
|
|
71
|
+
const cachedNow = shouldUseCachedMedia() ? cachedPathOf(attachment) : "";
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (!shouldUseCachedMedia()) return;
|
|
74
|
+
const hub = OfflineHub.getInstance();
|
|
75
|
+
const update = () => setOutOfReach(isMediaOutOfReach(attachmentRef.current));
|
|
76
|
+
update();
|
|
77
|
+
const subscriptions = [
|
|
78
|
+
hub.connected$.subscribe(update),
|
|
79
|
+
hub.disconnected$.subscribe(update),
|
|
80
|
+
hub.mediaReady$.subscribe((event) => {
|
|
81
|
+
if (urlKeyOf(event.urlKey) === remoteKey) setOutOfReach(false);
|
|
82
|
+
}),
|
|
83
|
+
];
|
|
84
|
+
return () => subscriptions.forEach((s) => s.unsubscribe());
|
|
85
|
+
}, [remoteKey, cachedNow]);
|
|
86
|
+
return outOfReach;
|
|
87
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For a screen that holds a list of its own, outside `CometChatList` and the message list: run
|
|
3
|
+
* `onConnected` each time the SDK's connection comes back (D83 rule 4: a list that shows its error
|
|
4
|
+
* view because the device was offline reloads once by itself on reconnect). Only a screen that
|
|
5
|
+
* opened with offline on listens; with offline off it never runs, so those screens stay today's.
|
|
6
|
+
* Each reconnect reads the gate again, so a kill switch that turned offline off since the screen
|
|
7
|
+
* opened stops the reload (Android `OfflineReconnectRefill.onConnected`).
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
import { useEffect, useRef } from "react";
|
|
12
|
+
import { isOfflineOn } from "./gate";
|
|
13
|
+
import { OfflineHub } from "./OfflineHub";
|
|
14
|
+
|
|
15
|
+
export function useOfflineReconnect(onConnected: () => void): void {
|
|
16
|
+
const onConnectedRef = useRef(onConnected);
|
|
17
|
+
onConnectedRef.current = onConnected;
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (!isOfflineOn()) return;
|
|
20
|
+
const subscription = OfflineHub.getInstance().connected$.subscribe(() => {
|
|
21
|
+
if (isOfflineOn()) onConnectedRef.current();
|
|
22
|
+
});
|
|
23
|
+
return () => subscription.unsubscribe();
|
|
24
|
+
}, []);
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For a screen that holds rows of its own, outside `CometChatList` and the message list: run
|
|
3
|
+
* `onEnded` when the session ends (logout, user switch, cache reset; UIK-LIFE-01), so nothing of it
|
|
4
|
+
* shows afterwards. Only a screen that opened with offline on reacts: the hub's login listener also
|
|
5
|
+
* hears a logout with offline off, and that screen stays today's.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
import { useEffect, useRef } from "react";
|
|
10
|
+
import { isOfflineOn } from "./gate";
|
|
11
|
+
import { OfflineHub } from "./OfflineHub";
|
|
12
|
+
|
|
13
|
+
export function useOfflineSessionEnded(onEnded: () => void): void {
|
|
14
|
+
const onEndedRef = useRef(onEnded);
|
|
15
|
+
onEndedRef.current = onEnded;
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const usedOffline = isOfflineOn();
|
|
18
|
+
if (!usedOffline) return;
|
|
19
|
+
const subscription = OfflineHub.getInstance().session$.subscribe(() => onEndedRef.current());
|
|
20
|
+
return () => subscription.unsubscribe();
|
|
21
|
+
}, []);
|
|
22
|
+
}
|