beeper-inbox-react 0.3.1 → 0.5.0

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/README.md CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  React embeddable inbox widget for Beeper.
4
4
 
5
- The widget resolves the realtime Convex backend from Beeper's `/widget/config` endpoint.
6
- Host apps do not pass a `convexUrl`.
5
+ The widget gets its realtime endpoint from Beeper's `/widget/config` endpoint: a WebSocket
6
+ (`realtime: { kind: "ws", url }`) on the current backend, or a Convex URL on older deployments.
7
+ Host apps pass neither. Without one, or if the socket fails, the inbox polls every 15 s.
7
8
 
8
9
  ## Public API
9
10
 
@@ -29,11 +30,11 @@ The provider talks to **two** origins:
29
30
 
30
31
  If you override `baseUrl`, it must be an origin serving Beeper widget REST routes.
31
32
 
32
- In this repo’s current backend architecture, that override points at the Convex HTTP deployment (`…convex.site`) and must support CORS preflight on `/api/v1/`.
33
+ In this repo that is the public API (`api.<domain>`, the `beeper-api` Worker), which handles CORS preflight on `/api/v1/` and serves the realtime socket at `/api/v1/projects/:projectId/widget/inbox/ws`.
33
34
 
34
35
  ### Why `POST /api/beeper/widget-session` runs often
35
36
 
36
- That call **mints** the short-lived `x-beeper-session-token`. It runs when there is no token yet, before inbox/read actions, and when refreshing an expiring session. If it runs in a tight loop, the mint endpoint is probably failing (check **503** / JSON `error` and server env `BEEPER_SERVER_KEY`, `CONVEX_SITE_URL`).
37
+ That call **mints** the short-lived `x-beeper-session-token`. It runs when there is no token yet, before inbox/read actions, and when refreshing an expiring session. If it runs in a tight loop, the mint endpoint is probably failing (check **503** / JSON `error` and server env `BEEPER_SERVER_KEY`).
37
38
 
38
39
  ## Install
39
40
 
@@ -123,6 +124,12 @@ as the page (resolved with `new URL(sessionEndpoint, window.location.origin)`),
123
124
  Your backend should mint a token with the **server** API key and return either
124
125
  `{ widgetSessionToken }` or `{ data: { widgetSessionToken } }` (matching the Beeper REST shape).
125
126
 
127
+ The minted token is kept in `sessionStorage` for that project and subscriber and reused on later
128
+ page loads in the same tab until it is about to expire, so a reload doesn't mint a new session.
129
+ It never outlives the tab. Pass `persistSession={false}` to mint on every load instead. A token
130
+ passed as `sessionToken` is used as given and never stored. The widget config (look and feel
131
+ from the dashboard) is reused the same way for up to 10 minutes.
132
+
126
133
  ### Beeper web app (`apps/web`)
127
134
 
128
135
  The Beeper dashboard mints sessions for its own inbox at `POST /api/internal/inbox-session`
@@ -131,7 +138,7 @@ It serves only the dashboard's own project. Configure server env:
131
138
 
132
139
  - `BEEPER_SERVER_KEY` — project API key with `widget:session:create` or `ingest:write`
133
140
  - `BEEPER_PROJECT_ID` (or `VITE_BEEPER_PROJECT_ID`) — the project it mints for; the request body is ignored
134
- - `CONVEX_SITE_URL` or `VITE_CONVEX_SITE_URL` — only needed by the server-side session proxy in this repo
141
+ - The API service binding `API` (or `API_URL` in local dev) — how the route reaches the Beeper API
135
142
 
136
143
  ### `BeeperInboxBell`
137
144
 
@@ -166,6 +173,16 @@ add no requests of their own:
166
173
  - `useBeeperInbox()` exposes `activeTab`, `setActiveTab`, `tabUnreadCount(tab)`, `unreadByKind`,
167
174
  `archive(id)` and `unarchive(id)` for a custom UI.
168
175
 
