@repzo/widget-react-native 1.0.9 → 1.1.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.
Files changed (144) hide show
  1. package/CHANGELOG.md +365 -0
  2. package/README.md +55 -4
  3. package/lib/commonjs/agent-ui-blocks.js +2 -1
  4. package/lib/commonjs/agent-ui-blocks.js.map +1 -1
  5. package/lib/commonjs/attachments/pickers.js +3 -6
  6. package/lib/commonjs/attachments/pickers.js.map +1 -1
  7. package/lib/commonjs/device-id.js +31 -3
  8. package/lib/commonjs/device-id.js.map +1 -1
  9. package/lib/commonjs/i18n.js +26 -4
  10. package/lib/commonjs/i18n.js.map +1 -1
  11. package/lib/commonjs/locales.js +21 -1
  12. package/lib/commonjs/locales.js.map +1 -1
  13. package/lib/commonjs/push/push-notifications.js +0 -1
  14. package/lib/commonjs/push/push-notifications.js.map +1 -1
  15. package/lib/commonjs/state/widget-store.js +57 -20
  16. package/lib/commonjs/state/widget-store.js.map +1 -1
  17. package/lib/commonjs/storage.js +13 -1
  18. package/lib/commonjs/storage.js.map +1 -1
  19. package/lib/commonjs/types.js +6 -3
  20. package/lib/commonjs/types.js.map +1 -1
  21. package/lib/commonjs/ui/WidgetProvider.js +74 -47
  22. package/lib/commonjs/ui/WidgetProvider.js.map +1 -1
  23. package/lib/commonjs/ui/components/AgentUiBlocks.js +35 -15
  24. package/lib/commonjs/ui/components/AgentUiBlocks.js.map +1 -1
  25. package/lib/commonjs/ui/components/Launcher.js +4 -1
  26. package/lib/commonjs/ui/components/Launcher.js.map +1 -1
  27. package/lib/commonjs/ui/components/Markdown.js +32 -2
  28. package/lib/commonjs/ui/components/Markdown.js.map +1 -1
  29. package/lib/commonjs/ui/components/RatingCard.js +6 -3
  30. package/lib/commonjs/ui/components/RatingCard.js.map +1 -1
  31. package/lib/commonjs/ui/hooks/use-widget-insets.js +0 -1
  32. package/lib/commonjs/ui/hooks/use-widget-insets.js.map +1 -1
  33. package/lib/commonjs/ui/screens/chat/ChatView.js +67 -29
  34. package/lib/commonjs/ui/screens/chat/ChatView.js.map +1 -1
  35. package/lib/commonjs/ui/utils/html.js +8 -1
  36. package/lib/commonjs/ui/utils/html.js.map +1 -1
  37. package/lib/commonjs/ui/utils/native-gradient.js +0 -1
  38. package/lib/commonjs/ui/utils/native-gradient.js.map +1 -1
  39. package/lib/module/agent-ui-blocks.js +2 -1
  40. package/lib/module/agent-ui-blocks.js.map +1 -1
  41. package/lib/module/attachments/pickers.js +3 -6
  42. package/lib/module/attachments/pickers.js.map +1 -1
  43. package/lib/module/device-id.js +31 -3
  44. package/lib/module/device-id.js.map +1 -1
  45. package/lib/module/i18n.js +26 -4
  46. package/lib/module/i18n.js.map +1 -1
  47. package/lib/module/locales.js +21 -1
  48. package/lib/module/locales.js.map +1 -1
  49. package/lib/module/push/push-notifications.js +0 -1
  50. package/lib/module/push/push-notifications.js.map +1 -1
  51. package/lib/module/state/widget-store.js +58 -20
  52. package/lib/module/state/widget-store.js.map +1 -1
  53. package/lib/module/storage.js +13 -1
  54. package/lib/module/storage.js.map +1 -1
  55. package/lib/module/types.js +6 -3
  56. package/lib/module/types.js.map +1 -1
  57. package/lib/module/ui/WidgetProvider.js +74 -47
  58. package/lib/module/ui/WidgetProvider.js.map +1 -1
  59. package/lib/module/ui/components/AgentUiBlocks.js +35 -15
  60. package/lib/module/ui/components/AgentUiBlocks.js.map +1 -1
  61. package/lib/module/ui/components/Launcher.js +4 -1
  62. package/lib/module/ui/components/Launcher.js.map +1 -1
  63. package/lib/module/ui/components/Markdown.js +32 -3
  64. package/lib/module/ui/components/Markdown.js.map +1 -1
  65. package/lib/module/ui/components/RatingCard.js +6 -3
  66. package/lib/module/ui/components/RatingCard.js.map +1 -1
  67. package/lib/module/ui/hooks/use-widget-insets.js +0 -1
  68. package/lib/module/ui/hooks/use-widget-insets.js.map +1 -1
  69. package/lib/module/ui/screens/chat/ChatView.js +68 -30
  70. package/lib/module/ui/screens/chat/ChatView.js.map +1 -1
  71. package/lib/module/ui/utils/html.js +8 -1
  72. package/lib/module/ui/utils/html.js.map +1 -1
  73. package/lib/module/ui/utils/native-gradient.js +0 -1
  74. package/lib/module/ui/utils/native-gradient.js.map +1 -1
  75. package/lib/typescript/commonjs/agent-ui-blocks.d.ts.map +1 -1
  76. package/lib/typescript/commonjs/attachments/pickers.d.ts.map +1 -1
  77. package/lib/typescript/commonjs/device-id.d.ts.map +1 -1
  78. package/lib/typescript/commonjs/i18n.d.ts +22 -1
  79. package/lib/typescript/commonjs/i18n.d.ts.map +1 -1
  80. package/lib/typescript/commonjs/locales.d.ts.map +1 -1
  81. package/lib/typescript/commonjs/push/push-notifications.d.ts.map +1 -1
  82. package/lib/typescript/commonjs/state/widget-store.d.ts +13 -7
  83. package/lib/typescript/commonjs/state/widget-store.d.ts.map +1 -1
  84. package/lib/typescript/commonjs/storage.d.ts.map +1 -1
  85. package/lib/typescript/commonjs/types.d.ts +14 -8
  86. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  87. package/lib/typescript/commonjs/ui/WidgetProvider.d.ts.map +1 -1
  88. package/lib/typescript/commonjs/ui/components/AgentUiBlocks.d.ts +14 -0
  89. package/lib/typescript/commonjs/ui/components/AgentUiBlocks.d.ts.map +1 -1
  90. package/lib/typescript/commonjs/ui/components/Launcher.d.ts.map +1 -1
  91. package/lib/typescript/commonjs/ui/components/Markdown.d.ts +8 -0
  92. package/lib/typescript/commonjs/ui/components/Markdown.d.ts.map +1 -1
  93. package/lib/typescript/commonjs/ui/components/RatingCard.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/ui/hooks/use-widget-insets.d.ts.map +1 -1
  95. package/lib/typescript/commonjs/ui/screens/chat/ChatView.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/ui/screens/chat/ChatView.types.d.ts +10 -0
  97. package/lib/typescript/commonjs/ui/screens/chat/ChatView.types.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/ui/utils/html.d.ts.map +1 -1
  99. package/lib/typescript/commonjs/ui/utils/native-gradient.d.ts.map +1 -1
  100. package/lib/typescript/module/agent-ui-blocks.d.ts.map +1 -1
  101. package/lib/typescript/module/attachments/pickers.d.ts.map +1 -1
  102. package/lib/typescript/module/device-id.d.ts.map +1 -1
  103. package/lib/typescript/module/i18n.d.ts +22 -1
  104. package/lib/typescript/module/i18n.d.ts.map +1 -1
  105. package/lib/typescript/module/locales.d.ts.map +1 -1
  106. package/lib/typescript/module/push/push-notifications.d.ts.map +1 -1
  107. package/lib/typescript/module/state/widget-store.d.ts +13 -7
  108. package/lib/typescript/module/state/widget-store.d.ts.map +1 -1
  109. package/lib/typescript/module/storage.d.ts.map +1 -1
  110. package/lib/typescript/module/types.d.ts +14 -8
  111. package/lib/typescript/module/types.d.ts.map +1 -1
  112. package/lib/typescript/module/ui/WidgetProvider.d.ts.map +1 -1
  113. package/lib/typescript/module/ui/components/AgentUiBlocks.d.ts +14 -0
  114. package/lib/typescript/module/ui/components/AgentUiBlocks.d.ts.map +1 -1
  115. package/lib/typescript/module/ui/components/Launcher.d.ts.map +1 -1
  116. package/lib/typescript/module/ui/components/Markdown.d.ts +8 -0
  117. package/lib/typescript/module/ui/components/Markdown.d.ts.map +1 -1
  118. package/lib/typescript/module/ui/components/RatingCard.d.ts.map +1 -1
  119. package/lib/typescript/module/ui/hooks/use-widget-insets.d.ts.map +1 -1
  120. package/lib/typescript/module/ui/screens/chat/ChatView.d.ts.map +1 -1
  121. package/lib/typescript/module/ui/screens/chat/ChatView.types.d.ts +10 -0
  122. package/lib/typescript/module/ui/screens/chat/ChatView.types.d.ts.map +1 -1
  123. package/lib/typescript/module/ui/utils/html.d.ts.map +1 -1
  124. package/lib/typescript/module/ui/utils/native-gradient.d.ts.map +1 -1
  125. package/package.json +14 -4
  126. package/src/agent-ui-blocks.ts +2 -1
  127. package/src/attachments/pickers.ts +3 -6
  128. package/src/device-id.ts +34 -3
  129. package/src/i18n.ts +25 -2
  130. package/src/locales.ts +20 -0
  131. package/src/push/push-notifications.ts +0 -1
  132. package/src/state/widget-store.ts +52 -19
  133. package/src/storage.ts +18 -1
  134. package/src/types.ts +14 -8
  135. package/src/ui/WidgetProvider.tsx +88 -46
  136. package/src/ui/components/AgentUiBlocks.tsx +40 -12
  137. package/src/ui/components/Launcher.tsx +6 -1
  138. package/src/ui/components/Markdown.tsx +34 -3
  139. package/src/ui/components/RatingCard.tsx +6 -2
  140. package/src/ui/hooks/use-widget-insets.ts +0 -1
  141. package/src/ui/screens/chat/ChatView.tsx +77 -42
  142. package/src/ui/screens/chat/ChatView.types.ts +10 -0
  143. package/src/ui/utils/html.ts +9 -1
  144. package/src/ui/utils/native-gradient.ts +0 -1
