@openpeepshq/react 0.3.0 → 0.3.2
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/groups/GroupOptionsMenu.js +8 -5
- package/dist/components/layout/IdentityContext.js +16 -10
- package/dist/components/post/feed/events/eventCalendar.d.ts +6 -5
- package/dist/components/post/feed/events/eventCalendar.js +46 -5
- package/dist/hooks/events/useEventsCalendar.js +3 -2
- package/dist/lib/unseenCountsOptimistic.d.ts +4 -1
- package/dist/lib/unseenCountsOptimistic.js +32 -8
- package/package.json +3 -3
- package/src/components/groups/GroupOptionsMenu.tsx +15 -8
- package/src/components/layout/IdentityContext.ts +20 -10
- package/src/components/post/feed/events/eventCalendar.ts +63 -5
- package/src/hooks/events/useEventsCalendar.ts +6 -1
- package/src/lib/unseenCountsOptimistic.ts +46 -11
|
@@ -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 {
|
|
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
|
|
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
|
-
|
|
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
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createContext, useContext, useEffect, useMemo, useState } from 'react';
|
|
2
2
|
import { parseScopesFromJwt } from '@openpeepshq/common';
|
|
3
|
-
import { AUTH_CREDENTIALS_STORAGE_KEY, OPENPEEPS_CREDENTIALS_CHANGED_EVENT, } from '../../auth/credentials/index.js';
|
|
3
|
+
import { AUTH_CREDENTIALS_STORAGE_KEY, OPENPEEPS_CREDENTIALS_CHANGED_EVENT, subscribeCredentialsChanged, } from '../../auth/credentials/index.js';
|
|
4
4
|
import { useCredentialsStore } from '../../contexts/credentialsStore/index.js';
|
|
5
5
|
export const IdentityContext = createContext({});
|
|
6
6
|
export const useIdentity = () => useContext(IdentityContext);
|
|
@@ -22,22 +22,28 @@ export const useAuthData = () => {
|
|
|
22
22
|
setScopes(next);
|
|
23
23
|
};
|
|
24
24
|
void refresh();
|
|
25
|
-
if (typeof window === 'undefined') {
|
|
26
|
-
return () => {
|
|
27
|
-
cancelled = true;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
25
|
const onCred = () => void refresh();
|
|
31
26
|
const onStorage = (e) => {
|
|
32
27
|
if (e.key === AUTH_CREDENTIALS_STORAGE_KEY || e.key === null)
|
|
33
28
|
void refresh();
|
|
34
29
|
};
|
|
35
|
-
window
|
|
36
|
-
|
|
30
|
+
const hasWindow = typeof window !== 'undefined' &&
|
|
31
|
+
typeof window.addEventListener === 'function';
|
|
32
|
+
if (hasWindow) {
|
|
33
|
+
window.addEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
|
|
34
|
+
window.addEventListener('storage', onStorage);
|
|
35
|
+
}
|
|
36
|
+
// No window (React Native): the store notifies listeners directly.
|
|
37
|
+
const unsubscribe = hasWindow
|
|
38
|
+
? undefined
|
|
39
|
+
: subscribeCredentialsChanged(onCred);
|
|
37
40
|
return () => {
|
|
38
41
|
cancelled = true;
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
unsubscribe?.();
|
|
43
|
+
if (hasWindow) {
|
|
44
|
+
window.removeEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
|
|
45
|
+
window.removeEventListener('storage', onStorage);
|
|
46
|
+
}
|
|
41
47
|
};
|
|
42
48
|
}, [credentialsStore]);
|
|
43
49
|
return useMemo(() => ({ profile, account, scopes }), [profile, account, scopes]);
|
|
@@ -12,13 +12,14 @@ export declare const endOfMonth: (year: number, month: number) => Date;
|
|
|
12
12
|
export declare const monthCells: (year: number, month: number) => MonthCell[];
|
|
13
13
|
export declare const eventStartIso: (post: PublicPost) => string | undefined;
|
|
14
14
|
export declare const eventEndIso: (post: PublicPost) => string | undefined;
|
|
15
|
-
/**
|
|
16
|
-
* Local calendar days an agenda row occupies.
|
|
17
|
-
* Agenda feeds return one occurrence per event, so a series shows on that
|
|
18
|
-
* occurrence only.
|
|
19
|
-
*/
|
|
15
|
+
/** Local calendar days one occurrence occupies. */
|
|
20
16
|
export declare const eventDayKeys: (post: PublicPost) => string[];
|
|
21
17
|
export declare const uniquePosts: (pages: PublicPost[][] | undefined) => PublicPost[];
|
|
18
|
+
/**
|
|
19
|
+
* Agenda feeds return one row per series. Place every occurrence that belongs
|
|
20
|
+
* in this window on its own date. Paging still uses the original rows.
|
|
21
|
+
*/
|
|
22
|
+
export declare const expandRecurringAgendaPosts: (posts: PublicPost[], window: EventsAgendaWindow, now?: Date) => PublicPost[];
|
|
22
23
|
export declare const groupPostsByDay: (posts: PublicPost[]) => Map<string, PublicPost[]>;
|
|
23
24
|
/**
|
|
24
25
|
* Upcoming pages are oldest-first and past pages are newest-first. Stop once
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { occurrencesForIndex, } from '@openpeepshq/common/lib';
|
|
1
2
|
const MAX_SPAN_DAYS = 370;
|
|
3
|
+
/** Matches the agenda: a missing end is one hour after start. */
|
|
4
|
+
const IMPLIED_EVENT_DURATION_MS = 60 * 60 * 1000;
|
|
2
5
|
export const localDateKey = (date) => {
|
|
3
6
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
4
7
|
const day = String(date.getDate()).padStart(2, '0');
|
|
@@ -54,11 +57,7 @@ export const eventEndIso = (post) => {
|
|
|
54
57
|
return undefined;
|
|
55
58
|
return post.occurrenceEnd ?? post.data.end;
|
|
56
59
|
};
|
|
57
|
-
/**
|
|
58
|
-
* Local calendar days an agenda row occupies.
|
|
59
|
-
* Agenda feeds return one occurrence per event, so a series shows on that
|
|
60
|
-
* occurrence only.
|
|
61
|
-
*/
|
|
60
|
+
/** Local calendar days one occurrence occupies. */
|
|
62
61
|
export const eventDayKeys = (post) => {
|
|
63
62
|
if (post.data.type !== 'event')
|
|
64
63
|
return [];
|
|
@@ -101,6 +100,48 @@ export const uniquePosts = (pages) => {
|
|
|
101
100
|
}
|
|
102
101
|
return out;
|
|
103
102
|
};
|
|
103
|
+
const occurrenceMatchesAgenda = (occurrence, window, now) => {
|
|
104
|
+
const startMs = new Date(occurrence.start).getTime();
|
|
105
|
+
if (!Number.isFinite(startMs))
|
|
106
|
+
return false;
|
|
107
|
+
const explicitEndMs = occurrence.end
|
|
108
|
+
? new Date(occurrence.end).getTime()
|
|
109
|
+
: Number.NaN;
|
|
110
|
+
const endMs = Number.isFinite(explicitEndMs)
|
|
111
|
+
? explicitEndMs
|
|
112
|
+
: startMs + IMPLIED_EVENT_DURATION_MS;
|
|
113
|
+
const nowMs = now.getTime();
|
|
114
|
+
if (window === 'past')
|
|
115
|
+
return endMs < nowMs;
|
|
116
|
+
if (window === 'current')
|
|
117
|
+
return startMs <= nowMs && endMs >= nowMs;
|
|
118
|
+
return (startMs > nowMs || (Number.isFinite(explicitEndMs) && explicitEndMs > nowMs));
|
|
119
|
+
};
|
|
120
|
+
const postForOccurrence = (post, occurrence) => {
|
|
121
|
+
const { occurrenceEnd: _previousEnd, ...rest } = post;
|
|
122
|
+
return {
|
|
123
|
+
...rest,
|
|
124
|
+
occurrenceRecurrenceId: occurrence.recurrenceId,
|
|
125
|
+
occurrenceStart: occurrence.start,
|
|
126
|
+
...(occurrence.end ? { occurrenceEnd: occurrence.end } : {}),
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Agenda feeds return one row per series. Place every occurrence that belongs
|
|
131
|
+
* in this window on its own date. Paging still uses the original rows.
|
|
132
|
+
*/
|
|
133
|
+
export const expandRecurringAgendaPosts = (posts, window, now = new Date()) => {
|
|
134
|
+
const rows = posts.flatMap((post) => {
|
|
135
|
+
if (post.data.type !== 'event' || !post.data.recurrence)
|
|
136
|
+
return [post];
|
|
137
|
+
const occurrences = occurrencesForIndex(post.data, now).filter((occurrence) => !occurrence.cancelled &&
|
|
138
|
+
occurrenceMatchesAgenda(occurrence, window, now));
|
|
139
|
+
if (occurrences.length === 0)
|
|
140
|
+
return [post];
|
|
141
|
+
return occurrences.map((occurrence) => postForOccurrence(post, occurrence));
|
|
142
|
+
});
|
|
143
|
+
return uniquePosts([rows]);
|
|
144
|
+
};
|
|
104
145
|
export const groupPostsByDay = (posts) => {
|
|
105
146
|
const map = new Map();
|
|
106
147
|
for (const post of posts) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
-
import { endOfMonth, eventStartIso, groupPostsByDay, localDateKey, monthCells, shouldFetchMoreAgenda, startOfMonth, uniquePosts, } from '../../components/post/feed/events/eventCalendar.js';
|
|
2
|
+
import { endOfMonth, eventStartIso, expandRecurringAgendaPosts, groupPostsByDay, localDateKey, monthCells, shouldFetchMoreAgenda, startOfMonth, uniquePosts, } from '../../components/post/feed/events/eventCalendar.js';
|
|
3
3
|
const WEEKDAY_START = new Date(2026, 0, 4);
|
|
4
4
|
export const eventName = (post) => post.data.type === 'event' ? post.data.name?.trim() || '-' : '-';
|
|
5
5
|
export const dayNumber = (key) => String(Number(key.slice(8, 10)));
|
|
@@ -16,7 +16,8 @@ export const useEventsCalendar = (query, agenda) => {
|
|
|
16
16
|
});
|
|
17
17
|
const [selectedKey, setSelectedKey] = useState(localDateKey(initial));
|
|
18
18
|
const posts = useMemo(() => uniquePosts(query.data?.pages), [query.data?.pages]);
|
|
19
|
-
const
|
|
19
|
+
const calendarPosts = useMemo(() => expandRecurringAgendaPosts(posts, agenda), [agenda, posts]);
|
|
20
|
+
const byDay = useMemo(() => groupPostsByDay(calendarPosts), [calendarPosts]);
|
|
20
21
|
const cells = useMemo(() => monthCells(cursor.year, cursor.month), [cursor.year, cursor.month]);
|
|
21
22
|
const inMonthKeys = cells
|
|
22
23
|
.filter((cell) => cell.inMonth)
|
|
@@ -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,
|
|
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) =>
|
|
28
|
-
queryKey
|
|
29
|
-
|
|
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
|
-
|
|
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,
|
|
34
|
-
await queryClient.invalidateQueries(
|
|
35
|
-
|
|
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({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openpeepshq/react",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"uuidv7": "^1.0.2",
|
|
53
53
|
"@openpeepshq/client": "^0.2.0",
|
|
54
54
|
"@openpeepshq/common": "^0.2.0",
|
|
55
|
-
"@openpeepshq/
|
|
56
|
-
"@openpeepshq/
|
|
55
|
+
"@openpeepshq/react-ui": "^0.2.0",
|
|
56
|
+
"@openpeepshq/greenscreen": "^0.2.0"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"react": "^18.3.0 || ^19.0.0",
|
|
@@ -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
|
|
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
|
-
|
|
96
|
-
window.alert(
|
|
103
|
+
toastError(
|
|
97
104
|
t('groups.markAllRead.error', {
|
|
98
105
|
defaultValue: 'Failed to mark group posts as read',
|
|
99
106
|
}),
|
|
@@ -10,6 +10,7 @@ import { parseScopesFromJwt } from '@openpeepshq/common';
|
|
|
10
10
|
import {
|
|
11
11
|
AUTH_CREDENTIALS_STORAGE_KEY,
|
|
12
12
|
OPENPEEPS_CREDENTIALS_CHANGED_EVENT,
|
|
13
|
+
subscribeCredentialsChanged,
|
|
13
14
|
} from '../../auth/credentials';
|
|
14
15
|
import { useCredentialsStore } from '../../contexts/credentialsStore';
|
|
15
16
|
|
|
@@ -46,23 +47,32 @@ export const useAuthData = (): AuthorizationData => {
|
|
|
46
47
|
|
|
47
48
|
void refresh();
|
|
48
49
|
|
|
49
|
-
if (typeof window === 'undefined') {
|
|
50
|
-
return () => {
|
|
51
|
-
cancelled = true;
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
50
|
const onCred = () => void refresh();
|
|
56
51
|
const onStorage = (e: StorageEvent) => {
|
|
57
52
|
if (e.key === AUTH_CREDENTIALS_STORAGE_KEY || e.key === null)
|
|
58
53
|
void refresh();
|
|
59
54
|
};
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
|
|
56
|
+
const hasWindow =
|
|
57
|
+
typeof window !== 'undefined' &&
|
|
58
|
+
typeof window.addEventListener === 'function';
|
|
59
|
+
|
|
60
|
+
if (hasWindow) {
|
|
61
|
+
window.addEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
|
|
62
|
+
window.addEventListener('storage', onStorage);
|
|
63
|
+
}
|
|
64
|
+
// No window (React Native): the store notifies listeners directly.
|
|
65
|
+
const unsubscribe = hasWindow
|
|
66
|
+
? undefined
|
|
67
|
+
: subscribeCredentialsChanged(onCred);
|
|
68
|
+
|
|
62
69
|
return () => {
|
|
63
70
|
cancelled = true;
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
unsubscribe?.();
|
|
72
|
+
if (hasWindow) {
|
|
73
|
+
window.removeEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
|
|
74
|
+
window.removeEventListener('storage', onStorage);
|
|
75
|
+
}
|
|
66
76
|
};
|
|
67
77
|
}, [credentialsStore]);
|
|
68
78
|
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
occurrencesForIndex,
|
|
3
|
+
type ExpandedOccurrence,
|
|
4
|
+
} from '@openpeepshq/common/lib';
|
|
1
5
|
import type { PublicPost } from '@openpeepshq/common/types';
|
|
2
6
|
|
|
3
7
|
export type EventsAgendaWindow = 'upcoming' | 'past' | 'current';
|
|
@@ -8,6 +12,8 @@ export type MonthCell = {
|
|
|
8
12
|
};
|
|
9
13
|
|
|
10
14
|
const MAX_SPAN_DAYS = 370;
|
|
15
|
+
/** Matches the agenda: a missing end is one hour after start. */
|
|
16
|
+
const IMPLIED_EVENT_DURATION_MS = 60 * 60 * 1000;
|
|
11
17
|
|
|
12
18
|
export const localDateKey = (date: Date): string => {
|
|
13
19
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
@@ -78,11 +84,7 @@ export const eventEndIso = (post: PublicPost): string | undefined => {
|
|
|
78
84
|
return post.occurrenceEnd ?? post.data.end;
|
|
79
85
|
};
|
|
80
86
|
|
|
81
|
-
/**
|
|
82
|
-
* Local calendar days an agenda row occupies.
|
|
83
|
-
* Agenda feeds return one occurrence per event, so a series shows on that
|
|
84
|
-
* occurrence only.
|
|
85
|
-
*/
|
|
87
|
+
/** Local calendar days one occurrence occupies. */
|
|
86
88
|
export const eventDayKeys = (post: PublicPost): string[] => {
|
|
87
89
|
if (post.data.type !== 'event') return [];
|
|
88
90
|
const startIso = eventStartIso(post);
|
|
@@ -131,6 +133,62 @@ export const uniquePosts = (
|
|
|
131
133
|
return out;
|
|
132
134
|
};
|
|
133
135
|
|
|
136
|
+
const occurrenceMatchesAgenda = (
|
|
137
|
+
occurrence: Pick<ExpandedOccurrence, 'start' | 'end'>,
|
|
138
|
+
window: EventsAgendaWindow,
|
|
139
|
+
now: Date,
|
|
140
|
+
): boolean => {
|
|
141
|
+
const startMs = new Date(occurrence.start).getTime();
|
|
142
|
+
if (!Number.isFinite(startMs)) return false;
|
|
143
|
+
const explicitEndMs = occurrence.end
|
|
144
|
+
? new Date(occurrence.end).getTime()
|
|
145
|
+
: Number.NaN;
|
|
146
|
+
const endMs = Number.isFinite(explicitEndMs)
|
|
147
|
+
? explicitEndMs
|
|
148
|
+
: startMs + IMPLIED_EVENT_DURATION_MS;
|
|
149
|
+
const nowMs = now.getTime();
|
|
150
|
+
if (window === 'past') return endMs < nowMs;
|
|
151
|
+
if (window === 'current') return startMs <= nowMs && endMs >= nowMs;
|
|
152
|
+
return (
|
|
153
|
+
startMs > nowMs || (Number.isFinite(explicitEndMs) && explicitEndMs > nowMs)
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const postForOccurrence = (
|
|
158
|
+
post: PublicPost,
|
|
159
|
+
occurrence: ExpandedOccurrence,
|
|
160
|
+
): PublicPost => {
|
|
161
|
+
const { occurrenceEnd: _previousEnd, ...rest } = post;
|
|
162
|
+
return {
|
|
163
|
+
...rest,
|
|
164
|
+
occurrenceRecurrenceId: occurrence.recurrenceId,
|
|
165
|
+
occurrenceStart: occurrence.start,
|
|
166
|
+
...(occurrence.end ? { occurrenceEnd: occurrence.end } : {}),
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Agenda feeds return one row per series. Place every occurrence that belongs
|
|
172
|
+
* in this window on its own date. Paging still uses the original rows.
|
|
173
|
+
*/
|
|
174
|
+
export const expandRecurringAgendaPosts = (
|
|
175
|
+
posts: PublicPost[],
|
|
176
|
+
window: EventsAgendaWindow,
|
|
177
|
+
now = new Date(),
|
|
178
|
+
): PublicPost[] => {
|
|
179
|
+
const rows = posts.flatMap((post) => {
|
|
180
|
+
if (post.data.type !== 'event' || !post.data.recurrence) return [post];
|
|
181
|
+
const occurrences = occurrencesForIndex(post.data, now).filter(
|
|
182
|
+
(occurrence) =>
|
|
183
|
+
!occurrence.cancelled &&
|
|
184
|
+
occurrenceMatchesAgenda(occurrence, window, now),
|
|
185
|
+
);
|
|
186
|
+
if (occurrences.length === 0) return [post];
|
|
187
|
+
return occurrences.map((occurrence) => postForOccurrence(post, occurrence));
|
|
188
|
+
});
|
|
189
|
+
return uniquePosts([rows]);
|
|
190
|
+
};
|
|
191
|
+
|
|
134
192
|
export const groupPostsByDay = (
|
|
135
193
|
posts: PublicPost[],
|
|
136
194
|
): Map<string, PublicPost[]> => {
|
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
import {
|
|
11
11
|
endOfMonth,
|
|
12
12
|
eventStartIso,
|
|
13
|
+
expandRecurringAgendaPosts,
|
|
13
14
|
groupPostsByDay,
|
|
14
15
|
localDateKey,
|
|
15
16
|
monthCells,
|
|
@@ -58,7 +59,11 @@ export const useEventsCalendar = (
|
|
|
58
59
|
() => uniquePosts(query.data?.pages),
|
|
59
60
|
[query.data?.pages],
|
|
60
61
|
);
|
|
61
|
-
const
|
|
62
|
+
const calendarPosts = useMemo(
|
|
63
|
+
() => expandRecurringAgendaPosts(posts, agenda),
|
|
64
|
+
[agenda, posts],
|
|
65
|
+
);
|
|
66
|
+
const byDay = useMemo(() => groupPostsByDay(calendarPosts), [calendarPosts]);
|
|
62
67
|
const cells = useMemo(
|
|
63
68
|
() => monthCells(cursor.year, cursor.month),
|
|
64
69
|
[cursor.year, cursor.month],
|
|
@@ -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 = (
|
|
46
|
-
queryKey[
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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.
|
|
66
|
-
|
|
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 (
|