@openpeepshq/react 0.5.1 → 0.6.1
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/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/components/markdown/CompiledMarkdown.d.ts +3 -1
- package/dist/components/markdown/CompiledMarkdown.js +17 -1
- package/dist/components/markdown/OpenpeepsMarkdown.d.ts +3 -7
- package/dist/components/markdown/OpenpeepsMarkdown.js +11 -7
- package/dist/components/markdown/classes.d.ts +3 -3
- package/dist/components/markdown/classes.js +3 -3
- package/dist/components/markdown/compileMarkdown.d.ts +3 -1
- package/dist/components/markdown/compileMarkdown.js +29 -4
- package/dist/components/markdown/index.d.ts +3 -1
- package/dist/components/markdown/linkTargets.d.ts +2 -3
- package/dist/components/markdown/linkTargets.js +2 -27
- package/dist/components/post/CompactReplyParent.js +8 -0
- package/dist/components/post/FeedPostContent.js +3 -0
- package/dist/components/post/NewResourceButton.d.ts +8 -0
- package/dist/components/post/NewResourceButton.js +36 -0
- package/dist/components/post/PostDetail.js +3 -0
- package/dist/components/post/index.d.ts +5 -0
- package/dist/components/post/index.js +3 -0
- package/dist/components/post/pieces/PostMenu.js +4 -0
- package/dist/components/post/post-form/PostTypeSwitcher.js +12 -2
- package/dist/components/post/post-form/ResourceForm.d.ts +7 -0
- package/dist/components/post/post-form/ResourceForm.js +68 -0
- package/dist/components/post/types/resource/FullResource.d.ts +5 -0
- package/dist/components/post/types/resource/FullResource.js +99 -0
- package/dist/components/resources/CategoryPathPicker.d.ts +7 -0
- package/dist/components/resources/CategoryPathPicker.js +26 -0
- package/dist/components/resources/ResourceCard.d.ts +8 -0
- package/dist/components/resources/ResourceCard.js +121 -0
- package/dist/components/resources/ResourceLibrary.d.ts +4 -0
- package/dist/components/resources/ResourceLibrary.js +106 -0
- package/dist/components/resources/index.d.ts +6 -0
- package/dist/components/resources/index.js +3 -0
- package/dist/contexts/openpeeps/hooks/admin.d.ts +1064 -38
- package/dist/contexts/openpeeps/hooks/jams.d.ts +1064 -38
- package/dist/contexts/openpeeps/hooks/posts.d.ts +4508 -39936
- package/dist/hooks/events/useEventComposer.d.ts +1184 -44
- package/dist/hooks/events/useEventsCalendar.d.ts +2113 -61
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/jams/useCreateNewJamForm.d.ts +39 -1
- package/dist/hooks/jams/useJamDetails.d.ts +1064 -38
- package/dist/hooks/jams/useJamRoom.d.ts +2113 -61
- package/dist/hooks/posts/useArticleComposer.d.ts +1184 -44
- package/dist/hooks/posts/useFeedPostPresentation.d.ts +2113 -61
- package/dist/hooks/posts/usePostDetailTitle.d.ts +1064 -38
- package/dist/hooks/posts/useResourceComposer.d.ts +12019 -0
- package/dist/hooks/posts/useResourceComposer.js +139 -0
- package/dist/hooks/resources/useResourceLibrary.d.ts +21880 -0
- package/dist/hooks/resources/useResourceLibrary.js +130 -0
- package/dist/hooks/settings/useNotificationPreferences.d.ts +1 -1
- package/dist/lib/analyticsClicks.js +1 -1
- package/dist/navigation/menuItems.js +5 -0
- package/dist/navigation/targets.d.ts +4 -0
- package/dist/pages/admin/analytics/Content.js +2 -1
- package/dist/pages/admin/analytics/Overview.js +15 -3
- package/dist/pages/groups/Show.js +7 -3
- package/dist/pages/index.d.ts +3 -0
- package/dist/pages/index.js +3 -0
- package/dist/pages/resources/Edit.d.ts +1 -0
- package/dist/pages/resources/Edit.js +26 -0
- package/dist/pages/resources/Index.d.ts +1 -0
- package/dist/pages/resources/Index.js +29 -0
- package/dist/pages/resources/New.d.ts +1 -0
- package/dist/pages/resources/New.js +15 -0
- package/dist/react-native.d.ts +3 -1
- package/dist/react-native.js +2 -1
- package/dist/stores/index.d.ts +1 -1
- package/dist/stores/index.js +1 -1
- package/dist/stores/newPosts.d.ts +79 -2
- package/dist/stores/newPosts.js +26 -2
- package/dist/stores/types.d.ts +2 -0
- package/package.json +7 -6
- package/src/components/index.ts +1 -0
- package/src/components/markdown/CompiledMarkdown.tsx +36 -7
- package/src/components/markdown/OpenpeepsMarkdown.tsx +16 -16
- package/src/components/markdown/classes.ts +3 -3
- package/src/components/markdown/compileMarkdown.ts +45 -5
- package/src/components/markdown/index.ts +6 -1
- package/src/components/markdown/linkTargets.ts +2 -31
- package/src/components/post/CompactReplyParent.tsx +8 -0
- package/src/components/post/FeedPostContent.tsx +3 -0
- package/src/components/post/NewResourceButton.tsx +75 -0
- package/src/components/post/PostDetail.tsx +3 -0
- package/src/components/post/index.ts +5 -0
- package/src/components/post/pieces/PostMenu.tsx +4 -0
- package/src/components/post/post-form/PostTypeSwitcher.tsx +19 -1
- package/src/components/post/post-form/ResourceForm.tsx +237 -0
- package/src/components/post/types/resource/FullResource.tsx +173 -0
- package/src/components/resources/CategoryPathPicker.tsx +95 -0
- package/src/components/resources/ResourceCard.tsx +335 -0
- package/src/components/resources/ResourceLibrary.tsx +374 -0
- package/src/components/resources/index.ts +6 -0
- package/src/contexts/openpeeps/hooks/posts.ts +11 -2
- package/src/hooks/index.ts +2 -0
- package/src/hooks/posts/useResourceComposer.ts +165 -0
- package/src/hooks/resources/useResourceLibrary.ts +156 -0
- package/src/lib/analyticsClicks.ts +1 -1
- package/src/navigation/menuItems.ts +6 -0
- package/src/navigation/targets.ts +1 -0
- package/src/pages/admin/analytics/Content.tsx +2 -1
- package/src/pages/admin/analytics/Overview.tsx +15 -3
- package/src/pages/groups/Show.tsx +30 -1
- package/src/pages/index.ts +3 -0
- package/src/pages/resources/Edit.tsx +73 -0
- package/src/pages/resources/Index.tsx +45 -0
- package/src/pages/resources/New.tsx +53 -0
- package/src/react-native.ts +12 -0
- package/src/stores/index.ts +1 -0
- package/src/stores/newPosts.ts +33 -2
- package/src/stores/types.ts +2 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { useMemo, useState } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
BookmarkCheck,
|
|
5
|
+
BookmarkPlus,
|
|
6
|
+
Link as LinkIcon,
|
|
7
|
+
ImageOff,
|
|
8
|
+
} from 'lucide-react';
|
|
9
|
+
import type {
|
|
10
|
+
MediaAttachmentData,
|
|
11
|
+
PublicPost,
|
|
12
|
+
ResourceKind,
|
|
13
|
+
} from '@openpeepshq/common/types';
|
|
14
|
+
import {
|
|
15
|
+
attachmentMediaKind,
|
|
16
|
+
normalizeResourceTags,
|
|
17
|
+
truncateMarkdown,
|
|
18
|
+
formatSize,
|
|
19
|
+
} from '@openpeepshq/common/lib';
|
|
20
|
+
import { useT } from '../../i18n';
|
|
21
|
+
import { useOpenpeeps } from '../../contexts/openpeeps';
|
|
22
|
+
import { useCurrentProfile } from '../layout/IdentityContext';
|
|
23
|
+
import { VideoPlayOverlay } from '../post/VideoPlayOverlay';
|
|
24
|
+
|
|
25
|
+
export interface ResourceCardProps {
|
|
26
|
+
post: PublicPost;
|
|
27
|
+
layout?: 'card' | 'list';
|
|
28
|
+
preview?: ReactNode;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const visualAttachments = (attachments: MediaAttachmentData[]) =>
|
|
32
|
+
attachments.filter((att) => attachmentMediaKind(att) !== 'audio');
|
|
33
|
+
|
|
34
|
+
const previewSrc = (att: MediaAttachmentData) => att.previewUrl ?? att.url;
|
|
35
|
+
|
|
36
|
+
/** Thumbnail size shared between list and card previews. */
|
|
37
|
+
const THUMB_CLASS = 'h-12 w-12 object-cover';
|
|
38
|
+
const CARD_IMG_CLASS = 'h-48 w-full object-cover';
|
|
39
|
+
|
|
40
|
+
const primaryActionLabel = (kind: ResourceKind, t: ReturnType<typeof useT>) => {
|
|
41
|
+
switch (kind) {
|
|
42
|
+
case 'video':
|
|
43
|
+
return t('resources.actions.watch', { defaultValue: 'Watch' });
|
|
44
|
+
case 'file':
|
|
45
|
+
return t('resources.actions.download', { defaultValue: 'Download' });
|
|
46
|
+
case 'audio':
|
|
47
|
+
return t('resources.actions.listen', { defaultValue: 'Listen' });
|
|
48
|
+
case 'gallery':
|
|
49
|
+
return t('resources.actions.view', { defaultValue: 'View images' });
|
|
50
|
+
default:
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const MediaPreview = ({
|
|
56
|
+
src,
|
|
57
|
+
alt,
|
|
58
|
+
className,
|
|
59
|
+
isVideo,
|
|
60
|
+
isLast,
|
|
61
|
+
extra,
|
|
62
|
+
}: {
|
|
63
|
+
src: string | null;
|
|
64
|
+
alt: string;
|
|
65
|
+
className: string;
|
|
66
|
+
isVideo: boolean;
|
|
67
|
+
isLast: boolean;
|
|
68
|
+
extra: number;
|
|
69
|
+
}) => {
|
|
70
|
+
const [errored, setErrored] = useState(false);
|
|
71
|
+
|
|
72
|
+
if (!src) {
|
|
73
|
+
return (
|
|
74
|
+
<div className="bg-surface flex h-full w-full items-center justify-center overflow-hidden">
|
|
75
|
+
<ImageOff className="text-muted-foreground/50 size-6" />
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (errored) {
|
|
81
|
+
return (
|
|
82
|
+
<div className="bg-surface flex h-full w-full items-center justify-center overflow-hidden">
|
|
83
|
+
<ImageOff className="text-muted-foreground/50 size-6" />
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<>
|
|
90
|
+
{/* eslint-disable-next-line jsx-a11y/img-has-alt -- handled by alt attribute */}
|
|
91
|
+
<img
|
|
92
|
+
src={src}
|
|
93
|
+
alt={alt}
|
|
94
|
+
className={className}
|
|
95
|
+
onError={() => setErrored(true)}
|
|
96
|
+
/>
|
|
97
|
+
{isVideo ? <VideoPlayOverlay video /> : null}
|
|
98
|
+
{isLast && extra > 0 ? (
|
|
99
|
+
<div className="absolute inset-0 flex items-center justify-center bg-black/60 text-lg font-semibold text-white">
|
|
100
|
+
+{extra}
|
|
101
|
+
</div>
|
|
102
|
+
) : null}
|
|
103
|
+
</>
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const ResourceCard = ({
|
|
108
|
+
post,
|
|
109
|
+
layout = 'card',
|
|
110
|
+
preview,
|
|
111
|
+
}: ResourceCardProps) => {
|
|
112
|
+
const t = useT();
|
|
113
|
+
const { openpeepsApi } = useOpenpeeps();
|
|
114
|
+
const me = useCurrentProfile();
|
|
115
|
+
if (post.data?.type !== 'resource') return null;
|
|
116
|
+
const resource = post.data;
|
|
117
|
+
const attachments = resource.attachments ?? [];
|
|
118
|
+
const visuals = visualAttachments(attachments);
|
|
119
|
+
const excerpt = truncateMarkdown(resource.content, 2, 240);
|
|
120
|
+
const tags = normalizeResourceTags(resource.tags);
|
|
121
|
+
const isGallery = resource.resourceKind === 'gallery' && visuals.length > 1;
|
|
122
|
+
const tiles = isGallery ? visuals.slice(0, 4) : visuals.slice(0, 1);
|
|
123
|
+
const extra = isGallery ? Math.max(0, visuals.length - 4) : 0;
|
|
124
|
+
const actionLabel = primaryActionLabel(resource.resourceKind, t);
|
|
125
|
+
const thumbnail = visuals[0];
|
|
126
|
+
const thumbSrc = thumbnail ? previewSrc(thumbnail) : null;
|
|
127
|
+
|
|
128
|
+
const metaText = useMemo(() => {
|
|
129
|
+
if (resource.resourceKind === 'gallery' && visuals.length > 1) {
|
|
130
|
+
return t('resources.meta.imagesCount', {
|
|
131
|
+
defaultValue: '{{count}} images',
|
|
132
|
+
count: visuals.length,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const fileAtt = attachments.find(
|
|
136
|
+
(att) =>
|
|
137
|
+
attachmentMediaKind(att) === 'file' ||
|
|
138
|
+
attachmentMediaKind(att) === 'video' ||
|
|
139
|
+
attachmentMediaKind(att) === 'audio',
|
|
140
|
+
);
|
|
141
|
+
const size = fileAtt?.meta?.size;
|
|
142
|
+
if (size) return formatSize(size);
|
|
143
|
+
return null;
|
|
144
|
+
}, [resource.resourceKind, visuals, attachments, t]);
|
|
145
|
+
|
|
146
|
+
const bookmarkedIdsQuery = openpeepsApi.useCurrentProfileBookmarkedIds();
|
|
147
|
+
const isBookmarked = bookmarkedIdsQuery.data?.includes(post.id) ?? false;
|
|
148
|
+
const bookmarkPost = openpeepsApi.bookmarkPostAction({ id: post.id });
|
|
149
|
+
const unbookmarkPost = openpeepsApi.unbookmarkPostAction({ id: post.id });
|
|
150
|
+
|
|
151
|
+
const toggleBookmark = () => {
|
|
152
|
+
if (!me) return;
|
|
153
|
+
if (isBookmarked) {
|
|
154
|
+
unbookmarkPost(undefined);
|
|
155
|
+
} else {
|
|
156
|
+
bookmarkPost(undefined);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const downloadUrl =
|
|
161
|
+
resource.resourceKind === 'file'
|
|
162
|
+
? attachments.find((att) => attachmentMediaKind(att) === 'file')?.url
|
|
163
|
+
: null;
|
|
164
|
+
|
|
165
|
+
const renderBookmarkButton = (className: string) => (
|
|
166
|
+
<button
|
|
167
|
+
type="button"
|
|
168
|
+
onClick={(e) => {
|
|
169
|
+
e.preventDefault();
|
|
170
|
+
e.stopPropagation();
|
|
171
|
+
toggleBookmark();
|
|
172
|
+
}}
|
|
173
|
+
disabled={!me}
|
|
174
|
+
className={className}
|
|
175
|
+
title={
|
|
176
|
+
isBookmarked
|
|
177
|
+
? t('resources.actions.saved', { defaultValue: 'Saved' })
|
|
178
|
+
: t('resources.actions.save', { defaultValue: 'Save' })
|
|
179
|
+
}
|
|
180
|
+
>
|
|
181
|
+
{isBookmarked ? (
|
|
182
|
+
<BookmarkCheck className="text-primary size-4" />
|
|
183
|
+
) : (
|
|
184
|
+
<BookmarkPlus className="size-4" />
|
|
185
|
+
)}
|
|
186
|
+
</button>
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const renderThumbnail = () => {
|
|
190
|
+
if (thumbSrc) {
|
|
191
|
+
return (
|
|
192
|
+
<img
|
|
193
|
+
src={thumbSrc}
|
|
194
|
+
alt={thumbnail?.description ?? resource.title}
|
|
195
|
+
className={THUMB_CLASS}
|
|
196
|
+
onError={(e) => {
|
|
197
|
+
(e.target as HTMLImageElement).style.display = 'none';
|
|
198
|
+
}}
|
|
199
|
+
/>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (resource.resourceKind === 'link' && resource.url) {
|
|
203
|
+
return (
|
|
204
|
+
<div className="bg-muted flex h-12 w-12 items-center justify-center rounded-md">
|
|
205
|
+
<LinkIcon className="text-muted-foreground size-5" />
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
if (layout === 'list') {
|
|
213
|
+
return (
|
|
214
|
+
<div className="flex w-full min-w-0 items-center gap-3 px-4 py-3">
|
|
215
|
+
{renderThumbnail()}
|
|
216
|
+
<div className="min-w-0 flex-1">
|
|
217
|
+
<h3 className="text-lg font-semibold">{resource.title}</h3>
|
|
218
|
+
{resource.categoryPath?.length ? (
|
|
219
|
+
<p className="text-muted-foreground text-xs">
|
|
220
|
+
{resource.categoryPath.join(' / ')}
|
|
221
|
+
</p>
|
|
222
|
+
) : null}
|
|
223
|
+
{excerpt ? (
|
|
224
|
+
<p className="text-muted-foreground text-sm">{excerpt}</p>
|
|
225
|
+
) : null}
|
|
226
|
+
{metaText ? (
|
|
227
|
+
<p className="text-muted-foreground text-xs">{metaText}</p>
|
|
228
|
+
) : null}
|
|
229
|
+
</div>
|
|
230
|
+
<div className="flex items-center gap-2">
|
|
231
|
+
{resource.resourceKind === 'link' &&
|
|
232
|
+
resource.url ? null : actionLabel ? (
|
|
233
|
+
resource.resourceKind === 'file' && downloadUrl ? (
|
|
234
|
+
<a
|
|
235
|
+
href={downloadUrl}
|
|
236
|
+
download
|
|
237
|
+
onClick={(e) => e.stopPropagation()}
|
|
238
|
+
className="text-primary text-xs font-medium"
|
|
239
|
+
>
|
|
240
|
+
{actionLabel}
|
|
241
|
+
</a>
|
|
242
|
+
) : (
|
|
243
|
+
<span className="text-muted-foreground text-xs">
|
|
244
|
+
{actionLabel}
|
|
245
|
+
</span>
|
|
246
|
+
)
|
|
247
|
+
) : null}
|
|
248
|
+
{renderBookmarkButton(
|
|
249
|
+
'text-muted-foreground hover:text-foreground disabled:opacity-50',
|
|
250
|
+
)}
|
|
251
|
+
</div>
|
|
252
|
+
</div>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<div className="flex w-full min-w-0 flex-col gap-2">
|
|
258
|
+
<div className="relative">
|
|
259
|
+
{preview ? (
|
|
260
|
+
preview
|
|
261
|
+
) : tiles.length ? (
|
|
262
|
+
<div
|
|
263
|
+
className={`overflow-hidden rounded-md ${
|
|
264
|
+
tiles.length > 1
|
|
265
|
+
? 'grid h-48 grid-cols-2 grid-rows-2 gap-1'
|
|
266
|
+
: 'h-48 w-full'
|
|
267
|
+
}`}
|
|
268
|
+
>
|
|
269
|
+
{tiles.map((att, index) => {
|
|
270
|
+
const src = previewSrc(att);
|
|
271
|
+
const last = index === tiles.length - 1 && extra > 0;
|
|
272
|
+
const kind = attachmentMediaKind(att);
|
|
273
|
+
return (
|
|
274
|
+
<div
|
|
275
|
+
key={`${att.url ?? index}-${index}`}
|
|
276
|
+
className="bg-surface relative h-full w-full overflow-hidden"
|
|
277
|
+
>
|
|
278
|
+
<MediaPreview
|
|
279
|
+
src={src}
|
|
280
|
+
alt={att.description ?? resource.title}
|
|
281
|
+
className={
|
|
282
|
+
tiles.length > 1
|
|
283
|
+
? 'h-full w-full object-cover'
|
|
284
|
+
: CARD_IMG_CLASS
|
|
285
|
+
}
|
|
286
|
+
isVideo={kind === 'video'}
|
|
287
|
+
isLast={last}
|
|
288
|
+
extra={extra}
|
|
289
|
+
/>
|
|
290
|
+
</div>
|
|
291
|
+
);
|
|
292
|
+
})}
|
|
293
|
+
</div>
|
|
294
|
+
) : resource.resourceKind === 'link' && resource.url ? (
|
|
295
|
+
<div className="bg-muted flex h-48 w-full items-center justify-center rounded-md">
|
|
296
|
+
<LinkIcon className="text-muted-foreground/50 size-8" />
|
|
297
|
+
</div>
|
|
298
|
+
) : null}
|
|
299
|
+
{renderBookmarkButton(
|
|
300
|
+
'bg-background text-muted-foreground hover:text-foreground absolute right-2 top-2 rounded-full p-1 opacity-70 hover:opacity-100 disabled:opacity-50',
|
|
301
|
+
)}
|
|
302
|
+
</div>
|
|
303
|
+
|
|
304
|
+
<h3 className="text-lg font-semibold">{resource.title}</h3>
|
|
305
|
+
{resource.categoryPath?.length ? (
|
|
306
|
+
<p className="text-muted-foreground text-xs">
|
|
307
|
+
{resource.categoryPath.join(' / ')}
|
|
308
|
+
</p>
|
|
309
|
+
) : null}
|
|
310
|
+
{excerpt ? (
|
|
311
|
+
<p className="text-muted-foreground text-sm">{excerpt}</p>
|
|
312
|
+
) : null}
|
|
313
|
+
{metaText ? (
|
|
314
|
+
<p className="text-muted-foreground text-xs">{metaText}</p>
|
|
315
|
+
) : null}
|
|
316
|
+
{tags.length ? (
|
|
317
|
+
<div className="flex flex-wrap gap-1">
|
|
318
|
+
{tags.map((value) => (
|
|
319
|
+
<span
|
|
320
|
+
key={value}
|
|
321
|
+
className="bg-muted text-muted-foreground rounded-full px-2 py-0.5 text-xs"
|
|
322
|
+
>
|
|
323
|
+
#{value}
|
|
324
|
+
</span>
|
|
325
|
+
))}
|
|
326
|
+
</div>
|
|
327
|
+
) : null}
|
|
328
|
+
<p className="text-muted-foreground text-xs uppercase">
|
|
329
|
+
{t(`resources.kinds.${resource.resourceKind}`, {
|
|
330
|
+
defaultValue: resource.resourceKind,
|
|
331
|
+
})}
|
|
332
|
+
</p>
|
|
333
|
+
</div>
|
|
334
|
+
);
|
|
335
|
+
};
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LayoutList,
|
|
3
|
+
LayoutGrid,
|
|
4
|
+
BookmarkIcon,
|
|
5
|
+
ChevronDown,
|
|
6
|
+
ChevronRight,
|
|
7
|
+
} from 'lucide-react';
|
|
8
|
+
import type { ResourceKind } from '@openpeepshq/common/types';
|
|
9
|
+
import { RESOURCE_KINDS } from '@openpeepshq/common/lib';
|
|
10
|
+
import { Button, Input, LoadingSpinner } from '@openpeepshq/react-ui';
|
|
11
|
+
import { useMemo, useState } from 'react';
|
|
12
|
+
import { useT } from '../../i18n';
|
|
13
|
+
import { useHrefOf } from '../../contexts/router';
|
|
14
|
+
import { useResourceLibrary } from '../../hooks';
|
|
15
|
+
import { ResourceCard } from './ResourceCard';
|
|
16
|
+
import type { CategoryNode } from '@openpeepshq/common/lib';
|
|
17
|
+
|
|
18
|
+
export interface ResourceLibraryProps {
|
|
19
|
+
groupId?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const COLLECTION_LIMIT = 5;
|
|
23
|
+
|
|
24
|
+
const Tree = ({
|
|
25
|
+
nodes,
|
|
26
|
+
selected,
|
|
27
|
+
onSelect,
|
|
28
|
+
counts,
|
|
29
|
+
expanded,
|
|
30
|
+
onToggle,
|
|
31
|
+
}: {
|
|
32
|
+
nodes: CategoryNode[];
|
|
33
|
+
selected: string[];
|
|
34
|
+
onSelect: (path: string[]) => void;
|
|
35
|
+
counts: Map<string, number>;
|
|
36
|
+
expanded: Set<string>;
|
|
37
|
+
onToggle: (nodeKey: string) => void;
|
|
38
|
+
}) => (
|
|
39
|
+
<ul className="space-y-1">
|
|
40
|
+
{nodes.map((node) => {
|
|
41
|
+
const nodeKey = node.path.join('/');
|
|
42
|
+
const nodeCount = counts.get(nodeKey) ?? 0;
|
|
43
|
+
const isExpanded = expanded.has(nodeKey);
|
|
44
|
+
const isSelected = selected.join('/') === nodeKey;
|
|
45
|
+
const hasChildren = node.children.length > 0;
|
|
46
|
+
return (
|
|
47
|
+
<li key={nodeKey}>
|
|
48
|
+
<div className="flex items-center gap-1">
|
|
49
|
+
<button
|
|
50
|
+
type="button"
|
|
51
|
+
className={`text-muted-foreground hover:text-foreground ${
|
|
52
|
+
hasChildren ? '' : 'invisible'
|
|
53
|
+
}`}
|
|
54
|
+
onClick={hasChildren ? () => onToggle(nodeKey) : undefined}
|
|
55
|
+
>
|
|
56
|
+
<ChevronRight
|
|
57
|
+
className={`size-3 transition-transform ${
|
|
58
|
+
isExpanded ? 'rotate-90' : ''
|
|
59
|
+
}`}
|
|
60
|
+
/>
|
|
61
|
+
</button>
|
|
62
|
+
<button
|
|
63
|
+
type="button"
|
|
64
|
+
className={`text-sm ${
|
|
65
|
+
isSelected
|
|
66
|
+
? 'font-semibold'
|
|
67
|
+
: 'text-muted-foreground hover:text-foreground'
|
|
68
|
+
}`}
|
|
69
|
+
onClick={() => onSelect(node.path)}
|
|
70
|
+
>
|
|
71
|
+
{node.name}
|
|
72
|
+
{nodeCount > 0 ? (
|
|
73
|
+
<span className="text-xs opacity-60">({nodeCount})</span>
|
|
74
|
+
) : null}
|
|
75
|
+
</button>
|
|
76
|
+
</div>
|
|
77
|
+
{hasChildren && isExpanded ? (
|
|
78
|
+
<div className="ml-4">
|
|
79
|
+
<Tree
|
|
80
|
+
nodes={node.children}
|
|
81
|
+
selected={selected}
|
|
82
|
+
onSelect={onSelect}
|
|
83
|
+
counts={counts}
|
|
84
|
+
expanded={expanded}
|
|
85
|
+
onToggle={onToggle}
|
|
86
|
+
/>
|
|
87
|
+
</div>
|
|
88
|
+
) : null}
|
|
89
|
+
</li>
|
|
90
|
+
);
|
|
91
|
+
})}
|
|
92
|
+
</ul>
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
export const ResourceLibrary = ({ groupId }: ResourceLibraryProps) => {
|
|
96
|
+
const t = useT();
|
|
97
|
+
const hrefOf = useHrefOf();
|
|
98
|
+
const library = useResourceLibrary(groupId);
|
|
99
|
+
const [expanded, setExpanded] = useState<Set<string>>(new Set());
|
|
100
|
+
|
|
101
|
+
const toggleNode = (nodeKey: string) => {
|
|
102
|
+
setExpanded((prev) => {
|
|
103
|
+
const next = new Set(prev);
|
|
104
|
+
if (next.has(nodeKey)) next.delete(nodeKey);
|
|
105
|
+
else next.add(nodeKey);
|
|
106
|
+
return next;
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const collectionCounts = useMemo(() => {
|
|
111
|
+
const counts = new Map<string, number>();
|
|
112
|
+
let rootCount = 0;
|
|
113
|
+
for (const post of library.posts) {
|
|
114
|
+
if (post.data?.type !== 'resource') continue;
|
|
115
|
+
const path = post.data.categoryPath;
|
|
116
|
+
if (!path?.length) continue;
|
|
117
|
+
rootCount++;
|
|
118
|
+
for (let i = 1; i <= path.length; i++) {
|
|
119
|
+
const key = path.slice(0, i).join('/');
|
|
120
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
counts.set('', rootCount);
|
|
124
|
+
return counts;
|
|
125
|
+
}, [library.posts]);
|
|
126
|
+
|
|
127
|
+
const treeNodes = useMemo(() => {
|
|
128
|
+
if (library.tree.length === 0) return [];
|
|
129
|
+
return [
|
|
130
|
+
{
|
|
131
|
+
name: t('resources.library.allFolders', {
|
|
132
|
+
defaultValue: 'All folders',
|
|
133
|
+
}),
|
|
134
|
+
path: [] as string[],
|
|
135
|
+
children: library.tree,
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
}, [library.tree, t]);
|
|
139
|
+
|
|
140
|
+
const sortedTags = useMemo(
|
|
141
|
+
() =>
|
|
142
|
+
library.tags
|
|
143
|
+
.filter((entry) => library.selectedTags.includes(entry.name))
|
|
144
|
+
.concat(
|
|
145
|
+
library.tags.filter(
|
|
146
|
+
(entry) => !library.selectedTags.includes(entry.name),
|
|
147
|
+
),
|
|
148
|
+
),
|
|
149
|
+
[library.tags, library.selectedTags],
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const visibleTags = library.tagsOpen
|
|
153
|
+
? sortedTags
|
|
154
|
+
: sortedTags.slice(0, COLLECTION_LIMIT);
|
|
155
|
+
const hasMoreTags = sortedTags.length > COLLECTION_LIMIT;
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<div>
|
|
159
|
+
<div className="flex flex-col gap-3 p-4">
|
|
160
|
+
{/* Search + view toggle */}
|
|
161
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
162
|
+
<Input
|
|
163
|
+
className="flex-1"
|
|
164
|
+
value={library.search}
|
|
165
|
+
onChange={(event) => library.setSearch(event.target.value)}
|
|
166
|
+
placeholder={t('resources.library.search', {
|
|
167
|
+
defaultValue: 'Search resources',
|
|
168
|
+
})}
|
|
169
|
+
data-testid="resources-search"
|
|
170
|
+
/>
|
|
171
|
+
<div className="ml-auto flex gap-1">
|
|
172
|
+
<Button
|
|
173
|
+
compact
|
|
174
|
+
size="sm"
|
|
175
|
+
variant={library.view === 'list' ? 'default' : 'outline'}
|
|
176
|
+
action={() => library.setView('list')}
|
|
177
|
+
data-testid="resources-view-list"
|
|
178
|
+
title={t('resources.library.viewList', {
|
|
179
|
+
defaultValue: 'List view',
|
|
180
|
+
})}
|
|
181
|
+
>
|
|
182
|
+
<LayoutList className="size-4" />
|
|
183
|
+
</Button>
|
|
184
|
+
<Button
|
|
185
|
+
compact
|
|
186
|
+
size="sm"
|
|
187
|
+
variant={library.view === 'cards' ? 'default' : 'outline'}
|
|
188
|
+
action={() => library.setView('cards')}
|
|
189
|
+
data-testid="resources-view-cards"
|
|
190
|
+
title={t('resources.library.viewCards', {
|
|
191
|
+
defaultValue: 'Cards view',
|
|
192
|
+
})}
|
|
193
|
+
>
|
|
194
|
+
<LayoutGrid className="size-4" />
|
|
195
|
+
</Button>
|
|
196
|
+
<Button
|
|
197
|
+
compact
|
|
198
|
+
size="sm"
|
|
199
|
+
variant={library.saved ? 'default' : 'outline'}
|
|
200
|
+
action={() => library.setSaved(!library.saved)}
|
|
201
|
+
data-testid="resources-saved"
|
|
202
|
+
title={t('resources.library.saved', { defaultValue: 'Saved' })}
|
|
203
|
+
>
|
|
204
|
+
<BookmarkIcon className="size-4" />
|
|
205
|
+
{t('resources.library.saved', { defaultValue: 'Saved' })}
|
|
206
|
+
</Button>
|
|
207
|
+
</div>
|
|
208
|
+
</div>
|
|
209
|
+
|
|
210
|
+
{/* Type filters (format-level) */}
|
|
211
|
+
<div className="flex flex-wrap gap-2">
|
|
212
|
+
<Button
|
|
213
|
+
compact
|
|
214
|
+
variant={library.kind === 'all' ? 'default' : 'outline'}
|
|
215
|
+
action={() => library.setKind('all')}
|
|
216
|
+
>
|
|
217
|
+
{t('resources.library.allKinds', { defaultValue: 'All' })}
|
|
218
|
+
</Button>
|
|
219
|
+
{RESOURCE_KINDS.map((kind) => (
|
|
220
|
+
<Button
|
|
221
|
+
key={kind}
|
|
222
|
+
compact
|
|
223
|
+
variant={library.kind === kind ? 'default' : 'outline'}
|
|
224
|
+
action={() => library.setKind(kind as ResourceKind)}
|
|
225
|
+
>
|
|
226
|
+
{t(`resources.kinds.${kind}`, { defaultValue: kind })}
|
|
227
|
+
</Button>
|
|
228
|
+
))}
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
{/* Breadcrumb + tree */}
|
|
232
|
+
<div>
|
|
233
|
+
<div className="flex items-center gap-2 text-sm">
|
|
234
|
+
<button
|
|
235
|
+
type="button"
|
|
236
|
+
className="text-muted-foreground hover:text-foreground"
|
|
237
|
+
onClick={() => library.setPathPrefix([])}
|
|
238
|
+
>
|
|
239
|
+
All
|
|
240
|
+
</button>
|
|
241
|
+
{library.pathPrefix.length > 0 ? (
|
|
242
|
+
<>
|
|
243
|
+
<span className="text-muted-foreground">/</span>
|
|
244
|
+
<span className="text-muted-foreground">
|
|
245
|
+
{library.pathPrefix.join(' / ')}
|
|
246
|
+
</span>
|
|
247
|
+
</>
|
|
248
|
+
) : null}
|
|
249
|
+
<button
|
|
250
|
+
type="button"
|
|
251
|
+
className="text-muted-foreground hover:text-foreground ml-auto"
|
|
252
|
+
onClick={() => library.setHierarchy(!library.hierarchy)}
|
|
253
|
+
>
|
|
254
|
+
{library.hierarchy ? (
|
|
255
|
+
<ChevronDown className="size-4" />
|
|
256
|
+
) : (
|
|
257
|
+
<ChevronRight className="size-4" />
|
|
258
|
+
)}
|
|
259
|
+
</button>
|
|
260
|
+
</div>
|
|
261
|
+
{library.hierarchy ? (
|
|
262
|
+
<div className="border-border mt-2 rounded-md border p-3">
|
|
263
|
+
{treeNodes.length ? (
|
|
264
|
+
<Tree
|
|
265
|
+
nodes={treeNodes}
|
|
266
|
+
selected={library.pathPrefix}
|
|
267
|
+
onSelect={library.setPathPrefix}
|
|
268
|
+
counts={collectionCounts}
|
|
269
|
+
expanded={expanded}
|
|
270
|
+
onToggle={toggleNode}
|
|
271
|
+
/>
|
|
272
|
+
) : null}
|
|
273
|
+
</div>
|
|
274
|
+
) : null}
|
|
275
|
+
</div>
|
|
276
|
+
|
|
277
|
+
{/* Tags (collapsible secondary discovery) */}
|
|
278
|
+
{library.tags.length ? (
|
|
279
|
+
<div className="flex flex-col gap-1">
|
|
280
|
+
<div className="flex flex-wrap gap-1">
|
|
281
|
+
{visibleTags.map((entry) => {
|
|
282
|
+
const isSelected = library.selectedTags.includes(entry.name);
|
|
283
|
+
return (
|
|
284
|
+
<button
|
|
285
|
+
key={entry.name}
|
|
286
|
+
type="button"
|
|
287
|
+
className={`rounded-full px-2 py-0.5 text-xs ${
|
|
288
|
+
isSelected
|
|
289
|
+
? 'bg-primary text-primary-foreground'
|
|
290
|
+
: 'bg-muted text-muted-foreground'
|
|
291
|
+
}`}
|
|
292
|
+
onClick={() => library.toggleTag(entry.name)}
|
|
293
|
+
>
|
|
294
|
+
#{entry.name} {entry.count}
|
|
295
|
+
</button>
|
|
296
|
+
);
|
|
297
|
+
})}
|
|
298
|
+
</div>
|
|
299
|
+
{hasMoreTags ? (
|
|
300
|
+
<button
|
|
301
|
+
type="button"
|
|
302
|
+
className="bg-muted text-muted-foreground rounded-full px-2 py-0.5 text-xs"
|
|
303
|
+
onClick={() => library.setTagsOpen(!library.tagsOpen)}
|
|
304
|
+
>
|
|
305
|
+
{library.tagsOpen
|
|
306
|
+
? t('resources.library.hideTags', {
|
|
307
|
+
defaultValue: 'Hide',
|
|
308
|
+
})
|
|
309
|
+
: t('resources.library.showHiddenTags', {
|
|
310
|
+
defaultValue: '+ {{count}}',
|
|
311
|
+
count: sortedTags.length - COLLECTION_LIMIT,
|
|
312
|
+
})}
|
|
313
|
+
</button>
|
|
314
|
+
) : null}
|
|
315
|
+
</div>
|
|
316
|
+
) : null}
|
|
317
|
+
|
|
318
|
+
{/* Resource count */}
|
|
319
|
+
{library.posts.length > 0 ? (
|
|
320
|
+
<p className="text-muted-foreground text-xs">
|
|
321
|
+
{t('resources.library.resourceCount', {
|
|
322
|
+
defaultValue: 'Showing {{count}} resources',
|
|
323
|
+
count: library.posts.length,
|
|
324
|
+
})}
|
|
325
|
+
</p>
|
|
326
|
+
) : null}
|
|
327
|
+
</div>
|
|
328
|
+
|
|
329
|
+
{library.query.isLoading ? (
|
|
330
|
+
<div className="flex h-32 items-center justify-center">
|
|
331
|
+
<LoadingSpinner />
|
|
332
|
+
</div>
|
|
333
|
+
) : library.posts.length === 0 ? (
|
|
334
|
+
<p className="text-muted-foreground p-8 text-center text-sm">
|
|
335
|
+
{library.activeFilterCount > 0
|
|
336
|
+
? t('resources.library.noResults', {
|
|
337
|
+
defaultValue: 'No resources match your current filters.',
|
|
338
|
+
})
|
|
339
|
+
: library.pathPrefix.length
|
|
340
|
+
? t('resources.library.emptyCollection', {
|
|
341
|
+
defaultValue: 'This collection has no resources yet.',
|
|
342
|
+
})
|
|
343
|
+
: t('resources.library.empty', {
|
|
344
|
+
defaultValue: 'No resources yet.',
|
|
345
|
+
})}
|
|
346
|
+
</p>
|
|
347
|
+
) : library.view === 'list' ? (
|
|
348
|
+
<div className="divide-border flex flex-col divide-y">
|
|
349
|
+
{library.posts.map((post) => (
|
|
350
|
+
<a
|
|
351
|
+
key={post.id}
|
|
352
|
+
href={hrefOf({ type: 'post', id: post.id })}
|
|
353
|
+
className="block"
|
|
354
|
+
>
|
|
355
|
+
<ResourceCard post={post} layout="list" />
|
|
356
|
+
</a>
|
|
357
|
+
))}
|
|
358
|
+
</div>
|
|
359
|
+
) : (
|
|
360
|
+
<div className="grid grid-cols-1 gap-4 p-4 sm:grid-cols-2 lg:grid-cols-3">
|
|
361
|
+
{library.posts.map((post) => (
|
|
362
|
+
<a
|
|
363
|
+
key={post.id}
|
|
364
|
+
href={hrefOf({ type: 'post', id: post.id })}
|
|
365
|
+
className="block"
|
|
366
|
+
>
|
|
367
|
+
<ResourceCard post={post} layout="card" />
|
|
368
|
+
</a>
|
|
369
|
+
))}
|
|
370
|
+
</div>
|
|
371
|
+
)}
|
|
372
|
+
</div>
|
|
373
|
+
);
|
|
374
|
+
};
|