turbodesk-livechat-react-native 0.1.0-alpha.3 → 0.1.0-alpha.30
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 +50 -0
- package/dist/api/conversation-api.d.ts +9 -0
- package/dist/api/conversation-api.d.ts.map +1 -1
- package/dist/api/conversation-api.js +15 -1
- package/dist/api/conversation-api.js.map +1 -1
- package/dist/hooks/use-live-chat.d.ts +1 -0
- package/dist/hooks/use-live-chat.d.ts.map +1 -1
- package/dist/hooks/use-live-chat.js +2 -0
- package/dist/hooks/use-live-chat.js.map +1 -1
- package/dist/hooks/use-send-message.d.ts +1 -0
- package/dist/hooks/use-send-message.d.ts.map +1 -1
- package/dist/hooks/use-send-message.js +20 -14
- package/dist/hooks/use-send-message.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/navigation/LiveChatPanel.d.ts.map +1 -1
- package/dist/navigation/LiveChatPanel.js +20 -1
- package/dist/navigation/LiveChatPanel.js.map +1 -1
- package/dist/provider/LiveChatContext.d.ts.map +1 -1
- package/dist/provider/LiveChatContext.js +2 -0
- package/dist/provider/LiveChatContext.js.map +1 -1
- package/dist/provider/LiveChatProvider.d.ts +1 -1
- package/dist/provider/LiveChatProvider.d.ts.map +1 -1
- package/dist/provider/LiveChatProvider.js +17 -2
- package/dist/provider/LiveChatProvider.js.map +1 -1
- package/dist/provider/types.d.ts +4 -0
- package/dist/provider/types.d.ts.map +1 -1
- package/dist/ui/components/ConversationHeader.d.ts.map +1 -1
- package/dist/ui/components/ConversationHeader.js +7 -4
- package/dist/ui/components/ConversationHeader.js.map +1 -1
- package/dist/ui/components/ConversationListScreen.d.ts.map +1 -1
- package/dist/ui/components/ConversationListScreen.js +11 -2
- package/dist/ui/components/ConversationListScreen.js.map +1 -1
- package/dist/ui/components/ConversationScreen.d.ts.map +1 -1
- package/dist/ui/components/ConversationScreen.js +13 -3
- package/dist/ui/components/ConversationScreen.js.map +1 -1
- package/dist/ui/components/HomeScreen.d.ts.map +1 -1
- package/dist/ui/components/HomeScreen.js +14 -4
- package/dist/ui/components/HomeScreen.js.map +1 -1
- package/dist/ui/components/MessageComposer.d.ts +1 -0
- package/dist/ui/components/MessageComposer.d.ts.map +1 -1
- package/dist/ui/components/MessageComposer.js +89 -30
- package/dist/ui/components/MessageComposer.js.map +1 -1
- package/dist/ui/safe-area.d.ts +9 -0
- package/dist/ui/safe-area.d.ts.map +1 -0
- package/dist/ui/safe-area.js +28 -0
- package/dist/ui/safe-area.js.map +1 -0
- package/package.json +11 -3
- package/src/api/conversation-api.ts +33 -8
- package/src/hooks/use-live-chat.ts +3 -0
- package/src/hooks/use-send-message.ts +169 -159
- package/src/index.ts +3 -0
- package/src/navigation/LiveChatPanel.tsx +26 -3
- package/src/provider/LiveChatContext.ts +2 -0
- package/src/provider/LiveChatProvider.tsx +396 -380
- package/src/provider/types.ts +63 -57
- package/src/ui/components/ConversationHeader.tsx +7 -6
- package/src/ui/components/ConversationListScreen.tsx +18 -5
- package/src/ui/components/ConversationScreen.tsx +369 -362
- package/src/ui/components/HomeScreen.tsx +21 -6
- package/src/ui/components/MessageComposer.tsx +116 -36
- package/src/ui/safe-area.ts +34 -0
|
@@ -1,362 +1,369 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import {
|
|
3
|
-
ActivityIndicator,
|
|
4
|
-
FlatList,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import type {
|
|
21
|
-
import
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
} from "
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (md.getTime() ===
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
else if (prevSame && nextSame) position = "
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (type === "
|
|
108
|
-
if (type === "
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const {
|
|
135
|
-
const {
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const [
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
const
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
<View style={
|
|
296
|
-
<
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
<
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
>
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
1
|
+
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
ActivityIndicator,
|
|
4
|
+
FlatList,
|
|
5
|
+
StyleSheet,
|
|
6
|
+
Text,
|
|
7
|
+
TouchableOpacity,
|
|
8
|
+
View,
|
|
9
|
+
} from "react-native";
|
|
10
|
+
import { KeyboardAvoidingView } from "react-native-keyboard-controller";
|
|
11
|
+
import { useLiveChatContext } from "../../provider/LiveChatContext";
|
|
12
|
+
import { conversationApi } from "../../api/conversation-api";
|
|
13
|
+
import { useMessages } from "../../hooks/use-messages";
|
|
14
|
+
import { useSendMessage } from "../../hooks/use-send-message";
|
|
15
|
+
import { ConversationHeader } from "./ConversationHeader";
|
|
16
|
+
import { MessageBubble } from "./MessageBubble";
|
|
17
|
+
import { LogMessage } from "./LogMessage";
|
|
18
|
+
import { MessageComposer } from "./MessageComposer";
|
|
19
|
+
import type { UploadedAttachment } from "./MessageComposer";
|
|
20
|
+
import type { InteractiveReplyPayload } from "./LivechatMessageRenderer";
|
|
21
|
+
import {
|
|
22
|
+
POWERED_BY_TEXT,
|
|
23
|
+
getWidgetGreeting,
|
|
24
|
+
} from "../../ui/theme";
|
|
25
|
+
import type { BubblePosition } from "./MessageBubble";
|
|
26
|
+
|
|
27
|
+
export type ConversationScreenProps = {
|
|
28
|
+
conversationId: string | null | undefined;
|
|
29
|
+
onBack?: () => void;
|
|
30
|
+
onClose?: () => void;
|
|
31
|
+
onConversationCreated?: (conversationId: string) => void;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// ── date separator ────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
function DateSeparator({ label, color }: { label: string; color: string }) {
|
|
37
|
+
return (
|
|
38
|
+
<View style={styles.dateSep}>
|
|
39
|
+
<Text style={[styles.dateSepText, { color }]}>{label}</Text>
|
|
40
|
+
</View>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getDateLabel(dateStr: string): string {
|
|
45
|
+
const d = new Date(dateStr);
|
|
46
|
+
if (isNaN(d.getTime())) return "";
|
|
47
|
+
const now = new Date();
|
|
48
|
+
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
49
|
+
const yesterday = new Date(today);
|
|
50
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
51
|
+
const md = new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
52
|
+
if (md.getTime() === today.getTime()) return "TODAY";
|
|
53
|
+
if (md.getTime() === yesterday.getTime()) return "YESTERDAY";
|
|
54
|
+
return d.toLocaleDateString([], { day: "2-digit", month: "short" }).toUpperCase();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ── helpers ───────────────────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
function makeGreetingMessage(greeting: string): any {
|
|
60
|
+
return {
|
|
61
|
+
_id: "__lc_greeting__",
|
|
62
|
+
senderType: "ai",
|
|
63
|
+
channelType: "livechat",
|
|
64
|
+
livechat: { type: "text", text: { body: greeting } },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function getBubblePosition(messages: any[], index: number): { position: BubblePosition; showFooter: boolean } {
|
|
69
|
+
const msg = messages[index];
|
|
70
|
+
const prev = messages[index - 1];
|
|
71
|
+
const next = messages[index + 1];
|
|
72
|
+
const prevSame = prev && !prev._isDateSep && prev.channelType !== "log" && prev.senderType === msg.senderType;
|
|
73
|
+
const nextSame = next && !next._isDateSep && next.channelType !== "log" && next.senderType === msg.senderType;
|
|
74
|
+
let position: BubblePosition = "single";
|
|
75
|
+
if (!prevSame && nextSame) position = "first";
|
|
76
|
+
else if (prevSame && nextSame) position = "middle";
|
|
77
|
+
else if (prevSame && !nextSame) position = "last";
|
|
78
|
+
return { position, showFooter: position === "single" || position === "last" };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function buildRenderList(messages: any[], greeting: string): any[] {
|
|
82
|
+
const result: any[] = [makeGreetingMessage(greeting)];
|
|
83
|
+
// Sort by createdAt ascending — mirrors web's transformMessagesToDateSeperators sort
|
|
84
|
+
const sorted = [...messages].sort((a, b) => {
|
|
85
|
+
const ta = new Date(a?.createdAt || a?.livechat?.timestamp || 0).getTime();
|
|
86
|
+
const tb = new Date(b?.createdAt || b?.livechat?.timestamp || 0).getTime();
|
|
87
|
+
return ta - tb;
|
|
88
|
+
});
|
|
89
|
+
let lastDateLabel = "";
|
|
90
|
+
for (const m of sorted) {
|
|
91
|
+
const dateStr = m?.createdAt || m?.livechat?.timestamp;
|
|
92
|
+
if (dateStr) {
|
|
93
|
+
const label = getDateLabel(dateStr);
|
|
94
|
+
if (label && label !== lastDateLabel) {
|
|
95
|
+
result.push({ _isDateSep: true, label, _id: "date-" + label });
|
|
96
|
+
lastDateLabel = label;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
result.push(m);
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getRoleLabel(message: any): string {
|
|
105
|
+
const type = message?.assigneeType ?? message?.senderType ?? null;
|
|
106
|
+
if (type === "ai") return "AI Agent";
|
|
107
|
+
if (type === "bot") return "Bot";
|
|
108
|
+
if (type === "agent" || type === "user")
|
|
109
|
+
return message?.assigneeRole ?? message?.userId?.role ?? "Agent";
|
|
110
|
+
if (message?.senderType === "agent")
|
|
111
|
+
return message?.assigneeRole ?? message?.userId?.role ?? "Agent";
|
|
112
|
+
return "";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function patchConversationShell(prev: any, conversationId: string | null | undefined, incoming: any): any {
|
|
116
|
+
if (!prev || !conversationId || !incoming?._id) return prev;
|
|
117
|
+
if (String(incoming._id) !== String(conversationId)) return prev;
|
|
118
|
+
const nested = prev.conversation;
|
|
119
|
+
if (nested != null && typeof nested === "object") {
|
|
120
|
+
return { ...prev, conversation: { ...nested, ...incoming } };
|
|
121
|
+
}
|
|
122
|
+
return { ...prev, ...incoming };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ── main screen ───────────────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
export function ConversationScreen({
|
|
128
|
+
conversationId,
|
|
129
|
+
onBack,
|
|
130
|
+
onClose,
|
|
131
|
+
onConversationCreated,
|
|
132
|
+
}: ConversationScreenProps) {
|
|
133
|
+
const { theme: t, widgetName, widgetConfig, wsClient, visitorQueryParams, setActiveConversationId } = useLiveChatContext();
|
|
134
|
+
const { messages, loading, loadingOlder, hasOlder, loadOlder, markAsRead } = useMessages(conversationId);
|
|
135
|
+
const { sendText, sendAttachment, sendInteractive, sending } = useSendMessage();
|
|
136
|
+
const flatListRef = useRef<FlatList>(null);
|
|
137
|
+
|
|
138
|
+
const settings = widgetConfig?.widgetSettings ?? {};
|
|
139
|
+
const greeting = getWidgetGreeting(settings);
|
|
140
|
+
|
|
141
|
+
// ── fetch conversation ────────────────────────────────────────────────────
|
|
142
|
+
const [conversation, setConversation] = useState<any>(null);
|
|
143
|
+
const [listPickerState, setListPickerState] = useState<{ sections: any[]; onSelect: (id: string, title: string) => void } | null>(null);
|
|
144
|
+
const prevConvIdRef = useRef<string | null | undefined>(null);
|
|
145
|
+
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
const isNew = !conversationId || conversationId === "new";
|
|
148
|
+
if (isNew || !visitorQueryParams) {
|
|
149
|
+
setConversation(null);
|
|
150
|
+
prevConvIdRef.current = conversationId;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (prevConvIdRef.current != null && prevConvIdRef.current !== "new" && prevConvIdRef.current !== conversationId) {
|
|
154
|
+
setConversation(null);
|
|
155
|
+
}
|
|
156
|
+
prevConvIdRef.current = conversationId;
|
|
157
|
+
|
|
158
|
+
const ac = new AbortController();
|
|
159
|
+
(async () => {
|
|
160
|
+
try {
|
|
161
|
+
const raw = await conversationApi.getConversation(conversationId, {
|
|
162
|
+
params: visitorQueryParams,
|
|
163
|
+
signal: ac.signal,
|
|
164
|
+
});
|
|
165
|
+
if (ac.signal.aborted) return;
|
|
166
|
+
setConversation(raw?.data ?? raw);
|
|
167
|
+
} catch (e: any) {
|
|
168
|
+
if (ac.signal.aborted || e?.name === "AbortError") return;
|
|
169
|
+
setConversation(null);
|
|
170
|
+
}
|
|
171
|
+
})();
|
|
172
|
+
return () => ac.abort();
|
|
173
|
+
}, [conversationId, visitorQueryParams]);
|
|
174
|
+
|
|
175
|
+
// ── WS patches ────────────────────────────────────────────────────────────
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
if (!wsClient || !conversationId || conversationId === "new") return;
|
|
178
|
+
const patch = (payload: any) => {
|
|
179
|
+
const data = payload?.data ?? payload;
|
|
180
|
+
const incoming = data?.conversation;
|
|
181
|
+
setConversation((prev: any) => patchConversationShell(prev, conversationId, incoming));
|
|
182
|
+
};
|
|
183
|
+
const u1 = wsClient.subscribe("conversation_resolved", patch);
|
|
184
|
+
const u2 = wsClient.subscribe("conversation_intervened", patch);
|
|
185
|
+
const u3 = wsClient.subscribe("conversation_unassigned", patch);
|
|
186
|
+
const u4 = wsClient.subscribe("conversation_transferred", patch);
|
|
187
|
+
return () => { u1(); u2(); u3(); u4(); };
|
|
188
|
+
}, [wsClient, conversationId]);
|
|
189
|
+
|
|
190
|
+
const isNewConversation = !conversationId || conversationId === "new";
|
|
191
|
+
const convDoc = conversation?.conversation ?? conversation ?? null;
|
|
192
|
+
const isResolved = !isNewConversation && !!conversation && convDoc?.status === "resolved";
|
|
193
|
+
|
|
194
|
+
const renderList = buildRenderList(messages, greeting);
|
|
195
|
+
const renderListRef = useRef<any[]>(renderList);
|
|
196
|
+
renderListRef.current = renderList;
|
|
197
|
+
|
|
198
|
+
useEffect(() => { if (conversationId && conversationId !== "new") markAsRead(); }, [conversationId, markAsRead]);
|
|
199
|
+
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (!conversationId || conversationId === "new") return;
|
|
202
|
+
setActiveConversationId(conversationId);
|
|
203
|
+
return () => setActiveConversationId(null);
|
|
204
|
+
}, [conversationId, setActiveConversationId]);
|
|
205
|
+
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (renderList.length > 0) {
|
|
208
|
+
setTimeout(() => flatListRef.current?.scrollToEnd({ animated: true }), 100);
|
|
209
|
+
}
|
|
210
|
+
}, [renderList.length]);
|
|
211
|
+
|
|
212
|
+
// Throws on failure so MessageComposer can preserve the draft and show the error.
|
|
213
|
+
const handleSend = useCallback(async (text: string, uploadedAttachments?: UploadedAttachment[]) => {
|
|
214
|
+
if (uploadedAttachments?.length) {
|
|
215
|
+
let activeId: string | null | undefined = conversationId;
|
|
216
|
+
for (let i = 0; i < uploadedAttachments.length; i++) {
|
|
217
|
+
const att = uploadedAttachments[i];
|
|
218
|
+
const caption = i === 0 && text ? text : undefined;
|
|
219
|
+
// sendAttachment throws on failure — let it propagate
|
|
220
|
+
const returnedId = await sendAttachment(
|
|
221
|
+
{ fileId: att.fileId, fileUrl: att.fileUrl, fileName: att.fileName, fileExtension: att.fileExtension, type: att.type, caption },
|
|
222
|
+
activeId === "new" ? null : activeId
|
|
223
|
+
);
|
|
224
|
+
if ((!activeId || activeId === "new") && returnedId) {
|
|
225
|
+
activeId = returnedId;
|
|
226
|
+
onConversationCreated?.(returnedId);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
// sendText throws on failure — let it propagate
|
|
232
|
+
const returnedId = await sendText(text, isNewConversation ? null : conversationId);
|
|
233
|
+
if (isNewConversation && returnedId) onConversationCreated?.(returnedId);
|
|
234
|
+
}, [conversationId, isNewConversation, sendText, sendAttachment, onConversationCreated]);
|
|
235
|
+
|
|
236
|
+
const renderItem = useCallback(({ item, index }: { item: any; index: number }) => {
|
|
237
|
+
if (item._isDateSep) {
|
|
238
|
+
return <DateSeparator label={item.label} color={t.colors.textMuted} />;
|
|
239
|
+
}
|
|
240
|
+
if (item.channelType === "log") {
|
|
241
|
+
return <LogMessage message={item} />;
|
|
242
|
+
}
|
|
243
|
+
const { position, showFooter } = getBubblePosition(renderListRef.current, index);
|
|
244
|
+
return (
|
|
245
|
+
<MessageBubble
|
|
246
|
+
message={item}
|
|
247
|
+
position={position}
|
|
248
|
+
showFooter={showFooter}
|
|
249
|
+
senderName={item?.userId?.name ?? item?.senderName ?? widgetName}
|
|
250
|
+
roleLabel={getRoleLabel(item)}
|
|
251
|
+
onInteractiveReply={(payload: InteractiveReplyPayload | string) => {
|
|
252
|
+
if (typeof payload === "string") {
|
|
253
|
+
handleSend(payload);
|
|
254
|
+
} else {
|
|
255
|
+
sendInteractive(
|
|
256
|
+
{ type: payload.type, id: payload.id, title: payload.title },
|
|
257
|
+
isNewConversation ? null : conversationId
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}}
|
|
261
|
+
onShowListPicker={(sections, onSelect) => setListPickerState({ sections, onSelect })}
|
|
262
|
+
/>
|
|
263
|
+
);
|
|
264
|
+
}, [t, widgetName, handleSend]);
|
|
265
|
+
|
|
266
|
+
return (
|
|
267
|
+
<KeyboardAvoidingView
|
|
268
|
+
style={[styles.flex, { backgroundColor: t.colors.background }]}
|
|
269
|
+
behavior="padding"
|
|
270
|
+
>
|
|
271
|
+
<ConversationHeader
|
|
272
|
+
conversation={isNewConversation ? null : conversation}
|
|
273
|
+
isLoading={!isNewConversation && conversation === null}
|
|
274
|
+
onBack={onBack}
|
|
275
|
+
onClose={onClose}
|
|
276
|
+
/>
|
|
277
|
+
|
|
278
|
+
{loadingOlder && (
|
|
279
|
+
<ActivityIndicator color={t.colors.brand} style={{ marginTop: 8 }} />
|
|
280
|
+
)}
|
|
281
|
+
|
|
282
|
+
<FlatList
|
|
283
|
+
ref={flatListRef}
|
|
284
|
+
data={renderList}
|
|
285
|
+
keyExtractor={(item, i) => item?._id ?? String(i)}
|
|
286
|
+
renderItem={renderItem}
|
|
287
|
+
style={styles.flex}
|
|
288
|
+
contentContainerStyle={{ paddingVertical: 8 }}
|
|
289
|
+
onEndReached={hasOlder ? loadOlder : undefined}
|
|
290
|
+
onEndReachedThreshold={0.2}
|
|
291
|
+
maintainVisibleContentPosition={{ minIndexForVisible: 0 }}
|
|
292
|
+
onContentSizeChange={() => flatListRef.current?.scrollToEnd({ animated: true })}
|
|
293
|
+
ListHeaderComponent={
|
|
294
|
+
loading && messages.length === 0 ? (
|
|
295
|
+
<View style={styles.center}>
|
|
296
|
+
<ActivityIndicator color={t.colors.brand} size="large" style={{ marginVertical: 24 }} />
|
|
297
|
+
</View>
|
|
298
|
+
) : null
|
|
299
|
+
}
|
|
300
|
+
ListEmptyComponent={
|
|
301
|
+
!loading ? (
|
|
302
|
+
<View style={[styles.flex, styles.center]}>
|
|
303
|
+
<Text style={{ color: t.colors.textMuted, fontSize: t.fontSizes.md }}>
|
|
304
|
+
Start the conversation
|
|
305
|
+
</Text>
|
|
306
|
+
</View>
|
|
307
|
+
) : null
|
|
308
|
+
}
|
|
309
|
+
/>
|
|
310
|
+
|
|
311
|
+
<MessageComposer
|
|
312
|
+
onSend={handleSend}
|
|
313
|
+
disabled={isResolved || sending}
|
|
314
|
+
placeholder={isResolved ? "This conversation is resolved" : "Message…"}
|
|
315
|
+
/>
|
|
316
|
+
|
|
317
|
+
{listPickerState && (
|
|
318
|
+
<View style={[styles.listPicker, { backgroundColor: t.colors.surface }]}>
|
|
319
|
+
<View style={[styles.listPickerHeader, { borderBottomColor: t.colors.border }]}>
|
|
320
|
+
<Text style={[styles.listPickerTitle, { color: t.colors.text }]}>Select an option</Text>
|
|
321
|
+
<TouchableOpacity onPress={() => setListPickerState(null)}>
|
|
322
|
+
<Text style={{ color: t.colors.textMuted, fontSize: 20 }}>×</Text>
|
|
323
|
+
</TouchableOpacity>
|
|
324
|
+
</View>
|
|
325
|
+
<FlatList
|
|
326
|
+
data={listPickerState.sections}
|
|
327
|
+
keyExtractor={(_, i) => String(i)}
|
|
328
|
+
renderItem={({ item: section }) => (
|
|
329
|
+
<View>
|
|
330
|
+
{section.title ? (
|
|
331
|
+
<Text style={[styles.listPickerSectionTitle, { color: t.colors.textMuted }]}>{section.title}</Text>
|
|
332
|
+
) : null}
|
|
333
|
+
{(section.rows ?? []).map((row: any) => (
|
|
334
|
+
<TouchableOpacity
|
|
335
|
+
key={row.id}
|
|
336
|
+
onPress={() => { listPickerState.onSelect(row.id, row.title); setListPickerState(null); }}
|
|
337
|
+
style={[styles.listPickerRow, { borderBottomColor: t.colors.border }]}
|
|
338
|
+
>
|
|
339
|
+
<Text style={[styles.listPickerRowTitle, { color: t.colors.text }]}>{row.title}</Text>
|
|
340
|
+
{row.description ? <Text style={[styles.listPickerRowDesc, { color: t.colors.textMuted }]}>{row.description}</Text> : null}
|
|
341
|
+
</TouchableOpacity>
|
|
342
|
+
))}
|
|
343
|
+
</View>
|
|
344
|
+
)}
|
|
345
|
+
/>
|
|
346
|
+
</View>
|
|
347
|
+
)}
|
|
348
|
+
|
|
349
|
+
<Text style={[styles.poweredBy, { color: t.colors.textMuted }]}>
|
|
350
|
+
{POWERED_BY_TEXT}
|
|
351
|
+
</Text>
|
|
352
|
+
</KeyboardAvoidingView>
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const styles = StyleSheet.create({
|
|
357
|
+
flex: { flex: 1 },
|
|
358
|
+
center: { alignItems: "center", justifyContent: "center" },
|
|
359
|
+
dateSep: { alignItems: "center", paddingVertical: 10 },
|
|
360
|
+
dateSepText: { fontSize: 11, fontWeight: "600", letterSpacing: 0.5 },
|
|
361
|
+
poweredBy: { textAlign: "center", fontSize: 11, paddingVertical: 8 },
|
|
362
|
+
listPicker: { position: "absolute", inset: 0, top: 0, left: 0, right: 0, bottom: 0, zIndex: 20 },
|
|
363
|
+
listPickerHeader: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 16, paddingVertical: 12, borderBottomWidth: StyleSheet.hairlineWidth },
|
|
364
|
+
listPickerTitle: { fontSize: 14, fontWeight: "600" },
|
|
365
|
+
listPickerSectionTitle: { fontSize: 11, fontWeight: "600", textTransform: "uppercase", letterSpacing: 0.5, paddingHorizontal: 16, paddingTop: 12, paddingBottom: 4 },
|
|
366
|
+
listPickerRow: { paddingHorizontal: 16, paddingVertical: 12, borderBottomWidth: StyleSheet.hairlineWidth },
|
|
367
|
+
listPickerRowTitle: { fontSize: 14, fontWeight: "500" },
|
|
368
|
+
listPickerRowDesc: { fontSize: 12, marginTop: 2 },
|
|
369
|
+
});
|