@openpeepshq/react 0.3.0 → 0.3.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.
@@ -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.addEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
36
- window.addEventListener('storage', onStorage);
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
- window.removeEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
40
- window.removeEventListener('storage', onStorage);
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 byDay = useMemo(() => groupPostsByDay(posts), [posts]);
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/react",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -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
- window.addEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
61
- window.addEventListener('storage', onStorage);
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
- window.removeEventListener(OPENPEEPS_CREDENTIALS_CHANGED_EVENT, onCred);
65
- window.removeEventListener('storage', onStorage);
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 byDay = useMemo(() => groupPostsByDay(posts), [posts]);
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],