@openpeepshq/react 0.7.1 → 0.8.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 (86) hide show
  1. package/dist/components/markdown/compileMarkdown.js +1 -0
  2. package/dist/components/navigation/FooterMobile.js +4 -3
  3. package/dist/components/navigation/HeaderMobile.js +5 -2
  4. package/dist/components/navigation/MobileMenuItem.d.ts +3 -1
  5. package/dist/components/navigation/MobileMenuItem.js +7 -2
  6. package/dist/components/navigation/NavWithTooltip.d.ts +9 -0
  7. package/dist/components/navigation/NavWithTooltip.js +4 -0
  8. package/dist/components/navigation/index.d.ts +2 -0
  9. package/dist/components/navigation/index.js +1 -0
  10. package/dist/components/navigation/sideBarNavLinkClass.d.ts +6 -4
  11. package/dist/components/navigation/sideBarNavLinkClass.js +6 -2
  12. package/dist/components/post/CompactReplyParent.js +5 -6
  13. package/dist/components/post/TruncatedMarkdown.d.ts +27 -0
  14. package/dist/components/post/TruncatedMarkdown.js +37 -0
  15. package/dist/components/post/helpers.d.ts +2 -2
  16. package/dist/components/post/helpers.js +3 -16
  17. package/dist/components/post/index.d.ts +2 -0
  18. package/dist/components/post/index.js +1 -0
  19. package/dist/components/post/pieces/EventLocation.js +14 -0
  20. package/dist/components/post/pieces/EventRsvpButton.js +3 -1
  21. package/dist/components/post/pieces/PollContent.js +8 -2
  22. package/dist/components/post/post-form/ArticleForm.js +1 -1
  23. package/dist/components/post/post-form/EventForm.js +23 -3
  24. package/dist/components/post/post-form/ResourceForm.d.ts +3 -1
  25. package/dist/components/post/post-form/ResourceForm.js +48 -7
  26. package/dist/components/post/post-form/VisibilitySelector.js +9 -3
  27. package/dist/components/post/types/Article.js +2 -3
  28. package/dist/components/post/types/Event.js +10 -1
  29. package/dist/components/post/types/resource/FullResource.js +3 -3
  30. package/dist/components/preview-link/local/WelcomePreview.js +4 -2
  31. package/dist/components/resources/ResourceCard.d.ts +5 -1
  32. package/dist/components/resources/ResourceCard.js +29 -5
  33. package/dist/components/resources/ResourceCard.test.d.ts +1 -0
  34. package/dist/components/resources/ResourceCard.test.js +75 -0
  35. package/dist/components/resources/ResourceLibrary.js +1 -1
  36. package/dist/hooks/events/useEventRsvp.d.ts +1 -0
  37. package/dist/hooks/events/useEventRsvp.js +15 -7
  38. package/dist/hooks/posts/useResourceComposer.d.ts +2 -0
  39. package/dist/hooks/posts/useResourceComposer.js +131 -25
  40. package/dist/hooks/resources/useResourceLibrary.d.ts +1 -1
  41. package/dist/hooks/resources/useResourceLibrary.js +6 -2
  42. package/dist/hooks/useWelcomePreview.d.ts +3 -0
  43. package/dist/hooks/useWelcomePreview.js +1 -0
  44. package/dist/navigation/menuItems.js +15 -0
  45. package/dist/navigation/targets.d.ts +2 -0
  46. package/dist/pages/admin/community/CommunityMarkdownPage.js +2 -1
  47. package/dist/pages/auth/Register.js +3 -1
  48. package/dist/pages/resources/Edit.js +3 -3
  49. package/dist/pages/resources/New.js +2 -2
  50. package/dist/pages/settings/Account.js +30 -2
  51. package/package.json +2 -2
  52. package/src/components/markdown/compileMarkdown.ts +1 -0
  53. package/src/components/navigation/FooterMobile.tsx +41 -33
  54. package/src/components/navigation/HeaderMobile.tsx +55 -41
  55. package/src/components/navigation/MobileMenuItem.tsx +15 -2
  56. package/src/components/navigation/NavWithTooltip.tsx +26 -0
  57. package/src/components/navigation/index.ts +2 -0
  58. package/src/components/navigation/sideBarNavLinkClass.ts +9 -6
  59. package/src/components/post/CompactReplyParent.tsx +17 -10
  60. package/src/components/post/TruncatedMarkdown.tsx +91 -0
  61. package/src/components/post/helpers.ts +4 -18
  62. package/src/components/post/index.ts +2 -0
  63. package/src/components/post/pieces/EventLocation.tsx +27 -0
  64. package/src/components/post/pieces/EventRsvpButton.tsx +3 -0
  65. package/src/components/post/pieces/PollContent.tsx +8 -2
  66. package/src/components/post/post-form/ArticleForm.tsx +1 -1
  67. package/src/components/post/post-form/EventForm.tsx +43 -15
  68. package/src/components/post/post-form/ResourceForm.tsx +132 -4
  69. package/src/components/post/post-form/VisibilitySelector.tsx +11 -3
  70. package/src/components/post/types/Article.tsx +5 -5
  71. package/src/components/post/types/Event.tsx +68 -7
  72. package/src/components/post/types/resource/FullResource.tsx +9 -1
  73. package/src/components/preview-link/local/WelcomePreview.tsx +11 -2
  74. package/src/components/resources/ResourceCard.tsx +68 -10
  75. package/src/components/resources/ResourceLibrary.tsx +2 -2
  76. package/src/hooks/events/useEventRsvp.ts +19 -7
  77. package/src/hooks/posts/useResourceComposer.ts +157 -33
  78. package/src/hooks/resources/useResourceLibrary.ts +6 -2
  79. package/src/hooks/useWelcomePreview.ts +4 -0
  80. package/src/navigation/menuItems.ts +16 -0
  81. package/src/navigation/targets.ts +2 -0
  82. package/src/pages/admin/community/CommunityMarkdownPage.tsx +5 -1
  83. package/src/pages/auth/Register.tsx +12 -0
  84. package/src/pages/resources/Edit.tsx +3 -2
  85. package/src/pages/resources/New.tsx +6 -1
  86. package/src/pages/settings/Account.tsx +51 -1
