@sensiblestats/widget-react 0.155.0 → 0.157.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.157.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),
@@ -8367,6 +8418,14 @@ function useMobileBinding(args) {
8367
8418
  });
8368
8419
  return () => binding.onHandle(null);
8369
8420
  }, [binding]);
8421
+ const { labels, theme } = args;
8422
+ const labelsKey = labels ? JSON.stringify([labels.launcher, labels.dismiss]) : "";
8423
+ useEffect30(() => {
8424
+ if (labels) binding?.onLabels?.(labels);
8425
+ }, [binding, labelsKey]);
8426
+ useEffect30(() => {
8427
+ if (theme) binding?.onTheme?.(theme);
8428
+ }, [binding, theme]);
8370
8429
  const onCloseRequested = useCallback26(() => bindingRef.current?.onCloseRequested(), []);
8371
8430
  const onRegisterBack = useCallback26((fn) => {
8372
8431
  backRef.current = fn;
@@ -8379,6 +8438,91 @@ function useMobileBinding(args) {
8379
8438
  return { confirmedShown, onCloseRequested, onRegisterBack, onRegisterPrice, onPriceResult, priceInFlight };
8380
8439
  }
8381
8440
 
8441
+ // src/webTeaser.ts
8442
+ function webTeaserBinding(host, deliver, drawsLauncher) {
8443
+ let price;
8444
+ let delivered = null;
8445
+ let detach = () => {
8446
+ };
8447
+ const waiting = /* @__PURE__ */ new Set();
8448
+ const emit = (teaser2) => {
8449
+ delivered = teaser2;
8450
+ try {
8451
+ deliver(teaser2);
8452
+ } catch {
8453
+ }
8454
+ };
8455
+ return {
8456
+ onTeaser: (teaser2) => {
8457
+ price = teaser2.price;
8458
+ emit(teaser2.text === null && teaser2.asks.length === 0 ? null : teaser2);
8459
+ },
8460
+ onCloseRequested: () => {
8461
+ },
8462
+ onPriceResult: (result) => {
8463
+ for (const w of [...waiting]) {
8464
+ if (w.matchOddsId !== result.matchOddsId) continue;
8465
+ waiting.delete(w);
8466
+ w.resolve(result);
8467
+ }
8468
+ },
8469
+ onHandle: (handle) => {
8470
+ detach();
8471
+ detach = () => {
8472
+ };
8473
+ if (!handle) {
8474
+ price = void 0;
8475
+ for (const w of [...waiting]) w.resolve(null);
8476
+ waiting.clear();
8477
+ if (delivered !== null) emit(null);
8478
+ return;
8479
+ }
8480
+ if (!host || !drawsLauncher) return;
8481
+ detach = attachTeaser(host, {
8482
+ tapAsk: handle.tapAsk,
8483
+ tapCta: handle.tapCta,
8484
+ tapTeaser: handle.tapTeaser,
8485
+ dismissTeaser: handle.dismissTeaser,
8486
+ teaserShown: handle.teaserShown,
8487
+ tapPrice: () => {
8488
+ if (!price) return Promise.resolve(null);
8489
+ const matchOddsId = price.matchOddsId;
8490
+ return new Promise((resolve) => {
8491
+ const w = { matchOddsId, resolve };
8492
+ waiting.add(w);
8493
+ if (!handle.tapPrice(matchOddsId)) {
8494
+ waiting.delete(w);
8495
+ resolve(null);
8496
+ }
8497
+ });
8498
+ }
8499
+ });
8500
+ }
8501
+ };
8502
+ }
8503
+
8504
+ // src/resolvedTheme.ts
8505
+ import { useSyncExternalStore } from "react";
8506
+ var QUERY = "(prefers-color-scheme: dark)";
8507
+ function systemDark() {
8508
+ try {
8509
+ return typeof window.matchMedia === "function" && window.matchMedia(QUERY).matches;
8510
+ } catch {
8511
+ return false;
8512
+ }
8513
+ }
8514
+ function subscribe(onChange) {
8515
+ if (typeof window.matchMedia !== "function") return () => {
8516
+ };
8517
+ const query = window.matchMedia(QUERY);
8518
+ query.addEventListener("change", onChange);
8519
+ return () => query.removeEventListener("change", onChange);
8520
+ }
8521
+ function useResolvedTheme(choice) {
8522
+ const dark = useSyncExternalStore(subscribe, systemDark, () => false);
8523
+ return choice === "auto" ? dark ? "dark" : "light" : choice;
8524
+ }
8525
+
8382
8526
  // src/openTelemetry.ts
8383
8527
  import { useEffect as useEffect31, useRef as useRef26 } from "react";
8384
8528
  function useOpenTelemetry(log, isOpen) {
@@ -22257,13 +22401,15 @@ var WIDGET_CSS = `.ss-w-root { all: revert; }
22257
22401
  // src/StatsWidget.tsx
22258
22402
  import { jsx as jsx164 } from "react/jsx-runtime";
22259
22403
  var NO_OWNER = {};
22260
- function buildClient(props) {
22404
+ function buildClient(props, onUnavailable) {
22261
22405
  try {
22262
22406
  if ("client" in props && props.client) {
22263
22407
  return { client: props.client, conversation: props.client.conversation() };
22264
22408
  }
22265
22409
  const cfg = props.config;
22266
- const client = new StatsWidgetClient(toClientOptions(cfg));
22410
+ const options = toClientOptions(cfg);
22411
+ const base = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
22412
+ const client = new StatsWidgetClient({ ...options, fetch: observingFetch(base, onUnavailable) });
22267
22413
  return { client, conversation: client.conversation(toUserContext(cfg)) };
22268
22414
  } catch (err) {
22269
22415
  console.error("[StatsWidget] failed to initialize:", err instanceof Error ? err.message : err);
@@ -22281,7 +22427,14 @@ function injectStyles(root) {
22281
22427
  function StatsWidget(props) {
22282
22428
  const injectedClient = "client" in props ? props.client : void 0;
22283
22429
  const cfg = props.config;
22284
- const built = useMemo17(() => buildClient(props), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl]);
22430
+ const onUnavailableRef = useRef50(cfg?.onUnavailable);
22431
+ onUnavailableRef.current = cfg?.onUnavailable;
22432
+ const built = useMemo17(() => buildClient(props, (reason) => {
22433
+ try {
22434
+ onUnavailableRef.current?.(reason);
22435
+ } catch {
22436
+ }
22437
+ }), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]);
22285
22438
  const trackingRef = useRef50({ envelope: { operatorId: "", widgetVersion: WIDGET_REACT_VERSION } });
22286
22439
  const track = useMemo17(() => createTracker(() => trackingRef.current), []);
22287
22440
  const eventTextRef = useRef50(false);
@@ -22308,7 +22461,7 @@ function StatsWidget(props) {
22308
22461
  }, []);
22309
22462
  const language = injectedClient ? void 0 : hostTag(cfg);
22310
22463
  const threadLanguage = familyOf(language);
22311
- const convStorageKey = `ss-w-conv:${cfg?.operatorId ?? ""}:${cfg?.userContext?.operatorUserId ?? "anon"}${threadLanguage ? `:lang=${threadLanguage}` : ""}`;
22464
+ const convStorageKey = `ss-w-conv:${cfg?.operatorId ?? ""}:${cfg?.userContext?.operatorUserId ?? "anon"}${cfg?.userContext?.anonymousUserId ? `:a=${cfg.userContext.anonymousUserId}` : ""}${threadLanguage ? `:lang=${threadLanguage}` : ""}`;
22312
22465
  const sportScope = useMemo17(
22313
22466
  () => ({ operatorId: cfg?.operatorId ?? "", operatorUserId: cfg?.userContext?.operatorUserId, anonymousUserId: cfg?.userContext?.anonymousUserId }),
22314
22467
  [cfg?.operatorId, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]
@@ -22550,8 +22703,14 @@ function StatsWidget(props) {
22550
22703
  const bubblePrice = !stringsPending && controller.state.popupText && bubble?.price && bubble.matchId ? { matchId: bubble.matchId, price: bubble.price, sport: bubble.sport } : null;
22551
22704
  const bubbleGame = !stringsPending && controller.state.popupText && bubble?.game ? instantMs(bubble.startsAtUtc) === null ? bubble.game : ui.v2GameLine(bubble.game, bubble.startsAtUtc) : void 0;
22552
22705
  const bubbleCta = controller.state.popupText ? controller.state.greetingBubble?.cta ?? null : null;
22706
+ const onTeaserRef = useRef50(cfg?.onTeaser);
22707
+ onTeaserRef.current = cfg?.onTeaser;
22708
+ const wantsTeaser = cfg?.launcher === false || cfg?.onTeaser !== void 0;
22709
+ const webBinding = useMemo17(() => wantsTeaser ? webTeaserBinding(props.host, (teaser2) => onTeaserRef.current?.(teaser2), cfg?.launcher === false) : void 0, [wantsTeaser, props.host, cfg?.launcher]);
22710
+ const hostDrawsLauncher = props.mobile !== void 0 || cfg?.launcher === false;
22711
+ const resolvedTheme = useResolvedTheme(oddsSettings.userTheme ?? ui.theme);
22553
22712
  const mobile = useMobileBinding({
22554
- binding: props.mobile,
22713
+ binding: props.mobile ?? webBinding,
22555
22714
  panelOpen,
22556
22715
  popupText: controller.state.popupText,
22557
22716
  cta: bubbleCta,
@@ -22566,12 +22725,14 @@ function StatsWidget(props) {
22566
22725
  asks,
22567
22726
  open: controller.open,
22568
22727
  close: controller.close,
22569
- dismissPopup: () => dismissPopupRef.current()
22728
+ dismissPopup: () => dismissPopupRef.current(),
22729
+ labels: stringsPending ? null : { launcher: ui.launcherLabel, dismiss: ui.dismissGreeting },
22730
+ theme: resolvedTheme
22570
22731
  });
22571
- const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, mobile.confirmedShown);
22732
+ const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22572
22733
  dismissPopupRef.current = dismissPopup;
22573
22734
  const greetingMeta = latestGreetingMeta(controller.state.messages);
22574
- useBubbleGreetingShown(logger, logger ?? NO_OWNER, controller.state.popupText, greetingMeta, mobile.confirmedShown);
22735
+ useBubbleGreetingShown(logger, logger ?? NO_OWNER, controller.state.popupText, greetingMeta, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22575
22736
  const [highlight, setHighlight] = useState89(null);
22576
22737
  const clearHighlight = useCallback31(() => setHighlight(null), []);
22577
22738
  useEffect59(() => {
@@ -22697,10 +22858,10 @@ function StatsWidget(props) {
22697
22858
  favouritesEnabled: favouritesEnabled && clubFixturesResult.tabReady && !clubFixturesResult.enabled,
22698
22859
  favourites: { controller: favourites, sport: favSport, sports: favSports, onSportChange: setFavSportPick, open: favouritesOpen, onOpenChange: setFavouritesOpen },
22699
22860
  builder: { options: builderOptions, sport: builderSport, onSportChange: setBuilderSportPick, onCoolOff: onBuilderCoolOff, tabs: monitor },
22700
- onCloseRequested: mobile.onCloseRequested,
22701
- onRegisterBack: mobile.onRegisterBack
22861
+ onCloseRequested: props.mobile ? mobile.onCloseRequested : void 0,
22862
+ onRegisterBack: props.mobile ? mobile.onRegisterBack : void 0
22702
22863
  }
22703
- ) }) }) : props.mobile ? bubblePrice && mobile.onRegisterPrice && mobile.onPriceResult && mobile.priceInFlight ? /* @__PURE__ */ jsx164(
22864
+ ) }) }) : hostDrawsLauncher ? bubblePrice && mobile.onRegisterPrice && mobile.onPriceResult && mobile.priceInFlight ? /* @__PURE__ */ jsx164(
22704
22865
  MobileBubblePrice,
22705
22866
  {
22706
22867
  matchId: bubblePrice.matchId,