@cometchat/chat-uikit-react-native 5.1.1 → 5.2.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 (227) hide show
  1. package/android/.gradle/7.4/checksums/checksums.lock +0 -0
  2. package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
  3. package/lib/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.d.ts +40 -0
  4. package/lib/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.js +467 -0
  5. package/lib/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.js.map +1 -0
  6. package/lib/CometChatAIAssistantChatHistory/Skeleton.d.ts +24 -0
  7. package/lib/CometChatAIAssistantChatHistory/Skeleton.js +130 -0
  8. package/lib/CometChatAIAssistantChatHistory/Skeleton.js.map +1 -0
  9. package/lib/CometChatAIAssistantChatHistory/index.d.ts +1 -0
  10. package/lib/CometChatAIAssistantChatHistory/index.js +2 -0
  11. package/lib/CometChatAIAssistantChatHistory/index.js.map +1 -0
  12. package/lib/CometChatAIAssistantChatHistory/style.d.ts +30 -0
  13. package/lib/CometChatAIAssistantChatHistory/style.js +122 -0
  14. package/lib/CometChatAIAssistantChatHistory/style.js.map +1 -0
  15. package/lib/CometChatConversations/CometChatConversations.js +26 -5
  16. package/lib/CometChatConversations/CometChatConversations.js.map +1 -1
  17. package/lib/CometChatMessageComposer/CometChatMessageComposer.d.ts +7 -0
  18. package/lib/CometChatMessageComposer/CometChatMessageComposer.js +101 -13
  19. package/lib/CometChatMessageComposer/CometChatMessageComposer.js.map +1 -1
  20. package/lib/CometChatMessageHeader/CometChatMessageHeader.d.ts +16 -0
  21. package/lib/CometChatMessageHeader/CometChatMessageHeader.js +63 -13
  22. package/lib/CometChatMessageHeader/CometChatMessageHeader.js.map +1 -1
  23. package/lib/CometChatMessageList/CometChatMessageList.d.ts +33 -0
  24. package/lib/CometChatMessageList/CometChatMessageList.js +535 -76
  25. package/lib/CometChatMessageList/CometChatMessageList.js.map +1 -1
  26. package/lib/CometChatMessageList/style.js +74 -2
  27. package/lib/CometChatMessageList/style.js.map +1 -1
  28. package/lib/calls/CometChatIncomingCall/CometChatIncomingCall.js +6 -12
  29. package/lib/calls/CometChatIncomingCall/CometChatIncomingCall.js.map +1 -1
  30. package/lib/calls/CometChatOutgoingCall/CometChatOutgoingCall.js +4 -3
  31. package/lib/calls/CometChatOutgoingCall/CometChatOutgoingCall.js.map +1 -1
  32. package/lib/extensions/Polls/Polls.js +38 -17
  33. package/lib/extensions/Polls/Polls.js.map +1 -1
  34. package/lib/index.d.ts +5 -1
  35. package/lib/index.js +6 -2
  36. package/lib/index.js.map +1 -1
  37. package/lib/shared/constants/UIKitConstants.d.ts +16 -0
  38. package/lib/shared/constants/UIKitConstants.js +16 -0
  39. package/lib/shared/constants/UIKitConstants.js.map +1 -1
  40. package/lib/shared/events/CometChatMessageEvents/index.d.ts +3 -0
  41. package/lib/shared/events/CometChatMessageEvents/index.js +3 -0
  42. package/lib/shared/events/CometChatMessageEvents/index.js.map +1 -1
  43. package/lib/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.js +3 -0
  44. package/lib/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.js.map +1 -1
  45. package/lib/shared/events/CometChatUIEventHandler/Listener.d.ts +3 -1
  46. package/lib/shared/events/CometChatUIEventHandler/Listener.js +4 -1
  47. package/lib/shared/events/CometChatUIEventHandler/Listener.js.map +1 -1
  48. package/lib/shared/events/ListenerInitializer.d.ts +3 -1
  49. package/lib/shared/events/ListenerInitializer.js +5 -1
  50. package/lib/shared/events/ListenerInitializer.js.map +1 -1
  51. package/lib/shared/events/messages.d.ts +2 -1
  52. package/lib/shared/events/messages.js +1 -0
  53. package/lib/shared/events/messages.js.map +1 -1
  54. package/lib/shared/framework/DataSource.d.ts +3 -1
  55. package/lib/shared/framework/DataSourceDecorator.d.ts +3 -1
  56. package/lib/shared/framework/DataSourceDecorator.js +8 -2
  57. package/lib/shared/framework/DataSourceDecorator.js.map +1 -1
  58. package/lib/shared/framework/MessageDataSource.d.ts +6 -0
  59. package/lib/shared/framework/MessageDataSource.js +64 -1
  60. package/lib/shared/framework/MessageDataSource.js.map +1 -1
  61. package/lib/shared/icons/components/ai-chat-history.d.ts +8 -0
  62. package/lib/shared/icons/components/ai-chat-history.js +9 -0
  63. package/lib/shared/icons/components/ai-chat-history.js.map +1 -0
  64. package/lib/shared/icons/components/ai-copy-option.d.ts +8 -0
  65. package/lib/shared/icons/components/ai-copy-option.js +9 -0
  66. package/lib/shared/icons/components/ai-copy-option.js.map +1 -0
  67. package/lib/shared/icons/components/ai-new-chat.d.ts +8 -0
  68. package/lib/shared/icons/components/ai-new-chat.js +9 -0
  69. package/lib/shared/icons/components/ai-new-chat.js.map +1 -0
  70. package/lib/shared/icons/components/ai-send-button.d.ts +8 -0
  71. package/lib/shared/icons/components/ai-send-button.js +9 -0
  72. package/lib/shared/icons/components/ai-send-button.js.map +1 -0
  73. package/lib/shared/icons/components/right-arrow.d.ts +3 -0
  74. package/lib/shared/icons/components/right-arrow.js +6 -0
  75. package/lib/shared/icons/components/right-arrow.js.map +1 -0
  76. package/lib/shared/icons/components/stop-fill.js +8 -7
  77. package/lib/shared/icons/components/stop-fill.js.map +1 -1
  78. package/lib/shared/icons/icon-mapping.d.ts +11 -0
  79. package/lib/shared/icons/icon-mapping.js +11 -0
  80. package/lib/shared/icons/icon-mapping.js.map +1 -1
  81. package/lib/shared/modals/CometChatAIAssistantTools.d.ts +47 -0
  82. package/lib/shared/modals/CometChatAIAssistantTools.js +44 -0
  83. package/lib/shared/modals/CometChatAIAssistantTools.js.map +1 -0
  84. package/lib/shared/modals/StreamMessage.d.ts +4 -0
  85. package/lib/shared/modals/StreamMessage.js +11 -0
  86. package/lib/shared/modals/StreamMessage.js.map +1 -0
  87. package/lib/shared/modals/index.d.ts +3 -1
  88. package/lib/shared/modals/index.js +3 -1
  89. package/lib/shared/modals/index.js.map +1 -1
  90. package/lib/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.d.ts +126 -0
  91. package/lib/shared/resources/CometChatLocalizeNew/resources/de/translation.json +7 -1
  92. package/lib/shared/resources/CometChatLocalizeNew/resources/en/translation.json +7 -1
  93. package/lib/shared/resources/CometChatLocalizeNew/resources/es/translation.json +7 -1
  94. package/lib/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +7 -1
  95. package/lib/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +7 -1
  96. package/lib/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +7 -1
  97. package/lib/shared/resources/CometChatLocalizeNew/resources/it/translation.json +7 -1
  98. package/lib/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +7 -1
  99. package/lib/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +7 -1
  100. package/lib/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +7 -1
  101. package/lib/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +7 -1
  102. package/lib/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +7 -1
  103. package/lib/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +7 -1
  104. package/lib/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +7 -1
  105. package/lib/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +7 -1
  106. package/lib/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +7 -1
  107. package/lib/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +7 -1
  108. package/lib/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +7 -1
  109. package/lib/shared/services/stream-message.service.d.ts +84 -0
  110. package/lib/shared/services/stream-message.service.js +406 -0
  111. package/lib/shared/services/stream-message.service.js.map +1 -0
  112. package/lib/shared/views/CometChatAIAssistantMessageBubble/CometChatAIAssistantMessageBubble.d.ts +9 -0
  113. package/lib/shared/views/CometChatAIAssistantMessageBubble/CometChatAIAssistantMessageBubble.js +248 -0
  114. package/lib/shared/views/CometChatAIAssistantMessageBubble/CometChatAIAssistantMessageBubble.js.map +1 -0
  115. package/lib/shared/views/CometChatAIAssistantMessageBubble/index.d.ts +3 -0
  116. package/lib/shared/views/CometChatAIAssistantMessageBubble/index.js +4 -0
  117. package/lib/shared/views/CometChatAIAssistantMessageBubble/index.js.map +1 -0
  118. package/lib/shared/views/CometChatEmojiKeyboard/CometChatEmojiKeyboard.js +5 -2
  119. package/lib/shared/views/CometChatEmojiKeyboard/CometChatEmojiKeyboard.js.map +1 -1
  120. package/lib/shared/views/CometChatImageBubble/CometChatImageLoader.js +1 -1
  121. package/lib/shared/views/CometChatImageBubble/CometChatImageLoader.js.map +1 -1
  122. package/lib/shared/views/CometChatImageViewerModal/ImageViewerModal.js +52 -32
  123. package/lib/shared/views/CometChatImageViewerModal/ImageViewerModal.js.map +1 -1
  124. package/lib/shared/views/CometChatSendButtonView/CometChatSendButtonView.d.ts +10 -0
  125. package/lib/shared/views/CometChatSendButtonView/CometChatSendButtonView.js +54 -0
  126. package/lib/shared/views/CometChatSendButtonView/CometChatSendButtonView.js.map +1 -0
  127. package/lib/shared/views/CometChatSendButtonView/index.d.ts +1 -0
  128. package/lib/shared/views/CometChatSendButtonView/index.js +2 -0
  129. package/lib/shared/views/CometChatSendButtonView/index.js.map +1 -0
  130. package/lib/shared/views/CometChatStreamMessageBubble/CometChatStreamMessageBubble.d.ts +8 -0
  131. package/lib/shared/views/CometChatStreamMessageBubble/CometChatStreamMessageBubble.js +436 -0
  132. package/lib/shared/views/CometChatStreamMessageBubble/CometChatStreamMessageBubble.js.map +1 -0
  133. package/lib/shared/views/CometChatStreamMessageBubble/index.d.ts +3 -0
  134. package/lib/shared/views/CometChatStreamMessageBubble/index.js +3 -0
  135. package/lib/shared/views/CometChatStreamMessageBubble/index.js.map +1 -0
  136. package/lib/shared/views/CometChatTextBubble/CometChatTextBubble.d.ts +14 -6
  137. package/lib/shared/views/CometChatTextBubble/CometChatTextBubble.js +82 -57
  138. package/lib/shared/views/CometChatTextBubble/CometChatTextBubble.js.map +1 -1
  139. package/lib/theme/context.d.ts +96 -15
  140. package/lib/theme/type.d.ts +14 -1
  141. package/package.json +6 -3
  142. package/src/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.tsx +627 -0
  143. package/src/CometChatAIAssistantChatHistory/Skeleton.tsx +228 -0
  144. package/src/CometChatAIAssistantChatHistory/index.ts +2 -0
  145. package/src/CometChatAIAssistantChatHistory/style.ts +160 -0
  146. package/src/CometChatConversations/CometChatConversations.tsx +36 -10
  147. package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +137 -19
  148. package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +106 -18
  149. package/src/CometChatMessageList/CometChatMessageList.tsx +721 -116
  150. package/src/CometChatMessageList/style.ts +74 -2
  151. package/src/calls/CometChatIncomingCall/CometChatIncomingCall.tsx +13 -29
  152. package/src/calls/CometChatOutgoingCall/CometChatOutgoingCall.tsx +8 -5
  153. package/src/extensions/Polls/Polls.tsx +50 -22
  154. package/src/index.ts +31 -1
  155. package/src/shared/constants/UIKitConstants.ts +17 -0
  156. package/src/shared/events/CometChatMessageEvents/index.ts +3 -1
  157. package/src/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.ts +3 -0
  158. package/src/shared/events/CometChatUIEventHandler/Listener.ts +4 -0
  159. package/src/shared/events/ListenerInitializer.ts +6 -1
  160. package/src/shared/events/messages.ts +1 -0
  161. package/src/shared/framework/DataSource.ts +4 -2
  162. package/src/shared/framework/DataSourceDecorator.tsx +10 -2
  163. package/src/shared/framework/MessageDataSource.tsx +75 -2
  164. package/src/shared/icons/Icon.tsx +1 -1
  165. package/src/shared/icons/components/ai-chat-history.tsx +21 -0
  166. package/src/shared/icons/components/ai-copy-option.tsx +21 -0
  167. package/src/shared/icons/components/ai-new-chat.tsx +21 -0
  168. package/src/shared/icons/components/ai-send-button.tsx +24 -0
  169. package/src/shared/icons/components/right-arrow.tsx +13 -0
  170. package/src/shared/icons/components/stop-fill.tsx +14 -12
  171. package/src/shared/icons/icon-mapping.tsx +14 -0
  172. package/src/shared/modals/CometChatAIAssistantTools.ts +65 -0
  173. package/src/shared/modals/StreamMessage.ts +12 -0
  174. package/src/shared/modals/index.ts +5 -1
  175. package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +7 -1
  176. package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +7 -1
  177. package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +7 -1
  178. package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +7 -1
  179. package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +7 -2
  180. package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +7 -1
  181. package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +7 -1
  182. package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +7 -1
  183. package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +7 -1
  184. package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +7 -1
  185. package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +7 -1
  186. package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +7 -1
  187. package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +7 -1
  188. package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +7 -1
  189. package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +7 -1
  190. package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +7 -1
  191. package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +7 -1
  192. package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +7 -1
  193. package/src/shared/services/stream-message.service.ts +470 -0
  194. package/src/shared/views/CometChatAIAssistantMessageBubble/CometChatAIAssistantMessageBubble.tsx +262 -0
  195. package/src/shared/views/CometChatAIAssistantMessageBubble/index.ts +4 -0
  196. package/src/shared/views/CometChatEmojiKeyboard/CometChatEmojiKeyboard.tsx +5 -1
  197. package/src/shared/views/CometChatImageBubble/CometChatImageLoader.tsx +1 -0
  198. package/src/shared/views/CometChatImageViewerModal/ImageViewerModal.tsx +57 -45
  199. package/src/shared/views/CometChatSendButtonView/CometChatSendButtonView.tsx +86 -0
  200. package/src/shared/views/CometChatSendButtonView/index.ts +1 -0
  201. package/src/shared/views/CometChatStreamMessageBubble/CometChatStreamMessageBubble.tsx +493 -0
  202. package/src/shared/views/CometChatStreamMessageBubble/index.ts +9 -0
  203. package/src/shared/views/CometChatTextBubble/CometChatTextBubble.tsx +121 -74
  204. package/src/theme/type.ts +14 -1
  205. package/lib/shared/resources/CometChatLocalize/CometChatLocalize.d.ts +0 -79
  206. package/lib/shared/resources/CometChatLocalize/CometChatLocalize.js +0 -152
  207. package/lib/shared/resources/CometChatLocalize/CometChatLocalize.js.map +0 -1
  208. package/lib/shared/resources/CometChatLocalize/index.d.ts +0 -2
  209. package/lib/shared/resources/CometChatLocalize/index.js +0 -3
  210. package/lib/shared/resources/CometChatLocalize/index.js.map +0 -1
  211. package/lib/shared/resources/CometChatLocalize/resources/ar/translation.json +0 -447
  212. package/lib/shared/resources/CometChatLocalize/resources/de/translation.json +0 -447
  213. package/lib/shared/resources/CometChatLocalize/resources/en/translation.json +0 -447
  214. package/lib/shared/resources/CometChatLocalize/resources/es/translation.json +0 -447
  215. package/lib/shared/resources/CometChatLocalize/resources/fr/translation.json +0 -446
  216. package/lib/shared/resources/CometChatLocalize/resources/hi/translation.json +0 -447
  217. package/lib/shared/resources/CometChatLocalize/resources/hu/translation.json +0 -447
  218. package/lib/shared/resources/CometChatLocalize/resources/lt/translation.json +0 -447
  219. package/lib/shared/resources/CometChatLocalize/resources/ms/translation.json +0 -447
  220. package/lib/shared/resources/CometChatLocalize/resources/pt/translation.json +0 -447
  221. package/lib/shared/resources/CometChatLocalize/resources/ru/translation.json +0 -447
  222. package/lib/shared/resources/CometChatLocalize/resources/sv/translation.json +0 -447
  223. package/lib/shared/resources/CometChatLocalize/resources/zh/translation.json +0 -447
  224. package/lib/shared/resources/CometChatLocalize/resources/zh-tw/translation.json +0 -446
  225. package/lib/shared/utils/CometChatMessagePreview/MessagePreviewStyle.d.ts +0 -29
  226. package/lib/shared/utils/CometChatMessagePreview/MessagePreviewStyle.js +0 -29
  227. package/lib/shared/utils/CometChatMessagePreview/MessagePreviewStyle.js.map +0 -1
