@openpeepshq/react 0.8.5 → 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.
Files changed (51) hide show
  1. package/dist/components/conversations/DirectMessageSearch.d.ts +7 -0
  2. package/dist/components/conversations/DirectMessageSearch.js +46 -0
  3. package/dist/components/conversations/MessageInThread.d.ts +2 -1
  4. package/dist/components/conversations/MessageInThread.js +3 -3
  5. package/dist/components/conversations/index.d.ts +1 -0
  6. package/dist/components/conversations/index.js +1 -0
  7. package/dist/components/post/pieces/modals/DeletePostModal.js +4 -4
  8. package/dist/components/post/post-form/ArticleForm.d.ts +4 -1
  9. package/dist/components/post/post-form/ArticleForm.js +8 -3
  10. package/dist/components/post/post-form/EventForm.d.ts +4 -1
  11. package/dist/components/post/post-form/EventForm.js +8 -4
  12. package/dist/components/post/post-form/NewPostModal.js +4 -5
  13. package/dist/components/post/post-form/OpenpeepsMarkdownInput.js +7 -1
  14. package/dist/components/post/post-form/OpenpeepsMarkdownInput.test.d.ts +1 -0
  15. package/dist/components/post/post-form/OpenpeepsMarkdownInput.test.js +64 -0
  16. package/dist/contexts/openpeeps/hooks/conversations.d.ts +3 -2
  17. package/dist/contexts/openpeeps/hooks/conversations.js +8 -1
  18. package/dist/hooks/events/useEventComposer.d.ts +3 -0
  19. package/dist/hooks/events/useEventComposer.js +24 -1
  20. package/dist/hooks/posts/useArticleComposer.d.ts +3 -0
  21. package/dist/hooks/posts/useArticleComposer.js +24 -1
  22. package/dist/pages/admin/ConfigurationCommunity.js +1 -1
  23. package/dist/pages/admin/community/CommunityAccess.d.ts +6 -0
  24. package/dist/pages/admin/community/CommunityAccess.js +60 -0
  25. package/dist/pages/articles/New.js +2 -2
  26. package/dist/pages/conversations/Index.js +17 -11
  27. package/dist/pages/conversations/Show.js +63 -12
  28. package/dist/pages/events/New.js +2 -2
  29. package/dist/pages/groups/Index.js +1 -1
  30. package/dist/pages/index.d.ts +1 -0
  31. package/dist/pages/index.js +1 -0
  32. package/package.json +5 -5
  33. package/src/components/conversations/DirectMessageSearch.tsx +130 -0
  34. package/src/components/conversations/MessageInThread.tsx +9 -2
  35. package/src/components/conversations/index.ts +1 -0
  36. package/src/components/post/pieces/modals/DeletePostModal.tsx +11 -9
  37. package/src/components/post/post-form/ArticleForm.tsx +28 -1
  38. package/src/components/post/post-form/EventForm.tsx +28 -2
  39. package/src/components/post/post-form/NewPostModal.tsx +5 -11
  40. package/src/components/post/post-form/OpenpeepsMarkdownInput.tsx +8 -1
  41. package/src/contexts/openpeeps/hooks/conversations.ts +16 -3
  42. package/src/hooks/events/useEventComposer.ts +29 -1
  43. package/src/hooks/posts/useArticleComposer.ts +29 -1
  44. package/src/pages/admin/ConfigurationCommunity.tsx +4 -0
  45. package/src/pages/admin/community/CommunityAccess.tsx +129 -0
  46. package/src/pages/articles/New.tsx +10 -1
  47. package/src/pages/conversations/Index.tsx +51 -32
  48. package/src/pages/conversations/Show.tsx +97 -18
  49. package/src/pages/events/New.tsx +10 -1
  50. package/src/pages/groups/Index.tsx +1 -1
  51. package/src/pages/index.ts +1 -0