176
+ ### What loads when
177
+
178
+ While the panel is closed, the provider keeps only the unread counts current (one small read).
179
+ The list loads when the panel opens, 20 items at a time, with a **Show older** button for the
180
+ rest (`hasMore`, `loadingMore` and `loadMore()` in `useBeeperInbox()`).
181
+
182
+ A component calling `useBeeperInbox()` keeps the list loaded while it is mounted, so a custom
183
+ panel gets `items` as before. For a custom badge, use `useBeeperInboxUnreadCount()`, which reads
184
+ only `unreadCount`, `unreadByKind` and `tabUnreadCount` and doesn't load the list.
185
+
169
186
  ## Web push token registration (FCM)
170
187
 
171
188
  After you obtain an FCM token in your app, pass it to the provider so the widget
package/dist/index.d.ts CHANGED
@@ -1,6 +1,19 @@
1
- import { type CSSProperties, type ReactNode, type RefObject } from "react";
2
- export type BeeperInboxTheme = "light" | "dark" | "system";
3
- export interface BeeperInboxCustomization {
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CSSProperties, RefObject, ReactNode } from 'react';
3
+
4
+ /**
5
+ * The WebSocket realtime client, kept free of runtime imports so the React
6
+ * widget can bundle it without pulling in `beeper-sdk` or `convex`.
7
+ */
8
+
9
+ /** The subset of a Convex client the inbox subscribes through. */
10
+ interface InboxRealtimeClient {
11
+ onUpdate(query: string, args: Record<string, unknown>, onResult: (result: unknown) => void, onError: (error: Error) => void): () => void;
12
+ close(): void | Promise<void>;
13
+ }
14
+
15
+ type BeeperInboxTheme = "light" | "dark" | "system";
16
+ interface BeeperInboxCustomization {
4
17
  accentColor?: string;
5
18
  radius?: number;
6
19
  fontScale?: number;
@@ -15,19 +28,19 @@ export interface BeeperInboxCustomization {
15
28
  * One tab of the panel. `kind` limits it to items whose `metadata.kind` is one
16
29
  * of these; omit it for an "all" tab. The tab's badge counts its kinds' unread.
17
30
  */
18
- export interface BeeperInboxTab {
31
+ interface BeeperInboxTab {
19
32
  key: string;
20
33
  label: string;
21
34
  kind?: string | string[];
22
35
  }
23
- export interface BeeperInboxSubscriber {
36
+ interface BeeperInboxSubscriber {
24
37
  subscriberId: string;
25
38
  displayName?: string;
26
39
  email?: string;
27
40
  avatarUrl?: string;
28
41
  metadata?: Record<string, unknown>;
29
42
  }
30
- export interface BeeperInboxPushRegistration {
43
+ interface BeeperInboxPushRegistration {
31
44
  token?: string;
32
45
  platform?: "web" | "ios" | "android";
33
46
  deviceLabel?: string;
@@ -35,7 +48,7 @@ export interface BeeperInboxPushRegistration {
35
48
  autoRegister?: boolean;
36
49
  autoUnregister?: boolean;
37
50
  }
38
- export interface BeeperInboxProviderProps {
51
+ interface BeeperInboxProviderProps {
39
52
  /**
40
53
  * Optional advanced override for the Beeper Widget API origin.
41
54
  * Defaults to the hosted Beeper API. Most host apps should not pass this.
@@ -47,6 +60,12 @@ export interface BeeperInboxProviderProps {
47
60
  getSessionToken?: (subscriber: BeeperInboxSubscriber) => Promise<string>;
48
61
  sessionEndpoint?: string;
49
62
  sessionRequestHeaders?: Record<string, string>;
63
+ /**
64
+ * Reuse a minted session across page loads in the same tab (kept in
65
+ * sessionStorage until it is about to expire), instead of minting one on
66
+ * every load. Ignored when `sessionToken` is passed. Default true.
67
+ */
68
+ persistSession?: boolean;
50
69
  subscriber: BeeperInboxSubscriber;
51
70
  theme?: BeeperInboxTheme;
52
71
  customization?: Partial<BeeperInboxCustomization>;
@@ -69,7 +88,7 @@ export interface BeeperInboxProviderProps {
69
88
  defaultTab?: string;
70
89
  children: ReactNode;
71
90
  }
72
- export type BeeperInboxEvent = {
91
+ type BeeperInboxEvent = {
73
92
  type: "session.refresh.start";
74
93
  projectId: string;
75
94
  subscriberId: string;
@@ -134,6 +153,12 @@ interface InboxItem {
134
153
  actionUrl?: string;
135
154
  }
136
155
  interface WidgetConfig {
156
+ /** Realtime over the backend's WebSocket (current backend). Preferred. */
157
+ realtime?: {
158
+ kind: "ws";
159
+ url: string;
160
+ };
161
+ /** Realtime over a Convex subscription (older deployments). */
137
162
  convexUrl?: string;
138
163
  modeDefault: BeeperInboxTheme;
139
164
  accentColor: string;
@@ -166,6 +191,11 @@ interface BeeperInboxContextValue {
166
191
  unarchive: (inboxItemId: string) => Promise<void>;
167
192
  /** Unread per kind named by the `tabs`, each capped at 100. */
168
193
  unreadByKind: Record<string, number>;
194
+ /** Older items exist beyond `items`; `loadMore()` fetches the next page. */
195
+ hasMore: boolean;
196
+ loadingMore: boolean;
197
+ /** Appends the next older page. No-op when `hasMore` is false. */
198
+ loadMore: () => Promise<void>;
169
199
  tabs: BeeperInboxTab[];
170
200
  activeTab: string | null;
171
201
  setActiveTab: (key: string) => void;
@@ -178,6 +208,22 @@ interface BeeperInboxContextValue {
178
208
  /** Attach to the open button so the panel can anchor under it. */
179
209
  triggerRef: RefObject<HTMLElement | null>;
180
210
  }
211
+ type RealtimeSource = {
212
+ kind: "ws" | "convex";
213
+ url: string;
214
+ };
215
+ /** The config's realtime endpoint: the WebSocket when offered, else Convex. */
216
+ declare function pickRealtimeSource(config: Partial<WidgetConfig>): RealtimeSource | null;
217
+ /**
218
+ * One socket per subscription. The list view applies pushed items to its
219
+ * page; the unread-only view (bell without a panel) reloads its counts.
220
+ */
221
+ declare function createWidgetSocketClient(url: string, publishableKey: string, loaders: {
222
+ current: {
223
+ head: () => Promise<unknown>;
224
+ unread: () => Promise<unknown>;
225
+ };
226
+ }): InboxRealtimeClient;
181
227
  declare function parseTokenExpiryMs(token: string): number | null;
182
228
  declare function isSessionExpiringSoon(expiresAt: number | null, now?: number): boolean;
183
229
  declare function tabKinds(tab: BeeperInboxTab | undefined): string[];
@@ -185,9 +231,20 @@ declare function tabKinds(tab: BeeperInboxTab | undefined): string[];
185
231
  declare function countTabUnread(tab: BeeperInboxTab, unreadCount: number, unreadByKind: Record<string, number>): number;
186
232
  /** `&kind=` for the active tab and `&unreadKind=` for every tab's badge. */
187
233
  declare function buildKindsQuery(activeKinds: string[], unreadKinds: string[]): string;
188
- export declare function BeeperInboxProvider({ baseUrl, projectId, publishableKey, sessionToken, getSessionToken, sessionEndpoint, sessionRequestHeaders, subscriber, theme, customization, push, actionLinkTarget, onActionClick, pollIntervalMs, onEvent, tabs, defaultTab, children, }: BeeperInboxProviderProps): import("react/jsx-runtime").JSX.Element;
189
- export declare function useBeeperInbox(): BeeperInboxContextValue;
190
- export type BeeperInboxBellRenderProps = {
234
+ declare function BeeperInboxProvider({ baseUrl, projectId, publishableKey, sessionToken, getSessionToken, sessionEndpoint, sessionRequestHeaders, persistSession, subscriber, theme, customization, push, actionLinkTarget, onActionClick, pollIntervalMs, onEvent, tabs, defaultTab, children, }: BeeperInboxProviderProps): react_jsx_runtime.JSX.Element;
235
+ /**
236
+ * The inbox state and actions. A component using it keeps the item list
237
+ * loaded while mounted (a custom panel needs `items`); for a badge alone, use
238
+ * `useBeeperInboxUnreadCount`, which doesn't.
239
+ */
240
+ declare function useBeeperInbox(): BeeperInboxContextValue;
241
+ /** Unread counts only: doesn't make the provider load the item list. */
242
+ declare function useBeeperInboxUnreadCount(): {
243
+ unreadCount: number;
244
+ unreadByKind: Record<string, number>;
245
+ tabUnreadCount: (tab: BeeperInboxTab) => number;
246
+ };
247
+ type BeeperInboxBellRenderProps = {
191
248
  unreadCount: number;
192
249
  open: boolean;
193
250
  toggle: () => void;
@@ -195,7 +252,7 @@ export type BeeperInboxBellRenderProps = {
195
252
  fontScale: number;
196
253
  triggerRef: RefObject<HTMLElement | null>;
197
254
  };
198
- export interface BeeperInboxBellProps {
255
+ interface BeeperInboxBellProps {
199
256
  /**
200
257
  * `bell` — icon + optional count badge (default).
201
258
  * `minimal` — text label + count pill (previous default look).
@@ -210,22 +267,24 @@ export interface BeeperInboxBellProps {
210
267
  /** Full control over the trigger; not wrapped in an extra button. */
211
268
  renderTrigger?: (props: BeeperInboxBellRenderProps) => ReactNode;
212
269
  }
213
- export declare function BeeperInboxBell({ variant, label, className, style, "aria-label": ariaLabel, renderTrigger, }: BeeperInboxBellProps): import("react/jsx-runtime").JSX.Element;
214
- export interface BeeperInboxPanelProps {
270
+ declare function BeeperInboxBell({ variant, label, className, style, "aria-label": ariaLabel, renderTrigger, }: BeeperInboxBellProps): react_jsx_runtime.JSX.Element;
271
+ interface BeeperInboxPanelProps {
215
272
  /**
216
273
  * `anchor` — fixed under the bell / `triggerRef` (default).
217
274
  * `fixed-bottom-end` — previous viewport corner placement (e.g. floating widget).
218
275
  */
219
276
  placement?: "anchor" | "fixed-bottom-end";
220
277
  }
221
- export declare function BeeperInboxPanel({ placement, }?: BeeperInboxPanelProps): import("react/jsx-runtime").JSX.Element | null;
222
- export declare const __internal: {
278
+ declare function BeeperInboxPanel({ placement, }?: BeeperInboxPanelProps): react_jsx_runtime.JSX.Element | null;
279
+ declare const __internal: {
223
280
  DEFAULT_BEEPER_WIDGET_API_BASE_URL: string;
224
281
  tabKinds: typeof tabKinds;
225
282
  countTabUnread: typeof countTabUnread;
226
283
  buildKindsQuery: typeof buildKindsQuery;
227
284
  parseTokenExpiryMs: typeof parseTokenExpiryMs;
228
285
  isSessionExpiringSoon: typeof isSessionExpiringSoon;
286
+ pickRealtimeSource: typeof pickRealtimeSource;
287
+ createWidgetSocketClient: typeof createWidgetSocketClient;
229
288
  };
230
- export {};
231
- //# sourceMappingURL=index.d.ts.map
289
+
290
+ export { BeeperInboxBell, type BeeperInboxBellProps, type BeeperInboxBellRenderProps, type BeeperInboxCustomization, type BeeperInboxEvent, BeeperInboxPanel, type BeeperInboxPanelProps, BeeperInboxProvider, type BeeperInboxProviderProps, type BeeperInboxPushRegistration, type BeeperInboxSubscriber, type BeeperInboxTab, type BeeperInboxTheme, __internal, useBeeperInbox, useBeeperInboxUnreadCount };