@matterfact/embed 0.13.0 → 0.15.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/react.cjs CHANGED
@@ -1610,10 +1610,12 @@ var init_context = __esm({
1610
1610
  var react_exports = {};
1611
1611
  __export(react_exports, {
1612
1612
  MatterfactAgent: () => MatterfactAgent,
1613
- MatterfactArtifact: () => MatterfactArtifact
1613
+ MatterfactArtifact: () => MatterfactArtifact,
1614
+ MatterfactAuthProvider: () => MatterfactAuthProvider,
1615
+ MatterfactDoc: () => MatterfactDoc
1614
1616
  });
1615
1617
  module.exports = __toCommonJS(react_exports);
1616
- var import_react = require("react");
1618
+ var import_react2 = require("react");
1617
1619
 
1618
1620
  // src/protocol.ts
1619
1621
  var PROTOCOL_VERSION = 3;
@@ -2417,9 +2419,63 @@ function mount(config) {
2417
2419
  return host;
2418
2420
  }
2419
2421
 
2420
- // src/react.tsx
2422
+ // src/react-context.tsx
2423
+ var import_react = require("react");
2421
2424
  var import_jsx_runtime = require("react/jsx-runtime");
2422
2425
  var DEFAULT_ORIGIN = "https://app.matterfact.com";
2426
+ var MatterfactAuthContext = (0, import_react.createContext)(null);
2427
+ function useMatterfactConfig() {
2428
+ return (0, import_react.useContext)(MatterfactAuthContext);
2429
+ }
2430
+ function MatterfactAuthProvider({
2431
+ publishableKey,
2432
+ widgetOrigin: widgetOrigin2,
2433
+ getAuthToken,
2434
+ theme,
2435
+ onEvent,
2436
+ children
2437
+ }) {
2438
+ const resolvedWidgetOrigin = widgetOrigin2 || DEFAULT_ORIGIN;
2439
+ const value = (0, import_react.useMemo)(
2440
+ () => ({
2441
+ publishableKey,
2442
+ widgetOrigin: resolvedWidgetOrigin,
2443
+ getAuthToken,
2444
+ theme: theme || "auto",
2445
+ onEvent
2446
+ }),
2447
+ [publishableKey, resolvedWidgetOrigin, getAuthToken, theme, onEvent]
2448
+ );
2449
+ (0, import_react.useEffect)(() => {
2450
+ if (typeof window === "undefined" || !getAuthToken) return;
2451
+ let inflight = null;
2452
+ const onMessage = (e) => {
2453
+ if (e.origin !== resolvedWidgetOrigin) return;
2454
+ if (!e.data || e.data.type !== "mf.hostAuth.request") return;
2455
+ const source = e.source;
2456
+ if (!source) return;
2457
+ try {
2458
+ inflight = inflight ?? Promise.resolve(getAuthToken()).catch(() => null);
2459
+ } catch {
2460
+ return;
2461
+ }
2462
+ const p = inflight;
2463
+ void p.then((token) => {
2464
+ if (p === inflight) inflight = null;
2465
+ if (typeof token === "string" && token) {
2466
+ source.postMessage({ type: "mf.hostAuth.grant", token }, resolvedWidgetOrigin);
2467
+ }
2468
+ });
2469
+ };
2470
+ window.addEventListener("message", onMessage);
2471
+ return () => window.removeEventListener("message", onMessage);
2472
+ }, [getAuthToken, resolvedWidgetOrigin]);
2473
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MatterfactAuthContext.Provider, { value, children });
2474
+ }
2475
+
2476
+ // src/react.tsx
2477
+ var import_jsx_runtime2 = require("react/jsx-runtime");
2478
+ var DEFAULT_ORIGIN2 = "https://app.matterfact.com";
2423
2479
  function writeActionsGlobal(actions) {
2424
2480
  if (typeof window === "undefined") return;
2425
2481
  const w = window;
@@ -2463,11 +2519,11 @@ function toolDescriptorKey(artifacts, resolve, tools) {
2463
2519
  });
2464
2520
  }
