@droppii-org/chat-mobile 0.2.79 → 0.2.80

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 (97) hide show
  1. package/lib/module/components/messages/BubbleTail.js +31 -0
  2. package/lib/module/components/messages/BubbleTail.js.map +1 -0
  3. package/lib/module/components/messages/BubbleTailRow.js +46 -0
  4. package/lib/module/components/messages/BubbleTailRow.js.map +1 -0
  5. package/lib/module/components/messages/bubbleTail.utils.js +19 -0
  6. package/lib/module/components/messages/bubbleTail.utils.js.map +1 -0
  7. package/lib/module/components/messages/fileMessage/index.js +72 -65
  8. package/lib/module/components/messages/fileMessage/index.js.map +1 -1
  9. package/lib/module/components/messages/imageMessage/index.js +23 -17
  10. package/lib/module/components/messages/imageMessage/index.js.map +1 -1
  11. package/lib/module/components/messages/linkMessage/index.js +11 -23
  12. package/lib/module/components/messages/linkMessage/index.js.map +1 -1
  13. package/lib/module/components/messages/mergedMessage/index.js +14 -7
  14. package/lib/module/components/messages/mergedMessage/index.js.map +1 -1
  15. package/lib/module/components/messages/quotedMessage/index.js +11 -23
  16. package/lib/module/components/messages/quotedMessage/index.js.map +1 -1
  17. package/lib/module/components/messages/revokedMessage/index.js +14 -7
  18. package/lib/module/components/messages/revokedMessage/index.js.map +1 -1
  19. package/lib/module/components/messages/stickerMessage/index.js +13 -7
  20. package/lib/module/components/messages/stickerMessage/index.js.map +1 -1
  21. package/lib/module/components/messages/textMessage/index.js +11 -26
  22. package/lib/module/components/messages/textMessage/index.js.map +1 -1
  23. package/lib/module/components/messages/types.js.map +1 -1
  24. package/lib/module/components/messages/videoMessage/index.js +16 -8
  25. package/lib/module/components/messages/videoMessage/index.js.map +1 -1
  26. package/lib/module/hooks/groups/useDismissGroup.js +2 -1
  27. package/lib/module/hooks/groups/useDismissGroup.js.map +1 -1
  28. package/lib/module/screens/chat-detail/ChatListLegend.js +71 -21
  29. package/lib/module/screens/chat-detail/ChatListLegend.js.map +1 -1
  30. package/lib/module/screens/chat-detail/legend/LegendChatDay.js +8 -2
  31. package/lib/module/screens/chat-detail/legend/LegendChatDay.js.map +1 -1
  32. package/lib/module/screens/chat-detail/legend/LegendChatMessage.js +38 -13
  33. package/lib/module/screens/chat-detail/legend/LegendChatMessage.js.map +1 -1
  34. package/lib/module/utils/legendListMessage.js +86 -12
  35. package/lib/module/utils/legendListMessage.js.map +1 -1
  36. package/lib/module/utils/message.js +19 -1
  37. package/lib/module/utils/message.js.map +1 -1
  38. package/lib/module/utils/messageUtils.js +6 -0
  39. package/lib/module/utils/messageUtils.js.map +1 -1
  40. package/lib/typescript/src/components/messages/BubbleTail.d.ts +13 -0
  41. package/lib/typescript/src/components/messages/BubbleTail.d.ts.map +1 -0
  42. package/lib/typescript/src/components/messages/BubbleTailRow.d.ts +25 -0
  43. package/lib/typescript/src/components/messages/BubbleTailRow.d.ts.map +1 -0
  44. package/lib/typescript/src/components/messages/bubbleTail.utils.d.ts +11 -0
  45. package/lib/typescript/src/components/messages/bubbleTail.utils.d.ts.map +1 -0
  46. package/lib/typescript/src/components/messages/fileMessage/index.d.ts +1 -1
  47. package/lib/typescript/src/components/messages/fileMessage/index.d.ts.map +1 -1
  48. package/lib/typescript/src/components/messages/imageMessage/index.d.ts +1 -1
  49. package/lib/typescript/src/components/messages/imageMessage/index.d.ts.map +1 -1
  50. package/lib/typescript/src/components/messages/linkMessage/index.d.ts +1 -1
  51. package/lib/typescript/src/components/messages/linkMessage/index.d.ts.map +1 -1
  52. package/lib/typescript/src/components/messages/mergedMessage/index.d.ts +1 -1
  53. package/lib/typescript/src/components/messages/mergedMessage/index.d.ts.map +1 -1
  54. package/lib/typescript/src/components/messages/quotedMessage/index.d.ts.map +1 -1
  55. package/lib/typescript/src/components/messages/revokedMessage/index.d.ts +1 -1
  56. package/lib/typescript/src/components/messages/revokedMessage/index.d.ts.map +1 -1
  57. package/lib/typescript/src/components/messages/stickerMessage/index.d.ts +1 -1
  58. package/lib/typescript/src/components/messages/stickerMessage/index.d.ts.map +1 -1
  59. package/lib/typescript/src/components/messages/textMessage/index.d.ts +1 -1
  60. package/lib/typescript/src/components/messages/textMessage/index.d.ts.map +1 -1
  61. package/lib/typescript/src/components/messages/types.d.ts +3 -0
  62. package/lib/typescript/src/components/messages/types.d.ts.map +1 -1
  63. package/lib/typescript/src/components/messages/videoMessage/index.d.ts +1 -1
  64. package/lib/typescript/src/components/messages/videoMessage/index.d.ts.map +1 -1
  65. package/lib/typescript/src/hooks/groups/useDismissGroup.d.ts.map +1 -1
  66. package/lib/typescript/src/screens/chat-detail/ChatListLegend.d.ts.map +1 -1
  67. package/lib/typescript/src/screens/chat-detail/legend/LegendChatDay.d.ts +6 -1
  68. package/lib/typescript/src/screens/chat-detail/legend/LegendChatDay.d.ts.map +1 -1
  69. package/lib/typescript/src/screens/chat-detail/legend/LegendChatMessage.d.ts +14 -1
  70. package/lib/typescript/src/screens/chat-detail/legend/LegendChatMessage.d.ts.map +1 -1
  71. package/lib/typescript/src/utils/legendListMessage.d.ts +19 -4
  72. package/lib/typescript/src/utils/legendListMessage.d.ts.map +1 -1
  73. package/lib/typescript/src/utils/message.d.ts +17 -0
  74. package/lib/typescript/src/utils/message.d.ts.map +1 -1
  75. package/lib/typescript/src/utils/messageUtils.d.ts +1 -0
  76. package/lib/typescript/src/utils/messageUtils.d.ts.map +1 -1
  77. package/package.json +1 -1
  78. package/src/components/messages/BubbleTail.tsx +35 -0
  79. package/src/components/messages/BubbleTailRow.tsx +64 -0
  80. package/src/components/messages/bubbleTail.utils.ts +21 -0
  81. package/src/components/messages/fileMessage/index.tsx +90 -83
  82. package/src/components/messages/imageMessage/index.tsx +26 -23
  83. package/src/components/messages/linkMessage/index.tsx +6 -18
  84. package/src/components/messages/mergedMessage/index.tsx +27 -15
  85. package/src/components/messages/quotedMessage/index.tsx +7 -19
  86. package/src/components/messages/revokedMessage/index.tsx +23 -14
  87. package/src/components/messages/stickerMessage/index.tsx +16 -11
  88. package/src/components/messages/textMessage/index.tsx +6 -22
  89. package/src/components/messages/types.ts +3 -0
  90. package/src/components/messages/videoMessage/index.tsx +13 -10
  91. package/src/hooks/groups/useDismissGroup.tsx +1 -0
  92. package/src/screens/chat-detail/ChatListLegend.tsx +97 -28
  93. package/src/screens/chat-detail/legend/LegendChatDay.tsx +25 -15
  94. package/src/screens/chat-detail/legend/LegendChatMessage.tsx +62 -12
  95. package/src/utils/legendListMessage.ts +123 -14
  96. package/src/utils/message.ts +19 -1
  97. package/src/utils/messageUtils.ts +8 -0