@@ -1,4 +1,4 @@
1
- import { useMemo, useRef, useState } from 'react';
1
+ import { useLayoutEffect, useMemo, useRef, useState } from 'react';
2
2
  import type { PublicProfile } from '@openpeepshq/common/types';
3
3
  import {
4
4
  Button,
@@ -48,6 +48,13 @@ export function OpenpeepsMarkdownInput({
48
48
  const [open, setOpen] = useState(false);
49
49
  const [showPreview, setShowPreview] = useState(false);
50
50
  const textareaRef = useRef<HTMLTextAreaElement>(null);
51
+
52
+ // Preview is local state. Clearing the text after send has to leave an
53
+ // editable field, not the preview overlay.
54
+ useLayoutEffect(() => {
55
+ if (value.length === 0) setShowPreview(false);
56
+ }, [value]);
57
+
51
58
  const anchorRef = useRef<HTMLDivElement>(null);
52
59
 
53
60
  const profilesQuery = openpeepsApi.useSearchProfiles(query);
@@ -1,5 +1,6 @@
1
1
  import type { OpenpeepsClient } from '@openpeepshq/client';
2
2
  import type {
3
+ DirectMessageSearchHit,
3
4
  PostCreationData,
4
5
  PublicPost,
5
6
  SuccessFailureResponse,
@@ -13,7 +14,11 @@ type Query<T> = UseQueryResult<T, SuccessFailureResponse>;
13
14
  export type ConversationHooks = {
14
15
  useConversations: () => Query<PublicPost[][]>;
15
16
  useArchivedConversations: () => Query<PublicPost[][]>;
16
- useConversation: (id: string) => Query<PublicPost[]>;
17
+ useConversation: (id: string, messageId?: string) => Query<PublicPost[]>;
18
+ useSearchDirectMessages: (
19
+ q: string,
20
+ conversationId?: string,
21
+ ) => Query<DirectMessageSearchHit[]>;
17
22
  createConversationPostAction: (defaultPathParams?: {
18
23
  id: string;
19
24
  }) => (input: PostCreationData) => Promise<PublicPost>;
@@ -30,8 +35,16 @@ export const conversationHooks = (
30
35
  client.conversations.archived
31
36
  ? apiHook(client.conversations.archived)
32
37
  : ({ data: [], isPending: false } as never),
33
- useConversation: (id: string) =>
34
- apiHook(client.conversations.findById, { pathParams: { id } }),
38
+ useConversation: (id: string, messageId?: string) =>
39
+ apiHook(client.conversations.findById, {
40
+ pathParams: { id },
41
+ queryParams: messageId ? { message: messageId } : undefined,
42
+ }),
43
+ useSearchDirectMessages: (q: string, conversationId?: string) =>
44
+ apiHook(client.conversations.search, {
45
+ queryParams: conversationId ? { q, conversationId } : { q },
46
+ enabled: q.trim().length >= 2,
47
+ }),
35
48
  createConversationPostAction: payloadMutation(client.conversations.newPost, [
36
49
  ['conversations'],
37
50
  ]),
@@ -1,4 +1,8 @@
1
1
  import { useCallback, useEffect, useMemo, useState } from 'react';
2
+ import {
3
+ canAnnouncePosts,
4
+ isCommunityWideVisibility,
5
+ } from '@openpeepshq/common';
2
6
  import type {
3
7
  Event,
4
8
  PostCreationData,
@@ -12,6 +16,7 @@ import {
12
16
  import { useOpenpeeps } from '../../contexts/openpeeps';
13
17
  import { useNavigate } from '../../contexts/router';
14
18
  import { useT } from '../../i18n/context';
19
+ import { useCurrentProfile } from '../../components/layout/IdentityContext';
15
20
  import { useServerInfo } from '../../components/server-data/context';
16
21
  import { eventSanitizer, getNewPostStores } from '../../stores/newPosts';
17
22
 
@@ -19,8 +24,10 @@ export const useNewEvent = () => {
19
24
  const t = useT();
20
25
  const navigate = useNavigate();
21
26
  const serverInfo = useServerInfo();
27
+ const me = useCurrentProfile();
22
28
  const { openpeepsApi } = useOpenpeeps();
23
29
  const createPost = openpeepsApi.createPostAction();
30
+ const announcePost = openpeepsApi.admin.announcePostAction();
24
31
  const stores = getNewPostStores();
25
32
  const sanitize = useMemo(
26
33
  () => eventSanitizer(serverInfo.publicContent),
@@ -30,9 +37,14 @@ export const useNewEvent = () => {
30
37
  const [postData, setPostDataState] = useState<PostCreationData>(() =>
31
38
  sanitize(stores.event),
32
39
  );
40
+ const [notify, setNotify] = useState(false);
33
41
  const [submitting, setSubmitting] = useState(false);
34
42
  const [error, setError] = useState<string | null>(null);
35
43
 
44
+ const showNotify =
45
+ canAnnouncePosts(me?.roles) &&
46
+ isCommunityWideVisibility(postData.visibility);
47
+
36
48
  const event = postData.data.type === 'event' ? postData.data : null;
37
49
  const canSubmit =
38
50
  !!event?.start &&
@@ -73,6 +85,9 @@ export const useNewEvent = () => {
73
85
  type: 'event',
74
86
  data: normalizeEventDataForSave(event),
75
87
  });
88
+ if (notify && showNotify && created.id) {
89
+ await announcePost({ id: created.id });
90
+ }
76
91
  stores.resetNewEventState();
77
92
  navigate({ type: 'post', id: created.id });
78
93
  } catch (err) {
@@ -80,7 +95,17 @@ export const useNewEvent = () => {
80
95
  } finally {
81
96
  setSubmitting(false);
82
97
  }
83
- }, [createPost, event, navigate, postData, stores, t]);
98
+ }, [
99
+ announcePost,
100
+ createPost,
101
+ event,
102
+ navigate,
103
+ notify,
104
+ postData,
105
+ showNotify,
106
+ stores,
107
+ t,
108
+ ]);
84
109
 
85
110
  return {
86
111
  postData,
@@ -89,6 +114,9 @@ export const useNewEvent = () => {
89
114
  submitting,
90
115
  error,
91
116
  clearError: () => setError(null),
117
+ notify,
118
+ setNotify,
119
+ showNotify,
92
120
  submit,
93
121
  };
94
122
  };
@@ -1,4 +1,8 @@
1
1
  import { useCallback, useEffect, useState } from 'react';
2
+ import {
3
+ canAnnouncePosts,
4
+ isCommunityWideVisibility,
5
+ } from '@openpeepshq/common';
2
6
  import type {
3
7
  Article,
4
8
  PostCreationData,
@@ -7,6 +11,7 @@ import type {
7
11
  import { useOpenpeeps } from '../../contexts/openpeeps';
8
12
  import { useNavigate } from '../../contexts/router';
9
13
  import { useT } from '../../i18n/context';
14
+ import { useCurrentProfile } from '../../components/layout/IdentityContext';
10
15
  import { useServerInfo } from '../../components/server-data';
11
16
  import { defaultNewArticle, getNewPostStores } from '../../stores';
12
17
 
@@ -14,8 +19,10 @@ export const useNewArticle = () => {
14
19
  const t = useT();
15
20
  const navigate = useNavigate();
16
21
  const serverInfo = useServerInfo();
22
+ const me = useCurrentProfile();
17
23
  const { openpeepsApi } = useOpenpeeps();
18
24
  const createPost = openpeepsApi.createPostAction();
25
+ const announcePost = openpeepsApi.admin.announcePostAction();
19
26
  const stores = getNewPostStores();
20
27
 
21
28
  const [postData, setPostDataState] = useState<PostCreationData>(() => {
@@ -25,9 +32,14 @@ export const useNewArticle = () => {
25
32
  }
26
33
  return defaultNewArticle(serverInfo.publicContent);
27
34
  });
35
+ const [notify, setNotify] = useState(false);
28
36
  const [submitting, setSubmitting] = useState(false);
29
37
  const [error, setError] = useState<string | null>(null);
30
38
 
39
+ const showNotify =
40
+ canAnnouncePosts(me?.roles) &&
41
+ isCommunityWideVisibility(postData.visibility);
42
+
31
43
  const article = postData.data.type === 'article' ? postData.data : null;
32
44
  const canSubmit =
33
45
  !!article?.title?.trim() &&
@@ -55,6 +67,9 @@ export const useNewArticle = () => {
55
67
  setSubmitting(true);
56
68
  try {
57
69
  const created = await createPost({ ...postData, type: 'article' });
70
+ if (notify && showNotify && created.id) {
71
+ await announcePost({ id: created.id });
72
+ }
58
73
  stores.resetNewArticleState();
59
74
  navigate({ type: 'post', id: created.id });
60
75
  } catch (err) {
@@ -62,7 +77,17 @@ export const useNewArticle = () => {
62
77
  } finally {
63
78
  setSubmitting(false);
64
79
  }
65
- }, [article, createPost, navigate, postData, stores, t]);
80
+ }, [
81
+ announcePost,
82
+ article,
83
+ createPost,
84
+ navigate,
85
+ notify,
86
+ postData,
87
+ showNotify,
88
+ stores,
89
+ t,
90
+ ]);
66
91
 
67
92
  return {
68
93
  postData,
@@ -71,6 +96,9 @@ export const useNewArticle = () => {
71
96
  submitting,
72
97
  error,
73
98
  clearError: () => setError(null),
99
+ notify,
100
+ setNotify,
101
+ showNotify,
74
102
  submit,
75
103
  };
76
104
  };
@@ -10,6 +10,10 @@ export function AdminConfigurationCommunity() {
10
10
  translationPrefix="configuration.community.info"
11
11
  action="/admin/configuration/community/info"
12
12
  />
13
+ <ConfigMenuButton
14
+ translationPrefix="configuration.community.access"
15
+ action="/admin/configuration/community/access"
16
+ />
13
17
  <ConfigMenuButton
14
18
  translationPrefix="configuration.community.favicons"
15
19
  action="/admin/configuration/community/favicons"
@@ -0,0 +1,129 @@
1
+ import { useState } from 'react';
2
+ import { type CoreConfig, type ConfigTree } from '@openpeepshq/common/types';
3
+ import {
4
+ useT,
5
+ useSetPageHeader,
6
+ useOpenpeeps,
7
+ diffConfigTrees,
8
+ } from '../../../index';
9
+ import { Label, Switch, Toast } from '@openpeepshq/react-ui';
10
+
11
+ /**
12
+ * Open signups and public content live in the `openpeeps.core` config under
13
+ * `server.*`, but are surfaced here (under Community Customization) so an
14
+ * admin can update them without opening the raw Server settings tree editor.
15
+ */
16
+ export function AdminConfigurationCommunityAccess() {
17
+ const t = useT();
18
+ const { openpeepsApi } = useOpenpeeps();
19
+ const configQuery = openpeepsApi.admin.useConfigRead('openpeeps', 'core');
20
+ const updateConfig = openpeepsApi.admin.updateConfigAction({
21
+ namespace: 'openpeeps',
22
+ name: 'core',
23
+ });
24
+
25
+ useSetPageHeader(
26
+ t('configuration.community.access.title', { defaultValue: 'Access' }),
27
+ );
28
+
29
+ const base = configQuery.data?.config as CoreConfig | undefined;
30
+ const [status, setStatus] = useState<{
31
+ type: 'success' | 'error';
32
+ message: string;
33
+ } | null>(null);
34
+
35
+ const signUpsOpen = base?.server?.signUpsOpen ?? true;
36
+ const publicContent = base?.server?.publicContent ?? false;
37
+
38
+ const persist = async (patch: CoreConfig['server']) => {
39
+ if (!base) return;
40
+ setStatus(null);
41
+ try {
42
+ const diff = diffConfigTrees(
43
+ base.server as unknown as ConfigTree,
44
+ patch as unknown as ConfigTree,
45
+ );
46
+ if (Object.keys(diff).length === 0) return;
47
+ await updateConfig({
48
+ config: { server: diff } as Partial<CoreConfig>,
49
+ });
50
+ setStatus({
51
+ type: 'success',
52
+ message: t('configuration.community.access.updated', {
53
+ defaultValue: 'Access settings updated',
54
+ }),
55
+ });
56
+ } catch (err) {
57
+ setStatus({ type: 'error', message: (err as Error).message });
58
+ }
59
+ };
60
+
61
+ if (configQuery.isLoading || !base) {
62
+ return (
63
+ <div className="text-muted-foreground flex h-32 items-center justify-center text-sm">
64
+ {t('common.form.loading', { defaultValue: 'Loading…' })}
65
+ </div>
66
+ );
67
+ }
68
+
69
+ return (
70
+ <div className="space-y-6 p-4">
71
+ <p className="text-muted-foreground text-sm">
72
+ {t('configuration.community.access.description', {
73
+ defaultValue:
74
+ 'Control who can join your community and who can see it',
75
+ })}
76
+ </p>
77
+
78
+ <Label
79
+ title={t('configuration.community.openRegistrations.title', {
80
+ defaultValue: 'Open Signups',
81
+ })}
82
+ description={t(
83
+ 'configuration.community.openRegistrations.description',
84
+ {
85
+ defaultValue: 'Allow visitors to sign up for the community',
86
+ },
87
+ )}
88
+ forCheckbox
89
+ >
90
+ <Switch
91
+ checked={signUpsOpen}
92
+ onCheckedChange={(next) =>
93
+ void persist({ ...base.server, signUpsOpen: next })
94
+ }
95
+ data-testid="admin-community-access-signups"
96
+ />
97
+ </Label>
98
+
99
+ <Label
100
+ title={t('configuration.community.publicContent.title', {
101
+ defaultValue: 'Public content',
102
+ })}
103
+ description={t('configuration.community.publicContent.description', {
104
+ defaultValue:
105
+ 'Allow visitors to read public posts without an account',
106
+ })}
107
+ forCheckbox
108
+ >
109
+ <Switch
110
+ checked={publicContent}
111
+ onCheckedChange={(next) =>
112
+ void persist({ ...base.server, publicContent: next })
113
+ }
114
+ data-testid="admin-community-access-public-content"
115
+ />
116
+ </Label>
117
+
118
+ {status ? (
119
+ <Toast
120
+ variant={status.type}
121
+ testId="admin-community-access-toast"
122
+ onDismiss={() => setStatus(null)}
123
+ >
124
+ {status.message}
125
+ </Toast>
126
+ ) : null}
127
+ </div>
128
+ );
129
+ }
@@ -13,6 +13,9 @@ export function NewArticle() {
13
13
  submitting,
14
14
  error,
15
15
  clearError,
16
+ notify,
17
+ setNotify,
18
+ showNotify,
16
19
  submit,
17
20
  } = useNewArticle();
18
21
 
@@ -39,7 +42,13 @@ export function NewArticle() {
39
42
 
40
43
  return (
41
44
  <div className="pb-12">
42
- <ArticleForm postData={postData} onChange={setPostData} />
45
+ <ArticleForm
46
+ postData={postData}
47
+ onChange={setPostData}
48
+ showNotify={showNotify}
49
+ notify={notify}
50
+ onNotifyChange={setNotify}
51
+ />
43
52
 
44
53
  {error ? (
45
54
  <div className="px-3">
@@ -1,4 +1,5 @@
1
1
  import { useMemo, useState, type ReactNode } from 'react';
2
+ import { useNavigate } from 'react-router-dom';
2
3
  import { MessageCircleOff, Calendar, MessageSquarePlus } from 'lucide-react';
3
4
  import type { PublicPost } from '@openpeepshq/common/types';
4
5
  import {
@@ -14,6 +15,7 @@ import {
14
15
  } from '../../index';
15
16
  import {
16
17
  Avatar,
18
+ DirectMessageSearch,
17
19
  PostMarkdown,
18
20
  useAuthData,
19
21
  useCurrentProfile,
@@ -121,6 +123,7 @@ function TabButton({
121
123
 
122
124
  export function ConversationsIndex() {
123
125
  const t = useT();
126
+ const navigate = useNavigate();
124
127
  const { openpeepsApi } = useOpenpeeps();
125
128
  const { openCreateConversation } = useCreateNewConversation();
126
129
  const authData = useAuthData();
@@ -131,6 +134,8 @@ export function ConversationsIndex() {
131
134
 
132
135
  const canCreate = canCreatePost(authData, 'note', 'direct');
133
136
  const [tab, setTab] = useState<ConversationsTab>('active');
137
+ const [messageSearch, setMessageSearch] = useState('');
138
+ const searching = messageSearch.trim().length >= 2;
134
139
 
135
140
  const conversations =
136
141
  (tab === 'active' ? query.data : archivedQuery.data) ?? [];
@@ -192,39 +197,53 @@ export function ConversationsIndex() {
192
197
  );
193
198
 
194
199
  return (
195
- <AccessDeniedLoader queries={[activeQuery]}>
196
- {conversations.length === 0 ? (
197
- <div className="flex h-[80vh] items-center justify-center gap-2">
198
- <MessageCircleOff size={40} />
199
- <p className="text-muted-foreground">
200
- {t('conversations.empty', {
201
- defaultValue: 'No direct messages here',
202
- })}
203
- </p>
204
- </div>
205
- ) : (
206
- <div>
207
- {conversations.map((conversation) => {
208
- const first = conversation[0];
209
- if (!first) return null;
210
- return (
211
- <a
212
- key={first.id}
213
- href={`/conversations/${first.id}`}
214
- className="hover:bg-surface block text-left transition-all"
215
- title={t('conversations.open', {
216
- defaultValue: 'Open conversation',
217
- })}
218
- >
219
- <ChatPreview
220
- conversation={conversation}
221
- unreadCount={unseenByConversation[first.id] ?? 0}
222
- />
223
- </a>
200
+ <div>
201
+ {activeQuery.isError ? null : (
202
+ <DirectMessageSearch
203
+ onQueryChange={setMessageSearch}
204
+ onSelect={(hit) => {
205
+ navigate(
206
+ `/conversations/${hit.conversationId}?message=${hit.message.id}`,
224
207
  );
225
- })}
226
- </div>
208
+ }}
209
+ />
210
+ )}
211
+ {searching ? null : (
212
+ <AccessDeniedLoader queries={[activeQuery]}>
213
+ {conversations.length === 0 ? (
214
+ <div className="flex h-[80vh] items-center justify-center gap-2">
215
+ <MessageCircleOff size={40} />
216
+ <p className="text-muted-foreground">
217
+ {t('conversations.empty', {
218
+ defaultValue: 'No direct messages here',
219
+ })}
220
+ </p>
221
+ </div>
222
+ ) : (
223
+ <div>
224
+ {conversations.map((conversation) => {
225
+ const first = conversation[0];
226
+ if (!first) return null;
227
+ return (
228
+ <a
229
+ key={first.id}
230
+ href={`/conversations/${first.id}`}
231
+ className="hover:bg-surface block text-left transition-all"
232
+ title={t('conversations.open', {
233
+ defaultValue: 'Open conversation',
234
+ })}
235
+ >
236
+ <ChatPreview
237
+ conversation={conversation}
238
+ unreadCount={unseenByConversation[first.id] ?? 0}
239
+ />
240
+ </a>
241
+ );
242
+ })}
243
+ </div>
244
+ )}
245
+ </AccessDeniedLoader>
227
246
  )}
228
- </AccessDeniedLoader>
247
+ </div>
229
248
  );
230
249
  }
@@ -1,7 +1,7 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'react';
2
- import { useParams } from 'react-router-dom';
3
- import type { PostCreationData } from '@openpeepshq/common/types';
4
- import { Info } from 'lucide-react';
2
+ import { useParams, useSearchParams } from 'react-router-dom';
3
+ import type { PostCreationData, PublicPost } from '@openpeepshq/common/types';
4
+ import { Info, Search } from 'lucide-react';
5
5
  import {
6
6
  useT,
7
7
  useOpenpeeps,
@@ -14,6 +14,7 @@ import { canCreatePost } from '@openpeepshq/common';
14
14
  import {
15
15
  Avatar,
16
16
  ConversationParticipantsModal,
17
+ DirectMessageSearch,
17
18
  MessageInThread,
18
19
  OpenpeepsMarkdownInput,
19
20
  ProfileCard,
@@ -25,21 +26,43 @@ import { Button, LoadingSpinner } from '@openpeepshq/react-ui';
25
26
 
26
27
  const MAX_LENGTH = 500;
27
28
 
29
+ const isMessageId = (value: string | null): value is string =>
30
+ !!value &&
31
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
32
+
28
33
  export function ConversationShow() {
29
34
  const t = useT();
30
35
  const { id = '' } = useParams<{ id: string }>();
36
+ const [searchParams, setSearchParams] = useSearchParams();
37
+ const messageParam = searchParams.get('message');
38
+ const focusId = isMessageId(messageParam) ? messageParam : undefined;
31
39
  const { openpeepsApi, queryClient, client } = useOpenpeeps();
32
40
  const me = useCurrentProfile();
33
41
  const authData = useAuthData();
34
42
  const toast = useToast();
35
43
  const endRef = useRef<HTMLDivElement | null>(null);
36
44
 
37
- const conversationQuery = openpeepsApi.useConversation(id);
45
+ const conversationQuery = openpeepsApi.useConversation(id, focusId);
38
46
  const createMessage = openpeepsApi.createConversationPostAction({ id });
39
47
  const markPostsSeen = openpeepsApi.markPostsSeenAction();
40
48
  const flushPostViews = usePostViewFlush();
41
49
 
42
- const messages = conversationQuery.data ?? [];
50
+ // Keep the open thread visible while a search hit refetches an older
51
+ // message. Switching conversations clears it so the previous thread
52
+ // does not linger.
53
+ const [shownMessages, setShownMessages] = useState<PublicPost[]>([]);
54
+ const [shownFor, setShownFor] = useState(id);
55
+ const [searchOpen, setSearchOpen] = useState(false);
56
+ if (shownFor !== id) {
57
+ setShownFor(id);
58
+ setShownMessages([]);
59
+ setSearchOpen(false);
60
+ }
61
+ useEffect(() => {
62
+ if (conversationQuery.data) setShownMessages(conversationQuery.data);
63
+ }, [conversationQuery.data]);
64
+
65
+ const messages = shownMessages;
43
66
  const lastMessage = messages[messages.length - 1];
44
67
  const participants =
45
68
  lastMessage?.audience?.filter((a) => a.id !== me?.id) ?? [];
@@ -77,20 +100,44 @@ export function ConversationShow() {
77
100
 
78
101
  const navigate = useNavigate();
79
102
 
103
+ const searchLabel = searchOpen
104
+ ? t('common.close', { defaultValue: 'Close' })
105
+ : t('conversations.search.toggle', {
106
+ defaultValue: 'Search this conversation',
107
+ });
108
+
80
109
  const headerActions = useMemo(
81
110
  () => (
82
- <button
83
- type="button"
84
- onClick={() => navigate({ type: 'conversation', id, view: 'info' })}
85
- title={t('conversations.info.title', {
86
- defaultValue: 'Conversation info',
87
- })}
88
- className="text-muted-foreground hover:text-foreground rounded-md p-1.5"
89
- >
90
- <Info size={18} />
91
- </button>
111
+ <div className="flex items-center">
112
+ <button
113
+ type="button"
114
+ onClick={() => setSearchOpen((open) => !open)}
115
+ title={searchLabel}
116
+ aria-label={searchLabel}
117
+ aria-expanded={searchOpen}
118
+ aria-controls={searchOpen ? 'conversation-message-search' : undefined}
119
+ data-testid="conversation-search-toggle"
120
+ className={`rounded-md p-1.5 ${
121
+ searchOpen
122
+ ? 'bg-muted text-foreground'
123
+ : 'text-muted-foreground hover:text-foreground'
124
+ }`}
125
+ >
126
+ <Search size={18} />
127
+ </button>
128
+ <button
129
+ type="button"
130
+ onClick={() => navigate({ type: 'conversation', id, view: 'info' })}
131
+ title={t('conversations.info.title', {
132
+ defaultValue: 'Conversation info',
133
+ })}
134
+ className="text-muted-foreground hover:text-foreground rounded-md p-1.5"
135
+ >
136
+ <Info size={18} />
137
+ </button>
138
+ </div>
92
139
  ),
93
- [id, navigate, t],
140
+ [id, navigate, searchLabel, searchOpen, t],
94
141
  );
95
142
 
96
143
  useSetPageHeader(headerTitle, headerActions);
@@ -127,8 +174,25 @@ export function ConversationShow() {
127
174
  };
128
175
 
129
176
  useEffect(() => {
177
+ if (focusId) return;
130
178
  endRef.current?.scrollIntoView({ behavior: 'smooth' });
131
- }, [messages.length]);
179
+ }, [messages.length, focusId]);
180
+
181
+ // The ?message= param sticks around after focusing, so scroll only the
182
+ // first time the message renders — re-scrolling on every messages change
183
+ // would fight the user while they read.
184
+ const scrolledToMessageRef = useRef<string | null>(null);
185
+ useEffect(() => {
186
+ if (!focusId) {
187
+ scrolledToMessageRef.current = null;
188
+ return;
189
+ }
190
+ if (scrolledToMessageRef.current === focusId) return;
191
+ const element = document.querySelector(`[data-message-id="${focusId}"]`);
192
+ if (!element) return;
193
+ scrolledToMessageRef.current = focusId;
194
+ element.scrollIntoView({ block: 'center' });
195
+ }, [focusId, messages]);
132
196
 
133
197
  useEffect(() => {
134
198
  if (!id) return;
@@ -159,7 +223,7 @@ export function ConversationShow() {
159
223
  void markPostsSeen({ postIds: unseenIds });
160
224
  }, [id, me, messages, conversationQuery.isLoading, markPostsSeen]);
161
225
 
162
- if (conversationQuery.isLoading) {
226
+ if (conversationQuery.isLoading && messages.length === 0) {
163
227
  return (
164
228
  <div className="text-muted-foreground flex h-32 items-center justify-center text-sm">
165
229
  <LoadingSpinner />
@@ -198,6 +262,20 @@ export function ConversationShow() {
198
262
  onOpenChange={setParticipantsOpen}
199
263
  participants={participants}
200
264
  />
265
+ {searchOpen ? (
266
+ <div id="conversation-message-search">
267
+ <DirectMessageSearch
268
+ conversationId={id}
269
+ autoFocus
270
+ onSelect={(hit) => {
271
+ const next = new URLSearchParams(searchParams);
272
+ next.set('message', hit.message.id);
273
+ setSearchParams(next);
274
+ setSearchOpen(false);
275
+ }}
276
+ />
277
+ </div>
278
+ ) : null}
201
279
 
202
280
  <div
203
281
  role="log"
@@ -214,6 +292,7 @@ export function ConversationShow() {
214
292
  message={m}
215
293
  multipleParticipants={multipleParticipants}
216
294
  conversationRootId={id}
295
+ highlighted={m.id === focusId}
217
296
  />
218
297
  ))}
219
298
  <div ref={endRef} />