@@ -0,0 +1,627 @@
1
+ import React, { useState, useRef, useCallback, useEffect, useMemo, JSX } from 'react';
2
+ import {
3
+ View,
4
+ Text,
5
+ SectionList,
6
+ TouchableOpacity,
7
+ ActivityIndicator,
8
+ GestureResponderEvent,
9
+ } from 'react-native';
10
+ import { CometChat } from "@cometchat/chat-sdk-react-native";
11
+ import { useTheme } from "../theme";
12
+ import { Icon } from "../shared/icons/Icon";
13
+ import { deepMerge } from "../shared/helper/helperFunctions";
14
+ import { DeepPartial } from "../shared/helper/types";
15
+ import { ChatHistoryStyle, getChatHistoryStyleLight } from "./style";
16
+ import { Skeleton } from "./Skeleton";
17
+ import { CometChatTooltipMenu } from "../shared/views/CometChatTooltipMenu";
18
+ import { CometChatConfirmDialog } from "../shared/views/CometChatConfirmDialog";
19
+ import Delete from "../shared/icons/components/delete";
20
+ import { useCometChatTranslation } from "../shared/resources/CometChatLocalizeNew";
21
+
22
+
23
+ interface CometChatAIAssistantChatHistoryProps {
24
+ /**
25
+ * A `CometChat.User` object representing the participant of the chat whose message history is displayed.
26
+ */
27
+ user?: CometChat.User;
28
+
29
+ /**
30
+ * A `CometChat.Group` object representing the group whose message history is displayed.
31
+ */
32
+ group?: CometChat.Group;
33
+
34
+ /**
35
+ * Callback function triggered when an error occurs during message fetching.
36
+ */
37
+ onError?: ((error: CometChat.CometChatException) => void) | null;
38
+
39
+ /**
40
+ * Callback function triggered when clicked on closeIcon button
41
+ */
42
+ onClose?: (() => void) | undefined;
43
+
44
+ /**
45
+ * Callback function triggered when clicked on a message
46
+ */
47
+ onMessageClicked?: ((message: CometChat.BaseMessage) => void) | undefined;
48
+
49
+ /**
50
+ * Callback when agent new chat button is clicked
51
+ */
52
+ onNewChatButtonClick?: () => void;
53
+
54
+ /**
55
+ * Custom styling for the chat history component.
56
+ */
57
+ style?: DeepPartial<ChatHistoryStyle>;
58
+ /**
59
+ * Custom loading state view component
60
+ */
61
+ LoadingStateView?: () => JSX.Element;
62
+ }
63
+
64
+ enum States {
65
+ loading = "loading",
66
+ loaded = "loaded",
67
+ empty = "empty",
68
+ error = "error"
69
+ }
70
+
71
+ interface GroupedMessage {
72
+ title: string;
73
+ data: CometChat.TextMessage[];
74
+ }
75
+
76
+ const CometChatAIAssistantChatHistory: React.FC<CometChatAIAssistantChatHistoryProps> = (props) => {
77
+ const {
78
+ user,
79
+ group,
80
+ onError,
81
+ onClose,
82
+ onMessageClicked,
83
+ onNewChatButtonClick,
84
+ style = {},
85
+ LoadingStateView
86
+ } = props;
87
+
88
+ const theme = useTheme();
89
+ const {t}=useCometChatTranslation();
90
+
91
+ // Create fallback styles if theme.chatHistoryStyles doesn't exist
92
+ const defaultStyles = useMemo(() => {
93
+ return (theme as any).chatHistoryStyles || getChatHistoryStyleLight(theme.color, theme.spacing, theme.typography);
94
+ }, [theme]);
95
+
96
+ const mergedStyle = deepMerge(defaultStyles, style);
97
+
98
+ // State variables
99
+ // Only show text messages in history
100
+ const [messageList, setMessageList] = useState<CometChat.TextMessage[]>([]);
101
+ const [listState, setListState] = useState<States>(States.loading);
102
+ const [isLoadingMore, setIsLoadingMore] = useState(false);
103
+
104
+ // Delete functionality states
105
+ const [confirmDelete, setConfirmDelete] = useState<number | undefined>(undefined);
106
+ const [tooltipVisible, setTooltipVisible] = useState(false);
107
+ const longPressedMessage = useRef<CometChat.BaseMessage | undefined>(undefined);
108
+ const tooltipPosition = useRef({
109
+ pageX: 0,
110
+ pageY: 0,
111
+ });
112
+
113
+ // Refs
114
+ const messageListBuilderRef = useRef<CometChat.MessagesRequest | null>(null);
115
+ const loggedInUserRef = useRef<CometChat.User | null>(null);
116
+ const isFetchingRef = useRef<boolean>(false);
117
+ const messagesCountRef = useRef<number>(0);
118
+
119
+ // Error handler
120
+ const errorHandler = useCallback((error: any, context: string) => {
121
+ console.log(`Error in ${context}:`, error);
122
+ if (onError) {
123
+ onError(error);
124
+ }
125
+ }, [onError]);
126
+
127
+ /**
128
+ * Function to delete a message
129
+ */
130
+ const deleteMessage = useCallback((messageId: number) => {
131
+ const messageToDelete = messageList.find(msg => msg.getId() === messageId);
132
+ if (!messageToDelete) return;
133
+
134
+ CometChat.deleteMessage(messageId.toString())
135
+ .then((deletedMessage) => {
136
+ setMessageList(prevList => {
137
+ const newList = prevList.filter(msg => msg.getId() !== messageId);
138
+ messagesCountRef.current = newList.length;
139
+ if (newList.length === 0) {
140
+ setListState(States.empty);
141
+ // Call parent to reset agentic/parent state
142
+ if (onNewChatButtonClick) {
143
+ onNewChatButtonClick();
144
+ }
145
+ }
146
+ return newList;
147
+ });
148
+ })
149
+ .catch((error) => {
150
+ errorHandler(error, "deleteMessage");
151
+ });
152
+ }, [messageList, errorHandler]);
153
+
154
+ /**
155
+ * Function to get formatted date label
156
+ */
157
+ const getDateLabel = useCallback((timestamp: number): string => {
158
+ const messageDate = new Date(timestamp * 1000);
159
+ const today = new Date();
160
+ const yesterday = new Date(today);
161
+ yesterday.setDate(yesterday.getDate() - 1);
162
+
163
+ if (messageDate.toDateString() === today.toDateString()) {
164
+ return 'Today';
165
+ } else if (messageDate.toDateString() === yesterday.toDateString()) {
166
+ return 'Yesterday';
167
+ } else {
168
+ // Check if it's within the last week
169
+ const oneWeekAgo = new Date(today);
170
+ oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
171
+
172
+ if (messageDate >= oneWeekAgo) {
173
+ return messageDate.toLocaleDateString('en-US', { weekday: 'long' });
174
+ } else {
175
+ return messageDate.toLocaleDateString('en-US', {
176
+ month: 'long',
177
+ day: 'numeric',
178
+ year: messageDate.getFullYear() !== today.getFullYear() ? 'numeric' : undefined
179
+ });
180
+ }
181
+ }
182
+ }, []);
183
+
184
+ /**
185
+ * Function to group messages by date
186
+ */
187
+ const groupedMessages = useMemo((): GroupedMessage[] => {
188
+ const groups: { [key: string]: CometChat.TextMessage[] } = {};
189
+
190
+ messageList.forEach(message => {
191
+ const label = getDateLabel(message.getSentAt());
192
+ if (!groups[label]) {
193
+ groups[label] = [];
194
+ }
195
+ groups[label].push(message);
196
+ });
197
+
198
+ // Sort groups by date priority
199
+ const sortOrder = ['Today', 'Yesterday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
200
+
201
+ return Object.entries(groups)
202
+ .sort(([a], [b]) => {
203
+ const aIndex = sortOrder.indexOf(a);
204
+ const bIndex = sortOrder.indexOf(b);
205
+
206
+ if (aIndex !== -1 && bIndex !== -1) {
207
+ return aIndex - bIndex;
208
+ } else if (aIndex !== -1) {
209
+ return -1;
210
+ } else if (bIndex !== -1) {
211
+ return 1;
212
+ } else {
213
+ // Both are dates, sort by most recent first
214
+ return b.localeCompare(a);
215
+ }
216
+ })
217
+ .map(([title, data]) => ({ title, data }));
218
+ }, [messageList, getDateLabel]);
219
+
220
+ /**
221
+ * Function to extract text content from a message
222
+ */
223
+ const getMessageText = useCallback((message: CometChat.BaseMessage): string => {
224
+ try {
225
+ if (message instanceof CometChat.TextMessage) {
226
+ return message.getText();
227
+ } else if (message instanceof CometChat.MediaMessage) {
228
+ return `${message.getType()} message`;
229
+ } else if (message instanceof CometChat.CustomMessage) {
230
+ // Handle assistant messages
231
+ if (message.getType() === 'assistant' && message.getData()) {
232
+ const data = message.getData();
233
+ if (data.assistantMessageData?.getText) {
234
+ return data.assistantMessageData.getText();
235
+ } else if (data.text) {
236
+ return data.text;
237
+ }
238
+ }
239
+ return 'Custom message';
240
+ } else if (message.getType() === 'groupMember') {
241
+ return 'Group action message';
242
+ } else {
243
+ return 'Message';
244
+ }
245
+ } catch (error) {
246
+ errorHandler(error, "getMessageText");
247
+ return 'Message';
248
+ }
249
+ }, [errorHandler]);
250
+
251
+ /**
252
+ * Function to check if message can be deleted
253
+ */
254
+ const canDeleteMessage = useCallback((message: CometChat.BaseMessage): boolean => {
255
+ // Only allow deletion if user is the sender, message is not deleted, and not system/group
256
+ const isSender = message.getSender().getUid() === loggedInUserRef.current?.getUid();
257
+ const isDeletableCategory = message.getCategory && message.getCategory() === 'message';
258
+ const isNotDeleted = !message.getDeletedAt || !message.getDeletedAt();
259
+ return isSender && isDeletableCategory && isNotDeleted;
260
+ }, []);
261
+
262
+ /**
263
+ * Function to append messages to the end of the current message list
264
+ */
265
+ const appendMessages = useCallback(
266
+ (messages: CometChat.BaseMessage[]) => {
267
+ return new Promise<boolean>((resolve) => {
268
+ try {
269
+ // Only keep text messages with non-empty text, safely check getText
270
+ const textMessages = messages.filter(
271
+ m =>
272
+ m instanceof CometChat.TextMessage &&
273
+ typeof m.getText === "function" &&
274
+ typeof m.getText() === "string" &&
275
+ m.getText().trim() !== ""
276
+ ) as CometChat.TextMessage[];
277
+ setMessageList((prevMessageList: CometChat.TextMessage[]) => {
278
+ const newList = [...prevMessageList, ...textMessages];
279
+ messagesCountRef.current = newList.length;
280
+ return newList;
281
+ });
282
+ resolve(true);
283
+ } catch (error: any) {
284
+ errorHandler(error, "appendMessages");
285
+ resolve(false);
286
+ }
287
+ });
288
+ },
289
+ [errorHandler]
290
+ );
291
+
292
+ /**
293
+ * Function to fetch previous messages
294
+ */
295
+ const fetchPreviousMessages = useCallback(() => {
296
+ return new Promise<boolean>(async (resolve, reject) => {
297
+ try {
298
+ if (isFetchingRef.current) {
299
+ resolve(true);
300
+ return;
301
+ }
302
+
303
+ isFetchingRef.current = true;
304
+ setIsLoadingMore(messagesCountRef.current > 0);
305
+
306
+ if (messageListBuilderRef.current) {
307
+ const messages = await messageListBuilderRef.current.fetchPrevious();
308
+
309
+ if (messages.length > 0) {
310
+ await appendMessages(messages.reverse());
311
+ setListState(States.loaded);
312
+ } else {
313
+ if (messages.length == 0 && messagesCountRef.current === 0) {
314
+ setListState(States.empty);
315
+ } else {
316
+ setListState(States.loaded);
317
+ }
318
+ }
319
+ }
320
+
321
+ isFetchingRef.current = false;
322
+ setIsLoadingMore(false);
323
+ resolve(true);
324
+ } catch (error: any) {
325
+ isFetchingRef.current = false;
326
+ setIsLoadingMore(false);
327
+ if (messagesCountRef.current <= 0) {
328
+ setListState(States.error);
329
+ }
330
+ errorHandler(error, "fetchPreviousMessages");
331
+ reject(error);
332
+ }
333
+ });
334
+ }, [appendMessages, errorHandler]);
335
+
336
+ /**
337
+ * Callback to be executed when the list is scrolled to the end
338
+ */
339
+ const onEndReached = useCallback(() => {
340
+ if (listState === States.loaded && !isFetchingRef.current) {
341
+ fetchPreviousMessages();
342
+ }
343
+ }, [fetchPreviousMessages, listState]);
344
+
345
+ /**
346
+ * Function to render section header
347
+ */
348
+ const renderSectionHeader = useCallback(({ section }: { section: GroupedMessage }) => {
349
+ return (
350
+ <View style={mergedStyle.sectionHeaderStyle}>
351
+ <Text style={mergedStyle.sectionHeaderTextStyle}>
352
+ {section.title}
353
+ </Text>
354
+ </View>
355
+ );
356
+ }, [mergedStyle]);
357
+
358
+ /**
359
+ * Function to render message item
360
+ */
361
+ const renderMessageItem = useCallback(({ item: message }: { item: CometChat.TextMessage }) => {
362
+ // Only render if message is a text message with non-empty text
363
+ if (!(message instanceof CometChat.TextMessage)) return null;
364
+ const messageText = getMessageText(message);
365
+ if (
366
+ !messageText ||
367
+ typeof messageText !== "string" ||
368
+ messageText.trim() === ""
369
+ )
370
+ return null;
371
+ const deletable = canDeleteMessage(message);
372
+ return (
373
+ <TouchableOpacity
374
+ style={mergedStyle.messageItemStyle}
375
+ onPress={() => {
376
+ if (onMessageClicked) {
377
+ onMessageClicked(message);
378
+ }
379
+ }}
380
+ onLongPress={(e: GestureResponderEvent) => {
381
+ if (deletable) {
382
+ longPressedMessage.current = message;
383
+ tooltipPosition.current = {
384
+ pageX: e.nativeEvent.pageX,
385
+ pageY: e.nativeEvent.pageY,
386
+ };
387
+ setTooltipVisible(true);
388
+ }
389
+ }}
390
+ >
391
+ <Text
392
+ style={mergedStyle.messageTextStyle}
393
+ numberOfLines={1}
394
+ ellipsizeMode="tail"
395
+ >
396
+ {messageText}
397
+ </Text>
398
+ </TouchableOpacity>
399
+ );
400
+ }, [getMessageText, mergedStyle, onMessageClicked, canDeleteMessage]);
401
+
402
+
403
+ /**
404
+ * Initialize component and fetch logged-in user
405
+ */
406
+ useEffect(() => {
407
+ CometChat.getLoggedinUser()
408
+ .then((userObject: CometChat.User | null) => {
409
+ if (userObject) {
410
+ loggedInUserRef.current = userObject;
411
+ }
412
+ })
413
+ .catch((error: CometChat.CometChatException) => {
414
+ errorHandler(error, "getLoggedinUser");
415
+ });
416
+ }, [user, group, errorHandler]);
417
+
418
+ /**
419
+ * Initialize message list manager when user or group changes
420
+ */
421
+ useEffect(() => {
422
+ const initializeChat = async () => {
423
+ try {
424
+ if (user || group) {
425
+ // Create messages request builder with hideReplies set to true to only show parent messages
426
+ const builder = new CometChat.MessagesRequestBuilder()
427
+ .hideReplies(true)
428
+ .setLimit(30)
429
+ if (user) {
430
+ builder.setUID(user.getUid());
431
+ } else if (group) {
432
+ builder.setGUID(group.getGuid());
433
+ }
434
+
435
+ messageListBuilderRef.current = builder.build();
436
+ messagesCountRef.current = 0;
437
+ setMessageList([]);
438
+ setListState(States.loading);
439
+
440
+ // Fetch initial messages
441
+ if (!isFetchingRef.current) {
442
+ isFetchingRef.current = true;
443
+ try {
444
+ const messages = await messageListBuilderRef.current.fetchPrevious();
445
+ if (messages.length > 0) {
446
+ const textMessages = messages
447
+ .reverse()
448
+ .filter(
449
+ m =>
450
+ m instanceof CometChat.TextMessage &&
451
+ typeof m.getText === "function" &&
452
+ typeof m.getText() === "string" &&
453
+ m.getText().trim() !== ""
454
+ ) as CometChat.TextMessage[];
455
+ setMessageList(textMessages);
456
+ messagesCountRef.current = textMessages.length;
457
+ setListState(textMessages.length > 0 ? States.loaded : States.empty);
458
+ } else {
459
+ setListState(States.empty);
460
+ }
461
+ } catch (error) {
462
+ setListState(States.error);
463
+ errorHandler(error, "initial fetch");
464
+ } finally {
465
+ isFetchingRef.current = false;
466
+ }
467
+ }
468
+ }
469
+ } catch (error) {
470
+ errorHandler(error, "useEffect - initialization");
471
+ }
472
+ };
473
+
474
+ initializeChat();
475
+ }, [user, group, errorHandler]);
476
+
477
+ const getLoadingView = () => {
478
+ if (LoadingStateView) return <LoadingStateView />;
479
+ return (
480
+ <Skeleton
481
+ style={{
482
+ containerBackgroundColor: mergedStyle.containerStyle?.backgroundColor || theme.color.background3,
483
+ }}
484
+ />
485
+ );
486
+ };
487
+
488
+ const getEmptyView = () => {
489
+ return (
490
+ <View style={mergedStyle.centerContainerStyle}>
491
+ <Text style={mergedStyle.stateTitleStyle}>
492
+ {t("NO_CONVERSATION_HISTORY_FOUND")}
493
+ </Text>
494
+ <Text style={mergedStyle.stateTextStyle}>
495
+ {t("START_A_CHAT")}
496
+ </Text>
497
+ </View>
498
+ );
499
+ };
500
+
501
+ const getErrorView = () => {
502
+ return (
503
+ <View style={mergedStyle.centerContainerStyle}>
504
+ <Text style={[mergedStyle.stateTitleStyle, { color: theme.color.error }]}>
505
+ {t("OOPS!")}
506
+ </Text>
507
+ <Text style={mergedStyle.stateTextStyle}>
508
+ {t("LOOKS_LIKE_SOMETHING_WENT_WRONG")}
509
+ </Text>
510
+ </View>
511
+ );
512
+ };
513
+
514
+ // const renderFooter = () => {
515
+ // if (isLoadingMore) {
516
+ // return (
517
+ // <View style={mergedStyle.footerLoaderStyle}>
518
+ // <ActivityIndicator size="small" color={theme.color.primary} />
519
+ // </View>
520
+ // );
521
+ // }
522
+ // return null;
523
+ // };
524
+
525
+ const keyExtractor = useCallback((item: CometChat.TextMessage, index: number) => {
526
+ return `${item.getId()}_${item.getMuid()}_${index}`;
527
+ }, []);
528
+
529
+ return (
530
+ <View style={mergedStyle.containerStyle}>
531
+ {/* Tooltip Menu for Delete Option */}
532
+ {/* Only show tooltip menu if the message is deletable */}
533
+ {longPressedMessage.current && canDeleteMessage(longPressedMessage.current) && (
534
+ <CometChatTooltipMenu
535
+ visible={tooltipVisible}
536
+ onClose={() => {
537
+ setTooltipVisible(false);
538
+ }}
539
+ event={{
540
+ nativeEvent: tooltipPosition.current,
541
+ }}
542
+ menuItems={[{
543
+ text: "Delete",
544
+ onPress: () => {
545
+ setConfirmDelete(longPressedMessage.current?.getId());
546
+ setTooltipVisible(false);
547
+ },
548
+ icon: (
549
+ <Delete
550
+ color={theme.color.error}
551
+ height={theme.spacing.spacing.s6}
552
+ width={theme.spacing.spacing.s6}
553
+ />
554
+ ),
555
+ textStyle: { color: theme.color.error },
556
+ }]}
557
+ />
558
+ )}
559
+
560
+ {/* Confirm Delete Dialog */}
561
+ <CometChatConfirmDialog
562
+ titleText="Delete Message"
563
+ icon={<Icon name='delete' size={theme.spacing.spacing.s12} color={theme.color.error} />}
564
+ cancelButtonText="Cancel"
565
+ confirmButtonText="Delete"
566
+ messageText="Are you sure you want to delete this message?"
567
+ isOpen={confirmDelete !== undefined}
568
+ onCancel={() => setConfirmDelete(undefined)}
569
+ onConfirm={() => {
570
+ if (confirmDelete) {
571
+ deleteMessage(confirmDelete);
572
+ }
573
+ setConfirmDelete(undefined);
574
+ }}
575
+ />
576
+
577
+ {/* Header */}
578
+ <View style={mergedStyle.headerStyle}>
579
+ <View style={mergedStyle.headerContentStyle}>
580
+ <TouchableOpacity
581
+ style={mergedStyle.closeButtonStyle}
582
+ onPress={onClose}
583
+ >
584
+ <Icon name="close-fill" width={24} height={24} color={theme.color.textSecondary} />
585
+ </TouchableOpacity>
586
+ <Text style={mergedStyle.headerTitleStyle}>
587
+ {t("CHAT_HISTORY")}
588
+ </Text>
589
+ </View>
590
+ </View>
591
+
592
+ {/* New Chat Button */}
593
+ <TouchableOpacity
594
+ style={mergedStyle.newChatButtonStyle}
595
+ onPress={onNewChatButtonClick}
596
+ >
597
+ <Icon name="ai-new-chat" width={24} height={24} color={theme.color.textSecondary} />
598
+ <Text style={mergedStyle.newChatTextStyle}>
599
+ {t("NEW_CHAT")}
600
+ </Text>
601
+ </TouchableOpacity>
602
+
603
+ {/* Content */}
604
+ {listState === States.loading ? (
605
+ getLoadingView()
606
+ ) : listState === States.error ? (
607
+ getErrorView()
608
+ ) : listState === States.empty ? (
609
+ getEmptyView()
610
+ ) : (
611
+ <SectionList
612
+ sections={groupedMessages}
613
+ renderItem={renderMessageItem}
614
+ renderSectionHeader={renderSectionHeader}
615
+ keyExtractor={keyExtractor}
616
+ onEndReached={onEndReached}
617
+ onEndReachedThreshold={0.1}
618
+ showsVerticalScrollIndicator={false}
619
+ contentContainerStyle={mergedStyle.listContainerStyle}
620
+ stickySectionHeadersEnabled={true}
621
+ />
622
+ )}
623
+ </View>
624
+ );
625
+ };
626
+
627
+ export { CometChatAIAssistantChatHistory };