@@ -41,6 +41,72 @@ export function FeedEvent({ post }: FeedEventProps) {
41
41
  allDay: event.wholeDay,
42
42
  })
43
43
  : fmtRange(start, end);
44
+ const viewEventHref = resolveStaticUrl(
45
+ `/posts/${post.id}`,
46
+ staticRender ? baseUrl : undefined,
47
+ );
48
+
49
+ if (staticRender) {
50
+ return (
51
+ <table
52
+ role="presentation"
53
+ width="100%"
54
+ cellPadding={0}
55
+ cellSpacing={0}
56
+ style={{ width: '100%', borderCollapse: 'collapse' }}
57
+ >
58
+ <tbody>
59
+ {event.image ? (
60
+ <tr>
61
+ <td style={{ paddingBottom: '8px' }}>
62
+ <img
63
+ src={event.image}
64
+ alt={`image for ${event.name ?? 'event'}`}
65
+ width="100%"
66
+ style={{
67
+ display: 'block',
68
+ width: '100%',
69
+ maxWidth: '100%',
70
+ height: 'auto',
71
+ }}
72
+ />
73
+ </td>
74
+ </tr>
75
+ ) : null}
76
+ <tr>
77
+ <td className="text-destructive pb-2 text-sm">{when}</td>
78
+ </tr>
79
+ <tr>
80
+ <td className="pb-2 text-lg font-medium">{event.name}</td>
81
+ </tr>
82
+ {event.recurrence ? (
83
+ <tr>
84
+ <td className="text-muted-foreground pb-2 text-sm">
85
+ {formatEventRecurrence(event.recurrence, t, event.start)}
86
+ </td>
87
+ </tr>
88
+ ) : null}
89
+ {event.physicalLocation || event.jam || event.url ? (
90
+ <tr>
91
+ <td className="pb-2">
92
+ <EventLocation post={post} />
93
+ </td>
94
+ </tr>
95
+ ) : null}
96
+ <tr>
97
+ <td>
98
+ <a
99
+ href={viewEventHref}
100
+ className="text-primary text-sm underline"
101
+ >
102
+ {t('posts.event.viewEvent', { defaultValue: 'View Event' })}
103
+ </a>
104
+ </td>
105
+ </tr>
106
+ </tbody>
107
+ </table>
108
+ );
109
+ }
44
110
 