@@ -8,7 +8,7 @@
8
8
  * `attachments/pickers.ts`).
9
9
  */
10
10
 
11
- import { Fragment, useCallback, useEffect, useRef, useState } from 'react';
11
+ import { Fragment, memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
12
12
  import {
13
13
  Animated,
14
14
  Easing,
@@ -33,6 +33,7 @@ import {
33
33
  type AttachmentProblem,
34
34
  type DraftAttachment,
35
35
  } from '../../../attachments/pickers';
36
+ import { formatLabel } from '../../../i18n';
36
37
  import type {
37
38
  AgentUiAction,
38
39
  AgentProcedureForm,
@@ -315,20 +316,23 @@ export function ChatView({
315
316
  [onIdentifyVisitor, onSendMessage, scrollToBottom]
316
317
  );
317
318
 
319
+ const handleSubmitProcedureForm = useCallback(
320
+ (body: string) => void onSendMessage(body).catch(() => {}),
321
+ [onSendMessage]
322
+ );
323
+
324
+ // Recomputed only when the list changes — never on a keystroke.
325
+ const thread = useMemo(() => deriveThread(messages), [messages]);
326
+ const locales = useMemo(() => [locale, defaultLocale], [locale, defaultLocale]);
327
+
318
328
  const headerTextColor = contrastingTextColor(color);
319
329
  const onDarkHeader = headerTextColor === palette.white;
320
330
  const isEmptyThread = !loading && messages.length === 0;
321
331
  const agentIsActive = agentOnline ?? isOnline;
322
332
  // "Waiting for a teammate" until someone replies: no teammate in the header
323
333
  // and the visitor spoke last (bot replies before that still count as waiting).
324
- const lastChatMessage = [...messages]
325
- .reverse()
326
- .find((message) => (message.messageType ?? 'message') === 'message' && !message.deletedAt);
327
- const awaitingTeammate = !agentName && !agentTyping && lastChatMessage?.direction === 'inbound';
328
- // "Sent" / "Seen" goes under the visitor's latest message only.
329
- const lastVisitorMessageId = [...messages]
330
- .reverse()
331
- .find((message) => message.direction === 'inbound' && (message.messageType ?? 'message') === 'message')?.id;
334
+ const awaitingTeammate =
335
+ !agentName && !agentTyping && thread.lastChatMessage?.direction === 'inbound';
332
336
  // Two stops off the brand color: flat fill looked like an unstyled block.
333
337
  // A non-hex brand color falls through mixHex unchanged and renders solid.
334
338
  const topInset = useTopInset();
@@ -493,21 +497,25 @@ export function ChatView({
493
497
  labels={labels}
494
498
  onOpenImage={setViewerImage}
495
499
  onRateConversation={onRateConversation}
496
- locales={[locale, defaultLocale]}
500
+ locales={locales}
497
501
  senderLabel={
498
502
  isAgentMessage && (startsRun || startsDay)
499
503
  ? message.senderName || agentName || labels.team
500
504
  : null
501
505
  }
502
506
  timeLabel={formatClockTime(message.createdAt, locale)}
503
- receiptLabel={message.id === lastVisitorMessageId ? receiptLabel(message, labels) : undefined}
507
+ receiptLabel={
508
+ message.id === thread.lastVisitorMessageId
509
+ ? receiptLabel(message, labels)
510
+ : undefined
511
+ }
504
512
  onSubmitProcedureForm={
505
- shouldShowProcedureForm(messages, message)
506
- ? (body) => void onSendMessage(body).catch(() => {})
513
+ message.id === thread.procedureFormMessageId
514
+ ? handleSubmitProcedureForm
507
515
  : undefined
508
516
  }
509
517
  onAgentUiAction={
510
- shouldShowAgentUiBlocks(messages, message) ? handleAgentUiAction : undefined
518
+ thread.agentBlocksShown.has(message.id) ? handleAgentUiAction : undefined
511
519
  }
512
520
  />
513
521
  </WidgetErrorBoundary>
@@ -635,7 +643,11 @@ export function ChatView({
635
643
  );
636
644
  }
637
645
 
638
- function MessageBubble({
646
+ /**
647
+ * Memoized: the composer re-renders the screen on every keystroke, and every
648
+ * prop here is stable unless this message itself changed.
649
+ */
650
+ const MessageBubble = memo(function MessageBubble({
639
651
  message,
640
652
  color,
641
653
  labels,
@@ -795,7 +807,7 @@ function MessageBubble({
795
807
  ) : null}
796
808
  </View>
797
809
  );
798
- }
810
+ });
799
811
 
800
812
  /** Sent once the server has it, Seen once a teammate opened the conversation. */
801
813
  function receiptLabel(message: Message, labels: ChatLabels): string | undefined {
@@ -900,17 +912,21 @@ function ProcedureFieldForm({
900
912
  style={[styles.procedureToggle, { backgroundColor: expanded ? palette.surface : color }]}>
901
913
  <Text
902
914
  style={[styles.procedureToggleLabel, { color: expanded ? palette.text : palette.white }]}>
903
- {submitted ? 'Details sent' : 'Share details'}
915
+ {submitted ? labels.procedureDetailsSent : labels.procedureShareDetails}
904
916
  </Text>
905
917
  <Text
906
918
  style={[styles.procedureToggleHint, { color: expanded ? palette.text : palette.white }]}>
907
- {expanded ? 'Hide' : `${form.fields.length} field${form.fields.length === 1 ? '' : 's'}`}
919
+ {expanded
920
+ ? labels.procedureHide
921
+ : form.fields.length === 1
922
+ ? labels.procedureFieldCountOne
923
+ : formatLabel(labels.procedureFieldCount, { count: form.fields.length })}
908
924
  </Text>
909
925
  </Pressable>
910
926
 
911
927
  {expanded ? (
912
928
  <>
913
- <Text style={styles.procedureTitle}>{form.procedureName || 'Details'}</Text>
929
+ <Text style={styles.procedureTitle}>{form.procedureName || labels.procedureTitle}</Text>
914
930
  <View style={styles.procedureFields}>
915
931
  {form.fields.map((field) => (
916
932
  <View key={field.name}>
@@ -923,7 +939,7 @@ function ProcedureFieldForm({
923
939
  onChangeText={(text) =>
924
940
  setValues((current) => ({ ...current, [field.name]: text }))
925
941
  }
926
- placeholder={placeholderForField(field)}
942
+ placeholder={placeholderForField(field, labels)}
927
943
  placeholderTextColor={palette.textSubtle}
928
944
  editable={!submitted}
929
945
  keyboardType={
@@ -999,9 +1015,9 @@ function TypingDot({ index, reduceMotion }: { index: number; reduceMotion: boole
999
1015
  return <Animated.View style={[styles.typingDot, motionStyle]} />;
1000
1016
  }
1001
1017
 
1002
- function placeholderForField(field: AgentProcedureForm['fields'][number]) {
1003
- if (field.type === 'email') return 'name@example.com';
1004
- if (field.type === 'phone') return '+962 7...';
1018
+ function placeholderForField(field: AgentProcedureForm['fields'][number], labels: ChatLabels) {
1019
+ if (field.type === 'email') return labels.procedureEmailPlaceholder;
1020
+ if (field.type === 'phone') return labels.procedurePhonePlaceholder;
1005
1021
  if (field.type === 'date' || field.type === 'time') return '';
1006
1022
  return field.label;
1007
1023
  }
@@ -1010,24 +1026,43 @@ function stringPayload(value: unknown) {
1010
1026
  return typeof value === 'string' && value.trim() ? value.trim() : undefined;
1011
1027
  }
1012
1028
 
1013
- function shouldShowProcedureForm(messages: Message[], message: Message) {
1014
- if (message.direction === 'inbound' || !getProcedureForm(message.metadata)) return false;
1015
- return messages.findIndex((candidate) => candidate.id === message.id) === messages.length - 1;
1016
- }
1029
+ /**
1030
+ * Everything the thread derives from the whole list, in one backwards pass.
1031
+ * (Per-message lookups made this O(n²) on every render, keystrokes included.)
1032
+ */
1033
+ function deriveThread(messages: Message[]) {
1034
+ /** Messages whose agent blocks stay actionable. */
1035
+ const agentBlocksShown = new Set<string>();
1036
+ const laterSlotPickerIds = new Set<string>();
1037
+ let lastChatMessage: Message | undefined;
1038
+ let lastVisitorMessageId: string | undefined;
1039
+
1040
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
1041
+ const message = messages[index]!;
1042
+ const isChat = (message.messageType ?? 'message') === 'message';
1043
+ if (!lastChatMessage && isChat && !message.deletedAt) lastChatMessage = message;
1044
+ // "Sent" / "Seen" goes under the visitor's latest message only.
1045
+ if (!lastVisitorMessageId && isChat && message.direction === 'inbound') {
1046
+ lastVisitorMessageId = message.id;
1047
+ }
1017
1048
 
1018
- /** Hide superseded slot pickers so only the newest one stays actionable. */
1019
- function shouldShowAgentUiBlocks(messages: Message[], message: Message) {
1020
- const blocks = getAgentUiBlocks(message.metadata);
1021
- if (blocks.length === 0) return false;
1022
- if (!blocks.every((block) => block.type === 'appointment_slot_picker')) return true;
1023
-
1024
- const messageIndex = messages.findIndex((candidate) => candidate.id === message.id);
1025
- const laterBlockIds = new Set(
1026
- messages
1027
- .slice(messageIndex + 1)
1028
- .flatMap((candidate) => getAgentUiBlocks(candidate.metadata))
1029
- .filter((block) => block.type === 'appointment_slot_picker')
1030
- .map((block) => block.id)
1031
- );
1032
- return blocks.some((block) => !laterBlockIds.has(block.id));
1049
+ const blocks = getAgentUiBlocks(message.metadata);
1050
+ if (blocks.length === 0) continue;
1051
+ // A slot picker a later message re-sent is superseded: only the newest
1052
+ // one stays actionable. Any other block always does.
1053
+ const onlySlotPickers = blocks.every((block) => block.type === 'appointment_slot_picker');
1054
+ if (!onlySlotPickers || blocks.some((block) => !laterSlotPickerIds.has(block.id))) {
1055
+ agentBlocksShown.add(message.id);
1056
+ }
1057
+ blocks.forEach((block) => {
1058
+ if (block.type === 'appointment_slot_picker') laterSlotPickerIds.add(block.id);
1059
+ });
1060
+ }
1061
+
1062
+ // A procedure form only while it is the newest message in the thread.
1063
+ const last = messages[messages.length - 1];
1064
+ const procedureFormMessageId =
1065
+ last && last.direction !== 'inbound' && getProcedureForm(last.metadata) ? last.id : null;
1066
+
1067
+ return { agentBlocksShown, lastChatMessage, lastVisitorMessageId, procedureFormMessageId };
1033
1068
  }
@@ -35,6 +35,16 @@ export interface ChatLabels {
35
35
  joinedConversation: string;
36
36
  procedureSend: string;
37
37
  procedureSent: string;
38
+ procedureShareDetails: string;
39
+ procedureDetailsSent: string;
40
+ procedureHide: string;
41
+ /** `{count}` = number of fields (2 or more). */
42
+ procedureFieldCount: string;
43
+ procedureFieldCountOne: string;
44
+ /** Shown when the procedure has no name. */
45
+ procedureTitle: string;
46
+ procedureEmailPlaceholder: string;
47
+ procedurePhonePlaceholder: string;
38
48
  blocks: AgentUiBlockLabels;
39
49
  rating: RatingCardLabels;
40
50
  attach: AttachLabels;
@@ -14,10 +14,18 @@ const ENTITIES: Record<string, string> = {
14
14
  '&nbsp;': ' ',
15
15
  };
16
16
 
17
+ /** `&#128512;` / `&#x1F600;` → 😀. Out-of-range or surrogate code points stay as written. */
18
+ function decodeNumericEntity(match: string, decimal?: string, hex?: string): string {
19
+ const code = decimal !== undefined ? Number(decimal) : parseInt(hex ?? '', 16);
20
+ const valid =
21
+ Number.isInteger(code) && code > 0 && code <= 0x10ffff && (code < 0xd800 || code > 0xdfff);
22
+ return valid ? String.fromCodePoint(code) : match;
23
+ }
24
+
17
25
  function decodeEntities(text: string): string {
18
26
  return text
19
27
  .replace(/&(amp|lt|gt|quot|#39|apos|nbsp);/g, (match) => ENTITIES[match] ?? match)
20
- .replace(/&#(\d+);/g, (_match, code) => String.fromCharCode(Number(code)));
28
+ .replace(/&#(?:(\d{1,7})|[xX]([0-9a-fA-F]{1,6}));/g, decodeNumericEntity);
21
29
  }
22
30
 
23
31
  /** Block-level tags become newlines so paragraphs survive the strip. */
@@ -29,7 +29,6 @@ export interface NativeGradientProps {
29
29
  let nativeGradient: ComponentType<NativeGradientProps> | null = null;
30
30
 
31
31
  try {
32
- // eslint-disable-next-line @typescript-eslint/no-var-requires
33
32
  const mod = require('react-native-linear-gradient');
34
33
  const component = mod?.default ?? mod?.LinearGradient;
35
34
  if (component) nativeGradient = component as ComponentType<NativeGradientProps>;