@@ -6,6 +6,7 @@ import ChatImageDimens, {
6
6
  } from '../../../utils/chatImageDimens';
7
7
  import { CHAT_BUBBLE_COLORS } from '../../../screens/chat-detail/constants';
8
8
  import { messageStyles } from '../styles';
9
+ import { BubbleTailRow } from '../BubbleTailRow';
9
10
  import type { BaseLegendMessageProps } from '../types';
10
11
  const formatDuration = (durationMs: number): string => {
11
12
  const totalSeconds = Math.floor(durationMs / 1000);
@@ -15,7 +16,7 @@ const formatDuration = (durationMs: number): string => {
15
16
  };
16
17
 
17
18
  export const VideoMessage = memo(
18
- ({ message, onMediaPress }: BaseLegendMessageProps) => {
19
+ ({ message, isOutgoing, onMediaPress }: BaseLegendMessageProps) => {
19
20
  const videoElem = message?.videoElem;
20
21
  const videoUrl = videoElem?.videoUrl;
21
22
  const duration = videoElem?.duration;
@@ -29,13 +30,15 @@ export const VideoMessage = memo(
29
30
 
30
31
  if (!videoUrl) {
31
32
  return (
32
- <KContainer.View
33
- style={[messageStyles.bubble, messageStyles.videoPlaceholder]}
34
- >
35
- <KLabel.Text typo="TextSmNormal" color={CHAT_BUBBLE_COLORS.text}>
36
- 📹 [Video unavailable]
37
- </KLabel.Text>
38
- </KContainer.View>
33
+ <BubbleTailRow isOutgoing={isOutgoing} withTail={false}>
34
+ <KContainer.View
35
+ style={[messageStyles.bubble, messageStyles.videoPlaceholder]}
36
+ >
37
+ <KLabel.Text typo="TextSmNormal" color={CHAT_BUBBLE_COLORS.text}>
38
+ 📹 [Video unavailable]
39
+ </KLabel.Text>
40
+ </KContainer.View>
41
+ </BubbleTailRow>
39
42
  );
40
43
  }
41
44
 
@@ -44,7 +47,7 @@ export const VideoMessage = memo(
44
47
  );
45
48
 
46
49
  return (
47
- <>
50
+ <BubbleTailRow isOutgoing={isOutgoing} withTail={false}>
48
51
  <KContainer.Touchable
49
52
  onPress={handlePress}
50
53
  style={[
@@ -80,7 +83,7 @@ export const VideoMessage = memo(
80
83
  </KContainer.View>
81
84
  )}
82
85
  </KContainer.Touchable>
83
- </>
86
+ </BubbleTailRow>
84
87
  );
85
88
  }
86
89
  );
@@ -14,6 +14,7 @@ export function useDismissGroup({ onSuccess }: UseDismissGroupProps) {
14
14
  header: {
15
15
  title: { text: trans('group:dismiss_confirm_title') },
16
16
  alignment: 'center',
17
+ showCloseButton: true,
17
18
  },
18
19
  body: {
19
20
  renderContent: () => {
@@ -32,7 +32,11 @@ import {
32
32
  KRadiusValue,
33
33
  KImage,
34
34
  } from '@droppii/libs';
35
- import { precomputeLegendListData } from '../../utils/legendListMessage';
35
+ import {
36
+ precomputeLegendListData,
37
+ type GroupPosition,
38
+ } from '../../utils/legendListMessage';
39
+ import { getMessageRenderId } from '../../utils/message';
36
40
  import type { ChatListProps } from './types';
37
41
  import { LegendChatMessage } from './legend/LegendChatMessage';
38
42
  import { LegendChatDay } from './legend/LegendChatDay';
@@ -50,7 +54,7 @@ import { ChatEvent, type ScrollToMessagePayload } from '../../types/events';
50
54
  import { useMessageStore } from '../../store/message';
51
55
  import { useChatContext } from '../../context';
52
56
  import { messageStyles } from './legend/messageTypes';
53
- import { MessageType } from '@droppii/openim-rn-client-sdk';
57
+ import { MessageType, PeerType } from '@droppii/openim-rn-client-sdk';
54
58
  import type { MessageItem } from '@droppii/openim-rn-client-sdk';
55
59
  import { useUserStore } from '../../store';
56
60
  import { ChatMessageAPI } from '../../services';
@@ -60,10 +64,19 @@ import ChatImageDimens, {
60
64
  CHAT_IMAGE_VIEW_SOURCE,
61
65
  } from '../../utils/chatImageDimens';
62
66
 
63
- // Row wrapper (`messageRowLeft`/`messageRowRight` in `components/messages/styles.ts`)
64
- // adds only a bottom margin no vertical padding so a fixed-size item's true
65
- // rendered height is `content height + this margin`.
66
- const ROW_MARGIN_BOTTOM = KSpacingValue['0.25rem'];
67
+ // Cluster spacing: a wide gap after the message that closes a cluster, a tight
68
+ // one between messages inside it. These override the flat margin on the row
69
+ // wrapper (`messageRowLeft`/`messageRowRight` in `components/messages/styles.ts`),
70
+ // which adds only a bottom margin and no vertical padding — so a fixed-size
71
+ // item's true rendered height stays `content height + this margin`, and
72
+ // getFixedItemSize can keep folding it in.
73
+ const ROW_MARGIN_CLUSTER_END = KSpacingValue['0.5rem'];
74
+ const ROW_MARGIN_IN_CLUSTER = KSpacingValue['0.25rem'];
75
+
76
+ const getRowMarginBottom = (position: GroupPosition): number =>
77
+ position === 'last' || position === 'solo'
78
+ ? ROW_MARGIN_CLUSTER_END
79
+ : ROW_MARGIN_IN_CLUSTER;
67
80
 
68
81
  // KeyboardAwareLegendList auto scroll when modal opened
69
82
  // TODO: Convert toolbox to absolute view
@@ -103,12 +116,23 @@ export const ChatListLegend = memo(
103
116
  const currentUserId = useUserStore((s) => s.user?.userID);
104
117
  const windowWidth = useWindowDimensions().width;
105
118
  // Precompute all message data in single pass (O(n))
119
+ const { isBotConversation, conversationFlow, conversation } =
120
+ useChatDetailContext();
121
+
122
+ // Avatar and sender name only appear in real group chats — a bot thread is
123
+ // a Group session too, but reads as a single counterpart.
124
+ const isGroupConversation =
125
+ conversation?.peerType === PeerType.Group && !isBotConversation;
126
+
106
127
  const messageDataMap = useMemo(
107
- () => precomputeLegendListData(messages),
108
- [messages]
128
+ () =>
129
+ precomputeLegendListData(messages, {
130
+ isBotConversation,
131
+ currentUserId,
132
+ }),
133
+ [messages, isBotConversation, currentUserId]
109
134
  );
110
135
 
111
- const { isBotConversation, conversationFlow } = useChatDetailContext();
112
136
  const { retryMessage } = useSendMessage();
113
137
 
114
138
  const lastMessageId = useMemo(() => {
@@ -163,17 +187,22 @@ export const ChatListLegend = memo(
163
187
  const isLastMessage =
164
188
  (message.clientMsgID || message.serverMsgID) === lastMessageId;
165
189
  const isLastIncoming = !isOutgoing && isLastMessage;
166
- const messageId =
167
- message.clientMsgID ||
168
- message.serverMsgID ||
169
- `temp-${message.sendTime || message.createTime}-${index}`;
190
+ const messageId = getMessageRenderId(message, index);
170
191
 
171
192
  const messageData = messageDataMap.get(messageId);
172
193
  if (!messageData) {
173
194
  return null;
174
195
  }
175
196
 
176
- const { dayStart, createdAt } = messageData;
197
+ const { dayStart, timeBreak, createdAt, groupPosition } = messageData;
198
+
199
+ const showSenderGutter = isGroupConversation && !isOutgoing;
200
+ const showName =
201
+ showSenderGutter &&
202
+ (groupPosition === 'first' || groupPosition === 'solo');
203
+ const showAvatar =
204
+ showSenderGutter &&
205
+ (groupPosition === 'last' || groupPosition === 'solo');
177
206
 
178
207
  let flowInputButtonsNodes: GroupChild<'input-buttons'>[] = [];
179
208
  if (
@@ -188,17 +217,20 @@ export const ChatListLegend = memo(
188
217
 
189
218
  return (
190
219
  <>
191
- {dayStart && <LegendChatDay createdAt={createdAt} />}
220
+ {(dayStart || timeBreak) && (
221
+ <LegendChatDay createdAt={createdAt} timeOnly={!dayStart} />
222
+ )}
192
223
  <KContainer.View
193
224
  ref={(node: View | null) => {
194
225
  if (node) rowRefsMap.current.set(messageId, node);
195
226
  else rowRefsMap.current.delete(messageId);
196
227
  }}
197
- style={
228
+ style={[
198
229
  isOutgoing
199
230
  ? messageStyles.messageRowRight
200
- : messageStyles.messageRowLeft
201
- }
231
+ : messageStyles.messageRowLeft,
232
+ { marginBottom: getRowMarginBottom(groupPosition) },
233
+ ]}
202
234
  collapsable={false}
203
235
  >
204
236
  <LegendChatMessage
@@ -215,6 +247,10 @@ export const ChatListLegend = memo(
215
247
  }
216
248
  customMessageItemFactory={customMessageItemFactory}
217
249
  isHighlighted={highlightedId === messageId}
250
+ showName={showName}
251
+ showAvatar={showAvatar}
252
+ showSenderGutter={showSenderGutter}
253
+ groupPosition={groupPosition}
218
254
  />
219
255
  </KContainer.View>
220
256
  {flowInputButtonsNodes && flowInputButtonsNodes.length > 0 && (
@@ -242,6 +278,7 @@ export const ChatListLegend = memo(
242
278
  currentUserId,
243
279
  lastMessageId,
244
280
  messageDataMap,
281
+ isGroupConversation,
245
282
  isBotConversation,
246
283
  onMediaPress,
247
284
  onPressUrl,
@@ -303,12 +340,22 @@ export const ChatListLegend = memo(
303
340
  const getFixedItemSize = useCallback(
304
341
  (item: MessageItem, index: number): number | undefined => {
305
342
  const message = item;
306
- const messageId =
307
- message.clientMsgID ||
308
- message.serverMsgID ||
309
- `temp-${message.sendTime || message.createTime}-${index}`;
343
+ const messageId = getMessageRenderId(message, index);
310
344
 
311
- if (messageDataMap.get(messageId)?.dayStart) return undefined;
345
+ const messageData = messageDataMap.get(messageId);
346
+ // Both separators add chrome above the row that this function does not
347
+ // account for, and a resolved size is never re-measured.
348
+ if (messageData?.dayStart || messageData?.timeBreak) return undefined;
349
+
350
+ // Incoming rows in a group chat carry an avatar column and a sender
351
+ // name whose height this function cannot predict — measure them.
352
+ if (isGroupConversation && message.sendID !== currentUserId) {
353
+ return undefined;
354
+ }
355
+
356
+ const rowMargin = getRowMarginBottom(
357
+ messageData?.groupPosition ?? 'solo'
358
+ );
312
359
 
313
360
  // Resolve the content-type candidate FIRST — for every type other
314
361
  // than these three the result is `undefined` regardless of anything
@@ -319,17 +366,16 @@ export const ChatListLegend = memo(
319
366
  switch (message.contentType) {
320
367
  case MessageType.PictureMessage:
321
368
  candidateSize =
322
- resolveImageDisplayDims(message, windowWidth).height +
323
- ROW_MARGIN_BOTTOM;
369
+ resolveImageDisplayDims(message, windowWidth).height + rowMargin;
324
370
  break;
325
371
  case MessageType.VideoMessage:
326
372
  candidateSize =
327
373
  ChatImageDimens.getDimensions(
328
374
  CHAT_IMAGE_VIEW_SOURCE.MessageListThumbnail
329
- ).height + ROW_MARGIN_BOTTOM;
375
+ ).height + rowMargin;
330
376
  break;
331
377
  case MessageType.StickerMessage:
332
- candidateSize = STICKER_SIZE + ROW_MARGIN_BOTTOM;
378
+ candidateSize = STICKER_SIZE + rowMargin;
333
379
  break;
334
380
  default:
335
381
  return undefined;
@@ -361,6 +407,7 @@ export const ChatListLegend = memo(
361
407
  messageDataMap,
362
408
  customMessageItemFactory,
363
409
  currentUserId,
410
+ isGroupConversation,
364
411
  lastMessageId,
365
412
  isBotConversation,
366
413
  conversationFlow?.flowCurrentStep,
@@ -368,6 +415,28 @@ export const ChatListLegend = memo(
368
415
  ]
369
416
  );
370
417
 
418
+ // LegendList memoises each rendered item on [itemKey, item, extraData], so
419
+ // an item whose own object is unchanged will not re-render. Cluster
420
+ // position depends on a message's NEIGHBOURS, which can change while the
421
+ // message itself does not — prepending older history is enough to turn a
422
+ // 'first' into a 'middle'. messageDataMap is memoised on exactly the inputs
423
+ // that can move those boundaries, so including it here is what keeps a
424
+ // stale tail from being left behind.
425
+ const listExtraData = useMemo(
426
+ () => ({
427
+ messageDataMap,
428
+ flowStepId: conversationFlow?.flowCurrentStep?.id ?? '',
429
+ lastMessageId,
430
+ highlightedId,
431
+ }),
432
+ [
433
+ messageDataMap,
434
+ conversationFlow?.flowCurrentStep?.id,
435
+ lastMessageId,
436
+ highlightedId,
437
+ ]
438
+ );
439
+
371
440
  const handleStartReached = useCallback(() => {
372
441
  if (__DEV__) {
373
442
  console.log('[LegendList] onStartReached triggered', {
@@ -525,7 +594,7 @@ export const ChatListLegend = memo(
525
594
  getFixedItemSize={getFixedItemSize}
526
595
  keyExtractor={(item) => String(item.clientMsgID || item.serverMsgID)}
527
596
  recycleItems={false}
528
- extraData={`${conversationFlow?.flowCurrentStep?.id ?? ''}-${lastMessageId ?? ''}-${highlightedId ?? ''}`}
597
+ extraData={listExtraData}
529
598
  alignItemsAtEnd
530
599
  maintainVisibleContentPosition={maintainVisiblePositionConfig}
531
600
  freeze={freeze}
@@ -46,25 +46,35 @@ const formatDayLabel = (createdAt: number): string | null => {
46
46
 
47
47
  interface LegendChatDayProps {
48
48
  createdAt: number;
49
+ /**
50
+ * Render just the clock time, without the day. Used where a cluster broke on
51
+ * elapsed time inside a single day — the date is already on screen above.
52
+ */
53
+ timeOnly?: boolean;
49
54
  }
50
55
 
51
- export const LegendChatDay = memo(({ createdAt }: LegendChatDayProps) => {
52
- const dateStr = useMemo(
53
- () => (createdAt == null ? null : formatDayLabel(createdAt)),
54
- [createdAt]
55
- );
56
+ export const LegendChatDay = memo(
57
+ ({ createdAt, timeOnly = false }: LegendChatDayProps) => {
58
+ const dateStr = useMemo(() => {
59
+ if (createdAt == null) return null;
60
+ if (!timeOnly) return formatDayLabel(createdAt);
56
61
 
57
- if (!dateStr) return null;
62
+ const date = new Date(createdAt);
63
+ return Number.isNaN(date.getTime()) ? null : formatTime(date);
64
+ }, [createdAt, timeOnly]);
58
65
 
59
- return (
60
- <KContainer.View center marginV="0.5rem">
61
- <KContainer.View>
62
- <KLabel.Text typo="TextXsNormal" color={CHAT_BUBBLE_COLORS.dayLabel}>
63
- {dateStr}
64
- </KLabel.Text>
66
+ if (!dateStr) return null;
67
+
68
+ return (
69
+ <KContainer.View center marginV="0.5rem">
70
+ <KContainer.View>
71
+ <KLabel.Text typo="TextXsNormal" color={CHAT_BUBBLE_COLORS.dayLabel}>
72
+ {dateStr}
73
+ </KLabel.Text>
74
+ </KContainer.View>
65
75
  </KContainer.View>
66
- </KContainer.View>
67
- );
68
- });
76
+ );
77
+ }
78
+ );
69
79
 
70
80
  LegendChatDay.displayName = 'LegendChatDay';
@@ -1,4 +1,10 @@
1
- import { KContainer, KImage, KColors, KSpacingValue } from '@droppii/libs';
1
+ import {
2
+ KContainer,
3
+ KImage,
4
+ KColors,
5
+ KLabel,
6
+ KSpacingValue,
7
+ } from '@droppii/libs';
2
8
  import { StyleSheet } from 'react-native';
3
9
  import { memo, useCallback, useMemo } from 'react';
4
10
  import type { ComponentType, PropsWithChildren, ReactNode } from 'react';
@@ -20,7 +26,10 @@ import type { BaseLegendMessageProps } from '../../../components/messages/types'
20
26
  import { messageStyles } from './messageTypes';
21
27
  import QuotedMessage from '../../../components/messages/quotedMessage';
22
28
  import { StickerMessage } from '../../../components/messages/stickerMessage';
23
- import { GROUP_NOTIFICATION_TEXT } from '../../../utils/messageUtils';
29
+ import { GROUP_NOTIFICATION_TYPES } from '../../../utils/messageUtils';
30
+ import type { GroupPosition } from '../../../utils/legendListMessage';
31
+ import { Avatar } from '../../../components/Avatar';
32
+ import { TAIL_WIDTH } from '../../../components/messages/BubbleTail';
24
33
 
25
34
  type MessageProps = BaseLegendMessageProps & {
26
35
  onPressUrl?: (url: string) => void;
@@ -38,18 +47,20 @@ interface LegendChatMessageProps {
38
47
  onLongPress?: () => void;
39
48
  customMessageItemFactory?: (m: MessageItem) => ReactNode;
40
49
  isHighlighted?: boolean;
50
+ /** Sender name above the bubble — first message of an incoming cluster. */
51
+ showName?: boolean;
52
+ /** Sender avatar beside the bubble — last message of an incoming cluster. */
53
+ showAvatar?: boolean;
54
+ /**
55
+ * Reserve the avatar column. True for every incoming message in a group
56
+ * conversation, not just the one carrying the avatar, so that the middle of
57
+ * a cluster stays aligned with its last message.
58
+ */
59
+ showSenderGutter?: boolean;
60
+ /** Position within its same-sender cluster — drives the bubble tail. */
61
+ groupPosition?: GroupPosition;
41
62
  }
42
63
 
43
- // Notification-style messages (group created/renamed/member changes/etc.)
44
- // are system banners, not user-authored text — they render centered,
45
- // full-width, without a bubble, avatar/status chrome, or left/right
46
- // alignment (see SYSTEM_MESSAGE_TYPES below). Driven by the same lookup
47
- // messageUtils uses to build their preview/thread text, so adding a new
48
- // notification content type there is enough to cover both places.
49
- const GROUP_NOTIFICATION_TYPES = Object.keys(GROUP_NOTIFICATION_TEXT).map(
50
- Number
51
- ) as MessageType[];
52
-
53
64
  const messageComponentMap: Partial<
54
65
  Record<MessageType, ComponentType<MessageProps>>
55
66
  > = {
@@ -88,6 +99,10 @@ export const LegendChatMessage = memo(
88
99
  onLongPress,
89
100
  customMessageItemFactory,
90
101
  isHighlighted,
102
+ showName = false,
103
+ showAvatar = false,
104
+ showSenderGutter = false,
105
+ groupPosition,
91
106
  }: LegendChatMessageProps) => {
92
107
  const handlePress = useCallback(() => {
93
108
  onPress?.(message);
@@ -127,12 +142,34 @@ export const LegendChatMessage = memo(
127
142
 
128
143
  return (
129
144
  <KContainer.View style={messageStyles.wrapper} row alignItems>
145
+ {showSenderGutter && (
146
+ <KContainer.View style={styles.avatarGutter}>
147
+ {showAvatar && (
148
+ <Avatar
149
+ size="sm"
150
+ source={message.senderFaceUrl}
151
+ fullName={message.senderNickname}
152
+ />
153
+ )}
154
+ </KContainer.View>
155
+ )}
130
156
  <MessageStatusIndicator
131
157
  message={message}
132
158
  isOutgoing={isOutgoing}
133
159
  onRetry={onRetry}
134
160
  />
135
161
  <KContainer.View style={styles.content}>
162
+ {showName && !!message.senderNickname && (
163
+ <KLabel.Text
164
+ typo="TextXsNormal"
165
+ color={KColors.gray.normal}
166
+ numberOfLines={1}
167
+ marginB={2}
168
+ style={styles.senderName}
169
+ >
170
+ {message.senderNickname}
171
+ </KLabel.Text>
172
+ )}
136
173
  <LongPressWrapper onPress={handlePress} onLongPress={handleLongPress}>
137
174
  <MessageComponent
138
175
  message={message}
@@ -141,6 +178,7 @@ export const LegendChatMessage = memo(
141
178
  onMediaPress={hasMediaPress ? onMediaPress : undefined}
142
179
  onPressUrl={hasUrlPress ? onPressUrl : undefined}
143
180
  isHighlighted={isHighlighted}
181
+ groupPosition={groupPosition}
144
182
  />
145
183
  </LongPressWrapper>
146
184
 
@@ -161,10 +199,22 @@ export const LegendChatMessage = memo(
161
199
 
162
200
  LegendChatMessage.displayName = 'LegendChatMessage';
163
201
 
202
+ const AVATAR_SIZE = 24;
203
+
164
204
  const styles = StyleSheet.create({
165
205
  content: {
166
206
  position: 'relative',
167
207
  },
208
+ // Bubbles sit behind a TAIL_WIDTH gutter (see BubbleTailRow); the name has to
209
+ // clear the same gutter or it hangs to the left of the bubble it labels.
210
+ senderName: {
211
+ marginLeft: TAIL_WIDTH,
212
+ },
213
+ avatarGutter: {
214
+ width: AVATAR_SIZE,
215
+ marginRight: KSpacingValue['0.25rem'],
216
+ alignSelf: 'flex-end',
217
+ },
168
218
  systemMessageWrapper: {
169
219
  width: '100%',
170
220
  marginVertical: KSpacingValue['0.25rem'],
@@ -1,4 +1,6 @@
1
1
  import { MessageType, type MessageItem } from '@droppii/openim-rn-client-sdk';
2
+ import { GROUP_NOTIFICATION_TYPES } from './messageUtils';
3
+ import { getMessageRenderId, getMessageTime } from './message';
2
4
 
3
5
  /**
4
6
  * Extract message text based on content type
@@ -39,41 +41,148 @@ export const getMessageText = (message: MessageItem): string => {
39
41
  }
40
42
  };
41
43
 
44
+ /** Where a message sits inside its run of consecutive same-sender messages. */
45
+ export type GroupPosition = 'first' | 'middle' | 'last' | 'solo';
46
+
42
47
  export interface PrecomputedMessageData {
43
48
  messageId: string;
44
49
  dayStart: boolean;
50
+ /**
51
+ * More than GROUP_TIME_GAP has passed since the previous message. The list
52
+ * marks these with a time label: the cluster break is real but, unlike a day
53
+ * change, nothing else on screen would show why the run stopped.
54
+ */
55
+ timeBreak: boolean;
45
56
  createdAt: number;
57
+ groupPosition: GroupPosition;
58
+ }
59
+
60
+ /** Two messages further apart than this never share a cluster. */
61
+ const GROUP_TIME_GAP = 300_000; // 5 minutes
62
+
63
+ /** Virtual sender for bot conversations — see getGroupKey. */
64
+ const BOT_GROUP_KEY = '__bot__';
65
+
66
+ const startOfDay = (timestamp: number): number => {
67
+ const date = new Date(timestamp);
68
+ date.setHours(0, 0, 0, 0);
69
+ return date.getTime();
70
+ };
71
+
72
+ /**
73
+ * System banners (group created/renamed/member changes) and anything without a
74
+ * sender are not user-authored, so they never join a cluster and always break
75
+ * the runs on both sides of them.
76
+ */
77
+ const isSystemMessage = (message: MessageItem): boolean =>
78
+ GROUP_NOTIFICATION_TYPES.includes(message.contentType) || !message.sendID;
79
+
80
+ /**
81
+ * Identity two messages must share to cluster together. In a bot conversation
82
+ * every incoming message is treated as one virtual sender: replies come from
83
+ * the bot and from human agents under different sendIDs, but read as a single
84
+ * continuous voice.
85
+ */
86
+ const getGroupKey = (
87
+ message: MessageItem,
88
+ isBotConversation: boolean,
89
+ currentUserId?: string
90
+ ): string => {
91
+ const isOutgoing = message.sendID === currentUserId;
92
+ if (isBotConversation && !isOutgoing) return BOT_GROUP_KEY;
93
+ return message.sendID;
94
+ };
95
+
96
+ export interface PrecomputeOptions {
97
+ isBotConversation?: boolean;
98
+ currentUserId?: string;
46
99
  }
47
100
 
48
101
  /**
49
- * Precompute all message rendering data in single pass O(n)
50
- * Eliminates redundant Date creation and type resolution during render
102
+ * Precompute per-message render data (day dividers + cluster position) so
103
+ * renderItem stays allocation-free.
51
104
  *
52
- * Returns: Map of messageId -> {dayStart, messageType, createdAt}
105
+ * Runs in two passes: day starts first, because cluster boundaries depend on
106
+ * them — a divider between two messages separates them however close in time
107
+ * they are.
53
108
  */
54
109
  export const precomputeLegendListData = (
55
- messages: MessageItem[]
110
+ messages: MessageItem[],
111
+ { isBotConversation = false, currentUserId }: PrecomputeOptions = {}
56
112
  ): Map<string, PrecomputedMessageData> => {
57
113
  const dataMap = new Map<string, PrecomputedMessageData>();
114
+
115
+ // Pass 1 — identity, timestamp, day divider.
116
+ const dayStarts: boolean[] = [];
117
+ const ids: string[] = [];
118
+ // Resolved timestamps, reused by pass 2. Shared with the list's sort key so
119
+ // cluster boundaries are computed over the same ordering that gets rendered.
120
+ const timestamps: number[] = [];
58
121
  let lastDay: number | null = null;
59
122
 
60
123
  messages.forEach((message, index) => {
61
- // Get message ID - use timestamp as fallback for newly sent messages
62
- const messageId =
63
- message.clientMsgID ||
64
- message.serverMsgID ||
65
- `temp-${message.sendTime || message.createTime}-${index}`;
124
+ const messageId = getMessageRenderId(message, index);
125
+
126
+ // A message still in flight can reach us with no usable timestamp at all.
127
+ // Left as 0 it lands on the epoch, which reads as a different calendar day
128
+ // from its neighbours — that inserts a bogus divider AND splits the cluster,
129
+ // stranding a tail on the message before it. Anchor it to the previous
130
+ // message instead, which is where it belongs in both senses.
131
+ const rawCreatedAt = getMessageTime(message);
132
+ const createdAt =
133
+ rawCreatedAt > 0 ? rawCreatedAt : (timestamps[index - 1] ?? rawCreatedAt);
134
+ const day = startOfDay(createdAt);
66
135
 
67
- const createdAt = message.sendTime || message.createTime;
68
- const day = new Date(createdAt).getDate();
136
+ const prevTimestamp = timestamps[index - 1];
137
+
138
+ ids[index] = messageId;
139
+ timestamps[index] = createdAt;
140
+ dayStarts[index] = lastDay !== day;
141
+ lastDay = day;
69
142
 
70
143
  dataMap.set(messageId, {
71
144
  messageId,
72
- dayStart: lastDay !== day,
145
+ dayStart: dayStarts[index]!,
146
+ timeBreak:
147
+ prevTimestamp !== undefined &&
148
+ createdAt - prevTimestamp > GROUP_TIME_GAP,
73
149
  createdAt,
150
+ groupPosition: 'solo',
74
151
  });
152
+ });
75
153
 
76
- lastDay = day;
154
+ // Pass 2 — cluster position, now that every day divider is known.
155
+ messages.forEach((message, index) => {
156
+ const entry = dataMap.get(ids[index]!);
157
+ if (!entry) return;
158
+ if (isSystemMessage(message)) return; // stays 'solo'
159
+
160
+ const key = getGroupKey(message, isBotConversation, currentUserId);
161
+ const prev = messages[index - 1];
162
+ const next = messages[index + 1];
163
+
164
+ const joinsPrev =
165
+ !!prev &&
166
+ !isSystemMessage(prev) &&
167
+ getGroupKey(prev, isBotConversation, currentUserId) === key &&
168
+ timestamps[index]! - timestamps[index - 1]! <= GROUP_TIME_GAP &&
169
+ // A divider above THIS message separates it from the one before it.
170
+ !dayStarts[index];
171
+
172
+ const joinsNext =
173
+ !!next &&
174
+ !isSystemMessage(next) &&
175
+ getGroupKey(next, isBotConversation, currentUserId) === key &&
176
+ timestamps[index + 1]! - timestamps[index]! <= GROUP_TIME_GAP &&
177
+ !dayStarts[index + 1];
178
+
179
+ entry.groupPosition = joinsPrev
180
+ ? joinsNext
181
+ ? 'middle'
182
+ : 'last'
183
+ : joinsNext
184
+ ? 'first'
185
+ : 'solo';
77
186
  });
78
187
 
79
188
  return dataMap;
@@ -90,7 +199,7 @@ export const computeDayStarts = (
90
199
  let lastDay: number | null = null;
91
200
 
92
201
  messages.forEach((message) => {
93
- const day = new Date(message.sendTime || message.createTime).getDate();
202
+ const day = startOfDay(getMessageTime(message));
94
203
  const messageId = message.clientMsgID || message.serverMsgID || '';
95
204
  dayStartMap.set(messageId, lastDay !== day);
96
205
  lastDay = day;