@repzo/widget-react-native 1.1.0 → 1.2.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/CHANGELOG.md +64 -0
- package/README.md +92 -3
- package/lib/commonjs/api/client.js +35 -0
- package/lib/commonjs/api/client.js.map +1 -1
- package/lib/commonjs/i18n.js +14 -0
- package/lib/commonjs/i18n.js.map +1 -1
- package/lib/commonjs/index.js +2 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/locales.js +13 -0
- package/lib/commonjs/locales.js.map +1 -1
- package/lib/commonjs/push/push-notifications.js +65 -8
- package/lib/commonjs/push/push-notifications.js.map +1 -1
- package/lib/commonjs/state/widget-store.js +451 -7
- package/lib/commonjs/state/widget-store.js.map +1 -1
- package/lib/commonjs/types.js +15 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/ui/WidgetProvider.js +64 -2
- package/lib/commonjs/ui/WidgetProvider.js.map +1 -1
- package/lib/commonjs/ui/components/Post.js +1241 -0
- package/lib/commonjs/ui/components/Post.js.map +1 -0
- package/lib/commonjs/ui/utils/html.js +1 -0
- package/lib/commonjs/ui/utils/html.js.map +1 -1
- package/lib/commonjs/ui/utils/post-html.js +276 -0
- package/lib/commonjs/ui/utils/post-html.js.map +1 -0
- package/lib/commonjs/ui/utils/safe-url.js +16 -0
- package/lib/commonjs/ui/utils/safe-url.js.map +1 -1
- package/lib/module/api/client.js +35 -0
- package/lib/module/api/client.js.map +1 -1
- package/lib/module/i18n.js +14 -0
- package/lib/module/i18n.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/locales.js +13 -0
- package/lib/module/locales.js.map +1 -1
- package/lib/module/push/push-notifications.js +65 -8
- package/lib/module/push/push-notifications.js.map +1 -1
- package/lib/module/state/widget-store.js +450 -6
- package/lib/module/state/widget-store.js.map +1 -1
- package/lib/module/types.js +15 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/ui/WidgetProvider.js +65 -3
- package/lib/module/ui/WidgetProvider.js.map +1 -1
- package/lib/module/ui/components/Post.js +1236 -0
- package/lib/module/ui/components/Post.js.map +1 -0
- package/lib/module/ui/utils/html.js +1 -1
- package/lib/module/ui/utils/html.js.map +1 -1
- package/lib/module/ui/utils/post-html.js +271 -0
- package/lib/module/ui/utils/post-html.js.map +1 -0
- package/lib/module/ui/utils/safe-url.js +15 -0
- package/lib/module/ui/utils/safe-url.js.map +1 -1
- package/lib/typescript/commonjs/api/client.d.ts +21 -1
- package/lib/typescript/commonjs/api/client.d.ts.map +1 -1
- package/lib/typescript/commonjs/i18n.d.ts +13 -0
- package/lib/typescript/commonjs/i18n.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +14 -1
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/locales.d.ts.map +1 -1
- package/lib/typescript/commonjs/push/push-notifications.d.ts +26 -2
- package/lib/typescript/commonjs/push/push-notifications.d.ts.map +1 -1
- package/lib/typescript/commonjs/state/widget-store.d.ts +84 -1
- package/lib/typescript/commonjs/state/widget-store.d.ts.map +1 -1
- package/lib/typescript/commonjs/types.d.ts +77 -0
- package/lib/typescript/commonjs/types.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/WidgetProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/components/Post.d.ts +69 -0
- package/lib/typescript/commonjs/ui/components/Post.d.ts.map +1 -0
- package/lib/typescript/commonjs/ui/utils/html.d.ts +1 -0
- package/lib/typescript/commonjs/ui/utils/html.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/utils/post-html.d.ts +54 -0
- package/lib/typescript/commonjs/ui/utils/post-html.d.ts.map +1 -0
- package/lib/typescript/commonjs/ui/utils/safe-url.d.ts +6 -0
- package/lib/typescript/commonjs/ui/utils/safe-url.d.ts.map +1 -1
- package/lib/typescript/module/api/client.d.ts +21 -1
- package/lib/typescript/module/api/client.d.ts.map +1 -1
- package/lib/typescript/module/i18n.d.ts +13 -0
- package/lib/typescript/module/i18n.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +14 -1
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/locales.d.ts.map +1 -1
- package/lib/typescript/module/push/push-notifications.d.ts +26 -2
- package/lib/typescript/module/push/push-notifications.d.ts.map +1 -1
- package/lib/typescript/module/state/widget-store.d.ts +84 -1
- package/lib/typescript/module/state/widget-store.d.ts.map +1 -1
- package/lib/typescript/module/types.d.ts +77 -0
- package/lib/typescript/module/types.d.ts.map +1 -1
- package/lib/typescript/module/ui/WidgetProvider.d.ts.map +1 -1
- package/lib/typescript/module/ui/components/Post.d.ts +69 -0
- package/lib/typescript/module/ui/components/Post.d.ts.map +1 -0
- package/lib/typescript/module/ui/utils/html.d.ts +1 -0
- package/lib/typescript/module/ui/utils/html.d.ts.map +1 -1
- package/lib/typescript/module/ui/utils/post-html.d.ts +54 -0
- package/lib/typescript/module/ui/utils/post-html.d.ts.map +1 -0
- package/lib/typescript/module/ui/utils/safe-url.d.ts +6 -0
- package/lib/typescript/module/ui/utils/safe-url.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/api/client.ts +32 -0
- package/src/i18n.ts +14 -0
- package/src/index.tsx +17 -0
- package/src/locales.ts +13 -0
- package/src/push/push-notifications.ts +74 -8
- package/src/state/widget-store.ts +429 -6
- package/src/types.ts +77 -0
- package/src/ui/WidgetProvider.tsx +78 -2
- package/src/ui/components/Post.tsx +943 -0
- package/src/ui/utils/html.ts +1 -1
- package/src/ui/utils/post-html.ts +256 -0
- package/src/ui/utils/safe-url.ts +15 -0
|
@@ -25,10 +25,13 @@ import type {
|
|
|
25
25
|
ConversationRatingResult,
|
|
26
26
|
Message,
|
|
27
27
|
OutgoingAttachment,
|
|
28
|
+
PostInteractionKind,
|
|
29
|
+
PostReply,
|
|
28
30
|
RepzoWidgetErrorCode,
|
|
29
31
|
WidgetAttachment,
|
|
30
32
|
WidgetConfig,
|
|
31
33
|
WidgetCustomAttributes,
|
|
34
|
+
WidgetPost,
|
|
32
35
|
WidgetSpaceType,
|
|
33
36
|
WidgetState,
|
|
34
37
|
WidgetUser,
|
|
@@ -37,7 +40,7 @@ import type {
|
|
|
37
40
|
import { setOpenUrlHandler } from '../ui/utils/safe-url';
|
|
38
41
|
|
|
39
42
|
/** Sent on every init; must match `package.json` (tests/version.test.js checks). */
|
|
40
|
-
const SDK_VERSION = '@repzo/widget-react-native@1.1
|
|
43
|
+
const SDK_VERSION = '@repzo/widget-react-native@1.2.1';
|
|
41
44
|
|
|
42
45
|
/** The platform this session reports — mobile sessions get the Mobile SDKs overrides. */
|
|
43
46
|
function sessionPlatform(): 'ios' | 'android' | 'web' {
|
|
@@ -66,6 +69,12 @@ const STREAM_REAUTH_MIN_INTERVAL_MS = 30_000;
|
|
|
66
69
|
const MARK_READ_DEBOUNCE_MS = 1_000;
|
|
67
70
|
/** Live events held back while a draft's first send is creating its thread. */
|
|
68
71
|
const DRAFT_EVENT_BUFFER_LIMIT = 50;
|
|
72
|
+
/** How long a post stays up after a reaction, thanking them, before it goes. */
|
|
73
|
+
export const POST_THANKS_MS = 1600;
|
|
74
|
+
|
|
75
|
+
/** Ask a moment after the server's "post stops" time, never waiting longer than this per timer. */
|
|
76
|
+
const POST_RECHECK_SLACK_MS = 1_000;
|
|
77
|
+
const POST_RECHECK_MAX_MS = 6 * 60 * 60 * 1000;
|
|
69
78
|
/** Plain-http `baseUrl` is only accepted for these (plus `*.local` and `__DEV__`). */
|
|
70
79
|
const LOCAL_DEV_HOSTS = new Set(['localhost', '127.0.0.1', '10.0.2.2']);
|
|
71
80
|
|
|
@@ -96,6 +105,8 @@ const INITIAL_STATE: WidgetState = {
|
|
|
96
105
|
outboundMessage: null,
|
|
97
106
|
visitorProfile: {},
|
|
98
107
|
inAppNotification: null,
|
|
108
|
+
post: null,
|
|
109
|
+
postExpanded: false,
|
|
99
110
|
};
|
|
100
111
|
|
|
101
112
|
/** Everything in state that belongs to the signed-in visitor. */
|
|
@@ -116,6 +127,8 @@ const USER_SCOPED_STATE: Partial<WidgetState> = {
|
|
|
116
127
|
outboundMessage: null,
|
|
117
128
|
visitorProfile: {},
|
|
118
129
|
inAppNotification: null,
|
|
130
|
+
post: null,
|
|
131
|
+
postExpanded: false,
|
|
119
132
|
};
|
|
120
133
|
|
|
121
134
|
export class WidgetStore {
|
|
@@ -123,9 +136,13 @@ export class WidgetStore {
|
|
|
123
136
|
|
|
124
137
|
readonly events = new EventEmitter();
|
|
125
138
|
|
|
126
|
-
readonly push = new PushManager(
|
|
127
|
-
|
|
128
|
-
|
|
139
|
+
readonly push = new PushManager(
|
|
140
|
+
this.client,
|
|
141
|
+
(conversationId) => {
|
|
142
|
+
void this.openConversation(conversationId);
|
|
143
|
+
},
|
|
144
|
+
(campaignId) => this.openPostFromPush(campaignId)
|
|
145
|
+
);
|
|
129
146
|
|
|
130
147
|
private state: WidgetState = INITIAL_STATE;
|
|
131
148
|
|
|
@@ -145,6 +162,35 @@ export class WidgetStore {
|
|
|
145
162
|
/** `boot({ inAppNotifications })` — the card over the app for new replies. */
|
|
146
163
|
private inAppNotificationsEnabled = true;
|
|
147
164
|
|
|
165
|
+
/** `boot({ posts })` — Send posts over the app. */
|
|
166
|
+
private postsEnabled = true;
|
|
167
|
+
|
|
168
|
+
/** Posts whose view was reported this session (the server also dedupes per day). */
|
|
169
|
+
private viewedPostIds = new Set<string>();
|
|
170
|
+
|
|
171
|
+
/** Bumped per post load, so an older answer never lands over a newer one. */
|
|
172
|
+
private postRequest = 0;
|
|
173
|
+
|
|
174
|
+
/** Bumped when a live answer or a saved reply joins the post, so a load sent before it is retried, not shown. */
|
|
175
|
+
private postRevision = 0;
|
|
176
|
+
|
|
177
|
+
/** Conversations whose unread replies include one the post can't show (attachments only): opening the post leaves them unread. */
|
|
178
|
+
private postUnshownReplies = new Set<string>();
|
|
179
|
+
|
|
180
|
+
private postRecheckTimer: ReturnType<typeof setTimeout> | null = null;
|
|
181
|
+
|
|
182
|
+
/** A post push tapped before the session was ready: open it once it is. */
|
|
183
|
+
private pendingPostId: string | null = null;
|
|
184
|
+
|
|
185
|
+
/** Posts closed with × whose report failed (offline, say): kept down, and the close re-sent when they come back. */
|
|
186
|
+
private unsentDismissIds = new Set<string>();
|
|
187
|
+
|
|
188
|
+
/** Bumped when the visitor's posts are reset (logout, wipe, posts off): older post callbacks stand down. */
|
|
189
|
+
private postSession = 0;
|
|
190
|
+
|
|
191
|
+
/** A reaction's "thanks" moment: when it ends, the post goes. */
|
|
192
|
+
private postThanksTimer: ReturnType<typeof setTimeout> | null = null;
|
|
193
|
+
|
|
148
194
|
/** The launch card (see showUnreadOnLaunch) is offered once per visitor session. */
|
|
149
195
|
private launchCardOffered = false;
|
|
150
196
|
|
|
@@ -314,6 +360,17 @@ export class WidgetStore {
|
|
|
314
360
|
if (options.inAppNotifications !== undefined) {
|
|
315
361
|
this.inAppNotificationsEnabled = options.inAppNotifications !== false;
|
|
316
362
|
}
|
|
363
|
+
if (options.posts !== undefined) {
|
|
364
|
+
const wasEnabled = this.postsEnabled;
|
|
365
|
+
this.postsEnabled = options.posts !== false;
|
|
366
|
+
if (!this.postsEnabled) {
|
|
367
|
+
this.resetPost();
|
|
368
|
+
this.pendingPostId = null;
|
|
369
|
+
} else if (!wasEnabled) {
|
|
370
|
+
// A booted session returns early below, so turning posts back on asks here.
|
|
371
|
+
this.loadPost();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
317
374
|
|
|
318
375
|
const sameTarget =
|
|
319
376
|
this.bootConfig?.baseUrl === baseUrl &&
|
|
@@ -518,6 +575,8 @@ export class WidgetStore {
|
|
|
518
575
|
this.connectRealtime(result.token);
|
|
519
576
|
this.watchAppState();
|
|
520
577
|
this.loadOutboundMessage();
|
|
578
|
+
// Audiences and closed posts are per person, so every new session asks again.
|
|
579
|
+
this.loadPost();
|
|
521
580
|
// Re-runs on login switches too — the token moves to the new identity.
|
|
522
581
|
void this.push.start();
|
|
523
582
|
|
|
@@ -724,6 +783,8 @@ export class WidgetStore {
|
|
|
724
783
|
this.resetThreadTracking();
|
|
725
784
|
this.appStateSubscription?.remove();
|
|
726
785
|
this.appStateSubscription = null;
|
|
786
|
+
this.resetPost();
|
|
787
|
+
this.pendingPostId = null;
|
|
727
788
|
this.setState({ ...INITIAL_STATE });
|
|
728
789
|
|
|
729
790
|
await this.enqueueIdentity(() => this.wipeSession());
|
|
@@ -760,6 +821,7 @@ export class WidgetStore {
|
|
|
760
821
|
this.visitorTypingActive = false;
|
|
761
822
|
this.lastTypingPingAt = 0;
|
|
762
823
|
this.launchCardOffered = false;
|
|
824
|
+
this.resetPost();
|
|
763
825
|
this.setState({ ...USER_SCOPED_STATE });
|
|
764
826
|
|
|
765
827
|
const token = await this.client.getToken();
|
|
@@ -877,6 +939,8 @@ export class WidgetStore {
|
|
|
877
939
|
// and the thread on screen so replies sent meanwhile show up.
|
|
878
940
|
await Promise.all([this.refreshConversations(), this.refreshOpenThread()]);
|
|
879
941
|
await this.refreshConfig();
|
|
942
|
+
// A post may have gone live (or ended) while the app was away.
|
|
943
|
+
if (!this.isStale(epoch)) this.loadPost();
|
|
880
944
|
}
|
|
881
945
|
|
|
882
946
|
/**
|
|
@@ -922,6 +986,8 @@ export class WidgetStore {
|
|
|
922
986
|
if (this.streamConnectedOnce || this.streamErrored) {
|
|
923
987
|
void this.refreshConversations();
|
|
924
988
|
void this.refreshOpenThread();
|
|
989
|
+
// Answers under the post too.
|
|
990
|
+
this.loadPost();
|
|
925
991
|
}
|
|
926
992
|
this.streamConnectedOnce = true;
|
|
927
993
|
this.streamErrored = false;
|
|
@@ -1155,6 +1221,18 @@ export class WidgetStore {
|
|
|
1155
1221
|
message.direction !== 'inbound' &&
|
|
1156
1222
|
message.senderType !== 'visitor';
|
|
1157
1223
|
const onScreen = this.isThreadOnScreen(conversationId);
|
|
1224
|
+
// A team answer in the shown post's conversation shows under the post —
|
|
1225
|
+
// and, with the post open in front of them, counts as read there. The
|
|
1226
|
+
// post only shows text: an attachment-only answer stays unread (card,
|
|
1227
|
+
// badge) for the messenger, and so does everything after it until the
|
|
1228
|
+
// conversation is opened there, since reading moves the server cursor past it.
|
|
1229
|
+
const post = this.state.post;
|
|
1230
|
+
const inPost = isTeamReply && !!conversationId && post?.conversationId === conversationId;
|
|
1231
|
+
const answerText = (message.bodyPlain ?? '').trim();
|
|
1232
|
+
if (inPost && !this.state.unreadByConversation[conversationId]) this.postUnshownReplies.delete(conversationId);
|
|
1233
|
+
const readInPost =
|
|
1234
|
+
inPost && !!answerText && this.state.postExpanded && !this.state.isOpen && !this.state.unreadByConversation[conversationId];
|
|
1235
|
+
if (inPost && !answerText) this.postUnshownReplies.add(conversationId);
|
|
1158
1236
|
|
|
1159
1237
|
// Set inside the updater, read after: `setState` runs synchronously.
|
|
1160
1238
|
let countedAsUnread = false;
|
|
@@ -1187,7 +1265,7 @@ export class WidgetStore {
|
|
|
1187
1265
|
};
|
|
1188
1266
|
}
|
|
1189
1267
|
|
|
1190
|
-
if (!isTeamReply || !conversationId) return { conversations };
|
|
1268
|
+
if (!isTeamReply || !conversationId || readInPost) return { conversations };
|
|
1191
1269
|
|
|
1192
1270
|
const unreadByConversation = {
|
|
1193
1271
|
...current.unreadByConversation,
|
|
@@ -1217,7 +1295,14 @@ export class WidgetStore {
|
|
|
1217
1295
|
|
|
1218
1296
|
// The visitor is reading it live — move the server cursor too, or the
|
|
1219
1297
|
// next list/init would badge a reply they already saw.
|
|
1220
|
-
if (onScreen && isTeamReply && conversationId) this.scheduleMarkRead(conversationId);
|
|
1298
|
+
if ((onScreen || readInPost) && isTeamReply && conversationId) this.scheduleMarkRead(conversationId);
|
|
1299
|
+
|
|
1300
|
+
const current = this.state.post;
|
|
1301
|
+
if (inPost && answerText && current?.id === post?.id) {
|
|
1302
|
+
const answer = { from: 'team' as const, text: answerText, senderName: message.senderName, at: message.createdAt };
|
|
1303
|
+
this.postRevision += 1;
|
|
1304
|
+
this.setState({ post: { ...current, thread: [...current.thread, answer] } });
|
|
1305
|
+
}
|
|
1221
1306
|
|
|
1222
1307
|
// Emitted after the state write so a listener reading the unread count sees
|
|
1223
1308
|
// the new value. Only unread-counting replies fire it, which keeps the event
|
|
@@ -1399,6 +1484,8 @@ export class WidgetStore {
|
|
|
1399
1484
|
this.setState({ isOpen: false, view: 'bubble', agentTyping: false });
|
|
1400
1485
|
this.clearTypingTimer();
|
|
1401
1486
|
this.track('widget_closed');
|
|
1487
|
+
// A post left open behind the messenger is back in front of them.
|
|
1488
|
+
this.readPostConversation();
|
|
1402
1489
|
}
|
|
1403
1490
|
|
|
1404
1491
|
toggle(space?: WidgetSpaceType): void {
|
|
@@ -1966,9 +2053,345 @@ export class WidgetStore {
|
|
|
1966
2053
|
.catch(() => {});
|
|
1967
2054
|
}
|
|
1968
2055
|
|
|
2056
|
+
// ─── Send posts ─────────────────────────────────────────────────────────
|
|
2057
|
+
|
|
2058
|
+
/** Ask the server for the post this visitor should see now. */
|
|
2059
|
+
loadPost(): void {
|
|
2060
|
+
if (!this.postsEnabled || !this.state.booted) return;
|
|
2061
|
+
const session = this.postSession;
|
|
2062
|
+
const request = ++this.postRequest;
|
|
2063
|
+
const revision = this.postRevision;
|
|
2064
|
+
this.client
|
|
2065
|
+
.getPost()
|
|
2066
|
+
.then(({ post, recheckInMs }) => {
|
|
2067
|
+
if (this.postStale(session) || request !== this.postRequest) return;
|
|
2068
|
+
// An answer or reply joined the post meanwhile, maybe after this was
|
|
2069
|
+
// read: ask again rather than show the post without it.
|
|
2070
|
+
if (revision !== this.postRevision) {
|
|
2071
|
+
this.loadPost();
|
|
2072
|
+
return;
|
|
2073
|
+
}
|
|
2074
|
+
const hydrated = post ? this.hydratePost(post) : null;
|
|
2075
|
+
const pushed = this.pendingPostId;
|
|
2076
|
+
this.pendingPostId = null;
|
|
2077
|
+
this.schedulePostRecheck(recheckInMs, session);
|
|
2078
|
+
if (hydrated && this.unsentDismissIds.has(hydrated.id)) {
|
|
2079
|
+
// Closed already; the server only missed hearing it.
|
|
2080
|
+
this.showPost(null);
|
|
2081
|
+
this.resendPostDismiss(hydrated.id, session);
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
// A full post opens by itself unless they minimized it; a tapped push always opens it.
|
|
2085
|
+
this.showPost(hydrated, !!hydrated && ((hydrated.sentAs === 'full' && !hydrated.minimized) || hydrated.id === pushed));
|
|
2086
|
+
})
|
|
2087
|
+
// Keep whatever is on screen; the next session or foreground asks again.
|
|
2088
|
+
.catch(() => {});
|
|
2089
|
+
}
|
|
2090
|
+
|
|
2091
|
+
/**
|
|
2092
|
+
* Coerces the server's post into the shape the view relies on: missing
|
|
2093
|
+
* strings become empty, unknown enum values fall back to the plainest
|
|
2094
|
+
* option, and malformed reactions or replies are dropped.
|
|
2095
|
+
*/
|
|
2096
|
+
private hydratePost(post: WidgetPost): WidgetPost | null {
|
|
2097
|
+
if (!post || typeof post.id !== 'string' || !post.id || typeof post.bodyHtml !== 'string') return null;
|
|
2098
|
+
const text = (value: unknown) => (typeof value === 'string' ? value : '');
|
|
2099
|
+
const sender = post.sender && text(post.sender.name) ? post.sender : null;
|
|
2100
|
+
const replies = Array.isArray(post.replies)
|
|
2101
|
+
? post.replies
|
|
2102
|
+
.filter((reply) => typeof reply?.text === 'string' && reply.text.length > 0)
|
|
2103
|
+
.map((reply) => ({ text: reply.text, at: text(reply.at) }))
|
|
2104
|
+
: [];
|
|
2105
|
+
return {
|
|
2106
|
+
id: post.id,
|
|
2107
|
+
bodyHtml: post.bodyHtml,
|
|
2108
|
+
snippet: text(post.snippet),
|
|
2109
|
+
sender: sender
|
|
2110
|
+
? {
|
|
2111
|
+
name: sender.name,
|
|
2112
|
+
initials: text(sender.initials),
|
|
2113
|
+
// Teammate photos can be server-relative media paths.
|
|
2114
|
+
avatarUrl: this.client.resolveUrl(typeof sender.avatarUrl === 'string' ? sender.avatarUrl : null),
|
|
2115
|
+
}
|
|
2116
|
+
: null,
|
|
2117
|
+
workspaceName: text(post.workspaceName),
|
|
2118
|
+
format: post.format === 'large' ? 'large' : 'small',
|
|
2119
|
+
replyType: post.replyType === 'text' || post.replyType === 'reaction' ? post.replyType : 'none',
|
|
2120
|
+
reactions: Array.isArray(post.reactions)
|
|
2121
|
+
? post.reactions.filter((emoji): emoji is string => typeof emoji === 'string' && emoji.length > 0)
|
|
2122
|
+
: [],
|
|
2123
|
+
sentAs: post.sentAs === 'badge' || post.sentAs === 'full' ? post.sentAs : 'snippet',
|
|
2124
|
+
reacted: typeof post.reacted === 'string' && post.reacted ? post.reacted : null,
|
|
2125
|
+
// Older Workstation versions don't send it.
|
|
2126
|
+
minimized: post.minimized === true,
|
|
2127
|
+
replies,
|
|
2128
|
+
thread: Array.isArray(post.thread)
|
|
2129
|
+
? post.thread
|
|
2130
|
+
.filter((message) => typeof message?.text === 'string' && message.text.length > 0)
|
|
2131
|
+
.map((message) => ({
|
|
2132
|
+
from: message.from === 'team' ? ('team' as const) : ('person' as const),
|
|
2133
|
+
text: message.text,
|
|
2134
|
+
senderName: message.from === 'team' ? text(message.senderName) || null : null,
|
|
2135
|
+
at: text(message.at),
|
|
2136
|
+
}))
|
|
2137
|
+
: replies.map((reply) => ({ from: 'person' as const, text: reply.text, senderName: null, at: reply.at })),
|
|
2138
|
+
conversationId: text(post.conversationId) || null,
|
|
2139
|
+
};
|
|
2140
|
+
}
|
|
2141
|
+
|
|
2142
|
+
/**
|
|
2143
|
+
* Puts a post on screen (or takes it down). A different post starts open or
|
|
2144
|
+
* as its card per `expand`; the same one keeps what the visitor left it as,
|
|
2145
|
+
* so a full post put back to its card doesn't pop open on every foreground.
|
|
2146
|
+
*/
|
|
2147
|
+
private showPost(post: WidgetPost | null, expand = false): void {
|
|
2148
|
+
const current = this.state.post;
|
|
2149
|
+
if (current && current.id !== post?.id) this.viewedPostIds.delete(current.id);
|
|
2150
|
+
const samePost = !!post && current?.id === post.id;
|
|
2151
|
+
const expanded = !!post && (samePost ? this.state.postExpanded : expand);
|
|
2152
|
+
this.setState({ post, postExpanded: expanded });
|
|
2153
|
+
if (post && expanded && !samePost) this.reportPostOpen(post);
|
|
2154
|
+
this.readPostConversation();
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
/** The post's card or full view is on screen. Reported once each time it's shown. */
|
|
2158
|
+
postDidShow(campaignId: string): void {
|
|
2159
|
+
if (this.state.post?.id !== campaignId || this.viewedPostIds.has(campaignId)) return;
|
|
2160
|
+
this.viewedPostIds.add(campaignId);
|
|
2161
|
+
this.client.sendPostInteraction(campaignId, 'view').catch(() => {});
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
/** The visitor tapped the post's card: open it in full. */
|
|
2165
|
+
expandPost(campaignId: string): void {
|
|
2166
|
+
const post = this.state.post;
|
|
2167
|
+
if (!post || post.id !== campaignId || this.state.postExpanded) return;
|
|
2168
|
+
this.setState({ postExpanded: true });
|
|
2169
|
+
this.reportPostOpen(post);
|
|
2170
|
+
this.readPostConversation();
|
|
2171
|
+
}
|
|
2172
|
+
|
|
2173
|
+
/**
|
|
2174
|
+
* The post is open in front of them (and the messenger isn't): the team's
|
|
2175
|
+
* answers under it are read, here and on the server. Not when one of them
|
|
2176
|
+
* is something the post can't show, or the post has fewer answers since
|
|
2177
|
+
* their last reply than are unread (an attachment from before this launch,
|
|
2178
|
+
* say): those wait for the messenger.
|
|
2179
|
+
*/
|
|
2180
|
+
private readPostConversation(): void {
|
|
2181
|
+
const { post, postExpanded, isOpen, unreadByConversation } = this.state;
|
|
2182
|
+
const conversationId = post?.conversationId;
|
|
2183
|
+
const unread = conversationId ? (unreadByConversation[conversationId] ?? 0) : 0;
|
|
2184
|
+
if (!post || !conversationId || !postExpanded || isOpen || unread === 0) return;
|
|
2185
|
+
if (this.postUnshownReplies.has(conversationId)) return;
|
|
2186
|
+
let shown = 0;
|
|
2187
|
+
for (let index = post.thread.length - 1; index >= 0 && post.thread[index]?.from === 'team'; index -= 1) shown += 1;
|
|
2188
|
+
if (shown < unread) return;
|
|
2189
|
+
this.setState({ unreadByConversation: this.clearUnreadFor(conversationId) });
|
|
2190
|
+
this.events.emit(RepzoWidgetEvents.ConversationDidRead, { conversationId });
|
|
2191
|
+
this.scheduleMarkRead(conversationId);
|
|
2192
|
+
}
|
|
2193
|
+
|
|
2194
|
+
/** Back to its pill or card for now, without telling the server (see PostOverlay's onCollapse). */
|
|
2195
|
+
collapsePost(campaignId: string): void {
|
|
2196
|
+
if (this.state.post?.id !== campaignId || !this.state.postExpanded) return;
|
|
2197
|
+
this.setState({ postExpanded: false });
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2200
|
+
/**
|
|
2201
|
+
* "Not now" — the ⌄ button, a swipe down, or Android's back: the post goes
|
|
2202
|
+
* back to its badge (or snippet card) and stays there, on later launches
|
|
2203
|
+
* too, instead of opening by itself again. Unlike ×, it can be opened again.
|
|
2204
|
+
*/
|
|
2205
|
+
minimizePost(campaignId: string): void {
|
|
2206
|
+
const post = this.state.post;
|
|
2207
|
+
if (!post || post.id !== campaignId || !this.state.postExpanded) return;
|
|
2208
|
+
this.setState({ post: { ...post, minimized: true }, postExpanded: false });
|
|
2209
|
+
if (!post.minimized) this.client.sendPostInteraction(campaignId, 'minimize').catch(() => {});
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
private reportPostOpen(post: WidgetPost): void {
|
|
2213
|
+
// "Opened" is opening it from its card; a full post is opened by showing it.
|
|
2214
|
+
if (post.sentAs === 'full') return;
|
|
2215
|
+
this.client.sendPostInteraction(post.id, 'open').catch(() => {});
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
/**
|
|
2219
|
+
* The visitor tapped a link, reacted, replied to or closed the post. Closing
|
|
2220
|
+
* takes it down at once and loads the next one; a reaction shows as picked
|
|
2221
|
+
* right away. A reply resolves with what was saved (shown under the post)
|
|
2222
|
+
* and rejects when it wasn't, so the reply box can keep the text.
|
|
2223
|
+
*/
|
|
2224
|
+
async recordPostInteraction(
|
|
2225
|
+
campaignId: string,
|
|
2226
|
+
kind: Exclude<PostInteractionKind, 'view' | 'open' | 'minimize'>,
|
|
2227
|
+
value?: string
|
|
2228
|
+
): Promise<PostReply | undefined> {
|
|
2229
|
+
const post = this.state.post;
|
|
2230
|
+
if (!post || post.id !== campaignId) return undefined;
|
|
2231
|
+
const session = this.postSession;
|
|
2232
|
+
if (kind === 'dismiss') {
|
|
2233
|
+
// A load already in flight would put the closed post back.
|
|
2234
|
+
this.postRequest += 1;
|
|
2235
|
+
this.showPost(null);
|
|
2236
|
+
}
|
|
2237
|
+
if (kind === 'reaction' && value) this.setState({ post: { ...post, reacted: value } });
|
|
2238
|
+
|
|
2239
|
+
const sent = this.client.sendPostInteraction(campaignId, kind, value);
|
|
2240
|
+
// A reaction answers the post: it thanks them for a moment, then goes
|
|
2241
|
+
// (another tap in the meantime changes their pick and restarts it).
|
|
2242
|
+
if (kind === 'reaction' && value) this.startPostThanks(campaignId, sent, session);
|
|
2243
|
+
|
|
2244
|
+
let result: { hidden: boolean; reply?: PostReply; conversationId?: string | null };
|
|
2245
|
+
try {
|
|
2246
|
+
result = await sent;
|
|
2247
|
+
} catch (error) {
|
|
2248
|
+
if (kind === 'reply') throw error;
|
|
2249
|
+
if (this.postStale(session)) return undefined;
|
|
2250
|
+
// × closes it for good, so it stays down until the server hears it.
|
|
2251
|
+
if (kind === 'dismiss') this.unsentDismissIds.add(campaignId);
|
|
2252
|
+
if (kind === 'reaction') {
|
|
2253
|
+
const current = this.state.post;
|
|
2254
|
+
if (current?.id === campaignId && current.reacted === value) {
|
|
2255
|
+
this.setState({ post: { ...current, reacted: post.reacted } });
|
|
2256
|
+
// Not saved: it stays, as it was, so they can try again — unless an
|
|
2257
|
+
// earlier pick was saved, which already answered it: that one leaves.
|
|
2258
|
+
if (post.reacted) this.startPostThanks(campaignId, Promise.resolve(), session);
|
|
2259
|
+
else this.clearPostThanks();
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
return undefined;
|
|
2263
|
+
}
|
|
2264
|
+
if (this.postStale(session)) return undefined;
|
|
2265
|
+
|
|
2266
|
+
if (kind === 'reply') {
|
|
2267
|
+
// Falls back to what was typed when the response has no usable reply.
|
|
2268
|
+
const saved = result?.reply;
|
|
2269
|
+
const reply: PostReply =
|
|
2270
|
+
typeof saved?.text === 'string' && saved.text
|
|
2271
|
+
? { text: saved.text, at: typeof saved.at === 'string' ? saved.at : new Date().toISOString() }
|
|
2272
|
+
: { text: value ?? '', at: new Date().toISOString() };
|
|
2273
|
+
const current = this.state.post;
|
|
2274
|
+
if (current?.id === campaignId) {
|
|
2275
|
+
// Their first reply starts the post's conversation: answers there join the thread from now on.
|
|
2276
|
+
const conversationId =
|
|
2277
|
+
typeof result?.conversationId === 'string' && result.conversationId ? result.conversationId : current.conversationId;
|
|
2278
|
+
this.postRevision += 1;
|
|
2279
|
+
this.setState({
|
|
2280
|
+
post: {
|
|
2281
|
+
...current,
|
|
2282
|
+
replies: [...current.replies, reply],
|
|
2283
|
+
thread: [...current.thread, { from: 'person', text: reply.text, senderName: null, at: reply.at }],
|
|
2284
|
+
conversationId,
|
|
2285
|
+
},
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
// The reply went to the post's conversation: show it in the Messenger's list.
|
|
2289
|
+
void this.refreshConversations();
|
|
2290
|
+
return reply;
|
|
2291
|
+
}
|
|
2292
|
+
// A reaction's post leaves when its thanks ends (see startPostThanks).
|
|
2293
|
+
if (result?.hidden && kind !== 'reaction') {
|
|
2294
|
+
if (this.state.post?.id === campaignId) this.showPost(null);
|
|
2295
|
+
this.loadPost();
|
|
2296
|
+
}
|
|
2297
|
+
return undefined;
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
private startPostThanks(campaignId: string, sent: Promise<unknown>, session: number): void {
|
|
2301
|
+
this.clearPostThanks();
|
|
2302
|
+
this.postThanksTimer = setTimeout(() => {
|
|
2303
|
+
this.postThanksTimer = null;
|
|
2304
|
+
if (this.postStale(session) || this.state.post?.id !== campaignId) return;
|
|
2305
|
+
this.postRequest += 1;
|
|
2306
|
+
this.showPost(null);
|
|
2307
|
+
// Ask for the next one once the server has the reaction, so it doesn't send this one back.
|
|
2308
|
+
sent.then(
|
|
2309
|
+
() => {
|
|
2310
|
+
if (!this.postStale(session)) this.loadPost();
|
|
2311
|
+
},
|
|
2312
|
+
() => {
|
|
2313
|
+
if (!this.postStale(session)) this.loadPost();
|
|
2314
|
+
}
|
|
2315
|
+
);
|
|
2316
|
+
}, POST_THANKS_MS);
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
private clearPostThanks(): void {
|
|
2320
|
+
if (this.postThanksTimer) clearTimeout(this.postThanksTimer);
|
|
2321
|
+
this.postThanksTimer = null;
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
private resendPostDismiss(campaignId: string, session: number): void {
|
|
2325
|
+
this.client
|
|
2326
|
+
.sendPostInteraction(campaignId, 'dismiss')
|
|
2327
|
+
.then(() => {
|
|
2328
|
+
if (this.postStale(session)) return;
|
|
2329
|
+
this.unsentDismissIds.delete(campaignId);
|
|
2330
|
+
this.loadPost();
|
|
2331
|
+
})
|
|
2332
|
+
// Still offline: it stays down and the next load tries again.
|
|
2333
|
+
.catch(() => {});
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
/**
|
|
2337
|
+
* A post push was tapped: show that post, open in full, as soon as the
|
|
2338
|
+
* session is ready. The messenger steps aside for it, the way a reply push
|
|
2339
|
+
* opens its conversation.
|
|
2340
|
+
*/
|
|
2341
|
+
private openPostFromPush(campaignId: string): void {
|
|
2342
|
+
if (!this.postsEnabled) return;
|
|
2343
|
+
if (this.state.isOpen) this.hide();
|
|
2344
|
+
this.pendingPostId = campaignId;
|
|
2345
|
+
if (this.state.post?.id === campaignId) {
|
|
2346
|
+
this.pendingPostId = null;
|
|
2347
|
+
this.expandPost(campaignId);
|
|
2348
|
+
return;
|
|
2349
|
+
}
|
|
2350
|
+
if (this.state.booted) this.loadPost();
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2353
|
+
private clearPostRecheck(): void {
|
|
2354
|
+
if (this.postRecheckTimer) clearTimeout(this.postRecheckTimer);
|
|
2355
|
+
this.postRecheckTimer = null;
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
/** The server knows when the shown post stops; ask again then, so it leaves on time. */
|
|
2359
|
+
private schedulePostRecheck(recheckInMs: number | null | undefined, session: number): void {
|
|
2360
|
+
this.clearPostRecheck();
|
|
2361
|
+
if (typeof recheckInMs !== 'number' || !Number.isFinite(recheckInMs)) return;
|
|
2362
|
+
const delay = Math.min(Math.max(recheckInMs, 0) + POST_RECHECK_SLACK_MS, POST_RECHECK_MAX_MS);
|
|
2363
|
+
this.postRecheckTimer = setTimeout(() => {
|
|
2364
|
+
this.postRecheckTimer = null;
|
|
2365
|
+
if (!this.postStale(session)) this.loadPost();
|
|
2366
|
+
}, delay);
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
/**
|
|
2370
|
+
* Post callbacks check this, not the identity epoch: a same-user login or
|
|
2371
|
+
* an anonymous re-login bumps the epoch but keeps the session (and the post),
|
|
2372
|
+
* and would otherwise drop the post's load and strand its timers.
|
|
2373
|
+
*/
|
|
2374
|
+
private postStale(session: number): boolean {
|
|
2375
|
+
return session !== this.postSession;
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
/** The visitor changed or posts were turned off: drop it and any pending load. */
|
|
2379
|
+
private resetPost(): void {
|
|
2380
|
+
this.postSession += 1;
|
|
2381
|
+
this.clearPostRecheck();
|
|
2382
|
+
this.clearPostThanks();
|
|
2383
|
+
this.postRequest += 1;
|
|
2384
|
+
this.viewedPostIds.clear();
|
|
2385
|
+
this.unsentDismissIds.clear();
|
|
2386
|
+
this.postUnshownReplies.clear();
|
|
2387
|
+
if (this.state.post || this.state.postExpanded) this.setState({ post: null, postExpanded: false });
|
|
2388
|
+
}
|
|
2389
|
+
|
|
1969
2390
|
// ─── Teardown (tests / hot reload) ──────────────────────────────────────
|
|
1970
2391
|
|
|
1971
2392
|
destroy(): void {
|
|
2393
|
+
this.clearPostRecheck();
|
|
2394
|
+
this.clearPostThanks();
|
|
1972
2395
|
this.realtime.close();
|
|
1973
2396
|
this.clearTypingTimer();
|
|
1974
2397
|
this.resetThreadTracking();
|
package/src/types.ts
CHANGED
|
@@ -243,6 +243,13 @@ export interface BootOptions {
|
|
|
243
243
|
* `<RepzoWidgetProvider>` to be mounted.
|
|
244
244
|
*/
|
|
245
245
|
inAppNotifications?: boolean;
|
|
246
|
+
/**
|
|
247
|
+
* Show Send posts from Workstation (Send → Post, "Show on: Mobile") over the
|
|
248
|
+
* app (default true): a card that opens into the full post, with replies or
|
|
249
|
+
* reactions. Requires `<RepzoWidgetProvider>`.
|
|
250
|
+
* @since 1.2.0
|
|
251
|
+
*/
|
|
252
|
+
posts?: boolean;
|
|
246
253
|
}
|
|
247
254
|
|
|
248
255
|
/** An option in the composer's attach menu. */
|
|
@@ -569,6 +576,18 @@ export interface WidgetState {
|
|
|
569
576
|
* app. Only set while the messenger is closed and its thread is unread.
|
|
570
577
|
*/
|
|
571
578
|
inAppNotification: InAppNotification | null;
|
|
579
|
+
/**
|
|
580
|
+
* The Send post this visitor should see now (Send → Post shown on Mobile),
|
|
581
|
+
* drawn over the app. One at a time: the next one loads once this one is
|
|
582
|
+
* closed. Always `null` with `boot({ posts: false })`.
|
|
583
|
+
* @since 1.2.0
|
|
584
|
+
*/
|
|
585
|
+
post: WidgetPost | null;
|
|
586
|
+
/**
|
|
587
|
+
* The post is open in full (tapped from its card, or sent as a full post).
|
|
588
|
+
* @since 1.2.0
|
|
589
|
+
*/
|
|
590
|
+
postExpanded: boolean;
|
|
572
591
|
}
|
|
573
592
|
|
|
574
593
|
export interface InAppNotification {
|
|
@@ -580,3 +599,61 @@ export interface InAppNotification {
|
|
|
580
599
|
preview: string;
|
|
581
600
|
hasAttachments: boolean;
|
|
582
601
|
}
|
|
602
|
+
|
|
603
|
+
/** What a visitor did with a post: saw it, opened it, tapped a link, reacted, replied, minimized or closed it. @since 1.2.0 */
|
|
604
|
+
export type PostInteractionKind = 'view' | 'open' | 'click' | 'reaction' | 'reply' | 'minimize' | 'dismiss';
|
|
605
|
+
|
|
606
|
+
/** Something the visitor already replied to a post (shown under it). @since 1.2.0 */
|
|
607
|
+
export interface PostReply {
|
|
608
|
+
text: string;
|
|
609
|
+
/** ISO time. */
|
|
610
|
+
at: string;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* A Send post as Workstation sends it for this visitor: already personalized
|
|
615
|
+
* and sanitized down to paragraphs, bold/italic/code, links, lists, code
|
|
616
|
+
* blocks, a button (`div[data-post-button]`) and social links
|
|
617
|
+
* (`div[data-post-social]`). Fields the SDK doesn't recognize fall back to the
|
|
618
|
+
* plainest option.
|
|
619
|
+
* @since 1.2.0
|
|
620
|
+
*/
|
|
621
|
+
export interface WidgetPost {
|
|
622
|
+
/** The Send campaign id. */
|
|
623
|
+
id: string;
|
|
624
|
+
bodyHtml: string;
|
|
625
|
+
/** First words of the post, for its card. */
|
|
626
|
+
snippet: string;
|
|
627
|
+
sender: { name: string; initials: string; avatarUrl: string | null } | null;
|
|
628
|
+
workspaceName: string;
|
|
629
|
+
/** `small` opens as a sheet, `large` as a full screen. */
|
|
630
|
+
format: 'small' | 'large';
|
|
631
|
+
replyType: 'text' | 'reaction' | 'none';
|
|
632
|
+
reactions: string[];
|
|
633
|
+
/** `badge`: a small pill; `snippet`: a card with the first words; `full`: opens right away. */
|
|
634
|
+
sentAs: 'badge' | 'snippet' | 'full';
|
|
635
|
+
/** The reaction this visitor already picked. */
|
|
636
|
+
reacted: string | null;
|
|
637
|
+
/** What they already replied, oldest first. */
|
|
638
|
+
replies: PostReply[];
|
|
639
|
+
/** They minimized it ("not now"): it waits as its badge or card instead of opening by itself. */
|
|
640
|
+
minimized: boolean;
|
|
641
|
+
/**
|
|
642
|
+
* The post's conversation with them, oldest first: their replies and the
|
|
643
|
+
* team's answers from the inbox. Older Workstation versions send only
|
|
644
|
+
* `replies`, which then fill it.
|
|
645
|
+
*/
|
|
646
|
+
thread: PostThreadMessage[];
|
|
647
|
+
/** That conversation, to continue it in the messenger (null before their first reply). */
|
|
648
|
+
conversationId: string | null;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** One message under a post: theirs, or a teammate's answer. @since 1.2.0 */
|
|
652
|
+
export interface PostThreadMessage {
|
|
653
|
+
from: 'person' | 'team';
|
|
654
|
+
text: string;
|
|
655
|
+
/** The teammate's name on team answers. */
|
|
656
|
+
senderName: string | null;
|
|
657
|
+
/** ISO time. */
|
|
658
|
+
at: string;
|
|
659
|
+
}
|