@sensiblestats/widget-react 0.155.0 → 0.156.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @sensiblestats/widget-react
2
2
 
3
+ ## 0.156.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ebf1a14: Web parity with the mobile wrappers. widget-sdk exports `UnavailableReason`, `Teaser`, `TeaserPrice`, `PriceResult` and `observingFetch` (the mobile bridge types now come from it; `MobileTeaser`, `MobileTeaserPrice` and `MobilePriceResult` are removed from widget-react). widget-react adds `launcher: false` (hides the built-in launcher), `onTeaser(teaser | null)` and `onUnavailable(reason)` (`widget_disabled`, `auth_failed`) to `StatsWidgetConfig`, and `tapAsk`, `tapPrice` (resolves `PriceResult | null`), `tapCta`, `tapTeaser`, `dismissTeaser`, `teaserShown` to the `WidgetHost` from `createWidgetHost()`. A mounted `<StatsWidget>` now rebuilds its client, starts a fresh thread, and drops the previous visitor's pins and slip marks when `userContext.operatorUserId` or `anonymousUserId` changes. The teaser methods act only when the widget has `launcher: false`, and `onTeaser(null)` is delivered when the widget unmounts. widget-embed adds `data-launcher="false"`, the `sensiblestats:teaser` (`detail.teaser`) and `sensiblestats:unavailable` (`detail.reason`) window events, and the six teaser actions on the `init()` handle and the `window.SensibleStatsWidget` global.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [ebf1a14]
12
+ - @sensiblestats/widget-sdk@0.114.0
13
+
3
14
  ## 0.155.0
4
15
 
5
16
  ### Minor Changes
package/dist/index.cjs CHANGED
@@ -1422,7 +1422,7 @@ function parseCustomTotal(text2, format2) {
1422
1422
  }
1423
1423
 
1424
1424
  // src/version.generated.ts
1425
- var WIDGET_REACT_VERSION = "0.155.0";
1425
+ var WIDGET_REACT_VERSION = "0.156.0";
1426
1426
 
1427
1427
  // src/config.ts
1428
1428
  var COLOR_KEYS = ["accent", "cta", "surface", "ink"];
@@ -2841,7 +2841,7 @@ function isValidId(value) {
2841
2841
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
2842
2842
  }
2843
2843
  function createWidgetHost() {
2844
- const state = { queue: [], sink: null };
2844
+ const state = { queue: [], sink: null, teaser: null };
2845
2845
  const dispatch = (command) => {
2846
2846
  if (state.sink) {
2847
2847
  state.sink(command);
@@ -2887,11 +2887,49 @@ function createWidgetHost() {
2887
2887
  if (isValidId(id)) dispatch({ kind: "matchOpened", matchId: id });
2888
2888
  } catch {
2889
2889
  }
2890
+ },
2891
+ // A teaser tap is about what is on screen now, so it is never queued for a widget that is not there.
2892
+ tapAsk(index) {
2893
+ teaser(state, (t) => t.tapAsk(index));
2894
+ },
2895
+ tapPrice() {
2896
+ try {
2897
+ return state.teaser ? state.teaser.tapPrice().catch(() => null) : Promise.resolve(null);
2898
+ } catch {
2899
+ return Promise.resolve(null);
2900
+ }
2901
+ },
2902
+ tapCta() {
2903
+ teaser(state, (t) => t.tapCta());
2904
+ },
2905
+ tapTeaser() {
2906
+ teaser(state, (t) => t.tapTeaser());
2907
+ },
2908
+ dismissTeaser() {
2909
+ teaser(state, (t) => t.dismissTeaser());
2910
+ },
2911
+ teaserShown() {
2912
+ teaser(state, (t) => t.teaserShown());
2890
2913
  }
2891
2914
  };
2892
2915
  STATES.set(host, state);
2893
2916
  return host;
2894
2917
  }
