@openpeepshq/react 0.9.0 → 0.9.20

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.
@@ -5,6 +5,7 @@ import { pollOptionsWithinLimit, resolvePollOptionContents, } from '@openpeepshq
5
5
  import { Button, Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@openpeepshq/react-ui';
6
6
  import { useOpenpeeps } from '../../../contexts/openpeeps/index.js';
7
7
  import { useT } from '../../../i18n/index.js';
8
+ import { apiErrorMessage } from '../../../lib/apiErrorMessage.js';
8
9
  import { useToast } from '../../layout/ToastProvider.js';
9
10
  import { useCurrentProfile } from '../../layout/IdentityContext.js';
10
11
  import { defaultNewNote, defaultNewQuestion, resetReplyData, useReplyStore, } from '../../../stores/newPosts.js';
@@ -137,10 +138,10 @@ export function ReplyModal({ post, onClose }) {
137
138
  }));
138
139
  onClose();
139
140
  }
140
- catch {
141
- toastError(t('posts.replyModal.errorToast', {
142
- defaultValue: 'Failed to post reply',
143
- }));
141
+ catch (err) {
142
+ toastError(apiErrorMessage(err, t, t('posts.replyModal.errorToast', {
143
+ defaultValue: 'An error occurred while creating the reply',
144
+ })));
144
145
  }
145
146
  finally {
146
147
  setSubmitting(false);
@@ -360,6 +360,7 @@ export declare const useNewEvent: () => {
360
360
  capabilitiesNeeded?: string[] | null | undefined;
361
361
  };
362
362
  setPostData: (data: PostCreationData) => void;
363
+ canCreate: boolean;
363
364
  canSubmit: boolean;
364
365
  submitting: boolean;
365
366
  error: string | null;
@@ -1,17 +1,20 @@
1
1
  import { useCallback, useEffect, useMemo, useState } from 'react';
2
2
  import { canAnnouncePosts, isCommunityWideVisibility, } from '@openpeepshq/common';
3
- import { effectiveEventTimes, normalizeEventDataForSave, upsertEventException, } from '@openpeepshq/common/lib';
3
+ import { canCreatePostTypeIgnoringScopes, effectiveEventTimes, normalizeEventDataForSave, upsertEventException, } from '@openpeepshq/common/lib';
4
4
  import { useOpenpeeps } from '../../contexts/openpeeps/index.js';
5
5
  import { useNavigate } from '../../contexts/router/index.js';
6
6
  import { useT } from '../../i18n/context.js';
7
- import { useCurrentProfile } from '../../components/layout/IdentityContext.js';
7
+ import { useAuthData } from '../../components/layout/IdentityContext.js';
8
8
  import { useServerInfo } from '../../components/server-data/context.js';
9
9
  import { eventSanitizer, getNewPostStores } from '../../stores/newPosts.js';
10
+ import { AUDIENCE_CHOICE_VALUES } from '../../lib/audienceChoices.js';
10
11
  export const useNewEvent = () => {
11
12
  const t = useT();
12
13
  const navigate = useNavigate();
13
14
  const serverInfo = useServerInfo();
14
- const me = useCurrentProfile();
15
+ const authData = useAuthData();
16
+ // The form only offers these audiences; other visibilities would dead-end.
17
+ const canCreate = canCreatePostTypeIgnoringScopes(authData, 'event', AUDIENCE_CHOICE_VALUES);
15
18
  const { openpeepsApi } = useOpenpeeps();
16
19
  const createPost = openpeepsApi.createPostAction();
17
20
  const announcePost = openpeepsApi.admin.announcePostAction();
@@ -21,7 +24,7 @@ export const useNewEvent = () => {
21
24
  const [notify, setNotify] = useState(false);
22
25
  const [submitting, setSubmitting] = useState(false);
23
26
  const [error, setError] = useState(null);
24
- const showNotify = canAnnouncePosts(me?.roles) &&
27
+ const showNotify = canAnnouncePosts(authData.profile?.roles) &&
25
28
  isCommunityWideVisibility(postData.visibility);
26
29
  const event = postData.data.type === 'event' ? postData.data : null;
27
30
  const canSubmit = !!event?.start &&
@@ -82,6 +85,7 @@ export const useNewEvent = () => {
82
85
  return {
83
86
  postData,
84
87
  setPostData,
88
+ canCreate,
85
89
  canSubmit,
86
90
  submitting,
87
91
  error,
@@ -23,7 +23,7 @@ export function AdminInvites() {
23
23
  return (_jsxs("div", { className: "p-4", children: [_jsx("form", { role: "search", className: "mb-4", onSubmit: (e) => {
24
24
  e.preventDefault();
25
25
  }, children: _jsx(Input, { placeholder: t('admin.invites.searchPlaceholder', {
26
- defaultValue: 'Search by name or email',
26
+ defaultValue: 'Search by name or handle',
27
27
  }), value: search, onChange: (e) => setSearch(e.target.value) }) }), filtered.length === 0 ? (_jsx("div", { className: "flex w-full items-center justify-center p-4", children: _jsx("h2", { className: "text-lg", children: t('admin.invites.noInvitesFound', {
28
28
  defaultValue: 'No invites found',
29
29
  }) }) })) : (_jsx("div", { className: "rounded-md border", children: _jsxs("table", { className: "w-full text-sm", children: [_jsx("thead", { className: "bg-surface", children: _jsxs("tr", { children: [_jsx("th", { className: "p-2 text-left", children: t('admin.invites.linkColumn', { defaultValue: 'Link' }) }), _jsx("th", { className: "p-2 text-left", children: t('admin.invites.createdByColumn', {
@@ -5,6 +5,7 @@ import { Info, Search } from 'lucide-react';
5
5
  import { useT, useOpenpeeps, useSetPageHeader, usePostViewFlush, adjustUnseenCounts, useNavigate, } from '../../index.js';
6
6
  import { canCreatePost } from '@openpeepshq/common';
7
7
  import { Avatar, ConversationParticipantsModal, DirectMessageSearch, MessageInThread, OpenpeepsMarkdownInput, ProfileCard, useAuthData, useCurrentProfile, useToast, } from '../../components/index.js';
8
+ import { apiErrorMessage } from '../../lib/apiErrorMessage.js';
8
9
  import { Button, LoadingSpinner } from '@openpeepshq/react-ui';
9
10
  const MAX_LENGTH = 500;
10
11
  const isMessageId = (value) => !!value &&
@@ -91,10 +92,10 @@ export function ConversationShow() {
91
92
  refetchType: 'all',
92
93
  });
93
94
  }
94
- catch {
95
- toast.error(t('conversations.sendError', {
95
+ catch (err) {
96
+ toast.error(apiErrorMessage(err, t, t('conversations.sendError', {
96
97
  defaultValue: 'Failed to send message',
97
- }));
98
+ })));
98
99
  }
99
100
  finally {
100
101
  setSending(false);
@@ -6,10 +6,21 @@ import { EventForm } from '../../components/index.js';
6
6
  import { Button, Toast } from '@openpeepshq/react-ui';
7
7
  export function NewEvent() {
8
8
  const t = useT();
9
- const { postData, setPostData, canSubmit, submitting, error, clearError, notify, setNotify, showNotify, submit, } = useNewEvent();
10
- const headerActions = useMemo(() => (_jsx(Button, { title: t('events.create.title', { defaultValue: 'Create Event' }), variant: "default", action: submit, disabled: submitting || !canSubmit, "data-testid": "events-create-submit", children: submitting
11
- ? t('common.submitting', { defaultValue: 'Creating…' })
12
- : t('events.create.title', { defaultValue: 'Create Event' }) })), [canSubmit, submit, submitting, t]);
9
+ const { postData, setPostData, canCreate, canSubmit, submitting, error, clearError, notify, setNotify, showNotify, submit, } = useNewEvent();
10
+ const headerActions = useMemo(() => {
11
+ if (!canCreate)
12
+ return null;
13
+ return (_jsx(Button, { title: t('events.create.title', { defaultValue: 'Create Event' }), variant: "default", action: submit, disabled: submitting || !canSubmit, "data-testid": "events-create-submit", children: submitting
14
+ ? t('common.submitting', { defaultValue: 'Creating…' })
15
+ : t('events.create.title', { defaultValue: 'Create Event' }) }));
16
+ }, [canCreate, canSubmit, submit, submitting, t]);
13
17
  useSetPageHeader(t('events.create.title', { defaultValue: 'Create Event' }), headerActions);
18
+ if (!canCreate) {
19
+ return (_jsxs("div", { className: "mx-auto flex max-w-md flex-col gap-2 px-6 py-16 text-center", "data-testid": "events-create-not-allowed", role: "status", children: [_jsx("p", { className: "text-xl font-semibold", children: t('events.create.notAllowed.title', {
20
+ defaultValue: "You can't create events",
21
+ }) }), _jsx("p", { className: "text-muted-foreground text-sm", children: t('events.create.notAllowed.description', {
22
+ defaultValue: "Your role isn't allowed to create events. Ask a community owner or moderator if you need to host one.",
23
+ }) })] }));
24
+ }
14
25
  return (_jsxs("div", { className: "pb-12", children: [_jsx(EventForm, { postData: postData, onChange: setPostData, showNotify: showNotify, notify: notify, onNotifyChange: setNotify }), error ? (_jsx("div", { className: "px-3", children: _jsx(Toast, { variant: "error", onDismiss: clearError, children: error }) })) : null] }));
15
26
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,69 @@
1
+ /** @vitest-environment jsdom */
2
+ import { act, createElement } from 'react';
3
+ import { createRoot } from 'react-dom/client';
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5
+ import { pageHeaderStore, usePageHeader } from '../../stores/pageHeader.js';
6
+ import { NewEvent } from './New.js';
7
+ const state = vi.hoisted(() => ({ canCreate: false }));
8
+ vi.mock('../../hooks/events/useEventComposer', () => ({
9
+ useNewEvent: () => ({
10
+ postData: { type: 'event', visibility: 'local', data: { type: 'event' } },
11
+ setPostData: () => undefined,
12
+ canCreate: state.canCreate,
13
+ canSubmit: true,
14
+ submitting: false,
15
+ error: null,
16
+ clearError: () => undefined,
17
+ submit: () => undefined,
18
+ }),
19
+ }));
20
+ vi.mock('../../components', () => ({
21
+ EventForm: () => createElement('div', { 'data-testid': 'events-form' }),
22
+ }));
23
+ vi.mock('../../index', async () => {
24
+ const { useSetPageHeader } = await import('../../stores/pageHeader.js');
25
+ const t = (key, opts) => opts?.defaultValue ?? key;
26
+ return { useT: () => t, useSetPageHeader };
27
+ });
28
+ const Shell = ({ children }) => {
29
+ const header = usePageHeader();
30
+ return createElement('div', null, header?.actions, children);
31
+ };
32
+ const text = (container) => container.textContent ?? '';
33
+ describe('NewEvent page', () => {
34
+ let root;
35
+ let container;
36
+ beforeEach(() => {
37
+ globalThis.IS_REACT_ACT_ENVIRONMENT = true;
38
+ state.canCreate = false;
39
+ container = document.createElement('div');
40
+ document.body.appendChild(container);
41
+ root = createRoot(container);
42
+ });
43
+ afterEach(() => {
44
+ act(() => {
45
+ root?.unmount();
46
+ });
47
+ container?.remove();
48
+ pageHeaderStore.set(undefined);
49
+ });
50
+ const render = () => {
51
+ act(() => {
52
+ root?.render(createElement(Shell, null, createElement(NewEvent)));
53
+ });
54
+ };
55
+ it('explains the restriction before showing the form', () => {
56
+ render();
57
+ expect(text(container)).toContain("You can't create events");
58
+ expect(text(container)).toContain("Your role isn't allowed to create events.");
59
+ expect(container.querySelector('[data-testid="events-form"]')).toBeNull();
60
+ expect(container.querySelector('[data-testid="events-create-submit"]')).toBeNull();
61
+ });
62
+ it('shows the form when the profile can create events', () => {
63
+ state.canCreate = true;
64
+ render();
65
+ expect(container.querySelector('[data-testid="events-form"]')).not.toBeNull();
66
+ expect(container.querySelector('[data-testid="events-create-submit"]')).not.toBeNull();
67
+ expect(text(container)).not.toContain("You can't create events");
68
+ });
69
+ });
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useCallback, useMemo } from 'react';
3
3
  import { FilePlus } from 'lucide-react';
4
- import { canCreatePostType } from '@openpeepshq/common/lib';
4
+ import { canCreatePostTypeIgnoringScopes } from '@openpeepshq/common/lib';
5
5
  import { useT, useSetPageHeader, useSetPlusButtonActions, useAuthData, } from '../../index.js';
6
6
  import { useNavigate } from '../../contexts/router/index.js';
7
7
  import { ResourceLibrary } from '../../components/resources/ResourceLibrary.js';
@@ -10,7 +10,7 @@ export const ResourcesIndex = () => {
10
10
  const t = useT();
11
11
  const navigate = useNavigate();
12
12
  const authData = useAuthData();
13
- const canCreate = canCreatePostType(authData, 'resource');
13
+ const canCreate = canCreatePostTypeIgnoringScopes(authData, 'resource');
14
14
  const openNew = useCallback(() => {
15
15
  const stores = getNewPostStores();
16
16
  stores.resource = { ...stores.resource, groupId: undefined };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/react",
3
- "version": "0.9.0",
3
+ "version": "0.9.20",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -51,10 +51,10 @@
51
51
  "react-i18next": "^15.4.1",
52
52
  "simple-icons": "^16.31.0",
53
53
  "uuidv7": "^1.0.2",
54
- "@openpeepshq/common": "^0.5.0",
55
- "@openpeepshq/react-ui": "^0.4.1",
54
+ "@openpeepshq/client": "^0.3.0",
56
55
  "@openpeepshq/greenscreen": "^0.2.1",
57
- "@openpeepshq/client": "^0.3.0"
56
+ "@openpeepshq/common": "^0.5.11",
57
+ "@openpeepshq/react-ui": "^0.4.1"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "react": "^18.3.0 || ^19.0.0",
@@ -15,6 +15,7 @@ import {
15
15
  } from '@openpeepshq/react-ui';
16
16
  import { useOpenpeeps } from '../../../contexts/openpeeps';
17
17
  import { useT } from '../../../i18n';
18
+ import { apiErrorMessage } from '../../../lib/apiErrorMessage';
18
19
  import { useToast } from '../../layout/ToastProvider';
19
20
  import { useCurrentProfile } from '../../layout/IdentityContext';
20
21
  import {
@@ -179,11 +180,15 @@ export function ReplyModal({ post, onClose }: ReplyModalProps) {
179
180
  }),
180
181
  );
181
182
  onClose();
182
- } catch {
183
+ } catch (err) {
183
184
  toastError(
184
- t('posts.replyModal.errorToast', {
185
- defaultValue: 'Failed to post reply',
186
- }),
185
+ apiErrorMessage(
186
+ err,
187
+ t,
188
+ t('posts.replyModal.errorToast', {
189
+ defaultValue: 'An error occurred while creating the reply',
190
+ }),
191
+ ),
187
192
  );
188
193
  } finally {
189
194
  setSubmitting(false);
@@ -9,6 +9,7 @@ import type {
9
9
  PostDataUnion,
10
10
  } from '@openpeepshq/common/types';
11
11
  import {
12
+ canCreatePostTypeIgnoringScopes,
12
13
  effectiveEventTimes,
13
14
  normalizeEventDataForSave,
14
15
  upsertEventException,
@@ -16,15 +17,22 @@ import {
16
17
  import { useOpenpeeps } from '../../contexts/openpeeps';
17
18
  import { useNavigate } from '../../contexts/router';
18
19
  import { useT } from '../../i18n/context';
19
- import { useCurrentProfile } from '../../components/layout/IdentityContext';
20
+ import { useAuthData } from '../../components/layout/IdentityContext';
20
21
  import { useServerInfo } from '../../components/server-data/context';
21
22
  import { eventSanitizer, getNewPostStores } from '../../stores/newPosts';
23
+ import { AUDIENCE_CHOICE_VALUES } from '../../lib/audienceChoices';
22
24
 
23
25
  export const useNewEvent = () => {
24
26
  const t = useT();
25
27
  const navigate = useNavigate();
26
28
  const serverInfo = useServerInfo();
27
- const me = useCurrentProfile();
29
+ const authData = useAuthData();
30
+ // The form only offers these audiences; other visibilities would dead-end.
31
+ const canCreate = canCreatePostTypeIgnoringScopes(
32
+ authData,
33
+ 'event',
34
+ AUDIENCE_CHOICE_VALUES,
35
+ );
28
36
  const { openpeepsApi } = useOpenpeeps();
29
37
  const createPost = openpeepsApi.createPostAction();
30
38
  const announcePost = openpeepsApi.admin.announcePostAction();
@@ -42,7 +50,7 @@ export const useNewEvent = () => {
42
50
  const [error, setError] = useState<string | null>(null);
43
51
 
44
52
  const showNotify =
45
- canAnnouncePosts(me?.roles) &&
53
+ canAnnouncePosts(authData.profile?.roles) &&
46
54
  isCommunityWideVisibility(postData.visibility);
47
55
 
48
56
  const event = postData.data.type === 'event' ? postData.data : null;
@@ -110,6 +118,7 @@ export const useNewEvent = () => {
110
118
  return {
111
119
  postData,
112
120
  setPostData,
121
+ canCreate,
113
122
  canSubmit,
114
123
  submitting,
115
124
  error,
@@ -64,7 +64,7 @@ export function AdminInvites() {
64
64
  >
65
65
  <Input
66
66
  placeholder={t('admin.invites.searchPlaceholder', {
67
- defaultValue: 'Search by name or email',
67
+ defaultValue: 'Search by name or handle',
68
68
  })}
69
69
  value={search}
70
70
  onChange={(e) => setSearch(e.target.value)}
@@ -22,6 +22,7 @@ import {
22
22
  useCurrentProfile,
23
23
  useToast,
24
24
  } from '../../components';
25
+ import { apiErrorMessage } from '../../lib/apiErrorMessage';
25
26
  import { Button, LoadingSpinner } from '@openpeepshq/react-ui';
26
27
 
27
28
  const MAX_LENGTH = 500;
@@ -162,11 +163,15 @@ export function ConversationShow() {
162
163
  queryKey: client.conversations.list.queryKey({}),
163
164
  refetchType: 'all',
164
165
  });
165
- } catch {
166
+ } catch (err) {
166
167
  toast.error(
167
- t('conversations.sendError', {
168
- defaultValue: 'Failed to send message',
169
- }),
168
+ apiErrorMessage(
169
+ err,
170
+ t,
171
+ t('conversations.sendError', {
172
+ defaultValue: 'Failed to send message',
173
+ }),
174
+ ),
170
175
  );
171
176
  } finally {
172
177
  setSending(false);
@@ -9,6 +9,7 @@ export function NewEvent() {
9
9
  const {
10
10
  postData,
11
11
  setPostData,
12
+ canCreate,
12
13
  canSubmit,
13
14
  submitting,
14
15
  error,
@@ -19,8 +20,9 @@ export function NewEvent() {
19
20
  submit,
20
21
  } = useNewEvent();
21
22
 
22
- const headerActions = useMemo(
23
- () => (
23
+ const headerActions = useMemo(() => {
24
+ if (!canCreate) return null;
25
+ return (
24
26
  <Button
25
27
  title={t('events.create.title', { defaultValue: 'Create Event' })}
26
28
  variant="default"
@@ -32,15 +34,36 @@ export function NewEvent() {
32
34
  ? t('common.submitting', { defaultValue: 'Creating…' })
33
35
  : t('events.create.title', { defaultValue: 'Create Event' })}
34
36
  </Button>
35
- ),
36
- [canSubmit, submit, submitting, t],
37
- );
37
+ );
38
+ }, [canCreate, canSubmit, submit, submitting, t]);
38
39
 
39
40
  useSetPageHeader(
40
41
  t('events.create.title', { defaultValue: 'Create Event' }),
41
42
  headerActions,
42
43
  );
43
44
 
45
+ if (!canCreate) {
46
+ return (
47
+ <div
48
+ className="mx-auto flex max-w-md flex-col gap-2 px-6 py-16 text-center"
49
+ data-testid="events-create-not-allowed"
50
+ role="status"
51
+ >
52
+ <p className="text-xl font-semibold">
53
+ {t('events.create.notAllowed.title', {
54
+ defaultValue: "You can't create events",
55
+ })}
56
+ </p>
57
+ <p className="text-muted-foreground text-sm">
58
+ {t('events.create.notAllowed.description', {
59
+ defaultValue:
60
+ "Your role isn't allowed to create events. Ask a community owner or moderator if you need to host one.",
61
+ })}
62
+ </p>
63
+ </div>
64
+ );
65
+ }
66
+
44
67
  return (
45
68
  <div className="pb-12">
46
69
  <EventForm
@@ -1,6 +1,6 @@
1
1
  import { useCallback, useMemo } from 'react';
2
2
  import { FilePlus } from 'lucide-react';
3
- import { canCreatePostType } from '@openpeepshq/common/lib';
3
+ import { canCreatePostTypeIgnoringScopes } from '@openpeepshq/common/lib';
4
4
  import {
5
5
  useT,
6
6
  useSetPageHeader,
@@ -15,7 +15,7 @@ export const ResourcesIndex = () => {
15
15
  const t = useT();
16
16
  const navigate = useNavigate();
17
17
  const authData = useAuthData();
18
- const canCreate = canCreatePostType(authData, 'resource');
18
+ const canCreate = canCreatePostTypeIgnoringScopes(authData, 'resource');
19
19
 
20
20
  const openNew = useCallback(() => {
21
21
  const stores = getNewPostStores();