@openpeepshq/react 0.3.1 → 0.3.3

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,9 +5,10 @@ import { checkGroupCapabilities, countGroupOwners, groupMembershipHasRole, } fro
5
5
  import { PopupMenu, PopupMenuButton } from '@openpeepshq/react-ui';
6
6
  import { useNavigate } from '../../contexts/router/index.js';
7
7
  import { useOpenpeeps } from '../../contexts/openpeeps/index.js';
8
- import { adjustUnseenCounts, invalidateUnseenCounts, } from '../../lib/unseenCountsOptimistic.js';
8
+ import { markGroupPostsRead } from '../../lib/unseenCountsOptimistic.js';
9
9
  import { useT } from '../../i18n/index.js';
10
10
  import { useAuthData, useCurrentProfile } from '../layout/IdentityContext.js';
11
+ import { useToast } from '../layout/ToastProvider.js';
11
12
  import { AddGroupMemberModal } from './AddGroupMemberModal.js';
12
13
  import { ConfirmGroupExitModal } from './ConfirmGroupExitModal.js';
13
14
  import { DeleteGroupModal } from './DeleteGroupModal.js';
@@ -17,6 +18,7 @@ export function GroupOptionsMenu({ group }) {
17
18
  const authData = useAuthData();
18
19
  const me = useCurrentProfile();
19
20
  const { openpeepsApi, queryClient, client } = useOpenpeeps();
21
+ const { success, error: toastError } = useToast();
20
22
  const membersQuery = openpeepsApi.useGroupMembers(group.id);
21
23
  const markGroupPostsSeen = openpeepsApi.markGroupPostsSeenAction();
22
24
  const [activeModal, setActiveModal] = useState(null);
@@ -41,13 +43,14 @@ export function GroupOptionsMenu({ group }) {
41
43
  setActiveModal('leave');
42
44
  };
43
45
  const handleMarkAllRead = async () => {
44
- adjustUnseenCounts(queryClient, client, { clearGroup: group.id });
45
46
  try {
46
- await markGroupPostsSeen({ groupId: group.id });
47
+ await markGroupPostsRead(queryClient, client, group.id, markGroupPostsSeen);
48
+ success(t('groups.markAllRead.success', {
49
+ defaultValue: 'Group posts marked as read',
50
+ }));
47
51
  }
48
52
  catch {
49
- await invalidateUnseenCounts(queryClient, client);
50
- window.alert(t('groups.markAllRead.error', {
53
+ toastError(t('groups.markAllRead.error', {
51
54
  defaultValue: 'Failed to mark group posts as read',
52
55
  }));
53
56
  }
@@ -11,5 +11,8 @@ export declare const applyUnseenCountsAdjustment: (current: UnseenPostCounts | u
11
11
  export declare const unseenCountsQueryKey: (client: ReturnType<typeof openpeepsClient>) => QueryKey;
12
12
  export declare const isUnseenCountsQueryKey: (queryKey: readonly unknown[]) => boolean;
13
13
  export declare const adjustUnseenCounts: (queryClient: QueryClient, client: ReturnType<typeof openpeepsClient>, adjustment: UnseenCountsAdjustment) => void;
14
- export declare const invalidateUnseenCounts: (queryClient: QueryClient, client: ReturnType<typeof openpeepsClient>) => Promise<void>;
14
+ export declare const invalidateUnseenCounts: (queryClient: QueryClient, _client: ReturnType<typeof openpeepsClient>) => Promise<void>;
15
+ export declare const markGroupPostsRead: (queryClient: QueryClient, client: ReturnType<typeof openpeepsClient>, groupId: string, markGroupPostsSeen: (params: {
16
+ groupId: string;
17
+ }) => Promise<unknown>) => Promise<void>;
15
18
  export declare const invalidatePostsExceptUnseenCounts: (queryClient: QueryClient) => Promise<void>;
@@ -24,16 +24,40 @@ export const applyUnseenCountsAdjustment = (current, adjustment) => {
24
24
  return { groups, direct };
25
25
  };
26
26
  export const unseenCountsQueryKey = (client) => client.posts.unseenCounts.queryKey({});
27
- export const isUnseenCountsQueryKey = (queryKey) => queryKey[0] === 'posts' &&
28
- queryKey[1] === 'unseen' &&
29
- queryKey[2] === 'counts';
27
+ export const isUnseenCountsQueryKey = (queryKey) => {
28
+ const parts = queryKey.filter((part) => typeof part === 'string' && part.length > 0);
29
+ const posts = parts.indexOf('posts');
30
+ return (posts !== -1 &&
31
+ parts[posts + 1] === 'unseen' &&
32
+ parts[posts + 2] === 'counts');
33
+ };
34
+ const unseenCountsQueryFilter = {
35
+ predicate: (query) => isUnseenCountsQueryKey(query.queryKey),
36
+ };
30
37
  export const adjustUnseenCounts = (queryClient, client, adjustment) => {
31
- queryClient.setQueryData(unseenCountsQueryKey(client), (current) => applyUnseenCountsAdjustment(current, adjustment));
38
+ const next = (current) => applyUnseenCountsAdjustment(current, adjustment);
39
+ // Menu pills subscribe to whatever key apiHook built; update every match.
40
+ queryClient.setQueriesData(unseenCountsQueryFilter, next);
41
+ const key = unseenCountsQueryKey(client);
42
+ if (queryClient.getQueryData(key) === undefined) {
43
+ queryClient.setQueryData(key, next(undefined));
44
+ }
32
45
  };
33
- export const invalidateUnseenCounts = async (queryClient, client) => {
34
- await queryClient.invalidateQueries({
35
- queryKey: unseenCountsQueryKey(client),
36
- });
46
+ export const invalidateUnseenCounts = async (queryClient, _client) => {
47
+ await queryClient.invalidateQueries(unseenCountsQueryFilter);
48
+ };
49
+ export const markGroupPostsRead = async (queryClient, client, groupId, markGroupPostsSeen) => {
50
+ await queryClient.cancelQueries(unseenCountsQueryFilter);
51
+ adjustUnseenCounts(queryClient, client, { clearGroup: groupId });
52
+ try {
53
+ await markGroupPostsSeen({ groupId });
54
+ // Re-apply in case a late refetch won the race during the request.
55
+ adjustUnseenCounts(queryClient, client, { clearGroup: groupId });
56
+ }
57
+ catch (error) {
58
+ await invalidateUnseenCounts(queryClient, client);
59
+ throw error;
60
+ }
37
61
  };
38
62
  export const invalidatePostsExceptUnseenCounts = async (queryClient) => {
39
63
  await queryClient.invalidateQueries({
@@ -39,7 +39,7 @@ export * from './components/post/attachmentPreview';
39
39
  export * from './components/profile/profileFieldDisplay';
40
40
  export { firstNWords, postReactionStats, stringToSegments, } from './components/post/helpers';
41
41
  export { initializeNewPostStores, getNewPostStores, useNewPostStores, resetStore, defaultNewArticle, defaultNewEvent, defaultNewNote, defaultNewQuestion, eventSanitizer, getReplyStore, resetReplyData, useReplyStore, } from './stores/newPosts';
42
- export type { NewPostsState } from './stores/types';
42
+ export type { NewPostsState, PlusButtonAction, PlusButtonActions, } from './stores/types';
43
43
  export { extractUrlsFromText, isValidUrl, } from './components/preview-link/helpers';
44
44
  export { JamProvider, useJamContext, useJamObserver, } from './components/jams/JamContext';
45
45
  export type { JamContextValue, JamProviderProps, } from './components/jams/JamContext';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/react",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -51,8 +51,8 @@
51
51
  "simple-icons": "^16.31.0",
52
52
  "uuidv7": "^1.0.2",
53
53
  "@openpeepshq/client": "^0.2.0",
54
- "@openpeepshq/common": "^0.2.0",
55
54
  "@openpeepshq/greenscreen": "^0.2.0",
55
+ "@openpeepshq/common": "^0.2.0",
56
56
  "@openpeepshq/react-ui": "^0.2.0"
57
57
  },
58
58
  "peerDependencies": {
@@ -16,12 +16,10 @@ import {
16
16
  import { PopupMenu, PopupMenuButton } from '@openpeepshq/react-ui';
17
17
  import { useNavigate } from '../../contexts/router';
18
18
  import { useOpenpeeps } from '../../contexts/openpeeps';
19
- import {
20
- adjustUnseenCounts,
21
- invalidateUnseenCounts,
22
- } from '../../lib/unseenCountsOptimistic';
19
+ import { markGroupPostsRead } from '../../lib/unseenCountsOptimistic';
23
20
  import { useT } from '../../i18n';
24
21
  import { useAuthData, useCurrentProfile } from '../layout/IdentityContext';
22
+ import { useToast } from '../layout/ToastProvider';
25
23
  import { AddGroupMemberModal } from './AddGroupMemberModal';
26
24
  import { ConfirmGroupExitModal } from './ConfirmGroupExitModal';
27
25
  import { DeleteGroupModal } from './DeleteGroupModal';
@@ -38,6 +36,7 @@ export function GroupOptionsMenu({ group }: GroupOptionsMenuProps) {
38
36
  const authData = useAuthData();
39
37
  const me = useCurrentProfile();
40
38
  const { openpeepsApi, queryClient, client } = useOpenpeeps();
39
+ const { success, error: toastError } = useToast();
41
40
  const membersQuery = openpeepsApi.useGroupMembers(group.id);
42
41
  const markGroupPostsSeen = openpeepsApi.markGroupPostsSeenAction();
43
42
  const [activeModal, setActiveModal] = useState<ActiveModal>(null);
@@ -88,12 +87,20 @@ export function GroupOptionsMenu({ group }: GroupOptionsMenuProps) {
88
87
  };
89
88
 
90
89
  const handleMarkAllRead = async () => {
91
- adjustUnseenCounts(queryClient, client, { clearGroup: group.id });
92
90
  try {
93
- await markGroupPostsSeen({ groupId: group.id });
91
+ await markGroupPostsRead(
92
+ queryClient,
93
+ client,
94
+ group.id,
95
+ markGroupPostsSeen,
96
+ );
97
+ success(
98
+ t('groups.markAllRead.success', {
99
+ defaultValue: 'Group posts marked as read',
100
+ }),
101
+ );
94
102
  } catch {
95
- await invalidateUnseenCounts(queryClient, client);
96
- window.alert(
103
+ toastError(
97
104
  t('groups.markAllRead.error', {
98
105
  defaultValue: 'Failed to mark group posts as read',
99
106
  }),
@@ -42,29 +42,64 @@ export const unseenCountsQueryKey = (
42
42
  client: ReturnType<typeof openpeepsClient>,
43
43
  ): QueryKey => client.posts.unseenCounts.queryKey({});
44
44
 
45
- export const isUnseenCountsQueryKey = (queryKey: readonly unknown[]): boolean =>
46
- queryKey[0] === 'posts' &&
47
- queryKey[1] === 'unseen' &&
48
- queryKey[2] === 'counts';
45
+ export const isUnseenCountsQueryKey = (
46
+ queryKey: readonly unknown[],
47
+ ): boolean => {
48
+ const parts = queryKey.filter(
49
+ (part): part is string => typeof part === 'string' && part.length > 0,
50
+ );
51
+ const posts = parts.indexOf('posts');
52
+ return (
53
+ posts !== -1 &&
54
+ parts[posts + 1] === 'unseen' &&
55
+ parts[posts + 2] === 'counts'
56
+ );
57
+ };
58
+
59
+ const unseenCountsQueryFilter = {
60
+ predicate: (query: { queryKey: readonly unknown[] }) =>
61
+ isUnseenCountsQueryKey(query.queryKey),
62
+ };
49
63
 
50
64
  export const adjustUnseenCounts = (
51
65
  queryClient: QueryClient,
52
66
  client: ReturnType<typeof openpeepsClient>,
53
67
  adjustment: UnseenCountsAdjustment,
54
68
  ): void => {
55
- queryClient.setQueryData<UnseenPostCounts>(
56
- unseenCountsQueryKey(client),
57
- (current) => applyUnseenCountsAdjustment(current, adjustment),
58
- );
69
+ const next = (current: UnseenPostCounts | undefined) =>
70
+ applyUnseenCountsAdjustment(current, adjustment);
71
+
72
+ // Menu pills subscribe to whatever key apiHook built; update every match.
73
+ queryClient.setQueriesData<UnseenPostCounts>(unseenCountsQueryFilter, next);
74
+ const key = unseenCountsQueryKey(client);
75
+ if (queryClient.getQueryData(key) === undefined) {
76
+ queryClient.setQueryData<UnseenPostCounts>(key, next(undefined));
77
+ }
59
78
  };
60
79
 
61
80
  export const invalidateUnseenCounts = async (
81
+ queryClient: QueryClient,
82
+ _client: ReturnType<typeof openpeepsClient>,
83
+ ): Promise<void> => {
84
+ await queryClient.invalidateQueries(unseenCountsQueryFilter);
85
+ };
86
+
87
+ export const markGroupPostsRead = async (
62
88
  queryClient: QueryClient,
63
89
  client: ReturnType<typeof openpeepsClient>,
90
+ groupId: string,
91
+ markGroupPostsSeen: (params: { groupId: string }) => Promise<unknown>,
64
92
  ): Promise<void> => {
65
- await queryClient.invalidateQueries({
66
- queryKey: unseenCountsQueryKey(client),
67
- });
93
+ await queryClient.cancelQueries(unseenCountsQueryFilter);
94
+ adjustUnseenCounts(queryClient, client, { clearGroup: groupId });
95
+ try {
96
+ await markGroupPostsSeen({ groupId });
97
+ // Re-apply in case a late refetch won the race during the request.
98
+ adjustUnseenCounts(queryClient, client, { clearGroup: groupId });
99
+ } catch (error) {
100
+ await invalidateUnseenCounts(queryClient, client);
101
+ throw error;
102
+ }
68
103
  };
69
104
 
70
105
  export const invalidatePostsExceptUnseenCounts = async (
@@ -83,7 +83,11 @@ export {
83
83
  resetReplyData,
84
84
  useReplyStore,
85
85
  } from './stores/newPosts';
86
- export type { NewPostsState } from './stores/types';
86
+ export type {
87
+ NewPostsState,
88
+ PlusButtonAction,
89
+ PlusButtonActions,
90
+ } from './stores/types';
87
91
  export {
88
92
  extractUrlsFromText,
89
93
  isValidUrl,