2465
2521
  function MatterfactAgent({
2466
- publishableKey,
2467
- widgetOrigin: widgetOrigin2,
2522
+ publishableKey: publishableKeyProp,
2523
+ widgetOrigin: widgetOriginProp,
2468
2524
  surface = "",
2469
- theme = "auto",
2470
- getAuthToken,
2525
+ theme: themeProp,
2526
+ getAuthToken: getAuthTokenProp,
2471
2527
  getPageContext,
2472
2528
  inline = false,
2473
2529
  className: className2,
@@ -2482,30 +2538,41 @@ function MatterfactAgent({
2482
2538
  onToolEvent,
2483
2539
  onEvent
2484
2540
  }) {
2485
- const authRef = (0, import_react.useRef)(getAuthToken);
2541
+ const ctx = useMatterfactConfig();
2542
+ const publishableKey = publishableKeyProp ?? ctx?.publishableKey;
2543
+ if (!publishableKey) {
2544
+ throw new Error(
2545
+ "MatterfactAgent requires publishableKey (as a prop or via MatterfactAuthProvider)"
2546
+ );
2547
+ }
2548
+ const widgetOrigin2 = widgetOriginProp ?? ctx?.widgetOrigin ?? DEFAULT_ORIGIN2;
2549
+ const getAuthToken = getAuthTokenProp ?? ctx?.getAuthToken;
2550
+ const theme = themeProp ?? ctx?.theme ?? "auto";
2551
+ const authRef = (0, import_react2.useRef)(getAuthToken);
2486
2552
  authRef.current = getAuthToken;
2487
- const pageContextRef = (0, import_react.useRef)(getPageContext);
2553
+ const pageContextRef = (0, import_react2.useRef)(getPageContext);
2488
2554
  pageContextRef.current = getPageContext;
2489
- const actionsRef = (0, import_react.useRef)(actions);
2555
+ const actionsRef = (0, import_react2.useRef)(actions);
2490
2556
  actionsRef.current = actions;
2491
2557
  const actionsKey = JSON.stringify(actions ?? null);
2492
- const slot = (0, import_react.useRef)(null);
2558
+ const slot = (0, import_react2.useRef)(null);
2493
2559
  const sitemapKey = JSON.stringify(sitemap ?? null);
2494
- (0, import_react.useEffect)(() => {
2560
+ (0, import_react2.useEffect)(() => {
2495
2561
  writeSitemapGlobal(sitemap);
2496
2562
  }, [sitemapKey]);
2497
- (0, import_react.useEffect)(() => {
2563
+ (0, import_react2.useEffect)(() => {
2498
2564
  writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
2499
2565
  });
2500
2566
  const toolsKey = toolDescriptorKey(artifacts, resolve, tools);
2501
- (0, import_react.useEffect)(() => {
2567
+ (0, import_react2.useEffect)(() => {
2502
2568
  if (typeof window === "undefined") return;
2503
2569
  if (inline && !slot.current) return;
2504
2570
  writeActionsGlobal(actionsRef.current);
2505
2571
  writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
2506
2572
  const config = {
2507
2573
  publishableKey,
2508
- origin: widgetOrigin2 || DEFAULT_ORIGIN,
2574
+ // Already fully resolved above (prop ?? context ?? DEFAULT_ORIGIN).
2575
+ origin: widgetOrigin2,
2509
2576
  theme,
2510
2577
  surface,
2511
2578
  // Always present; a null return (no getAuthToken supplied) makes the core fall
@@ -2547,7 +2614,7 @@ function MatterfactAgent({
2547
2614
  // eslint-disable-next-line react-hooks/exhaustive-deps
2548
2615
  ]);
2549
2616
  if (!inline) return null;
2550
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
2617
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2551
2618
  "div",
2552
2619
  {
2553
2620
  ref: slot,
@@ -2558,30 +2625,139 @@ function MatterfactAgent({
2558
2625
  }
2559
2626
  var DEFAULT_ARTIFACT_ORIGIN = "https://app.matterfact.com";
2560
2627
  function MatterfactArtifact({
2628
+ name: nameProp,
2561
2629
  slug,
2562
2630
  owner,
2563
2631
  token,
2564
- widgetOrigin: widgetOrigin2,
2565
- theme = "auto",
2632
+ widgetOrigin: widgetOriginProp,
2633
+ theme: themeProp,
2566
2634
  className: className2,
2567
2635
  style
2568
2636
  }) {
2569
- const origin = widgetOrigin2 || DEFAULT_ARTIFACT_ORIGIN;
2570
- const src = `${origin}/embed/artifacts/${encodeURIComponent(slug)}?owner=${encodeURIComponent(owner)}&t=${encodeURIComponent(token)}&theme=${theme}`;
2571
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
2637
+ const ctx = useMatterfactConfig();
2638
+ const artifactName = nameProp ?? slug;
2639
+ if (!artifactName) {
2640
+ throw new Error(
2641
+ "MatterfactArtifact requires name (or its deprecated alias, slug)"
2642
+ );
2643
+ }
2644
+ const origin = widgetOriginProp ?? ctx?.widgetOrigin ?? DEFAULT_ARTIFACT_ORIGIN;
2645
+ const theme = themeProp ?? ctx?.theme ?? "auto";
2646
+ let src;
2647
+ if (token) {
2648
+ src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&t=${encodeURIComponent(token)}&theme=${theme}`;
2649
+ } else if (ctx) {
2650
+ const hostOrigin = typeof window === "undefined" ? "" : window.location.origin;
2651
+ src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&k=${encodeURIComponent(ctx.publishableKey)}&o=${encodeURIComponent(hostOrigin)}&theme=${theme}`;
2652
+ } else {
2653
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2654
+ "div",
2655
+ {
2656
+ className: className2,
2657
+ style: {
2658
+ width: "100%",
2659
+ height: 600,
2660
+ display: "flex",
2661
+ alignItems: "center",
2662
+ justifyContent: "center",
2663
+ textAlign: "center",
2664
+ ...style
2665
+ },
2666
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: "This embedded artifact link is incomplete." })
2667
+ }
2668
+ );
2669
+ }
2670
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2572
2671
  "iframe",
2573
2672
  {
2574
2673
  src,
2575
- title: `matterfact artifact ${slug}`,
2674
+ title: `matterfact artifact ${artifactName}`,
2576
2675
  className: className2,
2577
2676
  style: { width: "100%", height: 600, border: 0, ...style },
2578
2677
  sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads"
2579
2678
  }
2580
2679
  );
2581
2680
  }
2681
+ var DEFAULT_DOC_ORIGIN = "https://app.matterfact.com";
2682
+ function diffDocParams(params, sent) {
2683
+ const changed = {};
2684
+ for (const [key, value] of Object.entries(params)) {
2685
+ if (value === void 0) continue;
2686
+ const snapshot2 = JSON.stringify(value);
2687
+ if (sent.get(key) === snapshot2) continue;
2688
+ changed[key] = value;
2689
+ sent.set(key, snapshot2);
2690
+ }
2691
+ return changed;
2692
+ }
2693
+ function MatterfactDoc({
2694
+ docKey,
2695
+ ticker = "",
2696
+ version = "latest",
2697
+ theme: themeProp,
2698
+ params,
2699
+ className: className2,
2700
+ style
2701
+ }) {
2702
+ const ctx = useMatterfactConfig();
2703
+ const iframeRef = (0, import_react2.useRef)(null);
2704
+ const sentParamsRef = (0, import_react2.useRef)(/* @__PURE__ */ new Map());
2705
+ const origin = ctx?.widgetOrigin ?? DEFAULT_DOC_ORIGIN;
2706
+ const paramsKey = JSON.stringify(params ?? null);
2707
+ const pushParams = (force) => {
2708
+ if (!params) return;
2709
+ const win = iframeRef.current?.contentWindow;
2710
+ if (!win) return;
2711
+ if (force) sentParamsRef.current.clear();
2712
+ const changed = diffDocParams(params, sentParamsRef.current);
2713
+ if (Object.keys(changed).length === 0) return;
2714
+ win.postMessage({ type: "host.docParams", params: changed }, origin);
2715
+ };
2716
+ (0, import_react2.useEffect)(() => {
2717
+ pushParams(false);
2718
+ }, [paramsKey]);
2719
+ if (!docKey) {
2720
+ throw new Error("MatterfactDoc requires docKey");
2721
+ }
2722
+ if (!ctx || !ctx.publishableKey) {
2723
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2724
+ "div",
2725
+ {
2726
+ className: className2,
2727
+ style: {
2728
+ width: "100%",
2729
+ height: 600,
2730
+ display: "flex",
2731
+ alignItems: "center",
2732
+ justifyContent: "center",
2733
+ textAlign: "center",
2734
+ ...style
2735
+ },
2736
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: "This embedded document link is incomplete." })
2737
+ }
2738
+ );
2739
+ }
2740
+ const theme = themeProp ?? ctx.theme ?? "auto";
2741
+ const hostOrigin = typeof window === "undefined" ? "" : window.location.origin;
2742
+ const src = `${origin}/embed/document?k=${encodeURIComponent(ctx.publishableKey)}&o=${encodeURIComponent(hostOrigin)}&key=${encodeURIComponent(docKey)}&ticker=${encodeURIComponent(ticker)}&version=${encodeURIComponent(version)}&theme=${theme}`;
2743
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2744
+ "iframe",
2745
+ {
2746
+ ref: iframeRef,
2747
+ src,
2748
+ title: `matterfact document ${docKey}`,
2749
+ className: className2,
2750
+ style: { width: "100%", height: 600, border: 0, ...style },
2751
+ sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads",
2752
+ onLoad: () => pushParams(true)
2753
+ }
2754
+ );
2755
+ }
2582
2756
  // Annotate the CommonJS export names for ESM import in node:
2583
2757
  0 && (module.exports = {
2584
2758
  MatterfactAgent,
2585
- MatterfactArtifact
2759
+ MatterfactArtifact,
2760
+ MatterfactAuthProvider,
2761
+ MatterfactDoc
2586
2762
  });
2587
2763
  //# sourceMappingURL=react.cjs.map