@droppii-org/chat-mobile 0.2.81 → 0.2.82

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 (151) hide show
  1. package/lib/module/components/ThreadCard/MentionBadge.js +24 -0
  2. package/lib/module/components/ThreadCard/MentionBadge.js.map +1 -0
  3. package/lib/module/components/ThreadCard/ThreadCard.js +5 -1
  4. package/lib/module/components/ThreadCard/ThreadCard.js.map +1 -1
  5. package/lib/module/components/messages/linkMessage/LinkPreviewCard.js +67 -0
  6. package/lib/module/components/messages/linkMessage/LinkPreviewCard.js.map +1 -0
  7. package/lib/module/components/messages/linkMessage/index.js +21 -68
  8. package/lib/module/components/messages/linkMessage/index.js.map +1 -1
  9. package/lib/module/components/messages/quotedMessage/QuotedMessagePreviewStrip.js +152 -0
  10. package/lib/module/components/messages/quotedMessage/QuotedMessagePreviewStrip.js.map +1 -0
  11. package/lib/module/components/messages/textMessage/index.js +102 -8
  12. package/lib/module/components/messages/textMessage/index.js.map +1 -1
  13. package/lib/module/core/useChatListener.js +1 -1
  14. package/lib/module/core/useChatListener.js.map +1 -1
  15. package/lib/module/hooks/message/useSendMessage.js +64 -3
  16. package/lib/module/hooks/message/useSendMessage.js.map +1 -1
  17. package/lib/module/hooks/query-keys.js +4 -0
  18. package/lib/module/hooks/query-keys.js.map +1 -1
  19. package/lib/module/hooks/useChatCompose.js +3 -2
  20. package/lib/module/hooks/useChatCompose.js.map +1 -1
  21. package/lib/module/screens/chat-detail/ChatComposer.js +170 -48
  22. package/lib/module/screens/chat-detail/ChatComposer.js.map +1 -1
  23. package/lib/module/screens/chat-detail/ChatListLegend.js +1 -0
  24. package/lib/module/screens/chat-detail/ChatListLegend.js.map +1 -1
  25. package/lib/module/screens/chat-detail/components/ChatMessageInput.js +23 -2
  26. package/lib/module/screens/chat-detail/components/ChatMessageInput.js.map +1 -1
  27. package/lib/module/screens/chat-detail/components/MentionHighlightAdapter.js +51 -0
  28. package/lib/module/screens/chat-detail/components/MentionHighlightAdapter.js.map +1 -0
  29. package/lib/module/screens/chat-detail/components/MentionSuggestionItem.js +49 -0
  30. package/lib/module/screens/chat-detail/components/MentionSuggestionItem.js.map +1 -0
  31. package/lib/module/screens/chat-detail/components/MentionSuggestionList.js +114 -0
  32. package/lib/module/screens/chat-detail/components/MentionSuggestionList.js.map +1 -0
  33. package/lib/module/screens/chat-detail/hooks/useChatComposerState.js +8 -0
  34. package/lib/module/screens/chat-detail/hooks/useChatComposerState.js.map +1 -1
  35. package/lib/module/screens/chat-detail/hooks/useMentionComposer.js +251 -0
  36. package/lib/module/screens/chat-detail/hooks/useMentionComposer.js.map +1 -0
  37. package/lib/module/screens/chat-detail/legend/LegendChatMessage.js +5 -1
  38. package/lib/module/screens/chat-detail/legend/LegendChatMessage.js.map +1 -1
  39. package/lib/module/services/mentionSearch.js +49 -0
  40. package/lib/module/services/mentionSearch.js.map +1 -0
  41. package/lib/module/services/message.js +6 -1
  42. package/lib/module/services/message.js.map +1 -1
  43. package/lib/module/translation/resources/i18n.js +5 -0
  44. package/lib/module/translation/resources/i18n.js.map +1 -1
  45. package/lib/module/types/chat.js.map +1 -1
  46. package/lib/module/types/mention.js +2 -0
  47. package/lib/module/types/mention.js.map +1 -0
  48. package/lib/module/utils/mentionSerializer.js +113 -0
  49. package/lib/module/utils/mentionSerializer.js.map +1 -0
  50. package/lib/module/utils/mentions/index.js +7 -0
  51. package/lib/module/utils/mentions/index.js.map +1 -0
  52. package/lib/module/utils/mentions/mentionDetector.js +49 -0
  53. package/lib/module/utils/mentions/mentionDetector.js.map +1 -0
  54. package/lib/module/utils/mentions/mentionDiff.js +66 -0
  55. package/lib/module/utils/mentions/mentionDiff.js.map +1 -0
  56. package/lib/module/utils/mentions/mentionRange.js +188 -0
  57. package/lib/module/utils/mentions/mentionRange.js.map +1 -0
  58. package/lib/module/utils/mentions/mentionTokenize.js +50 -0
  59. package/lib/module/utils/mentions/mentionTokenize.js.map +1 -0
  60. package/lib/module/utils/messageUtils.js +6 -1
  61. package/lib/module/utils/messageUtils.js.map +1 -1
  62. package/lib/module/utils/url.js +33 -0
  63. package/lib/module/utils/url.js.map +1 -1
  64. package/lib/typescript/src/components/ThreadCard/MentionBadge.d.ts +7 -0
  65. package/lib/typescript/src/components/ThreadCard/MentionBadge.d.ts.map +1 -0
  66. package/lib/typescript/src/components/ThreadCard/ThreadCard.d.ts.map +1 -1
  67. package/lib/typescript/src/components/messages/linkMessage/LinkPreviewCard.d.ts +16 -0
  68. package/lib/typescript/src/components/messages/linkMessage/LinkPreviewCard.d.ts.map +1 -0
  69. package/lib/typescript/src/components/messages/linkMessage/index.d.ts.map +1 -1
  70. package/lib/typescript/src/components/messages/quotedMessage/QuotedMessagePreviewStrip.d.ts +13 -0
  71. package/lib/typescript/src/components/messages/quotedMessage/QuotedMessagePreviewStrip.d.ts.map +1 -0
  72. package/lib/typescript/src/components/messages/textMessage/index.d.ts +3 -1
  73. package/lib/typescript/src/components/messages/textMessage/index.d.ts.map +1 -1
  74. package/lib/typescript/src/hooks/message/useSendMessage.d.ts +5 -2
  75. package/lib/typescript/src/hooks/message/useSendMessage.d.ts.map +1 -1
  76. package/lib/typescript/src/hooks/query-keys.d.ts +4 -0
  77. package/lib/typescript/src/hooks/query-keys.d.ts.map +1 -1
  78. package/lib/typescript/src/hooks/useChatCompose.d.ts +2 -1
  79. package/lib/typescript/src/hooks/useChatCompose.d.ts.map +1 -1
  80. package/lib/typescript/src/screens/chat-detail/ChatComposer.d.ts.map +1 -1
  81. package/lib/typescript/src/screens/chat-detail/ChatListLegend.d.ts.map +1 -1
  82. package/lib/typescript/src/screens/chat-detail/components/ChatMessageInput.d.ts +10 -2
  83. package/lib/typescript/src/screens/chat-detail/components/ChatMessageInput.d.ts.map +1 -1
  84. package/lib/typescript/src/screens/chat-detail/components/MentionHighlightAdapter.d.ts +29 -0
  85. package/lib/typescript/src/screens/chat-detail/components/MentionHighlightAdapter.d.ts.map +1 -0
  86. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionItem.d.ts +8 -0
  87. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionItem.d.ts.map +1 -0
  88. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionList.d.ts +12 -0
  89. package/lib/typescript/src/screens/chat-detail/components/MentionSuggestionList.d.ts.map +1 -0
  90. package/lib/typescript/src/screens/chat-detail/hooks/useChatComposerState.d.ts +1 -1
  91. package/lib/typescript/src/screens/chat-detail/hooks/useChatComposerState.d.ts.map +1 -1
  92. package/lib/typescript/src/screens/chat-detail/hooks/useMentionComposer.d.ts +48 -0
  93. package/lib/typescript/src/screens/chat-detail/hooks/useMentionComposer.d.ts.map +1 -0
  94. package/lib/typescript/src/screens/chat-detail/legend/LegendChatMessage.d.ts.map +1 -1
  95. package/lib/typescript/src/services/mentionSearch.d.ts +15 -0
  96. package/lib/typescript/src/services/mentionSearch.d.ts.map +1 -0
  97. package/lib/typescript/src/services/message.d.ts +1 -1
  98. package/lib/typescript/src/services/message.d.ts.map +1 -1
  99. package/lib/typescript/src/translation/resources/i18n.d.ts.map +1 -1
  100. package/lib/typescript/src/types/chat.d.ts +23 -0
  101. package/lib/typescript/src/types/chat.d.ts.map +1 -1
  102. package/lib/typescript/src/types/mention.d.ts +65 -0
  103. package/lib/typescript/src/types/mention.d.ts.map +1 -0
  104. package/lib/typescript/src/utils/mentionSerializer.d.ts +78 -0
  105. package/lib/typescript/src/utils/mentionSerializer.d.ts.map +1 -0
  106. package/lib/typescript/src/utils/mentions/index.d.ts +5 -0
  107. package/lib/typescript/src/utils/mentions/index.d.ts.map +1 -0
  108. package/lib/typescript/src/utils/mentions/mentionDetector.d.ts +18 -0
  109. package/lib/typescript/src/utils/mentions/mentionDetector.d.ts.map +1 -0
  110. package/lib/typescript/src/utils/mentions/mentionDiff.d.ts +20 -0
  111. package/lib/typescript/src/utils/mentions/mentionDiff.d.ts.map +1 -0
  112. package/lib/typescript/src/utils/mentions/mentionRange.d.ts +86 -0
  113. package/lib/typescript/src/utils/mentions/mentionRange.d.ts.map +1 -0
  114. package/lib/typescript/src/utils/mentions/mentionTokenize.d.ts +29 -0
  115. package/lib/typescript/src/utils/mentions/mentionTokenize.d.ts.map +1 -0
  116. package/lib/typescript/src/utils/messageUtils.d.ts.map +1 -1
  117. package/lib/typescript/src/utils/url.d.ts +12 -0
  118. package/lib/typescript/src/utils/url.d.ts.map +1 -1
  119. package/package.json +1 -1
  120. package/src/components/ThreadCard/MentionBadge.tsx +28 -0
  121. package/src/components/ThreadCard/ThreadCard.tsx +3 -0
  122. package/src/components/messages/linkMessage/LinkPreviewCard.tsx +92 -0
  123. package/src/components/messages/linkMessage/index.tsx +25 -89
  124. package/src/components/messages/quotedMessage/QuotedMessagePreviewStrip.tsx +168 -0
  125. package/src/components/messages/textMessage/index.tsx +124 -4
  126. package/src/core/useChatListener.ts +1 -1
  127. package/src/hooks/message/useSendMessage.ts +60 -1
  128. package/src/hooks/query-keys.ts +6 -0
  129. package/src/hooks/useChatCompose.ts +115 -107
  130. package/src/screens/chat-detail/ChatComposer.tsx +201 -67
  131. package/src/screens/chat-detail/ChatListLegend.tsx +1 -0
  132. package/src/screens/chat-detail/components/ChatMessageInput.tsx +35 -2
  133. package/src/screens/chat-detail/components/MentionHighlightAdapter.tsx +57 -0
  134. package/src/screens/chat-detail/components/MentionSuggestionItem.tsx +53 -0
  135. package/src/screens/chat-detail/components/MentionSuggestionList.tsx +133 -0
  136. package/src/screens/chat-detail/hooks/useChatComposerState.ts +7 -1
  137. package/src/screens/chat-detail/hooks/useMentionComposer.ts +344 -0
  138. package/src/screens/chat-detail/legend/LegendChatMessage.tsx +7 -1
  139. package/src/services/mentionSearch.ts +57 -0
  140. package/src/services/message.ts +9 -1
  141. package/src/translation/resources/i18n.ts +6 -0
  142. package/src/types/chat.ts +22 -0
  143. package/src/types/mention.ts +70 -0
  144. package/src/utils/mentionSerializer.ts +155 -0
  145. package/src/utils/mentions/index.ts +16 -0
  146. package/src/utils/mentions/mentionDetector.ts +58 -0
  147. package/src/utils/mentions/mentionDiff.ts +95 -0
  148. package/src/utils/mentions/mentionRange.ts +207 -0
  149. package/src/utils/mentions/mentionTokenize.ts +53 -0
  150. package/src/utils/messageUtils.ts +6 -1
  151. package/src/utils/url.ts +30 -0
