@pushwoosh/websdk-common 6.30.1 → 6.31.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/websdk-common",
3
- "version": "6.30.1",
3
+ "version": "6.31.0",
4
4
  "description": "Code shared by the Pushwoosh WebSDK and the Control Panel, one entry point per module (web-popups: web popup model and renderer; subscription-widget: push subscription widget model and renderer; inbox: web inbox widget and inbox cell previews)",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -34,8 +34,8 @@
34
34
  "@types/react-dom": "^19.3.0",
35
35
  "jsdom": "^30.1.3",
36
36
  "preact": "^10.29.8",
37
- "react": "^19.3.0",
38
- "react-dom": "^19.3.0",
37
+ "react": "19.3.0",
38
+ "react-dom": "19.3.0",
39
39
  "typescript": "^6.0.3",
40
40
  "vitest": "^4.1.12"
41
41
  },
@@ -10,6 +10,7 @@ import { webPopupBackgroundImageStyle, webPopupContainerStyle, webPopupScrimGrad
10
10
  import { resolveWebPopupBoxChrome, resolveWebPopupColors, webPopupColorAlpha, webPopupDarkEnabled, webPopupImageSrc } from './palette';
11
11
  import { ProductsSlotView, WebPopupProductsContext } from './productsSlot';
12
12
  import { WEB_POPUP_CLOSE_ATTRIBUTE } from './styles';
13
+ import { useWebPopupSubscribeSession, WebPopupSubscribeContext } from './subscribeSession';
13
14
  import { webPopupTimerLabels, webPopupTimerRemaining } from './timer';
14
15
  // Renders a popup from its model by interpreting each page's embedded layout
15
16
  // tree — the same node vocabulary the editor's canvas builds from: container
@@ -55,6 +56,7 @@ function ButtonSlotView({
55
56
  onSubscribeClose
56
57
  }) {
57
58
  const form = useContext(WebPopupFormSessionContext);
59
+ const subscribe = useContext(WebPopupSubscribeContext);
58
60
  const [askPhase, setAskPhase] = useState('idle');
59
61
  const pill = webPopupButtonPillStyle(slot.variant, colors);
60
62
  const wrapper = {};
@@ -86,8 +88,8 @@ function ButtonSlotView({
86
88
  action
87
89
  } = slot;
88
90
  // 'asking' guards the double click (one prompt, one registerDevice);
89
- // 'denied' stays disabled because the browser grants no second prompt.
90
- const spent = askPhase !== 'idle';
91
+ // an answered ask stays disabled because the browser grants no second prompt.
92
+ const spent = askPhase !== 'idle' || subscribe.answered;
91
93
  const ask = () => {
92
94
  if (!onRequestSubscribe || spent) return;
93
95
  setAskPhase('asking');
@@ -96,11 +98,14 @@ function ButtonSlotView({
96
98
  setAskPhase('idle');
97
99
  return;
98
100
  }
99
- if (applyWebPopupSubscribeOutcome(action, result, logger)) {
101
+ subscribe.markAnswered();
102
+ const next = applyWebPopupSubscribeOutcome(action, result, logger);
103
+ if (next === 'close') {
100
104
  onSubscribeClose?.();
101
105
  return;
102
106
  }
103
- setAskPhase('denied');
107
+ setAskPhase(result);
108
+ if (next !== 'stay') onPageChange(next.page);
104
109
  }).catch(() => setAskPhase('idle'));
105
110
  };
106
111
  return _jsx("div", {
@@ -1233,6 +1238,7 @@ export function WebPopupBox({
1233
1238
  logger
1234
1239
  }) {
1235
1240
  const formSession = useWebPopupFormSession(params, page.key, onPageChange, onSubmitFormValues);
1241
+ const subscribeSession = useWebPopupSubscribeSession();
1236
1242
  const colors = resolveWebPopupColors(params, colorScheme);
1237
1243
  const box = resolveWebPopupBoxChrome(params, colorScheme);
1238
1244
  const style = {
@@ -1272,25 +1278,28 @@ export function WebPopupBox({
1272
1278
  value: formSession,
1273
1279
  children: _jsx(WebPopupProductsContext.Provider, {
1274
1280
  value: products,
1275
- children: _jsx("div", {
1276
- style: pageStyle,
1277
- children: _jsx(LayoutNodeView, {
1278
- node: page.layout,
1279
- page: page,
1280
- colors: colors,
1281
- mode: mode,
1282
- scheme: colors.scheme,
1283
- leaf: leafRenderers,
1284
- onRequestClose: onRequestClose,
1285
- onPageChange: onPageChange ?? (() => {}),
1286
- onSubmitForm: onSubmitForm,
1287
- onSubmitChoice: onSubmitChoice,
1288
- onSubmitRating: onSubmitRating,
1289
- onRequestSubscribe: onRequestSubscribe,
1290
- onSubscribeClose: onSubscribeClose,
1291
- logger: logger
1292
- })
1293
- }, page.key)
1281
+ children: _jsx(WebPopupSubscribeContext.Provider, {
1282
+ value: subscribeSession,
1283
+ children: _jsx("div", {
1284
+ style: pageStyle,
1285
+ children: _jsx(LayoutNodeView, {
1286
+ node: page.layout,
1287
+ page: page,
1288
+ colors: colors,
1289
+ mode: mode,
1290
+ scheme: colors.scheme,
1291
+ leaf: leafRenderers,
1292
+ onRequestClose: onRequestClose,
1293
+ onPageChange: onPageChange ?? (() => {}),
1294
+ onSubmitForm: onSubmitForm,
1295
+ onSubmitChoice: onSubmitChoice,
1296
+ onSubmitRating: onSubmitRating,
1297
+ onRequestSubscribe: onRequestSubscribe,
1298
+ onSubscribeClose: onSubscribeClose,
1299
+ logger: logger
1300
+ })
1301
+ }, page.key)
1302
+ })
1294
1303
  })
1295
1304
  })
1296
1305
  });
@@ -10,13 +10,17 @@ export declare function webPopupLinkTargetAttrs(target: string | null | undefine
10
10
  target: '_blank' | '_self';
11
11
  rel?: 'noreferrer';
12
12
  };
13
+ /** What the content does after a subscribe answer: close, stay as it is, or turn to a page. */
14
+ export type WebPopupSubscribeNext = 'close' | 'stay' | {
15
+ page: string;
16
+ };
13
17
  /**
14
- * Carries out a subscribe button's setting for the visitor's answer; true = the content closes.
18
+ * Carries out a subscribe button's setting for the visitor's answer.
15
19
  * A new tab opens with noopener, so `window.open` returns null and a blocked tab goes unnoticed.
16
20
  */
17
21
  export declare function applyWebPopupSubscribeOutcome(action: Extract<WebPopupButtonAction, {
18
22
  type: 'subscribe';
19
- }>, result: 'granted' | 'denied', logger: WebPopupLogger | undefined): boolean;
23
+ }>, result: 'granted' | 'denied', logger: WebPopupLogger | undefined): WebPopupSubscribeNext;
20
24
  /**
21
25
  * Sanitize a text slot's html before it's injected into the page. The editor
22
26
  * only ever writes inline formatting, but the json travels through a public
@@ -27,15 +27,18 @@ export function webPopupLinkTargetAttrs(target) {
27
27
  };
28
28
  }
29
29
  /**
30
- * Carries out a subscribe button's setting for the visitor's answer; true = the content closes.
30
+ * Carries out a subscribe button's setting for the visitor's answer.
31
31
  * A new tab opens with noopener, so `window.open` returns null and a blocked tab goes unnoticed.
32
32
  */
33
33
  export function applyWebPopupSubscribeOutcome(action, result, logger) {
34
34
  const outcome = result === 'granted' ? action.onGranted : action.onDenied;
35
35
  if (outcome?.type === 'href') openWebPopupSubscribeLink(outcome, logger);
36
+ if (outcome?.type === 'page') return {
37
+ page: outcome.page
38
+ };
36
39
  // Granted always closes, as before the setting; denied closes only when set to.
37
- if (result === 'granted') return true;
38
- return outcome?.type === 'close';
40
+ if (result === 'granted' || outcome?.type === 'close') return 'close';
41
+ return 'stay';
39
42
  }
40
43
  function openWebPopupSubscribeLink(outcome, logger) {
41
44
  const href = typeof outcome.url === 'string' ? safeUrl(outcome.url) : undefined;
@@ -0,0 +1,9 @@
1
+ /** Whether the visitor has answered a subscribe prompt in this box; it outlives page switches. */
2
+ export type WebPopupSubscribeSession = {
3
+ answered: boolean;
4
+ markAnswered: () => void;
5
+ };
6
+ /** What a subscribe button reads; a slot outside any WebPopupBox gets this inert session. */
7
+ export declare const WebPopupSubscribeContext: import("react").Context<WebPopupSubscribeSession>;
8
+ /** The subscribe session a WebPopupBox owns, so a button met again after a page turn stays spent. */
9
+ export declare function useWebPopupSubscribeSession(): WebPopupSubscribeSession;
@@ -0,0 +1,14 @@
1
+ import { createContext, useMemo, useState } from 'react';
2
+ /** What a subscribe button reads; a slot outside any WebPopupBox gets this inert session. */
3
+ export const WebPopupSubscribeContext = createContext({
4
+ answered: false,
5
+ markAnswered: () => {}
6
+ });
7
+ /** The subscribe session a WebPopupBox owns, so a button met again after a page turn stays spent. */
8
+ export function useWebPopupSubscribeSession() {
9
+ const [answered, setAnswered] = useState(false);
10
+ return useMemo(() => ({
11
+ answered,
12
+ markAnswered: () => setAnswered(true)
13
+ }), [answered]);
14
+ }
@@ -19,7 +19,8 @@ export type WebPopupButtonAction =
19
19
  type: 'page';
20
20
  page: string;
21
21
  }
22
- /** Ask for push permission; without `onGranted` / `onDenied` 'granted' closes and 'denied' disables the button. */
22
+ /** Ask for push permission; without `onGranted` / `onDenied` 'granted' closes and 'denied' disables the button,
23
+ * a `page` outcome turns to that page instead and keeps the content open. */
23
24
  | {
24
25
  type: 'subscribe';
25
26
  onGranted?: WebPopupSubscribeOutcome;
@@ -27,7 +28,7 @@ export type WebPopupButtonAction =
27
28
  };
28
29
  /** What the subscribe button does once the visitor answered. */
29
30
  export type WebPopupSubscribeOutcome = Extract<WebPopupButtonAction, {
30
- type: 'close' | 'href';
31
+ type: 'close' | 'href' | 'page';
31
32
  }>;
32
33
  /** What the host's subscribe handler settled on — the browser's permission after the ask. */
33
34
  export type WebPopupSubscribeResult = 'granted' | 'denied' | 'default';