2918
+ function teaser(state, call) {
2919
+ try {
2920
+ if (state.teaser) call(state.teaser);
2921
+ } catch {
2922
+ }
2923
+ }
2924
+ function attachTeaser(host, control) {
2925
+ const state = STATES.get(host);
2926
+ if (!state) return () => {
2927
+ };
2928
+ state.teaser = control;
2929
+ return () => {
2930
+ if (state.teaser === control) state.teaser = null;
2931
+ };
2932
+ }
2895
2933
  function attachHost(host, sink) {
2896
2934
  const state = STATES.get(host);
2897
2935
  if (!state) return () => {
@@ -3127,6 +3165,12 @@ function useHostEvents(opts) {
3127
3165
  const { host, operatorId, currentUser, logger, isOpen, closePanel } = opts;
3128
3166
  const [added, setAdded] = (0, import_react2.useState)({});
3129
3167
  const [settled, setSettled] = (0, import_react2.useState)({});
3168
+ const [marksUser, setMarksUser] = (0, import_react2.useState)(currentUser);
3169
+ if (marksUser !== currentUser) {
3170
+ setMarksUser(currentUser);
3171
+ setAdded({});
3172
+ setSettled({});
3173
+ }
3130
3174
  const markAdded = (0, import_react2.useCallback)((key) => {
3131
3175
  setAdded((prev) => prev[key] ? prev : { ...prev, [key]: true });
3132
3176
  }, []);
@@ -4742,7 +4786,13 @@ function phaseForPin(snapshot, pin) {
4742
4786
  function useLiveMonitor(opts) {
4743
4787
  const sessionIdRef = (0, import_react8.useRef)(crypto.randomUUID());
4744
4788
  const [pins, setPins] = (0, import_react8.useState)([]);
4745
- const [store] = (0, import_react8.useState)(() => createPinStore(opts.storageKey, sessionIdRef.current, setPins));
4789
+ const [owned, setOwned] = (0, import_react8.useState)(() => ({ key: opts.storageKey, store: createPinStore(opts.storageKey, sessionIdRef.current, setPins) }));
4790
+ let current = owned;
4791
+ if (owned.key !== opts.storageKey) {
4792
+ current = { key: opts.storageKey, store: createPinStore(opts.storageKey, sessionIdRef.current, setPins) };
4793
+ setOwned(current);
4794
+ }
4795
+ const store = current.store;
4746
4796
  const [enabled, setEnabled] = (0, import_react8.useState)(true);
4747
4797
  const [snapshot, setSnapshotState] = (0, import_react8.useState)(null);
4748
4798
  const { activeTab, setActiveTab: setActiveTabState } = useTabs("chat");
@@ -8397,8 +8447,9 @@ function useMobileBinding(args) {
8397
8447
  tapTeaser: () => latest.current.onTeaserTap(),
8398
8448
  tapPrice: (matchOddsId) => {
8399
8449
  const { panelOpen: open, popupText: text2, price: shown2 } = latest.current;
8400
- if (open || !text2 || shown2?.matchOddsId !== matchOddsId) return;
8401
- priceRef.current?.();
8450
+ if (open || !text2 || shown2?.matchOddsId !== matchOddsId || !priceRef.current) return false;
8451
+ priceRef.current();
8452
+ return true;
8402
8453
  },
8403
8454
  dismissTeaser: () => latest.current.dismissPopup(),
8404
8455
  teaserShown: () => setConfirmedShown(latest.current.popupText),
@@ -8418,6 +8469,69 @@ function useMobileBinding(args) {
8418
8469
  return { confirmedShown, onCloseRequested, onRegisterBack, onRegisterPrice, onPriceResult, priceInFlight };
8419
8470
  }
8420
8471
 
8472
+ // src/webTeaser.ts
8473
+ function webTeaserBinding(host, deliver, drawsLauncher) {
8474
+ let price;
8475
+ let delivered = null;
8476
+ let detach = () => {
8477
+ };
8478
+ const waiting = /* @__PURE__ */ new Set();
8479
+ const emit = (teaser2) => {
8480
+ delivered = teaser2;
8481
+ try {
8482
+ deliver(teaser2);
8483
+ } catch {
8484
+ }
8485
+ };
8486
+ return {
8487
+ onTeaser: (teaser2) => {
8488
+ price = teaser2.price;
8489
+ emit(teaser2.text === null && teaser2.asks.length === 0 ? null : teaser2);
8490
+ },
8491
+ onCloseRequested: () => {
8492
+ },
8493
+ onPriceResult: (result) => {
8494
+ for (const w of [...waiting]) {
8495
+ if (w.matchOddsId !== result.matchOddsId) continue;
8496
+ waiting.delete(w);
8497
+ w.resolve(result);
8498
+ }
8499
+ },
8500
+ onHandle: (handle) => {
8501
+ detach();
8502
+ detach = () => {
8503
+ };
8504
+ if (!handle) {
8505
+ price = void 0;
8506
+ for (const w of [...waiting]) w.resolve(null);
8507
+ waiting.clear();
8508
+ if (delivered !== null) emit(null);
8509
+ return;
8510
+ }
8511
+ if (!host || !drawsLauncher) return;
8512
+ detach = attachTeaser(host, {
8513
+ tapAsk: handle.tapAsk,
8514
+ tapCta: handle.tapCta,
8515
+ tapTeaser: handle.tapTeaser,
8516
+ dismissTeaser: handle.dismissTeaser,
8517
+ teaserShown: handle.teaserShown,
8518
+ tapPrice: () => {
8519
+ if (!price) return Promise.resolve(null);
8520
+ const matchOddsId = price.matchOddsId;
8521
+ return new Promise((resolve) => {
8522
+ const w = { matchOddsId, resolve };
8523
+ waiting.add(w);
8524
+ if (!handle.tapPrice(matchOddsId)) {
8525
+ waiting.delete(w);
8526
+ resolve(null);
8527
+ }
8528
+ });
8529
+ }
8530
+ });
8531
+ }
8532
+ };
8533
+ }
8534
+
8421
8535
  // src/openTelemetry.ts
8422
8536
  var import_react57 = require("react");
8423
8537
  function useOpenTelemetry(log, isOpen) {
@@ -22296,13 +22410,15 @@ var WIDGET_CSS = `.ss-w-root { all: revert; }
22296
22410
  // src/StatsWidget.tsx
22297
22411
  var import_jsx_runtime164 = require("react/jsx-runtime");
22298
22412
  var NO_OWNER = {};
22299
- function buildClient(props) {
22413
+ function buildClient(props, onUnavailable) {
22300
22414
  try {
22301
22415
  if ("client" in props && props.client) {
22302
22416
  return { client: props.client, conversation: props.client.conversation() };
22303
22417
  }
22304
22418
  const cfg = props.config;
22305
- const client = new import_widget_sdk39.StatsWidgetClient(toClientOptions(cfg));
22419
+ const options = toClientOptions(cfg);
22420
+ const base = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
22421
+ const client = new import_widget_sdk39.StatsWidgetClient({ ...options, fetch: (0, import_widget_sdk39.observingFetch)(base, onUnavailable) });
22306
22422
  return { client, conversation: client.conversation(toUserContext(cfg)) };
22307
22423
  } catch (err) {
22308
22424
  console.error("[StatsWidget] failed to initialize:", err instanceof Error ? err.message : err);
@@ -22320,7 +22436,14 @@ function injectStyles(root) {
22320
22436
  function StatsWidget(props) {
22321
22437
  const injectedClient = "client" in props ? props.client : void 0;
22322
22438
  const cfg = props.config;
22323
- const built = (0, import_react123.useMemo)(() => buildClient(props), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl]);
22439
+ const onUnavailableRef = (0, import_react123.useRef)(cfg?.onUnavailable);
22440
+ onUnavailableRef.current = cfg?.onUnavailable;
22441
+ const built = (0, import_react123.useMemo)(() => buildClient(props, (reason) => {
22442
+ try {
22443
+ onUnavailableRef.current?.(reason);
22444
+ } catch {
22445
+ }
22446
+ }), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]);
22324
22447
  const trackingRef = (0, import_react123.useRef)({ envelope: { operatorId: "", widgetVersion: WIDGET_REACT_VERSION } });
22325
22448
  const track = (0, import_react123.useMemo)(() => createTracker(() => trackingRef.current), []);
22326
22449
  const eventTextRef = (0, import_react123.useRef)(false);
@@ -22347,7 +22470,7 @@ function StatsWidget(props) {
22347
22470
  }, []);
22348
22471
  const language = injectedClient ? void 0 : hostTag(cfg);
22349
22472
  const threadLanguage = familyOf(language);
22350
- const convStorageKey = `ss-w-conv:${cfg?.operatorId ?? ""}:${cfg?.userContext?.operatorUserId ?? "anon"}${threadLanguage ? `:lang=${threadLanguage}` : ""}`;
22473
+ const convStorageKey = `ss-w-conv:${cfg?.operatorId ?? ""}:${cfg?.userContext?.operatorUserId ?? "anon"}${cfg?.userContext?.anonymousUserId ? `:a=${cfg.userContext.anonymousUserId}` : ""}${threadLanguage ? `:lang=${threadLanguage}` : ""}`;
22351
22474
  const sportScope = (0, import_react123.useMemo)(
22352
22475
  () => ({ operatorId: cfg?.operatorId ?? "", operatorUserId: cfg?.userContext?.operatorUserId, anonymousUserId: cfg?.userContext?.anonymousUserId }),
22353
22476
  [cfg?.operatorId, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]
@@ -22589,8 +22712,13 @@ function StatsWidget(props) {
22589
22712
  const bubblePrice = !stringsPending && controller.state.popupText && bubble?.price && bubble.matchId ? { matchId: bubble.matchId, price: bubble.price, sport: bubble.sport } : null;
22590
22713
  const bubbleGame = !stringsPending && controller.state.popupText && bubble?.game ? instantMs(bubble.startsAtUtc) === null ? bubble.game : ui.v2GameLine(bubble.game, bubble.startsAtUtc) : void 0;
22591
22714
  const bubbleCta = controller.state.popupText ? controller.state.greetingBubble?.cta ?? null : null;
22715
+ const onTeaserRef = (0, import_react123.useRef)(cfg?.onTeaser);
22716
+ onTeaserRef.current = cfg?.onTeaser;
22717
+ const wantsTeaser = cfg?.launcher === false || cfg?.onTeaser !== void 0;
22718
+ const webBinding = (0, import_react123.useMemo)(() => wantsTeaser ? webTeaserBinding(props.host, (teaser2) => onTeaserRef.current?.(teaser2), cfg?.launcher === false) : void 0, [wantsTeaser, props.host, cfg?.launcher]);
22719
+ const hostDrawsLauncher = props.mobile !== void 0 || cfg?.launcher === false;
22592
22720
  const mobile = useMobileBinding({
22593
- binding: props.mobile,
22721
+ binding: props.mobile ?? webBinding,
22594
22722
  panelOpen,
22595
22723
  popupText: controller.state.popupText,
22596
22724
  cta: bubbleCta,
@@ -22607,10 +22735,10 @@ function StatsWidget(props) {
22607
22735
  close: controller.close,
22608
22736
  dismissPopup: () => dismissPopupRef.current()
22609
22737
  });
22610
- const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, mobile.confirmedShown);
22738
+ const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22611
22739
  dismissPopupRef.current = dismissPopup;
22612
22740
  const greetingMeta = latestGreetingMeta(controller.state.messages);
22613
- useBubbleGreetingShown(logger, logger ?? NO_OWNER, controller.state.popupText, greetingMeta, mobile.confirmedShown);
22741
+ useBubbleGreetingShown(logger, logger ?? NO_OWNER, controller.state.popupText, greetingMeta, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22614
22742
  const [highlight, setHighlight] = (0, import_react123.useState)(null);
22615
22743
  const clearHighlight = (0, import_react123.useCallback)(() => setHighlight(null), []);
22616
22744
  (0, import_react123.useEffect)(() => {
@@ -22736,10 +22864,10 @@ function StatsWidget(props) {
22736
22864
  favouritesEnabled: favouritesEnabled && clubFixturesResult.tabReady && !clubFixturesResult.enabled,
22737
22865
  favourites: { controller: favourites, sport: favSport, sports: favSports, onSportChange: setFavSportPick, open: favouritesOpen, onOpenChange: setFavouritesOpen },
22738
22866
  builder: { options: builderOptions, sport: builderSport, onSportChange: setBuilderSportPick, onCoolOff: onBuilderCoolOff, tabs: monitor },
22739
- onCloseRequested: mobile.onCloseRequested,
22740
- onRegisterBack: mobile.onRegisterBack
22867
+ onCloseRequested: props.mobile ? mobile.onCloseRequested : void 0,
22868
+ onRegisterBack: props.mobile ? mobile.onRegisterBack : void 0
22741
22869
  }
22742
- ) }) }) : props.mobile ? bubblePrice && mobile.onRegisterPrice && mobile.onPriceResult && mobile.priceInFlight ? /* @__PURE__ */ (0, import_jsx_runtime164.jsx)(
22870
+ ) }) }) : hostDrawsLauncher ? bubblePrice && mobile.onRegisterPrice && mobile.onPriceResult && mobile.priceInFlight ? /* @__PURE__ */ (0, import_jsx_runtime164.jsx)(
22743
22871
  MobileBubblePrice,
22744
22872
  {
22745
22873
  matchId: bubblePrice.matchId,