@@ -5,8 +5,11 @@ import {
5
5
  TextInput,
6
6
  type NativeSyntheticEvent,
7
7
  type TextInputContentSizeChangeEventData,
8
+ type TextInputSelectionChangeEventData,
8
9
  } from 'react-native';
9
10
  import { KContainer, KImage, KColors, KSpacingValue } from '@droppii/libs';
11
+ import { MentionHighlightAdapter } from './MentionHighlightAdapter';
12
+ import type { Mention } from '../../../types/mention';
10
13
 
11
14
  const INPUT_FONT_SIZE = 16;
12
15
  const INPUT_LINE_HEIGHT = 22.4;
@@ -22,6 +25,15 @@ export interface ChatMessageInputProps {
22
25
  onContentSizeChange: (
23
26
  event: NativeSyntheticEvent<TextInputContentSizeChangeEventData>
24
27
  ) => void;
28
+ onSelectionChange?: (
29
+ event: NativeSyntheticEvent<TextInputSelectionChangeEventData>
30
+ ) => void;
31
+ /** Committed mentions — when non-empty, the input switches from plain
32
+ * `value` to a nested-Text `children` tree (MentionHighlightAdapter) so
33
+ * mentions render highlighted. Omitted/empty keeps the original plain
34
+ * `value`-controlled path, so the overwhelming majority of messages (no
35
+ * mentions) never pay for this. */
36
+ mentions?: Mention[];
25
37
  isInputFocused: boolean;
26
38
  isMultilineInput: boolean;
27
39
  onEmojiPress?: () => void;
@@ -37,12 +49,24 @@ export const ChatMessageInput = memo(
37
49
  onFocus,
38
50
  onBlur,
39
51
  onContentSizeChange,
52
+ onSelectionChange,
53
+ mentions,
40
54
  isInputFocused,
41
55
  isMultilineInput,
42
56
  onEmojiPress,
43
57
  onSuggestPress,
44
58
  inputRef,
45
59
  }: ChatMessageInputProps) => {
60
+ // Re-enabled: the earlier flicker/"Maximum update depth exceeded" loop
61
+ // was root-caused to two bugs in the composer wiring (unstable callback
62
+ // deps on `mentionComposer` recreating ChatMessageInput's onEmojiPress
63
+ // every render, and redundant/stale mention re-detection from
64
+ // onSelectionChange) — not the nested-Text children-mode rendering
65
+ // itself, which was disabled here as a precaution while that was still
66
+ // unclear. Both root causes are fixed in useMentionComposer.ts /
67
+ // ChatComposer.tsx. If a highlight-specific issue turns up on device,
68
+ // set this back to `false` — it's a one-line, fully reversible gate.
69
+ const hasMentions = !!mentions && mentions.length > 0;
46
70
  const renderEmojiNode = useCallback(
47
71
  () => (
48
72
  <KContainer.Touchable marginR="0.5rem" onPress={onEmojiPress}>
@@ -85,20 +109,29 @@ export const ChatMessageInput = memo(
85
109
  ref={inputRef}
86
110
  placeholder={placeholder}
87
111
  placeholderTextColor={KColors.palette.gray.w200}
88
- value={value}
112
+ {...(hasMentions ? {} : { value })}
89
113
  onChangeText={onChangeText}
90
114
  onFocus={onFocus}
91
115
  onBlur={onBlur}
92
116
  onContentSizeChange={onContentSizeChange}
117
+ onSelectionChange={onSelectionChange}
93
118
  multiline
94
119
  blurOnSubmit={false}
120
+ autoCorrect={false}
95
121
  scrollEnabled
96
122
  allowFontScaling={false}
97
123
  underlineColorAndroid={KColors.transparent}
98
124
  selectionColor={KColors.selector}
99
125
  textAlignVertical={isMultilineInput ? 'top' : 'center'}
100
126
  style={[styles.textInput]}
101
- />
127
+ >
128
+ {hasMentions && (
129
+ <MentionHighlightAdapter
130
+ text={value ?? ''}
131
+ mentions={mentions!}
132
+ />
133
+ )}
134
+ </TextInput>
102
135
 
103
136
  {renderEmojiNode()}
104
137
  </KContainer.View>
@@ -0,0 +1,57 @@
1
+ import { memo, useMemo } from 'react';
2
+ import { Text } from 'react-native';
3
+ import { KColors } from '@droppii/libs';
4
+ import { tokenize } from '../../../utils/mentions';
5
+ import type { Mention } from '../../../types/mention';
6
+
7
+ interface MentionHighlightAdapterProps {
8
+ text: string;
9
+ mentions: Mention[];
10
+ }
11
+
12
+ /**
13
+ * Renders `text` as nested styled `<Text>` children for a TextInput's
14
+ * "undocumented" rich-text-via-children capability — the same primitive
15
+ * `react-native-controlled-mentions` and Bluesky's social-app both use.
16
+ *
17
+ * Deliberately STATELESS: every render calls tokenize() fresh and rebuilds
18
+ * the whole children tree from scratch — no attempt to preserve/diff span
19
+ * identity across renders. This is the Bluesky-informed choice (see the
20
+ * mention plan's Phase A3): react-native-controlled-mentions instead tries
21
+ * to maintain *stateful* span identity across renders, and its known
22
+ * Android bugs (#34668 duplicate input, #41647 broken selection, #50731
23
+ * "spans recreated on every render") read like that statefulness fighting
24
+ * itself — Bluesky ships live-colored nested-Text in production by never
25
+ * attempting to preserve identity in the first place. Only tokenize()
26
+ * itself is memoized (on [text, mentions]); the resulting element tree is
27
+ * not further diffed or cached.
28
+ *
29
+ * Only mounted when mentions.length > 0 (see ChatMessageInput) — a plain
30
+ * `value`-controlled TextInput is used otherwise, so the vast majority of
31
+ * messages (no mentions) never pay for this at all.
32
+ */
33
+ export const MentionHighlightAdapter = memo(
34
+ ({ text, mentions }: MentionHighlightAdapterProps) => {
35
+ const segments = useMemo(() => tokenize(text, mentions), [text, mentions]);
36
+
37
+ return (
38
+ <>
39
+ {segments.map((segment, index) =>
40
+ segment.type === 'mention' ? (
41
+ <Text key={segment.mention.id} style={mentionTextStyle}>
42
+ {`@${segment.mention.displayName}`}
43
+ </Text>
44
+ ) : (
45
+ // Plain-text runs have no stable identity of their own — index
46
+ // is fine for a tree rebuilt from scratch every render.
47
+ <Text key={`text-${index}`}>{segment.text}</Text>
48
+ )
49
+ )}
50
+ </>
51
+ );
52
+ }
53
+ );
54
+
55
+ MentionHighlightAdapter.displayName = 'MentionHighlightAdapter';
56
+
57
+ const mentionTextStyle = { color: KColors.palette.primary.w400 };
@@ -0,0 +1,53 @@
1
+ import { memo, useCallback } from 'react';
2
+ import { KContainer, KLabel, KColors, KImage } from '@droppii/libs';
3
+ import Avatar from '../../../components/Avatar/Avatar';
4
+ import { trans } from '../../../translation';
5
+ import type { MentionSuggestion } from '../../../types/mention';
6
+
7
+ const ALL_MEMBERS_USER_ID = 'ALL';
8
+
9
+ interface MentionSuggestionItemProps {
10
+ item: MentionSuggestion;
11
+ onPress: (item: MentionSuggestion) => void;
12
+ }
13
+
14
+ export const MentionSuggestionItem = memo(
15
+ ({ item, onPress }: MentionSuggestionItemProps) => {
16
+ const isAllOption = item.userId === ALL_MEMBERS_USER_ID;
17
+
18
+ const handlePress = useCallback(() => onPress(item), [onPress, item]);
19
+
20
+ return (
21
+ <KContainer.Touchable row alignItems onPress={handlePress}>
22
+ {isAllOption ? (
23
+ <KContainer.View width={40} height={40} center>
24
+ <KImage.VectorIcons
25
+ name="user-three-b"
26
+ size={24}
27
+ color={KColors.palette.gray.w900}
28
+ />
29
+ </KContainer.View>
30
+ ) : (
31
+ <Avatar source={item.avatar} fullName={item.displayName} size="nm" />
32
+ )}
33
+
34
+ <KContainer.View flex marginL="0.75rem">
35
+ <KLabel.Text typo="TextMdMedium" numberOfLines={1}>
36
+ {item.displayName}
37
+ </KLabel.Text>
38
+ {isAllOption && (
39
+ <KLabel.Text
40
+ typo="TextXsNormal"
41
+ color={KColors.palette.gray.w600}
42
+ numberOfLines={1}
43
+ >
44
+ {trans('group:tag_all_desc')}
45
+ </KLabel.Text>
46
+ )}
47
+ </KContainer.View>
48
+ </KContainer.Touchable>
49
+ );
50
+ }
51
+ );
52
+
53
+ MentionSuggestionItem.displayName = 'MentionSuggestionItem';
@@ -0,0 +1,133 @@
1
+ import { memo, useCallback } from 'react';
2
+ import { ActivityIndicator, StyleSheet } from 'react-native';
3
+ import { LegendList } from '@legendapp/list/react-native';
4
+ import { KContainer, KLabel, KColors, KSpacingValue } from '@droppii/libs';
5
+ import { MentionSuggestionItem } from './MentionSuggestionItem';
6
+ import { trans } from '../../../translation';
7
+ import type { MentionSuggestion } from '../../../types/mention';
8
+
9
+ // Figma (node 15066-319156): regular rows are 40px (single line, avatar
10
+ // height defines it), the "Tất cả" row is 45px (title + subtitle) — 40 is
11
+ // used as the shared estimate since most rows are the single-line kind.
12
+ const ROW_HEIGHT = 40;
13
+ const ROW_GAP = 8;
14
+ const MAX_VISIBLE_ROWS = 6;
15
+ const MAX_PANEL_HEIGHT =
16
+ ROW_HEIGHT * MAX_VISIBLE_ROWS + ROW_GAP * (MAX_VISIBLE_ROWS - 1);
17
+
18
+ interface MentionSuggestionListProps {
19
+ suggestions: MentionSuggestion[];
20
+ isSearching: boolean;
21
+ isLoadingMore?: boolean;
22
+ hasMore?: boolean;
23
+ onLoadMore?: () => void;
24
+ onSelect: (item: MentionSuggestion) => void;
25
+ }
26
+
27
+ export const MentionSuggestionList = memo(
28
+ ({
29
+ suggestions,
30
+ isSearching,
31
+ isLoadingMore,
32
+ hasMore,
33
+ onLoadMore,
34
+ onSelect,
35
+ }: MentionSuggestionListProps) => {
36
+ const renderItem = useCallback(
37
+ ({ item }: { item: MentionSuggestion }) => (
38
+ <MentionSuggestionItem item={item} onPress={onSelect} />
39
+ ),
40
+ [onSelect]
41
+ );
42
+
43
+ const keyExtractor = useCallback(
44
+ (item: MentionSuggestion) => item.userId,
45
+ []
46
+ );
47
+
48
+ const renderFooter = useCallback(() => {
49
+ if (!isLoadingMore) return null;
50
+ return (
51
+ <KContainer.View paddingV="0.5rem" center>
52
+ <ActivityIndicator color={KColors.palette.primary.w400} />
53
+ </KContainer.View>
54
+ );
55
+ }, [isLoadingMore]);
56
+
57
+ const renderSeparator = useCallback(
58
+ () => <KContainer.View style={styles.separator} />,
59
+ []
60
+ );
61
+
62
+ return (
63
+ <KContainer.View style={styles.shadowWrapper}>
64
+ <KContainer.View background={KColors.white} style={styles.container}>
65
+ <KLabel.Text
66
+ typo="TextNmBold"
67
+ color={KColors.palette.gray.w600}
68
+ paddingH="0.75rem"
69
+ paddingV="0.5rem"
70
+ >
71
+ {trans('group:tag_title')}
72
+ </KLabel.Text>
73
+
74
+ {suggestions.length === 0 && !isSearching ? (
75
+ <KLabel.Text
76
+ typo="TextSmNormal"
77
+ color={KColors.palette.gray.w600}
78
+ paddingH="0.75rem"
79
+ paddingB="0.75rem"
80
+ >
81
+ {trans('group:tag_no_member_found')}
82
+ </KLabel.Text>
83
+ ) : suggestions.length === 0 && isSearching ? (
84
+ <KContainer.View paddingV="0.75rem" center>
85
+ <ActivityIndicator color={KColors.palette.primary.w400} />
86
+ </KContainer.View>
87
+ ) : (
88
+ <KContainer.View style={styles.listWrapper} padding="0.5rem">
89
+ <LegendList
90
+ data={suggestions}
91
+ renderItem={renderItem}
92
+ keyExtractor={keyExtractor}
93
+ estimatedItemSize={ROW_HEIGHT}
94
+ keyboardShouldPersistTaps="handled"
95
+ onEndReached={hasMore ? onLoadMore : undefined}
96
+ onEndReachedThreshold={0.5}
97
+ ListFooterComponent={renderFooter}
98
+ ItemSeparatorComponent={renderSeparator}
99
+ />
100
+ </KContainer.View>
101
+ )}
102
+ </KContainer.View>
103
+ </KContainer.View>
104
+ );
105
+ }
106
+ );
107
+
108
+ MentionSuggestionList.displayName = 'MentionSuggestionList';
109
+
110
+ const styles = StyleSheet.create({
111
+ // Shadow lives on its own, non-clipping wrapper — a single view can't
112
+ // both clip to a rounded corner (overflow:'hidden', required below) and
113
+ // cast a shadow, since the clip cuts the shadow off too.
114
+ shadowWrapper: {
115
+ shadowColor: KColors.black,
116
+ shadowOffset: { width: 0, height: -8 },
117
+ shadowOpacity: 0.08,
118
+ shadowRadius: 16,
119
+ elevation: 4,
120
+ },
121
+ container: {
122
+ borderRadius: KSpacingValue['0.75rem'],
123
+ borderTopLeftRadius: KSpacingValue['0.75rem'],
124
+ borderTopRightRadius: KSpacingValue['0.75rem'],
125
+ overflow: 'hidden',
126
+ },
127
+ listWrapper: {
128
+ maxHeight: MAX_PANEL_HEIGHT,
129
+ },
130
+ separator: {
131
+ height: ROW_GAP,
132
+ },
133
+ });
@@ -1,6 +1,12 @@
1
1
  import { useState, useMemo, useRef } from 'react';
2
2
 
3
- export type ActivePanelType = 'attachment' | 'sticker' | null;
3
+ // 'mention' is used only for mutual-exclusion (closing attachment/sticker
4
+ // when the mention picker opens) — the mention picker itself renders as its
5
+ // own absolutely-positioned sibling in ChatComposer, not through this
6
+ // state's render slot, since it must float above the input while the
7
+ // keyboard stays open (attachment/sticker intentionally take over the
8
+ // keyboard area, which the mention picker must not do).
9
+ export type ActivePanelType = 'attachment' | 'sticker' | 'mention' | null;
4
10
 
5
11
  interface UseChatComposerStateReturn {
6
12
  activePanelType: ActivePanelType;
@@ -0,0 +1,344 @@
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import type { TextInput, NativeSyntheticEvent } from 'react-native';
3
+ import type { TextInputSelectionChangeEventData } from 'react-native';
4
+ import { useInfiniteQuery } from '@tanstack/react-query';
5
+ import debounce from 'lodash/debounce';
6
+ import {
7
+ applyTextChange,
8
+ findActiveMention,
9
+ insertMention,
10
+ } from '../../../utils/mentions';
11
+ import { trans } from '../../../translation';
12
+ import { mentionQueryKeys } from '../../../hooks/query-keys';
13
+ import type {
14
+ ActiveMention,
15
+ Mention,
16
+ MentionSearchSource,
17
+ MentionSuggestion,
18
+ } from '../../../types/mention';
19
+
20
+ const SEARCH_DEBOUNCE_MS = 200;
21
+ // Matches MentionSuggestionList's MAX_VISIBLE_ROWS — one page fills exactly
22
+ // one screenful of the panel, so scrolling further triggers loadMore instead
23
+ // of over-fetching members the user hasn't scrolled to yet.
24
+ const SEARCH_LIMIT = 4;
25
+ const ALL_MEMBERS_USER_ID = 'ALL';
26
+
27
+ const allMembersSuggestion = (): MentionSuggestion => ({
28
+ userId: ALL_MEMBERS_USER_ID,
29
+ displayName: trans('group:tag_all'),
30
+ });
31
+
32
+ interface UseMentionComposerParams {
33
+ text: string;
34
+ setText: (text: string) => void;
35
+ searchSource: MentionSearchSource;
36
+ /** Namespaces the react-query cache per conversation — see the query
37
+ * itself for why this matters even though the hook is expected to remount
38
+ * per chat-detail screen. */
39
+ groupID?: string;
40
+ /** Mention only makes sense in group conversations (tagging a specific
41
+ * other member) — false for 1-1/bot/customer threads disables "@"
42
+ * detection entirely, not just the search results. Without this, typing
43
+ * "@" in a non-group chat would still open the picker (empty, since
44
+ * there's no real MentionSearchSource for it) instead of behaving as a
45
+ * plain character. Defaults to true so existing group-only callers don't
46
+ * need to pass it. */
47
+ enabled?: boolean;
48
+ inputRef: React.RefObject<TextInput | null>;
49
+ }
50
+
51
+ export interface UseMentionComposerReturn {
52
+ mentions: Mention[];
53
+ activeMention: ActiveMention | null;
54
+ suggestions: MentionSuggestion[];
55
+ isSearching: boolean;
56
+ isLoadingMore: boolean;
57
+ hasMore: boolean;
58
+ loadMore: () => void;
59
+ isPanelOpen: boolean;
60
+ handleChangeText: (text?: string) => void;
61
+ handleSelectionChange: (
62
+ event: NativeSyntheticEvent<TextInputSelectionChangeEventData>
63
+ ) => void;
64
+ selectSuggestion: (item: MentionSuggestion) => void;
65
+ closeMentionPanel: () => void;
66
+ clear: () => void;
67
+ }
68
+
69
+ /**
70
+ * Orchestrates the @mention composer: wires the pure Phase A1 logic
71
+ * (findActiveMention / applyTextChange / insertMention) and an injected
72
+ * MentionSearchSource into the live TextInput event loop.
73
+ *
74
+ * `text` stays owned by useChatCompose (the existing composer state) — this
75
+ * hook doesn't duplicate it, only mirrors it into a ref for synchronous
76
+ * access within its own event handlers (see textRef below).
77
+ */
78
+ export function useMentionComposer({
79
+ text,
80
+ setText,
81
+ searchSource,
82
+ groupID,
83
+ enabled = true,
84
+ inputRef,
85
+ }: UseMentionComposerParams): UseMentionComposerReturn {
86
+ const [mentions, setMentions] = useState<Mention[]>([]);
87
+ const [activeMention, setActiveMention] = useState<ActiveMention | null>(
88
+ null
89
+ );
90
+ // The keyword actually being searched for — distinct from
91
+ // activeMention.keyword, which updates on every keystroke. This only
92
+ // changes 200ms after typing pauses (see debouncedSetKeywordRef below),
93
+ // and IS the react-query key: typing further characters within the debounce
94
+ // window never touches the query at all, so the previous page's results
95
+ // stay on screen with zero flicker until the debounce actually fires.
96
+ // null means "no active search" (panel closed) — distinct from '' (an
97
+ // active search for the empty keyword, which also lists all members).
98
+ const [debouncedKeyword, setDebouncedKeyword] = useState<string | null>(null);
99
+
100
+ // Synchronous mirrors of the externally-owned `text` and internally-owned
101
+ // `mentions` state. React state updates aren't visible until the next
102
+ // render, but applyTextChange needs the *actual* latest committed values
103
+ // within the same tick a keystroke is processed — these refs are updated
104
+ // in lockstep with every setText/setMentions call below so they never lag.
105
+ const textRef = useRef(text);
106
+ const mentionsRef = useRef<Mention[]>([]);
107
+ const activeMentionRef = useRef<ActiveMention | null>(null);
108
+
109
+ const searchSourceRef = useRef(searchSource);
110
+ searchSourceRef.current = searchSource;
111
+
112
+ const enabledRef = useRef(enabled);
113
+ enabledRef.current = enabled;
114
+
115
+ useEffect(() => {
116
+ // External text changes not driven by this hook (e.g. clearing the
117
+ // composer after send) — keep the mirror and mention state in sync.
118
+ if (text !== textRef.current) {
119
+ textRef.current = text;
120
+ if (text === '') {
121
+ mentionsRef.current = [];
122
+ setMentions([]);
123
+ }
124
+ }
125
+ }, [text]);
126
+
127
+ const debouncedSetKeywordRef = useRef(
128
+ debounce(
129
+ (keyword: string) => setDebouncedKeyword(keyword),
130
+ SEARCH_DEBOUNCE_MS,
131
+ { leading: false, trailing: true }
132
+ )
133
+ );
134
+ useEffect(() => () => debouncedSetKeywordRef.current.cancel(), []);
135
+
136
+ const isActiveSearch = debouncedKeyword !== null;
137
+
138
+ const { data, isFetching, isFetchingNextPage, hasNextPage, fetchNextPage } =
139
+ useInfiniteQuery({
140
+ queryKey: mentionQueryKeys.search(groupID ?? '', debouncedKeyword ?? ''),
141
+ queryFn: ({ pageParam }) =>
142
+ searchSourceRef.current.search({
143
+ keyword: debouncedKeyword ?? '',
144
+ limit: SEARCH_LIMIT,
145
+ offset: pageParam,
146
+ }),
147
+ // Neither getGroupMemberList nor searchGroupMembers returns a total, so
148
+ // "is there a next page" is inferred from page size (see
149
+ // MentionSearchResult.hasMore's doc comment) instead of an exact count.
150
+ getNextPageParam: (lastPage, allPages) =>
151
+ lastPage.hasMore
152
+ ? allPages.reduce((sum, p) => sum + p.items.length, 0)
153
+ : undefined,
154
+ initialPageParam: 0,
155
+ enabled: isActiveSearch,
156
+ // Deliberately no maxPages cap — this is a scrolling picker, not a
157
+ // one-shot search; capping pages would drop already-loaded members as
158
+ // the user scrolls further, which is a worse experience than the memory
159
+ // cost of keeping them.
160
+ });
161
+
162
+ const includeAll = debouncedKeyword === '';
163
+ const suggestions = useMemo(() => {
164
+ const items = data?.pages.flatMap((page) => page.items) ?? [];
165
+ return includeAll ? [allMembersSuggestion(), ...items] : items;
166
+ }, [data, includeAll]);
167
+
168
+ const loadMore = useCallback(() => {
169
+ if (!isActiveSearch || !hasNextPage || isFetchingNextPage) return;
170
+ fetchNextPage();
171
+ }, [isActiveSearch, hasNextPage, isFetchingNextPage, fetchNextPage]);
172
+
173
+ const updateActiveMentionAndSearch = useCallback(
174
+ (currentText: string, cursor: number) => {
175
+ // Non-group conversation — "@" is a plain character, never a trigger.
176
+ // Checked here (not just left to the search source returning empty)
177
+ // so the picker UI never opens at all in this case.
178
+ const active = enabledRef.current
179
+ ? findActiveMention(currentText, cursor)
180
+ : null;
181
+ activeMentionRef.current = active;
182
+ setActiveMention(active);
183
+
184
+ if (!active) {
185
+ debouncedSetKeywordRef.current.cancel();
186
+ // Functional updater so an already-null value doesn't force a
187
+ // render on every ordinary keystroke (the common case).
188
+ setDebouncedKeyword((prev) => (prev === null ? prev : null));
189
+ return;
190
+ }
191
+
192
+ debouncedSetKeywordRef.current(active.keyword);
193
+ },
194
+ []
195
+ );
196
+
197
+ // Simplified from an earlier microtask-buffered design (see git history /
198
+ // plan doc for why that existed) after it caused a real, reproduced
199
+ // "Maximum update depth exceeded" loop in production testing. Root cause
200
+ // wasn't fully isolated without device logs, but the buffering added a
201
+ // second, redundant path (onSelectionChange re-running mention detection
202
+ // with native selection data that could be stale relative to the edit
203
+ // onChangeText had just processed) that could set state back and forth.
204
+ // applyTextChange already derives an authoritative cursor from the text
205
+ // diff itself — it never needed to wait for onSelectionChange for the
206
+ // edit case, so handling each event directly, synchronously, removes an
207
+ // entire dimension of timing uncertainty instead of trying to patch it.
208
+ const handleChangeText = useCallback(
209
+ (nextText?: string) => {
210
+ const nextTextRaw = nextText ?? '';
211
+ const previousText = textRef.current;
212
+ if (nextTextRaw === previousText) return;
213
+
214
+ const result = applyTextChange(
215
+ previousText,
216
+ mentionsRef.current,
217
+ nextTextRaw
218
+ );
219
+ textRef.current = result.text;
220
+ mentionsRef.current = result.mentions;
221
+ setText(result.text);
222
+ setMentions(result.mentions);
223
+
224
+ if (result.mentionRemoved) {
225
+ // The native buffer currently shows nextTextRaw, not our corrected
226
+ // result.text — force the cursor back to a position that's valid
227
+ // once React re-renders the controlled value.
228
+ requestAnimationFrame(() => {
229
+ inputRef.current?.setSelection(result.cursor, result.cursor);
230
+ });
231
+ }
232
+
233
+ updateActiveMentionAndSearch(result.text, result.cursor);
234
+ },
235
+ [inputRef, setText, updateActiveMentionAndSearch]
236
+ );
237
+
238
+ // Only meaningful for a pure cursor move with NO accompanying text change
239
+ // (tap elsewhere in the draft, arrow keys) — an edit's cursor is already
240
+ // handled authoritatively inside handleChangeText above. Deliberately
241
+ // does not re-run search/active-mention detection here to avoid the
242
+ // redundant double-handling that caused the loop this replaced.
243
+ const handleSelectionChange = useCallback(
244
+ (event: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
245
+ const selection = event.nativeEvent.selection;
246
+ if (selection.start !== selection.end) return;
247
+ if (selection.start === textRef.current.length) return; // typical post-edit echo, already handled
248
+
249
+ updateActiveMentionAndSearch(textRef.current, selection.start);
250
+ },
251
+ [updateActiveMentionAndSearch]
252
+ );
253
+
254
+ const selectSuggestion = useCallback(
255
+ (item: MentionSuggestion) => {
256
+ const active = activeMentionRef.current;
257
+ if (!active) return;
258
+
259
+ const result = insertMention(
260
+ textRef.current,
261
+ mentionsRef.current,
262
+ active,
263
+ {
264
+ userId: item.userId,
265
+ displayName: item.displayName,
266
+ }
267
+ );
268
+
269
+ textRef.current = result.text;
270
+ mentionsRef.current = result.mentions;
271
+ setText(result.text);
272
+ setMentions(result.mentions);
273
+ activeMentionRef.current = null;
274
+ setActiveMention(null);
275
+ debouncedSetKeywordRef.current.cancel();
276
+ setDebouncedKeyword(null);
277
+
278
+ requestAnimationFrame(() => {
279
+ inputRef.current?.setSelection(result.cursor, result.cursor);
280
+ });
281
+ },
282
+ [inputRef, setText]
283
+ );
284
+
285
+ // Called by ChatComposer when the user explicitly opens the
286
+ // attachment/sticker panel while a mention query is active — the mutual
287
+ // exclusion works both directions: opening mention closes attachment/
288
+ // sticker (ChatComposer's own handlers already do that reactively, see
289
+ // the effect wired there), and this closes mention when the user
290
+ // deliberately switches to a different panel.
291
+ const closeMentionPanel = useCallback(() => {
292
+ activeMentionRef.current = null;
293
+ setActiveMention(null);
294
+ debouncedSetKeywordRef.current.cancel();
295
+ setDebouncedKeyword(null);
296
+ }, []);
297
+
298
+ // Defensive: if `enabled` ever flips false while a mention query happens
299
+ // to be open (e.g. conversation type resolves after mount), close it
300
+ // immediately rather than leaving a stale picker for a thread that
301
+ // shouldn't have one at all.
302
+ useEffect(() => {
303
+ if (!enabled) closeMentionPanel();
304
+ // closeMentionPanel is stable ([] deps) — listing it here doesn't cause
305
+ // extra runs, it just keeps the deps array honest instead of suppressing
306
+ // the lint rule.
307
+ }, [enabled, closeMentionPanel]);
308
+
309
+ // Called explicitly by ChatComposer right when it clears the composer
310
+ // after send. The existing `useEffect` watching the `text` prop *would*
311
+ // eventually sync mentions to [] once `compose.setText('')` propagates
312
+ // down as a new `text` prop — but that's a render later, and in between
313
+ // there's a real transient bug: `value` is already '' while `mentions`
314
+ // still holds the old (now-stale) entries, and tokenize('', oldMentions)
315
+ // happily emits a mention segment anchored past the end of an empty
316
+ // string — a "ghost" mention with no surrounding text. Clearing
317
+ // synchronously here, in the same tick as setText(''), removes that
318
+ // window entirely instead of just shrinking it.
319
+ const clear = useCallback(() => {
320
+ textRef.current = '';
321
+ mentionsRef.current = [];
322
+ activeMentionRef.current = null;
323
+ setMentions([]);
324
+ setActiveMention(null);
325
+ debouncedSetKeywordRef.current.cancel();
326
+ setDebouncedKeyword(null);
327
+ }, []);
328
+
329
+ return {
330
+ mentions,
331
+ activeMention,
332
+ suggestions,
333
+ isSearching: isActiveSearch && isFetching && !isFetchingNextPage,
334
+ isLoadingMore: isFetchingNextPage,
335
+ hasMore: !!hasNextPage,
336
+ loadMore,
337
+ isPanelOpen: activeMention !== null,
338
+ handleChangeText,
339
+ handleSelectionChange,
340
+ selectSuggestion,
341
+ closeMentionPanel,
342
+ clear,
343
+ };
344
+ }