@openpeepshq/rn-components 0.8.7 → 0.9.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/rn-components",
3
- "version": "0.8.7",
3
+ "version": "0.9.0",
4
4
  "description": "React Native community screens and shared UI (extracted from the Black Ambition app) for the OpenPeeps stack.",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./src/index.ts",
@@ -142,8 +142,8 @@
142
142
  "timezones-list": "^3.1.0",
143
143
  "validator": "^13.15.23",
144
144
  "zustand": "^5.0.3",
145
- "@openpeepshq/common": "^0.4.0",
146
- "@openpeepshq/react": "^0.8.6"
145
+ "@openpeepshq/common": "^0.5.0",
146
+ "@openpeepshq/react": "^0.9.0"
147
147
  },
148
148
  "devDependencies": {
149
149
  "@babel/core": "^7.26.7",
@@ -0,0 +1,138 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { ActivityIndicator, Pressable, ScrollView, View } from 'react-native';
3
+ import {
4
+ conversationLabel,
5
+ messagePreviewText,
6
+ profileName,
7
+ type DirectMessageSearchHit,
8
+ } from '@openpeepshq/common';
9
+ import { useCurrentProfile, useOpenpeeps } from '@openpeepshq/react';
10
+ import { useTranslation } from 'react-i18next';
11
+ import { UpdatingDate } from '../custom/date/updating-date';
12
+ import { Input } from '../ui/input';
13
+ import { ThemedText } from '../ui/themed-text';
14
+
15
+ export const DirectMessageSearch = ({
16
+ conversationId,
17
+ autoFocus,
18
+ onQueryChange,
19
+ onSelect,
20
+ }: {
21
+ conversationId?: string;
22
+ autoFocus?: boolean;
23
+ onQueryChange?: (query: string) => void;
24
+ onSelect: (hit: DirectMessageSearchHit) => void;
25
+ }) => {
26
+ const { t } = useTranslation();
27
+ const me = useCurrentProfile();
28
+ const { openpeepsApi } = useOpenpeeps();
29
+ const [input, setInput] = useState('');
30
+ const [query, setQuery] = useState('');
31
+
32
+ useEffect(() => {
33
+ const handle = setTimeout(() => setQuery(input.trim()), 300);
34
+ return () => clearTimeout(handle);
35
+ }, [input]);
36
+
37
+ useEffect(() => {
38
+ onQueryChange?.(query);
39
+ }, [query, onQueryChange]);
40
+
41
+ const search = openpeepsApi.useSearchDirectMessages(query, conversationId);
42
+ const active = query.length >= 2;
43
+ const hits = search.data ?? [];
44
+ const placeholder = conversationId
45
+ ? t('conversations.search.inConversation', {
46
+ defaultValue: 'Search this conversation…',
47
+ })
48
+ : t('conversations.search.placeholder', {
49
+ defaultValue: 'Search all messages…',
50
+ });
51
+
52
+ return (
53
+ <View className="border-border gap-2 border-b p-3">
54
+ <Input
55
+ value={input}
56
+ onChangeText={setInput}
57
+ placeholder={placeholder}
58
+ accessibilityLabel={placeholder}
59
+ autoFocus={autoFocus}
60
+ autoCapitalize="none"
61
+ autoCorrect={false}
62
+ returnKeyType="search"
63
+ testID="dm-search-input"
64
+ />
65
+ {active && search.isFetching ? (
66
+ <View className="flex-row items-center gap-2">
67
+ <ActivityIndicator size="small" />
68
+ <ThemedText className="text-muted-foreground text-sm">
69
+ {t('conversations.search.searching', {
70
+ defaultValue: 'Searching…',
71
+ })}
72
+ </ThemedText>
73
+ </View>
74
+ ) : null}
75
+ {active && search.isError ? (
76
+ <ThemedText className="text-destructive text-sm">
77
+ {t('conversations.search.error', { defaultValue: 'Search failed' })}
78
+ </ThemedText>
79
+ ) : null}
80
+ {active && !search.isFetching && !search.isError && hits.length === 0 ? (
81
+ <ThemedText className="text-muted-foreground text-sm">
82
+ {t('conversations.search.empty', {
83
+ defaultValue: 'No messages match "{{query}}"',
84
+ query,
85
+ })}
86
+ </ThemedText>
87
+ ) : null}
88
+ {active && hits.length > 0 ? (
89
+ <ScrollView
90
+ style={{ maxHeight: 240 }}
91
+ accessibilityLabel={t('conversations.search.results', {
92
+ defaultValue: 'Message search results',
93
+ })}
94
+ testID="dm-search-results"
95
+ >
96
+ {hits.map((hit) => {
97
+ const senderId = hit.message.profile.id;
98
+ const sender =
99
+ senderId === me?.id
100
+ ? t('common.you', { defaultValue: 'You' })
101
+ : profileName(hit.message.profile);
102
+ const others = conversationLabel(hit.message, me?.id);
103
+ return (
104
+ <Pressable
105
+ key={hit.message.id}
106
+ className="rounded-md px-2 py-2"
107
+ accessibilityLabel={t('conversations.search.openResult', {
108
+ defaultValue: 'Show this message',
109
+ })}
110
+ testID="dm-search-result"
111
+ onPress={() => onSelect(hit)}
112
+ >
113
+ {!conversationId && others ? (
114
+ <ThemedText
115
+ className="text-sm font-semibold"
116
+ numberOfLines={1}
117
+ >
118
+ {others}
119
+ </ThemedText>
120
+ ) : null}
121
+ <ThemedText
122
+ className="text-muted-foreground text-sm"
123
+ numberOfLines={1}
124
+ >
125
+ {sender}
126
+ </ThemedText>
127
+ <UpdatingDate date={hit.message.createdAt} />
128
+ <ThemedText className="text-sm" numberOfLines={2}>
129
+ {messagePreviewText(hit.message)}
130
+ </ThemedText>
131
+ </Pressable>
132
+ );
133
+ })}
134
+ </ScrollView>
135
+ ) : null}
136
+ </View>
137
+ );
138
+ };
@@ -38,6 +38,7 @@ export interface MessageInThreadProps {
38
38
  message: PublicPost;
39
39
  multipleParticipants?: boolean;
40
40
  conversationRootId?: string;
41
+ highlighted?: boolean;
41
42
  }
42
43
 
43
44
  export const MessageInThread = ({
@@ -45,6 +46,7 @@ export const MessageInThread = ({
45
46
  message,
46
47
  multipleParticipants = true,
47
48
  conversationRootId,
49
+ highlighted = false,
48
50
  }: MessageInThreadProps) => {
49
51
  const { t } = useTranslation();
50
52
  const me = useCurrentProfile();
@@ -61,7 +63,7 @@ export const MessageInThread = ({
61
63
  const isMe = message.profile.id === me?.id;
62
64
 
63
65
  return (
64
- <>
66
+ <View className={highlighted ? 'bg-muted rounded-md' : undefined}>
65
67
  {!previous ? (
66
68
  <ThemedText className="mt-2 w-full text-center text-sm">
67
69
  {message.inReplyToId
@@ -126,6 +128,6 @@ export const MessageInThread = ({
126
128
  ) : null}
127
129
  <UpdatingDate date={message.createdAt} />
128
130
  </View>
129
- </>
131
+ </View>
130
132
  );
131
133
  };
@@ -4,5 +4,6 @@ export * from './ConversationPreviewCard';
4
4
  export * from './ConversationProfileHeader';
5
5
  export * from './ConversationSettingsSheet';
6
6
  export * from './CreateNewConversationContext';
7
+ export * from './DirectMessageSearch';
7
8
  export * from './DirectMessagesHeaderActions';
8
9
  export * from './MessageInThread';
@@ -29,6 +29,7 @@ export type MainStackParamList = {
29
29
  MyJams: undefined;
30
30
  Conversation: {
31
31
  id: string;
32
+ messageId?: string;
32
33
  };
33
34
  ConversationInfo: {
34
35
  id: string;
@@ -26,6 +26,7 @@ import { canCreatePost } from '@openpeepshq/common';
26
26
  import { BottomSheetModal } from '@gorhom/bottom-sheet';
27
27
  import {
28
28
  ConversationPreviewCard,
29
+ DirectMessageSearch,
29
30
  useCreateNewConversation,
30
31
  } from '../../components/conversations/index';
31
32
  import { useTranslation } from 'react-i18next';
@@ -47,6 +48,8 @@ export const ConversationsIndex: React.FC<MessagesProps> = ({ navigation }) => {
47
48
  const settingsRef = useRef<BottomSheetModal>(null);
48
49
  const [refreshing, setRefreshing] = useState(false);
49
50
  const [tab, setTab] = useState<ConversationsTab>('active');
51
+ const [messageSearch, setMessageSearch] = useState('');
52
+ const searching = messageSearch.trim().length >= 2;
50
53
 
51
54
  const query = openpeepsApi.useConversations();
52
55
  const archivedQuery = openpeepsApi.useArchivedConversations();
@@ -125,34 +128,52 @@ export const ConversationsIndex: React.FC<MessagesProps> = ({ navigation }) => {
125
128
  contentContainerClassName="grow"
126
129
  className="w-full flex bg-background relative p-2"
127
130
  >
128
- {isLoading && <ActivityIndicator size={'small'} />}
129
- {!isLoading && conversations?.length === 0 && (
130
- <View className="flex h-[80vh] items-center justify-center gap-2">
131
- <MessageCircleOffIcon size={40} className="text-muted-foreground" />
132
- <ThemedText className="text-muted-foreground">
133
- {t('conversations.empty', {
134
- defaultValue: 'No direct messages here',
135
- })}
136
- </ThemedText>
137
- </View>
131
+ {activeQuery.isError ? null : (
132
+ <DirectMessageSearch
133
+ onQueryChange={setMessageSearch}
134
+ onSelect={(hit) =>
135
+ navigation.navigate('Conversation', {
136
+ id: hit.conversationId,
137
+ messageId: hit.message.id,
138
+ })
139
+ }
140
+ />
141
+ )}
142
+ {searching ? null : (
143
+ <>
144
+ {isLoading && <ActivityIndicator size={'small'} />}
145
+ {!isLoading && conversations?.length === 0 && (
146
+ <View className="flex h-[80vh] items-center justify-center gap-2">
147
+ <MessageCircleOffIcon
148
+ size={40}
149
+ className="text-muted-foreground"
150
+ />
151
+ <ThemedText className="text-muted-foreground">
152
+ {t('conversations.empty', {
153
+ defaultValue: 'No direct messages here',
154
+ })}
155
+ </ThemedText>
156
+ </View>
157
+ )}
158
+ {!isLoading &&
159
+ conversations?.map((conversation, idx) => (
160
+ <ConversationPreviewCard
161
+ key={idx}
162
+ conversation={conversation}
163
+ unreadCount={
164
+ conversation[0]?.id
165
+ ? (unseenByConversation[conversation[0].id] ?? 0)
166
+ : 0
167
+ }
168
+ onPress={() =>
169
+ navigation.navigate('Conversation', {
170
+ id: conversation[0]?.id,
171
+ })
172
+ }
173
+ />
174
+ ))}
175
+ </>
138
176
  )}
139
- {!isLoading &&
140
- conversations?.map((conversation, idx) => (
141
- <ConversationPreviewCard
142
- key={idx}
143
- conversation={conversation}
144
- unreadCount={
145
- conversation[0]?.id
146
- ? (unseenByConversation[conversation[0].id] ?? 0)
147
- : 0
148
- }
149
- onPress={() =>
150
- navigation.navigate('Conversation', {
151
- id: conversation[0]?.id,
152
- })
153
- }
154
- />
155
- ))}
156
177
  </KeyboardAwareScrollView>
157
178
  <ConversationSettingsSheet
158
179
  ref={settingsRef}
@@ -16,8 +16,8 @@ import {
16
16
  Platform,
17
17
  ActivityIndicator,
18
18
  } from 'react-native';
19
- import { InfoIcon } from '../../components/icons/index';
20
- import { MediaAttachmentData } from '@openpeepshq/common';
19
+ import { InfoIcon, SearchIcon } from '../../components/icons/index';
20
+ import { MediaAttachmentData, type PublicPost } from '@openpeepshq/common';
21
21
  import { ComposeAttachments } from '../../components/post/post-form/ComposeAttachments';
22
22
  import { OpenpeepsMarkdownInput } from '../../components/post/post-form/OpenpeepsMarkdownInput';
23
23
  import { maxContentLength } from '../../lib/utils';
@@ -28,6 +28,7 @@ import { ThemedText } from '../../components/ui/themed-text';
28
28
  import {
29
29
  ConversationParticipantsModal,
30
30
  ConversationProfileHeader,
31
+ DirectMessageSearch,
31
32
  MessageInThread,
32
33
  } from '../../components/conversations/index';
33
34
 
@@ -56,19 +57,41 @@ const ConversationParticipantDetail = ({
56
57
  );
57
58
  };
58
59
 
60
+ const isMessageId = (value: string | undefined): value is string =>
61
+ !!value &&
62
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
63
+
59
64
  type ConversationProps = MainScreenProps<'Conversation'>;
60
65
 
61
66
  export const ConversationShow = ({ route, navigation }: ConversationProps) => {
62
67
  const { t } = useTranslation();
63
68
  const { id } = route.params;
69
+ const messageId = isMessageId(route.params.messageId)
70
+ ? route.params.messageId
71
+ : undefined;
64
72
  const { openpeepsApi, currentProfile, queryClient, client } = useOpenpeeps();
65
- const { data: messages, isLoading } = openpeepsApi.useConversation(id);
73
+ const conversationQuery = openpeepsApi.useConversation(id, messageId);
74
+ const [shownMessages, setShownMessages] = useState<PublicPost[]>([]);
75
+ const [shownFor, setShownFor] = useState(id);
76
+ const [searchOpen, setSearchOpen] = useState(false);
77
+ if (shownFor !== id) {
78
+ setShownFor(id);
79
+ setShownMessages([]);
80
+ setSearchOpen(false);
81
+ }
82
+ useEffect(() => {
83
+ if (conversationQuery.data) setShownMessages(conversationQuery.data);
84
+ }, [conversationQuery.data]);
85
+ const messages = shownMessages;
86
+ const isLoading = conversationQuery.isLoading && messages.length === 0;
66
87
  const markPostsSeen = openpeepsApi.markPostsSeenAction();
67
88
  const flushPostViews = usePostViewFlush();
68
89
  const [isSending, setIsSending] = useState(false);
69
90
  const [attachments, setAttachments] = useState<MediaAttachmentData[]>([]);
70
91
  const [content, setContent] = useState('');
71
92
  const scrollViewRef = useRef<ScrollView>(null);
93
+ const messageOffsets = useRef<Record<string, number>>({});
94
+ const scrolledToMessageRef = useRef<string | null>(null);
72
95
  const [participantsOpen, setParticipantsOpen] = useState(false);
73
96
 
74
97
  const sendMessage = openpeepsApi.createConversationPostAction({ id });
@@ -144,6 +167,12 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
144
167
  void markPostsSeen({ postIds: unseenIds });
145
168
  }, [id, currentProfile, messages, isLoading, markPostsSeen]);
146
169
 
170
+ const searchLabel = searchOpen
171
+ ? t('common.close', { defaultValue: 'Close' })
172
+ : t('conversations.search.toggle', {
173
+ defaultValue: 'Search this conversation',
174
+ });
175
+
147
176
  useSetPageHeader(
148
177
  isLoading ? undefined : (
149
178
  <ConversationProfileHeader
@@ -154,11 +183,27 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
154
183
  />
155
184
  ),
156
185
  isLoading ? undefined : (
157
- <TouchableOpacity
158
- onPress={() => navigation.navigate('ConversationInfo', { id })}
159
- >
160
- <InfoIcon size={18} className="text-foreground" />
161
- </TouchableOpacity>
186
+ <View className="flex-row items-center">
187
+ <TouchableOpacity
188
+ onPress={() => setSearchOpen((open) => !open)}
189
+ accessibilityRole="button"
190
+ accessibilityLabel={searchLabel}
191
+ accessibilityState={{ expanded: searchOpen }}
192
+ testID="conversation-search-toggle"
193
+ className={`rounded-md p-1.5 ${searchOpen ? 'bg-muted' : ''}`}
194
+ >
195
+ <SearchIcon
196
+ size={18}
197
+ className={searchOpen ? 'text-foreground' : 'text-muted-foreground'}
198
+ />
199
+ </TouchableOpacity>
200
+ <TouchableOpacity
201
+ onPress={() => navigation.navigate('ConversationInfo', { id })}
202
+ className="rounded-md p-1.5"
203
+ >
204
+ <InfoIcon size={18} className="text-foreground" />
205
+ </TouchableOpacity>
206
+ </View>
162
207
  )
163
208
  );
164
209
 
@@ -208,20 +253,62 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
208
253
  behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
209
254
  keyboardVerticalOffset={Platform.OS === 'ios' ? 90 : 0}
210
255
  >
256
+ {searchOpen ? (
257
+ <View nativeID="conversation-message-search">
258
+ <DirectMessageSearch
259
+ conversationId={id}
260
+ autoFocus
261
+ onSelect={(hit) => {
262
+ navigation.setParams({ messageId: hit.message.id });
263
+ setSearchOpen(false);
264
+ }}
265
+ />
266
+ </View>
267
+ ) : null}
211
268
  <ScrollView
212
269
  ref={scrollViewRef}
213
270
  className="flex-1 pb-8"
214
- onContentSizeChange={() =>
215
- scrollViewRef.current?.scrollToEnd({ animated: true })
216
- }
271
+ onContentSizeChange={() => {
272
+ if (!messageId) {
273
+ scrollViewRef.current?.scrollToEnd({ animated: true });
274
+ return;
275
+ }
276
+ // The messageId param persists, so scroll to the focused
277
+ // message only once — re-scrolling on every content change
278
+ // would fight the user while they read.
279
+ if (scrolledToMessageRef.current === messageId) return;
280
+ const y = messageOffsets.current[messageId];
281
+ if (y !== undefined) {
282
+ scrolledToMessageRef.current = messageId;
283
+ scrollViewRef.current?.scrollTo({ y, animated: true });
284
+ }
285
+ }}
217
286
  >
218
287
  {messages?.map((message, idx) => (
219
- <View key={message.id} className="px-2">
288
+ <View
289
+ key={message.id}
290
+ className="px-2"
291
+ onLayout={(event) => {
292
+ messageOffsets.current[message.id] =
293
+ event.nativeEvent.layout.y;
294
+ if (
295
+ message.id === messageId &&
296
+ scrolledToMessageRef.current !== messageId
297
+ ) {
298
+ scrolledToMessageRef.current = messageId;
299
+ scrollViewRef.current?.scrollTo({
300
+ y: event.nativeEvent.layout.y,
301
+ animated: true,
302
+ });
303
+ }
304
+ }}
305
+ >
220
306
  <MessageInThread
221
307
  previous={messages[idx - 1]}
222
308
  message={message}
223
309
  multipleParticipants={multipleParticipants}
224
310
  conversationRootId={id}
311
+ highlighted={message.id === messageId}
225
312
  />
226
313
  </View>
227
314
  ))}