@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/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/StatsWidget.tsx
2
2
  import { useCallback as useCallback31, useEffect as useEffect59, useMemo as useMemo17, useRef as useRef50, useState as useState89 } from "react";
3
- import { StatsWidgetClient } from "@sensiblestats/widget-sdk";
3
+ import { StatsWidgetClient, observingFetch } from "@sensiblestats/widget-sdk";
4
4
 
5
5
  // src/strings/tag.ts
6
6
  function familyOf(tag2) {
@@ -1383,7 +1383,7 @@ function parseCustomTotal(text2, format2) {
1383
1383
  }
1384
1384
 
1385
1385
  // src/version.generated.ts
1386
- var WIDGET_REACT_VERSION = "0.155.0";
1386
+ var WIDGET_REACT_VERSION = "0.156.0";
1387
1387
 
1388
1388
  // src/config.ts
1389
1389
  var COLOR_KEYS = ["accent", "cta", "surface", "ink"];
@@ -2802,7 +2802,7 @@ function isValidId(value) {
2802
2802
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
2803
2803
  }
2804
2804
  function createWidgetHost() {
2805
- const state = { queue: [], sink: null };
2805
+ const state = { queue: [], sink: null, teaser: null };
2806
2806
  const dispatch = (command) => {
2807
2807
  if (state.sink) {
2808
2808
  state.sink(command);
@@ -2848,11 +2848,49 @@ function createWidgetHost() {
2848
2848
  if (isValidId(id)) dispatch({ kind: "matchOpened", matchId: id });
2849
2849
  } catch {
2850
2850
  }
2851
+ },
2852
+ // A teaser tap is about what is on screen now, so it is never queued for a widget that is not there.
2853
+ tapAsk(index) {
2854
+ teaser(state, (t) => t.tapAsk(index));
2855
+ },
2856
+ tapPrice() {
2857
+ try {
2858
+ return state.teaser ? state.teaser.tapPrice().catch(() => null) : Promise.resolve(null);
2859
+ } catch {
2860
+ return Promise.resolve(null);
2861
+ }
2862
+ },
2863
+ tapCta() {
2864
+ teaser(state, (t) => t.tapCta());
2865
+ },
2866
+ tapTeaser() {
2867
+ teaser(state, (t) => t.tapTeaser());
2868
+ },
2869
+ dismissTeaser() {
2870
+ teaser(state, (t) => t.dismissTeaser());
2871
+ },
2872
+ teaserShown() {
2873
+ teaser(state, (t) => t.teaserShown());
2851
2874
  }
2852
2875
  };
2853
2876
  STATES.set(host, state);
2854
2877
  return host;
2855
2878
  }
2879
+ function teaser(state, call) {
2880
+ try {
2881
+ if (state.teaser) call(state.teaser);
2882
+ } catch {
2883
+ }
2884
+ }
2885
+ function attachTeaser(host, control) {
2886
+ const state = STATES.get(host);
2887
+ if (!state) return () => {
2888
+ };
2889
+ state.teaser = control;
2890
+ return () => {
2891
+ if (state.teaser === control) state.teaser = null;
2892
+ };
2893
+ }
2856
2894
  function attachHost(host, sink) {
2857
2895
  const state = STATES.get(host);
2858
2896
  if (!state) return () => {
@@ -3088,6 +3126,12 @@ function useHostEvents(opts) {
3088
3126
  const { host, operatorId, currentUser, logger, isOpen, closePanel } = opts;
3089
3127
  const [added, setAdded] = useState2({});
3090
3128
  const [settled, setSettled] = useState2({});
3129
+ const [marksUser, setMarksUser] = useState2(currentUser);
3130
+ if (marksUser !== currentUser) {
3131
+ setMarksUser(currentUser);
3132
+ setAdded({});
3133
+ setSettled({});
3134
+ }
3091
3135
  const markAdded = useCallback((key) => {
3092
3136
  setAdded((prev) => prev[key] ? prev : { ...prev, [key]: true });
3093
3137
  }, []);
@@ -4703,7 +4747,13 @@ function phaseForPin(snapshot, pin) {
4703
4747
  function useLiveMonitor(opts) {
4704
4748
  const sessionIdRef = useRef6(crypto.randomUUID());
4705
4749
  const [pins, setPins] = useState6([]);
4706
- const [store] = useState6(() => createPinStore(opts.storageKey, sessionIdRef.current, setPins));
4750
+ const [owned, setOwned] = useState6(() => ({ key: opts.storageKey, store: createPinStore(opts.storageKey, sessionIdRef.current, setPins) }));
4751
+ let current = owned;
4752
+ if (owned.key !== opts.storageKey) {
4753
+ current = { key: opts.storageKey, store: createPinStore(opts.storageKey, sessionIdRef.current, setPins) };
4754
+ setOwned(current);
4755
+ }
4756
+ const store = current.store;
4707
4757
  const [enabled, setEnabled] = useState6(true);
4708
4758
  const [snapshot, setSnapshotState] = useState6(null);
4709
4759
  const { activeTab, setActiveTab: setActiveTabState } = useTabs("chat");
@@ -8358,8 +8408,9 @@ function useMobileBinding(args) {
8358
8408
  tapTeaser: () => latest.current.onTeaserTap(),
8359
8409
  tapPrice: (matchOddsId) => {
8360
8410
  const { panelOpen: open, popupText: text2, price: shown2 } = latest.current;
8361
- if (open || !text2 || shown2?.matchOddsId !== matchOddsId) return;
8362
- priceRef.current?.();
8411
+ if (open || !text2 || shown2?.matchOddsId !== matchOddsId || !priceRef.current) return false;
8412
+ priceRef.current();
8413
+ return true;
8363
8414
  },
8364
8415
  dismissTeaser: () => latest.current.dismissPopup(),
8365
8416
  teaserShown: () => setConfirmedShown(latest.current.popupText),
@@ -8379,6 +8430,69 @@ function useMobileBinding(args) {
8379
8430
  return { confirmedShown, onCloseRequested, onRegisterBack, onRegisterPrice, onPriceResult, priceInFlight };
8380
8431
  }
8381
8432
 
8433
+ // src/webTeaser.ts
8434
+ function webTeaserBinding(host, deliver, drawsLauncher) {
8435
+ let price;
8436
+ let delivered = null;
8437
+ let detach = () => {
8438
+ };
8439
+ const waiting = /* @__PURE__ */ new Set();
8440
+ const emit = (teaser2) => {
8441
+ delivered = teaser2;
8442
+ try {
8443
+ deliver(teaser2);
8444
+ } catch {
8445
+ }
8446
+ };
8447
+ return {
8448
+ onTeaser: (teaser2) => {
8449
+ price = teaser2.price;
8450
+ emit(teaser2.text === null && teaser2.asks.length === 0 ? null : teaser2);
8451
+ },
8452
+ onCloseRequested: () => {
8453
+ },
8454
+ onPriceResult: (result) => {
8455
+ for (const w of [...waiting]) {
8456
+ if (w.matchOddsId !== result.matchOddsId) continue;
8457
+ waiting.delete(w);
8458
+ w.resolve(result);
8459
+ }
8460
+ },
8461
+ onHandle: (handle) => {
8462
+ detach();
8463
+ detach = () => {
8464
+ };
8465
+ if (!handle) {
8466
+ price = void 0;
8467
+ for (const w of [...waiting]) w.resolve(null);
8468
+ waiting.clear();
8469
+ if (delivered !== null) emit(null);
8470
+ return;
8471
+ }
8472
+ if (!host || !drawsLauncher) return;
8473
+ detach = attachTeaser(host, {
8474
+ tapAsk: handle.tapAsk,
8475
+ tapCta: handle.tapCta,
8476
+ tapTeaser: handle.tapTeaser,
8477
+ dismissTeaser: handle.dismissTeaser,
8478
+ teaserShown: handle.teaserShown,
8479
+ tapPrice: () => {
8480
+ if (!price) return Promise.resolve(null);
8481
+ const matchOddsId = price.matchOddsId;
8482
+ return new Promise((resolve) => {
8483
+ const w = { matchOddsId, resolve };
8484
+ waiting.add(w);
8485
+ if (!handle.tapPrice(matchOddsId)) {
8486
+ waiting.delete(w);
8487
+ resolve(null);
8488
+ }
8489
+ });
8490
+ }
8491
+ });
8492
+ }
8493
+ };
8494
+ }
8495
+
8382
8496
  // src/openTelemetry.ts
8383
8497
  import { useEffect as useEffect31, useRef as useRef26 } from "react";
8384
8498
  function useOpenTelemetry(log, isOpen) {
@@ -22257,13 +22371,15 @@ var WIDGET_CSS = `.ss-w-root { all: revert; }
22257
22371
  // src/StatsWidget.tsx
22258
22372
  import { jsx as jsx164 } from "react/jsx-runtime";
22259
22373
  var NO_OWNER = {};
22260
- function buildClient(props) {
22374
+ function buildClient(props, onUnavailable) {
22261
22375
  try {
22262
22376
  if ("client" in props && props.client) {
22263
22377
  return { client: props.client, conversation: props.client.conversation() };
22264
22378
  }
22265
22379
  const cfg = props.config;
22266
- const client = new StatsWidgetClient(toClientOptions(cfg));
22380
+ const options = toClientOptions(cfg);
22381
+ const base = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
22382
+ const client = new StatsWidgetClient({ ...options, fetch: observingFetch(base, onUnavailable) });
22267
22383
  return { client, conversation: client.conversation(toUserContext(cfg)) };
22268
22384
  } catch (err) {
22269
22385
  console.error("[StatsWidget] failed to initialize:", err instanceof Error ? err.message : err);
@@ -22281,7 +22397,14 @@ function injectStyles(root) {
22281
22397
  function StatsWidget(props) {
22282
22398
  const injectedClient = "client" in props ? props.client : void 0;
22283
22399
  const cfg = props.config;
22284
- const built = useMemo17(() => buildClient(props), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl]);
22400
+ const onUnavailableRef = useRef50(cfg?.onUnavailable);
22401
+ onUnavailableRef.current = cfg?.onUnavailable;
22402
+ const built = useMemo17(() => buildClient(props, (reason) => {
22403
+ try {
22404
+ onUnavailableRef.current?.(reason);
22405
+ } catch {
22406
+ }
22407
+ }), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]);
22285
22408
  const trackingRef = useRef50({ envelope: { operatorId: "", widgetVersion: WIDGET_REACT_VERSION } });
22286
22409
  const track = useMemo17(() => createTracker(() => trackingRef.current), []);
22287
22410
  const eventTextRef = useRef50(false);
@@ -22308,7 +22431,7 @@ function StatsWidget(props) {
22308
22431
  }, []);
22309
22432
  const language = injectedClient ? void 0 : hostTag(cfg);
22310
22433
  const threadLanguage = familyOf(language);
22311
- const convStorageKey = `ss-w-conv:${cfg?.operatorId ?? ""}:${cfg?.userContext?.operatorUserId ?? "anon"}${threadLanguage ? `:lang=${threadLanguage}` : ""}`;
22434
+ const convStorageKey = `ss-w-conv:${cfg?.operatorId ?? ""}:${cfg?.userContext?.operatorUserId ?? "anon"}${cfg?.userContext?.anonymousUserId ? `:a=${cfg.userContext.anonymousUserId}` : ""}${threadLanguage ? `:lang=${threadLanguage}` : ""}`;
22312
22435
  const sportScope = useMemo17(
22313
22436
  () => ({ operatorId: cfg?.operatorId ?? "", operatorUserId: cfg?.userContext?.operatorUserId, anonymousUserId: cfg?.userContext?.anonymousUserId }),
22314
22437
  [cfg?.operatorId, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]
@@ -22550,8 +22673,13 @@ function StatsWidget(props) {
22550
22673
  const bubblePrice = !stringsPending && controller.state.popupText && bubble?.price && bubble.matchId ? { matchId: bubble.matchId, price: bubble.price, sport: bubble.sport } : null;
22551
22674
  const bubbleGame = !stringsPending && controller.state.popupText && bubble?.game ? instantMs(bubble.startsAtUtc) === null ? bubble.game : ui.v2GameLine(bubble.game, bubble.startsAtUtc) : void 0;
22552
22675
  const bubbleCta = controller.state.popupText ? controller.state.greetingBubble?.cta ?? null : null;
22676
+ const onTeaserRef = useRef50(cfg?.onTeaser);
22677
+ onTeaserRef.current = cfg?.onTeaser;
22678
+ const wantsTeaser = cfg?.launcher === false || cfg?.onTeaser !== void 0;
22679
+ const webBinding = useMemo17(() => wantsTeaser ? webTeaserBinding(props.host, (teaser2) => onTeaserRef.current?.(teaser2), cfg?.launcher === false) : void 0, [wantsTeaser, props.host, cfg?.launcher]);
22680
+ const hostDrawsLauncher = props.mobile !== void 0 || cfg?.launcher === false;
22553
22681
  const mobile = useMobileBinding({
22554
- binding: props.mobile,
22682
+ binding: props.mobile ?? webBinding,
22555
22683
  panelOpen,
22556
22684
  popupText: controller.state.popupText,
22557
22685
  cta: bubbleCta,
@@ -22568,10 +22696,10 @@ function StatsWidget(props) {
22568
22696
  close: controller.close,
22569
22697
  dismissPopup: () => dismissPopupRef.current()
22570
22698
  });
22571
- const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, mobile.confirmedShown);
22699
+ const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22572
22700
  dismissPopupRef.current = dismissPopup;
22573
22701
  const greetingMeta = latestGreetingMeta(controller.state.messages);
22574
- useBubbleGreetingShown(logger, logger ?? NO_OWNER, controller.state.popupText, greetingMeta, mobile.confirmedShown);
22702
+ useBubbleGreetingShown(logger, logger ?? NO_OWNER, controller.state.popupText, greetingMeta, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22575
22703
  const [highlight, setHighlight] = useState89(null);
22576
22704
  const clearHighlight = useCallback31(() => setHighlight(null), []);
22577
22705
  useEffect59(() => {
@@ -22697,10 +22825,10 @@ function StatsWidget(props) {
22697
22825
  favouritesEnabled: favouritesEnabled && clubFixturesResult.tabReady && !clubFixturesResult.enabled,
22698
22826
  favourites: { controller: favourites, sport: favSport, sports: favSports, onSportChange: setFavSportPick, open: favouritesOpen, onOpenChange: setFavouritesOpen },
22699
22827
  builder: { options: builderOptions, sport: builderSport, onSportChange: setBuilderSportPick, onCoolOff: onBuilderCoolOff, tabs: monitor },
22700
- onCloseRequested: mobile.onCloseRequested,
22701
- onRegisterBack: mobile.onRegisterBack
22828
+ onCloseRequested: props.mobile ? mobile.onCloseRequested : void 0,
22829
+ onRegisterBack: props.mobile ? mobile.onRegisterBack : void 0
22702
22830
  }
22703
- ) }) }) : props.mobile ? bubblePrice && mobile.onRegisterPrice && mobile.onPriceResult && mobile.priceInFlight ? /* @__PURE__ */ jsx164(
22831
+ ) }) }) : hostDrawsLauncher ? bubblePrice && mobile.onRegisterPrice && mobile.onPriceResult && mobile.priceInFlight ? /* @__PURE__ */ jsx164(
22704
22832
  MobileBubblePrice,
22705
22833
  {
22706
22834
  matchId: bubblePrice.matchId,