@openpeepshq/react 0.6.0 → 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/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/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 +1 -1
- package/dist/react-native.js +1 -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 +6 -6
- package/src/components/index.ts +1 -0
- 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/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 +1 -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,139 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { normalizeResourceTags, resourceHasRequiredMedia, } from '@openpeepshq/common/lib';
|
|
3
|
+
import { useOpenpeeps } from '../../contexts/openpeeps/index.js';
|
|
4
|
+
import { useNavigate } from '../../contexts/router/index.js';
|
|
5
|
+
import { useT } from '../../i18n/context.js';
|
|
6
|
+
import { useServerInfo } from '../../components/server-data/index.js';
|
|
7
|
+
import { defaultNewResource, getNewPostStores } from '../../stores/index.js';
|
|
8
|
+
const asResource = (data) => data.type === 'resource' ? data : null;
|
|
9
|
+
const preparedResource = (resource) => ({
|
|
10
|
+
...resource,
|
|
11
|
+
title: resource.title.trim(),
|
|
12
|
+
tags: normalizeResourceTags(resource.tags),
|
|
13
|
+
categoryPath: (resource.categoryPath ?? [])
|
|
14
|
+
.map((segment) => segment.trim())
|
|
15
|
+
.filter(Boolean),
|
|
16
|
+
url: resource.url?.trim() || undefined,
|
|
17
|
+
});
|
|
18
|
+
export const useNewResource = () => {
|
|
19
|
+
const t = useT();
|
|
20
|
+
const navigate = useNavigate();
|
|
21
|
+
const serverInfo = useServerInfo();
|
|
22
|
+
const { openpeepsApi } = useOpenpeeps();
|
|
23
|
+
const createPost = openpeepsApi.createPostAction();
|
|
24
|
+
const stores = getNewPostStores();
|
|
25
|
+
const [postData, setPostDataState] = useState(() => {
|
|
26
|
+
const stored = stores.resource;
|
|
27
|
+
if (stored.type === 'resource' && stored.data.type === 'resource') {
|
|
28
|
+
return stored;
|
|
29
|
+
}
|
|
30
|
+
return defaultNewResource(serverInfo.publicContent);
|
|
31
|
+
});
|
|
32
|
+
const [submitting, setSubmitting] = useState(false);
|
|
33
|
+
const [error, setError] = useState(null);
|
|
34
|
+
const resource = asResource(postData.data);
|
|
35
|
+
const canSubmit = !!resource?.title.trim() &&
|
|
36
|
+
!!resource &&
|
|
37
|
+
resourceHasRequiredMedia(resource) &&
|
|
38
|
+
!(postData.visibility === 'direct' && !postData.audience?.length);
|
|
39
|
+
const setPostData = (data) => {
|
|
40
|
+
setPostDataState(data);
|
|
41
|
+
stores.resource = data;
|
|
42
|
+
};
|
|
43
|
+
const submit = useCallback(async () => {
|
|
44
|
+
setError(null);
|
|
45
|
+
if (!resource?.title.trim()) {
|
|
46
|
+
setError(t('resources.validation.titleRequired', {
|
|
47
|
+
defaultValue: 'Title is required',
|
|
48
|
+
}));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (!resourceHasRequiredMedia(resource)) {
|
|
52
|
+
setError(t('resources.validation.mediaRequired', {
|
|
53
|
+
defaultValue: 'Add the media or link this resource needs',
|
|
54
|
+
}));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (postData.visibility === 'direct' && !postData.audience?.length) {
|
|
58
|
+
setError(t('resources.validation.directAudience', {
|
|
59
|
+
defaultValue: 'Choose at least one recipient for a direct resource.',
|
|
60
|
+
}));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
setSubmitting(true);
|
|
64
|
+
try {
|
|
65
|
+
const created = await createPost({
|
|
66
|
+
...postData,
|
|
67
|
+
type: 'resource',
|
|
68
|
+
data: preparedResource(resource),
|
|
69
|
+
});
|
|
70
|
+
stores.resetNewResourceState();
|
|
71
|
+
navigate({ type: 'post', id: created.id });
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
setError(err.message);
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
setSubmitting(false);
|
|
78
|
+
}
|
|
79
|
+
}, [createPost, navigate, postData, resource, stores, t]);
|
|
80
|
+
return {
|
|
81
|
+
postData,
|
|
82
|
+
setPostData,
|
|
83
|
+
canSubmit,
|
|
84
|
+
submitting,
|
|
85
|
+
error,
|
|
86
|
+
clearError: () => setError(null),
|
|
87
|
+
submit,
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
export const useEditResource = (resourceId) => {
|
|
91
|
+
const navigate = useNavigate();
|
|
92
|
+
const { openpeepsApi } = useOpenpeeps();
|
|
93
|
+
const postQuery = openpeepsApi.usePost(resourceId);
|
|
94
|
+
const updatePost = openpeepsApi.updatePostAction({ id: resourceId });
|
|
95
|
+
const [postData, setPostData] = useState(null);
|
|
96
|
+
const [submitting, setSubmitting] = useState(false);
|
|
97
|
+
const [error, setError] = useState(null);
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (postQuery.data && postQuery.data.type === 'resource') {
|
|
100
|
+
setPostData({
|
|
101
|
+
visibility: postQuery.data.visibility,
|
|
102
|
+
type: 'resource',
|
|
103
|
+
groupId: postQuery.data.groupId ?? undefined,
|
|
104
|
+
audience: postQuery.data.audience ?? undefined,
|
|
105
|
+
data: postQuery.data.data,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}, [postQuery.data]);
|
|
109
|
+
const resourceTitle = asResource(postQuery.data?.data)?.title;
|
|
110
|
+
const submit = useCallback(async () => {
|
|
111
|
+
if (!postData)
|
|
112
|
+
return;
|
|
113
|
+
const resource = asResource(postData.data);
|
|
114
|
+
if (!resource)
|
|
115
|
+
return;
|
|
116
|
+
setError(null);
|
|
117
|
+
setSubmitting(true);
|
|
118
|
+
try {
|
|
119
|
+
await updatePost(preparedResource(resource));
|
|
120
|
+
navigate({ type: 'post', id: resourceId });
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
setError(err.message);
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
setSubmitting(false);
|
|
127
|
+
}
|
|
128
|
+
}, [navigate, postData, resourceId, updatePost]);
|
|
129
|
+
return {
|
|
130
|
+
postQuery,
|
|
131
|
+
postData,
|
|
132
|
+
setPostData,
|
|
133
|
+
resourceTitle,
|
|
134
|
+
submitting,
|
|
135
|
+
error,
|
|
136
|
+
clearError: () => setError(null),
|
|
137
|
+
submit,
|
|
138
|
+
};
|
|
139
|
+
};
|