45
111
  return (
46
112
  <div className="flex w-full flex-col gap-2">
@@ -56,15 +122,10 @@ export function FeedEvent({ post }: FeedEventProps) {
56
122
  <div className="flex w-full items-center justify-between">
57
123
  <span className="text-destructive text-sm">
58
124
  {when}{' '}
59
- {staticRender ? null : (
60
- <span className="text-muted-foreground">(your local time)</span>
61
- )}
125
+ <span className="text-muted-foreground">(your local time)</span>
62
126
  </span>
63
127
  <Button
64
- action={resolveStaticUrl(
65
- `/posts/${post.id}`,
66
- staticRender ? baseUrl : undefined,
67
- )}
128
+ action={viewEventHref}
68
129
  variant="outline"
69
130
  compact
70
131
  className="shrink-0"
@@ -97,6 +97,8 @@ export const FullResource = ({ post }: FullResourceProps) => {
97
97
  return (
98
98
  <ResourceCard
99
99
  post={post}
100
+ showDescription={false}
101
+ inGroup={!!post.group}
100
102
  preview={
101
103
  <VideoPlayer
102
104
  attachment={videoAtt}
@@ -116,6 +118,8 @@ export const FullResource = ({ post }: FullResourceProps) => {
116
118
  return (
117
119
  <ResourceCard
118
120
  post={post}
121
+ showDescription={false}
122
+ inGroup={!!post.group}
119
123
  preview={
120
124
  <AudioAttachment
121
125
  src={audioAtt.url}
@@ -131,7 +135,11 @@ export const FullResource = ({ post }: FullResourceProps) => {
131
135
  // All kinds: ResourceCard with click overlay
132
136
  return (
133
137
  <div className="relative">
134
- <ResourceCard post={post} />
138
+ <ResourceCard
139
+ post={post}
140
+ showDescription={false}
141
+ inGroup={!!post.group}
142
+ />
135
143
  <div className="pointer-events-none absolute left-0 right-0 top-0 flex h-48 items-center justify-center">
136
144
  <div
137
145
  className="pointer-events-auto flex size-20 cursor-pointer items-center justify-center rounded-full bg-black/60 text-white"
@@ -1,7 +1,9 @@
1
1
  import { useWelcomePreview } from '../../../hooks/useWelcomePreview';
2
+ import { truncateMarkdown, stripMarkdownImages } from '@openpeepshq/common/lib';
3
+ import { TruncatedMarkdown } from '../../post/TruncatedMarkdown';
2
4
 
3
5
  export const WelcomePreview = () => {
4
- const { eyebrow, title, description, imageUrl, imageFit } =
6
+ const { eyebrow, title, description, markdown, imageUrl, imageFit } =
5
7
  useWelcomePreview();
6
8
 
7
9
  return (
@@ -25,7 +27,14 @@ export const WelcomePreview = () => {
25
27
  <p className="text-muted-foreground truncate text-xs">{eyebrow}</p>
26
28
  ) : null}
27
29
  <p className="line-clamp-2 font-semibold">{title}</p>
28
- {description ? (
30
+ {markdown ? (
31
+ <TruncatedMarkdown
32
+ source={markdown}
33
+ snippet={truncateMarkdown(stripMarkdownImages(markdown), 2, 240)}
34
+ maxLines={2}
35
+ className="break-words text-sm text-muted-foreground"
36
+ />
37
+ ) : description ? (
29
38
  <p className="text-muted-foreground line-clamp-3 break-words text-sm">
30
39
  {description}
31
40
  </p>
@@ -13,19 +13,26 @@ import type {
13
13
  } from '@openpeepshq/common/types';
14
14
  import {
15
15
  attachmentMediaKind,
16
- normalizeResourceTags,
17
- truncateMarkdown,
18
16
  formatSize,
17
+ groupName,
18
+ normalizeResourceTags,
19
19
  } from '@openpeepshq/common/lib';
20
+ import { useHrefOf } from '../../contexts/router';
20
21
  import { useT } from '../../i18n';
21
22
  import { useOpenpeeps } from '../../contexts/openpeeps';
22
23
  import { useCurrentProfile } from '../layout/IdentityContext';
23
24
  import { VideoPlayOverlay } from '../post/VideoPlayOverlay';
25
+ import { TruncatedMarkdown } from '../post/TruncatedMarkdown';
26
+ import { useNavigate } from '../../contexts/router';
24
27
 
25
28
  export interface ResourceCardProps {
26
29
  post: PublicPost;
27
30
  layout?: 'card' | 'list';
28
31
  preview?: ReactNode;
32
+ /** Set false when the full description is rendered by the surrounding layout (e.g. the post detail page). */
33
+ showDescription?: boolean;
34
+ /** When true, hides the group label (already shown by the group context). */
35
+ inGroup?: boolean;
29
36
  }
30
37
 
31
38
  const visualAttachments = (attachments: MediaAttachmentData[]) =>
@@ -107,15 +114,18 @@ export const ResourceCard = ({
107
114
  post,
108
115
  layout = 'card',
109
116
  preview,
117
+ showDescription = true,
118
+ inGroup = false,
110
119
  }: ResourceCardProps) => {
111
120
  const t = useT();
121
+ const navigate = useNavigate();
122
+ const hrefOf = useHrefOf();
112
123
  const { openpeepsApi } = useOpenpeeps();
113
124
  const me = useCurrentProfile();
114
125
  if (post.data?.type !== 'resource') return null;
115
126
  const resource = post.data;
116
127
  const attachments = resource.attachments ?? [];
117
128
  const visuals = visualAttachments(attachments);
118
- const excerpt = truncateMarkdown(resource.content, 2, 240);
119
129
  const tags = normalizeResourceTags(resource.tags);
120
130
  const isGallery = resource.resourceKind === 'gallery' && visuals.length > 1;
121
131
  const tiles = isGallery ? visuals.slice(0, 4) : visuals.slice(0, 1);
@@ -124,6 +134,9 @@ export const ResourceCard = ({
124
134
  const thumbnail = visuals[0];
125
135
  const thumbSrc = thumbnail ? previewSrc(thumbnail) : null;
126
136
 
137
+ const group = post.group;
138
+ const groupLabel = group ? groupName(group) : null;
139
+
127
140
  const metaText = useMemo(() => {
128
141
  if (resource.resourceKind === 'gallery' && visuals.length > 1) {
129
142
  return t('resources.meta.imagesCount', {
@@ -185,6 +198,31 @@ export const ResourceCard = ({
185
198
  </button>
186
199
  );
187
200
 
201
+ const renderGroupLabel = () => {
202
+ if (!group || !groupLabel || inGroup) return null;
203
+ const groupHref = hrefOf({ type: 'group', handle: group.handle });
204
+ return (
205
+ <a
206
+ href={groupHref}
207
+ onClick={(e) => {
208
+ e.stopPropagation();
209
+ }}
210
+ className="text-muted-foreground hover:text-foreground inline-flex items-center gap-1 text-xs"
211
+ title={t('resources.group.inGroup', {
212
+ defaultValue: 'in {{groupName}}',
213
+ groupName: groupLabel,
214
+ })}
215
+ >
216
+ <span className="bg-muted rounded-full px-2 py-0.5 text-xs">
217
+ {t('resources.group.inGroup', {
218
+ defaultValue: 'in {{groupName}}',
219
+ groupName: groupLabel,
220
+ })}
221
+ </span>
222
+ </a>
223
+ );
224
+ };
225
+
188
226
  const renderThumbnail = () => {
189
227
  if (thumbSrc) {
190
228
  return (
@@ -219,8 +257,15 @@ export const ResourceCard = ({
219
257
  {resource.categoryPath.join(' / ')}
220
258
  </p>
221
259
  ) : null}
222
- {excerpt ? (
223
- <p className="text-muted-foreground text-sm">{excerpt}</p>
260
+ {renderGroupLabel()}
261
+ {showDescription ? (
262
+ <TruncatedMarkdown
263
+ source={resource.content ?? ''}
264
+ maxBlocks={2}
265
+ maxChars={240}
266
+ maxLines={2}
267
+ className="text-sm text-muted-foreground"
268
+ />
224
269
  ) : null}
225
270
  {metaText ? (
226
271
  <p className="text-muted-foreground text-xs">{metaText}</p>
@@ -306,8 +351,15 @@ export const ResourceCard = ({
306
351
  {resource.categoryPath.join(' / ')}
307
352
  </p>
308
353
  ) : null}
309
- {excerpt ? (
310
- <p className="text-muted-foreground text-sm">{excerpt}</p>
354
+ {renderGroupLabel()}
355
+ {showDescription ? (
356
+ <TruncatedMarkdown
357
+ source={resource.content ?? ''}
358
+ maxBlocks={2}
359
+ maxChars={240}
360
+ maxLines={2}
361
+ className="text-sm text-muted-foreground"
362
+ />
311
363
  ) : null}
312
364
  {metaText ? (
313
365
  <p className="text-muted-foreground text-xs">{metaText}</p>
@@ -315,12 +367,18 @@ export const ResourceCard = ({
315
367
  {tags.length ? (
316
368
  <div className="flex flex-wrap gap-1">
317
369
  {tags.map((value) => (
318
- <span
370
+ <button
319
371
  key={value}
320
- className="bg-muted text-muted-foreground rounded-full px-2 py-0.5 text-xs"
372
+ type="button"
373
+ className="bg-muted text-muted-foreground hover:text-foreground rounded-full px-2 py-0.5 text-xs"
374
+ onClick={(e) => {
375
+ e.preventDefault();
376
+ e.stopPropagation();
377
+ navigate({ type: 'tags', hashtag: value });
378
+ }}
321
379
  >
322
380
  #{value}
323
- </span>
381
+ </button>
324
382
  ))}
325
383
  </div>
326
384
  ) : null}
@@ -352,7 +352,7 @@ export const ResourceLibrary = ({ groupId }: ResourceLibraryProps) => {
352
352
  href={hrefOf({ type: 'post', id: post.id })}
353
353
  className="block"
354
354
  >
355
- <ResourceCard post={post} layout="list" />
355
+ <ResourceCard post={post} layout="list" inGroup={!!groupId} />
356
356
  </a>
357
357
  ))}
358
358
  </div>
@@ -364,7 +364,7 @@ export const ResourceLibrary = ({ groupId }: ResourceLibraryProps) => {
364
364
  href={hrefOf({ type: 'post', id: post.id })}
365
365
  className="block"
366
366
  >
367
- <ResourceCard post={post} layout="card" />
367
+ <ResourceCard post={post} layout="card" inGroup={!!groupId} />
368
368
  </a>
369
369
  ))}
370
370
  </div>
@@ -6,10 +6,12 @@ import {
6
6
  checkPostCapabilities,
7
7
  defaultRsvpRecurrenceId,
8
8
  displayRsvpForProfile,
9
+ effectiveEventTimes,
9
10
  eventRsvpOpen,
10
11
  isCapacityEvent,
11
12
  isRecurringEvent,
12
13
  listRsvpOccurrences,
14
+ occurrenceHasEnded,
13
15
  offerStillOpen,
14
16
  seriesHasClaimableOccurrence,
15
17
  } from '@openpeepshq/common/lib';
@@ -58,13 +60,21 @@ export const useEventRsvp = ({
58
60
 
59
61
  const eventData = post.data?.type === 'event' ? post.data : undefined;
60
62
  const recurring = eventData ? isRecurringEvent(eventData) : false;
63
+ const viewedTimes = eventData
64
+ ? effectiveEventTimes(eventData, recurrenceId)
65
+ : undefined;
66
+ // Series home shows its first start. Once that time has passed, RSVP stays
67
+ // off that page; upcoming dates are opened from their own links.
68
+ const viewedEnded =
69
+ !!viewedTimes && occurrenceHasEnded(viewedTimes.start, viewedTimes.end);
70
+ // A selected date, or a one-off, is closed on its own end time.
61
71
  const rsvpClosed = eventData
62
- ? !eventRsvpOpen(eventData, { recurrenceId, lockToOccurrence })
72
+ ? !eventRsvpOpen(
73
+ eventData,
74
+ recurrenceId ? { recurrenceId, lockToOccurrence: true } : undefined,
75
+ )
63
76
  : false;
64
- const queriedOccurrenceClosed =
65
- !!eventData &&
66
- !!recurrenceId &&
67
- !eventRsvpOpen(eventData, { recurrenceId, lockToOccurrence: true });
77
+ const queriedOccurrenceClosed = !!recurrenceId && rsvpClosed;
68
78
  const defaultId = eventData
69
79
  ? defaultRsvpRecurrenceId(
70
80
  eventData,
@@ -131,7 +141,7 @@ export const useEventRsvp = ({
131
141
  };
132
142
 
133
143
  const requestJoinWaitlist = () => {
134
- if (rsvpClosed) return;
144
+ if (rsvpClosed || viewedEnded) return;
135
145
  if (!recurring || lockToOccurrence) {
136
146
  setError(null);
137
147
  setConfirmingWaitlist(true);
@@ -152,6 +162,7 @@ export const useEventRsvp = ({
152
162
  };
153
163
 
154
164
  const claimSpot = () => {
165
+ if (rsvpClosed || viewedEnded) return;
155
166
  const id = recurring
156
167
  ? (myRsvp?.recurrenceId ?? (lockToOccurrence ? recurrenceId : defaultId))
157
168
  : undefined;
@@ -159,7 +170,7 @@ export const useEventRsvp = ({
159
170
  };
160
171
 
161
172
  const requestRespond = (response: RsvpChoice) => {
162
- if (rsvpClosed) return;
173
+ if (rsvpClosed || viewedEnded) return;
163
174
  if (!recurring || lockToOccurrence) {
164
175
  writeRsvp(response, recurrenceId ? [recurrenceId] : undefined).catch(
165
176
  () => undefined,
@@ -193,6 +204,7 @@ export const useEventRsvp = ({
193
204
  myRsvp,
194
205
  canRsvp,
195
206
  rsvpClosed,
207
+ hideRsvpButton: rsvpClosed || viewedEnded,
196
208
  recurring,
197
209
  defaultId,
198
210
  occurrences,
@@ -1,17 +1,26 @@
1
- import { useCallback, useEffect, useState } from 'react';
1
+ import { useCallback, useEffect, useMemo, useState } from 'react';
2
2
  import type {
3
+ GroupWithMeta,
3
4
  PostCreationData,
4
5
  PostDataUnion,
5
6
  ResourcePost,
6
7
  } from '@openpeepshq/common/types';
7
8
  import {
9
+ checkGroupCapabilities,
8
10
  normalizeResourceTags,
9
- resourceHasRequiredMedia,
11
+ resourceFormIssues,
12
+ resourceVisibilityForCapabilities,
13
+ type ResourceFormIssue,
10
14
  } from '@openpeepshq/common/lib';
11
15
  import { useOpenpeeps } from '../../contexts/openpeeps';
12
16
  import { useNavigate } from '../../contexts/router';
13
17
  import { useT } from '../../i18n/context';
18
+ import {
19
+ useAuthData,
20
+ useCurrentProfile,
21
+ } from '../../components/layout/IdentityContext';
14
22
  import { useServerInfo } from '../../components/server-data';
23
+ import { buildAudienceChoiceValues } from '../../lib/audienceChoices';
15
24
  import { defaultNewResource, getNewPostStores } from '../../stores';
16
25
 
17
26
  const asResource = (data: PostDataUnion | undefined): ResourcePost | null =>
@@ -24,13 +33,79 @@ const preparedResource = (resource: ResourcePost): ResourcePost => ({
24
33
  categoryPath: (resource.categoryPath ?? [])
25
34
  .map((segment) => segment.trim())
26
35
  .filter(Boolean),
27
- url: resource.url?.trim() || undefined,
36
+ url:
37
+ resource.resourceKind === 'link'
38
+ ? resource.url?.trim() || undefined
39
+ : undefined,
28
40
  });
29
41
 
42
+ const issuesFor = (postData: PostCreationData): ResourceFormIssue[] => {
43
+ const resource = asResource(postData.data);
44
+ if (!resource) return ['title'];
45
+ return resourceFormIssues({
46
+ title: resource.title,
47
+ resourceKind: resource.resourceKind,
48
+ url: resource.url,
49
+ attachments: resource.attachments,
50
+ visibility: postData.visibility,
51
+ audience: postData.audience,
52
+ groupId: postData.groupId,
53
+ });
54
+ };
55
+
56
+ const issueMessage = (
57
+ issue: ResourceFormIssue,
58
+ t: (key: string, options: { defaultValue: string }) => string,
59
+ ): string => {
60
+ if (issue === 'title') {
61
+ return t('resources.validation.titleRequired', {
62
+ defaultValue: 'Title is required',
63
+ });
64
+ }
65
+ if (issue === 'titleLength') {
66
+ return t('resources.validation.titleTooLong', {
67
+ defaultValue: 'Title must be 200 characters or fewer',
68
+ });
69
+ }
70
+ if (issue === 'url') {
71
+ return t('resources.validation.url', {
72
+ defaultValue: 'Enter a real link, like https://example.com',
73
+ });
74
+ }
75
+ if (issue === 'media') {
76
+ return t('resources.validation.mediaRequired', {
77
+ defaultValue: 'Add the media or link this resource needs',
78
+ });
79
+ }
80
+ if (issue === 'group') {
81
+ return t('resources.validation.groupRequired', {
82
+ defaultValue: 'Choose a group before creating this resource.',
83
+ });
84
+ }
85
+ return t('resources.validation.directAudience', {
86
+ defaultValue: 'Choose at least one recipient for a direct resource.',
87
+ });
88
+ };
89
+
90
+ const submitErrorMessage = (
91
+ err: unknown,
92
+ t: (key: string, options: { defaultValue: string }) => string,
93
+ ) => {
94
+ const message = err instanceof Error ? err.message : '';
95
+ if (message === 'Forbidden') {
96
+ return t('resources.validation.audienceForbidden', {
97
+ defaultValue: 'Choose a group you can post this resource in.',
98
+ });
99
+ }
100
+ return message;
101
+ };
102
+
30
103
  export const useNewResource = () => {
31
104
  const t = useT();
32
105
  const navigate = useNavigate();
33
106
  const serverInfo = useServerInfo();
107
+ const authData = useAuthData();
108
+ const me = useCurrentProfile();
34
109
  const { openpeepsApi } = useOpenpeeps();
35
110
  const createPost = openpeepsApi.createPostAction();
36
111
  const stores = getNewPostStores();
@@ -45,12 +120,70 @@ export const useNewResource = () => {
45
120
  const [submitting, setSubmitting] = useState(false);
46
121
  const [error, setError] = useState<string | null>(null);
47
122
 
123
+ const audience = useMemo(() => {
124
+ if (!authData.profile || authData.scopes.length === 0) return null;
125
+ const choices = buildAudienceChoiceValues('resource', authData, t, {
126
+ publicContent: serverInfo.publicContent,
127
+ showDirect: true,
128
+ });
129
+ const memberships = me?.memberships;
130
+ const postableGroupIds =
131
+ memberships == null
132
+ ? null
133
+ : memberships
134
+ .map((membership) => membership.group as GroupWithMeta)
135
+ .filter(
136
+ (group) =>
137
+ checkGroupCapabilities(
138
+ authData,
139
+ ['core-posts-create-resource'],
140
+ group,
141
+ ).success,
142
+ )
143
+ .map((group) => group.id);
144
+ const next = resourceVisibilityForCapabilities({
145
+ visibility: postData.visibility,
146
+ groupId: postData.groupId,
147
+ allowed: choices.map((choice) => choice.value),
148
+ postableGroupIds,
149
+ });
150
+ return {
151
+ ...next,
152
+ groupOnly: choices.length === 1 && choices[0]?.value === 'group',
153
+ hasChoice: choices.length > 0,
154
+ };
155
+ }, [
156
+ authData,
157
+ me?.memberships,
158
+ postData.groupId,
159
+ postData.visibility,
160
+ serverInfo.publicContent,
161
+ t,
162
+ ]);
163
+
164
+ useEffect(() => {
165
+ if (!audience?.hasChoice) return;
166
+ if (
167
+ audience.visibility === postData.visibility &&
168
+ (audience.groupId ?? undefined) === (postData.groupId ?? undefined)
169
+ ) {
170
+ return;
171
+ }
172
+ const next = {
173
+ ...postData,
174
+ visibility: audience.visibility,
175
+ groupId: audience.groupId,
176
+ };
177
+ setPostDataState(next);
178
+ stores.resource = next;
179
+ }, [audience, postData, stores]);
180
+
48
181
  const resource = asResource(postData.data);
49
- const canSubmit =
50
- !!resource?.title.trim() &&
51
- !!resource &&
52
- resourceHasRequiredMedia(resource) &&
53
- !(postData.visibility === 'direct' && !postData.audience?.length);
182
+ const audienceReady =
183
+ !!audience?.hasChoice &&
184
+ audience.visibility === postData.visibility &&
185
+ (audience.groupId ?? undefined) === (postData.groupId ?? undefined);
186
+ const canSubmit = audienceReady && issuesFor(postData).length === 0;
54
187
 
55
188
  const setPostData = (data: PostCreationData) => {
56
189
  setPostDataState(data);
@@ -59,28 +192,10 @@ export const useNewResource = () => {
59
192
 
60
193
  const submit = useCallback(async () => {
61
194
  setError(null);
62
- if (!resource?.title.trim()) {
63
- setError(
64
- t('resources.validation.titleRequired', {
65
- defaultValue: 'Title is required',
66
- }),
67
- );
68
- return;
69
- }
70
- if (!resourceHasRequiredMedia(resource)) {
71
- setError(
72
- t('resources.validation.mediaRequired', {
73
- defaultValue: 'Add the media or link this resource needs',
74
- }),
75
- );
76
- return;
77
- }
78
- if (postData.visibility === 'direct' && !postData.audience?.length) {
79
- setError(
80
- t('resources.validation.directAudience', {
81
- defaultValue: 'Choose at least one recipient for a direct resource.',
82
- }),
83
- );
195
+ if (!audienceReady) return;
196
+ const issue = issuesFor(postData)[0];
197
+ if (!resource || issue) {
198
+ if (issue) setError(issueMessage(issue, t));
84
199
  return;
85
200
  }
86
201
  setSubmitting(true);
@@ -93,15 +208,16 @@ export const useNewResource = () => {
93
208
  stores.resetNewResourceState();
94
209
  navigate({ type: 'post', id: created.id });
95
210
  } catch (err) {
96
- setError((err as Error).message);
211
+ setError(submitErrorMessage(err, t));
97
212
  } finally {
98
213
  setSubmitting(false);
99
214
  }
100
- }, [createPost, navigate, postData, resource, stores, t]);
215
+ }, [audienceReady, createPost, navigate, postData, resource, stores, t]);
101
216
 
102
217
  return {
103
218
  postData,
104
219
  setPostData,
220
+ groupOnly: audience?.groupOnly ?? false,
105
221
  canSubmit,
106
222
  submitting,
107
223
  error,
@@ -111,6 +227,7 @@ export const useNewResource = () => {
111
227
  };
112
228
 
113
229
  export const useEditResource = (resourceId: string) => {
230
+ const t = useT();
114
231
  const navigate = useNavigate();
115
232
  const { openpeepsApi } = useOpenpeeps();
116
233
  const postQuery = openpeepsApi.usePost(resourceId);
@@ -137,12 +254,18 @@ export const useEditResource = (resourceId: string) => {
137
254
  }, [postQuery.data]);
138
255
 
139
256
  const resourceTitle = asResource(postQuery.data?.data)?.title;
257
+ const canSubmit = !!postData && issuesFor(postData).length === 0;
140
258
 
141
259
  const submit = useCallback(async () => {
142
260
  if (!postData) return;
143
261
  const resource = asResource(postData.data);
144
262
  if (!resource) return;
145
263
  setError(null);
264
+ const issue = issuesFor(postData)[0];
265
+ if (issue) {
266
+ setError(issueMessage(issue, t));
267
+ return;
268
+ }
146
269
  setSubmitting(true);
147
270
  try {
148
271
  await updatePost(preparedResource(resource));
@@ -152,13 +275,14 @@ export const useEditResource = (resourceId: string) => {
152
275
  } finally {
153
276
  setSubmitting(false);
154
277
  }
155
- }, [navigate, postData, resourceId, updatePost]);
278
+ }, [navigate, postData, resourceId, t, updatePost]);
156
279
 
157
280
  return {
158
281
  postQuery,
159
282
  postData,
160
283
  setPostData,
161
284
  resourceTitle,
285
+ canSubmit,
162
286
  submitting,
163
287
  error,
164
288
  clearError: () => setError(null),
@@ -8,12 +8,16 @@ import {
8
8
  import { useOpenpeeps } from '../../contexts/openpeeps';
9
9
  import { useFeedListParams } from '../useResolvedFeedFormat';
10
10
 
11
- export const useResourceLibrary = (groupId?: string) => {
11
+ export const useResourceLibrary = (groupId?: string, ungrouped?: boolean) => {
12
12
  const { openpeepsApi } = useOpenpeeps();
13
13
  const params = useFeedListParams({ format: 'linear' });
14
14
  const query = openpeepsApi.usePostsByType('resource', {
15
15
  ...params,
16
- ...(groupId ? { groupId } : { ungrouped: 'true' as const }),
16
+ ...(groupId
17
+ ? { groupId }
18
+ : ungrouped
19
+ ? { ungrouped: 'true' as const }
20
+ : {}),
17
21
  });
18
22
  const bookmarkedIds = openpeepsApi.useCurrentProfileBookmarkedIds();
19
23