@machinemetrics/mm-react-embeddable 0.10.1 → 0.10.2-1

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.
@@ -1,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),y=require("prop-types"),C=require("styled-components"),R=e.createContext(null),x=({children:t})=>{const[a,d]=e.useState({}),s=e.useRef({}),f=e.useCallback((n,r,o)=>{d(i=>({...i,[n]:{origin:r,frameRef:o}}))},[]);e.useEffect(()=>{const n=(o=[],i)=>{o.forEach(u=>u(i))},r=o=>{var M,g,w,l,p;if(!((M=o==null?void 0:o.data)!=null&&M.instanceId))return;const{instanceId:i,type:u}=o.data,m=s.current;n((g=m[i])==null?void 0:g[u],o.data),n((w=m["*"])==null?void 0:w[u],o.data),n((l=m[i])==null?void 0:l["*"],o.data),n((p=m["*"])==null?void 0:p["*"],o.data)};return window.addEventListener("message",r),()=>{window.removeEventListener("message",r)}},[a]);const c=e.useCallback((n,r,o)=>{const i=a[n];if(!i){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}i.frameRef.current.contentWindow.postMessage({type:r,...o,instanceId:n},i.origin)},[a]),E=e.useCallback((n,r)=>{c(n,"load-options",r)},[c]),b=e.useCallback((n,r)=>{c(n,"validate",r)},[c]),I={registerFrame:f,sendMessage:c,loadOptions:E,validate:b,addListener:(n,r,o)=>(s.current[n]||(s.current[n]={}),s.current[n][r]||(s.current[n][r]=[]),s.current[n][r].push(o),()=>{s.current[n][r]=s.current[n][r].filter(i=>i!==o)})};return e.createElement(R.Provider,{value:I},t)},h={},P=async t=>{try{const a=new URL(t).origin;if(h[a]!==void 0)return h[a];const s=await(await fetch(`${a}/mm-app-manifest.json`)).json();if(!s.version)throw new Error("Invalid manifest");return h[a]=s,s}catch{}return h[origin]=!1,!1},$=async t=>!!await P(t),L=(t,a,d)=>{const s=e.useContext(R);if(s===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const f=e.useRef(d);return e.useEffect(()=>{f.current=d},[d]),e.useEffect(()=>{if(!(!t||!a))return s.addListener(t,a,(...c)=>f.current(...c))},[s,t,a]),e.useCallback((c,E)=>s.sendMessage(t,c,E),[s,t])};x.propTypes={children:y.node.isRequired};const Z=C.default||C,v=Z.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),M=require("prop-types"),x=require("styled-components"),R=t.createContext(null),P=({children:r})=>{const[a,c]=t.useState({}),o=t.useRef({}),l=t.useCallback((e,s,n)=>{c(i=>({...i,[e]:{origin:s,frameRef:n}}))},[]),f=t.useCallback(e=>{c(s=>{const n={...s};return delete n[e],n})},[]);t.useEffect(()=>{const e=(n=[],i)=>{n.forEach(d=>d(i))},s=n=>{var y,E,w,b,p;if(!((y=n==null?void 0:n.data)!=null&&y.instanceId))return;const{instanceId:i,type:d}=n.data,g=o.current;e((E=g[i])==null?void 0:E[d],n.data),e((w=g["*"])==null?void 0:w[d],n.data),e((b=g[i])==null?void 0:b["*"],n.data),e((p=g["*"])==null?void 0:p["*"],n.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[a]);const u=t.useCallback((e,s,n)=>{console.log("sendMessage",e,s,n);const i=a[e];if(!i){console.warn(`Origin not set for id: ${e}. Unable to send message.`);return}i.frameRef.current.contentWindow.postMessage({type:s,...n,instanceId:e},i.origin)},[a]),m=t.useCallback((e,s)=>{u(e,"load-options",s)},[u]),I=t.useCallback((e,s)=>{u(e,"validate",s)},[u]),C={registerFrame:l,unregisterFrame:f,sendMessage:u,loadOptions:m,validate:I,addListener:(e,s,n)=>(o.current[e]||(o.current[e]={}),o.current[e][s]||(o.current[e][s]=[]),o.current[e][s].push(n),()=>{o.current[e][s]=o.current[e][s].filter(i=>i!==n)})};return t.createElement(R.Provider,{value:C},r)},h={},$=async r=>{try{const a=new URL(r).origin;if(h[a]!==void 0)return h[a];const o=await(await fetch(`${a}/mm-app-manifest.json`)).json();if(!o.version)throw new Error("Invalid manifest");return h[a]=o,o}catch{}return h[origin]=!1,!1},L=async r=>!!await $(r),S=(r,a,c)=>{const o=t.useContext(R);if(o===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=t.useRef(c);return t.useEffect(()=>{l.current=c},[c]),t.useEffect(()=>{if(!(!r||!a))return o.addListener(r,a,(...f)=>l.current(...f))},[o,r,a]),t.useCallback((f,u)=>o.sendMessage(r,f,u),[o,r])};P.propTypes={children:M.node.isRequired};const v=x.default||x,F=v.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
- visibility: ${t=>t.$isReady?"visible":"hidden"};
6
- `,S=({id:t,src:a,path:d,onInit:s=()=>{},context:f,isConfiguring:c,...E})=>{const b=e.useRef(),[I,n]=e.useState(),[r,o]=e.useState(0),{registerFrame:i,sendMessage:u}=e.useContext(R),[m,M]=e.useState(!0),[g,w]=e.useState();return e.useEffect(()=>{r&&u(t,"set-context",{context:f})},[f,t,r]),e.useEffect(()=>{d!==void 0&&w(d)},[d]),e.useEffect(()=>{g===void 0||!r||u(t,"navigate",{path:g})},[g,r,t,u]),e.useEffect(()=>{r&&u(t,"set-configuring",{isConfiguring:c})},[r,c]),e.useEffect(()=>{!a||!t||!b||(async()=>{const l=await $(a);if(M(l),l){const p=new URL(a);i(t,p.origin,b),s(),n(`${p.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`);return}})()},[a,t,i,b]),L(t,"ready",()=>o(l=>l+1)),m?e.createElement(v,{$isReady:r,ref:b,src:I,...E}):e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};S.propTypes={id:y.string,src:y.string.isRequired,path:y.string,onInit:y.func};exports.MMEmbeddableZone=S;exports.MMEmbeddableZoneContext=R;exports.MMEmbeddableZoneProvider=x;exports.getAppInfo=P;exports.testIsEmbeddable=$;exports.useEmbeddedEffect=L;
5
+ visibility: ${r=>r.$isReady?"visible":"hidden"};
6
+ `,Z=({id:r,src:a,path:c,onInit:o=()=>{},context:l,isConfiguring:f,...u})=>{const m=t.useRef(),[I,C]=t.useState(),[e,s]=t.useState(0),{registerFrame:n,unregisterFrame:i,sendMessage:d}=t.useContext(R),[g,y]=t.useState(!0),[E,w]=t.useState();return t.useEffect(()=>{e&&d(r,"set-context",{context:l})},[l,r,e]),t.useEffect(()=>{c!==void 0&&w(c)},[c]),t.useEffect(()=>{E===void 0||!e||d(r,"navigate",{path:E})},[E,e,r,d]),t.useEffect(()=>{e&&d(r,"set-configuring",{isConfiguring:f})},[e,f]),t.useEffect(()=>{if(!(!a||!r||!m))return(async()=>{const b=await L(a);if(y(b),b){const p=new URL(a);n(r,p.origin,m),o(),C(`${p.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`);return}})(),()=>{i(r)}},[a,r,n,m]),S(r,"ready",()=>s(b=>b+1)),g?t.createElement(F,{$isReady:e,ref:m,src:I,...u}):t.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},t.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};Z.propTypes={id:M.string,src:M.string.isRequired,path:M.string,onInit:M.func};exports.MMEmbeddableZone=Z;exports.MMEmbeddableZoneContext=R;exports.MMEmbeddableZoneProvider=P;exports.getAppInfo=$;exports.testIsEmbeddable=L;exports.useEmbeddedEffect=S;
@@ -1,129 +1,141 @@
1
- import $, { createContext as C, useState as w, useRef as L, useCallback as y, useEffect as u, useContext as A } from "react";
2
- import h from "prop-types";
1
+ import x, { createContext as C, useState as h, useRef as F, useCallback as E, useEffect as f, useContext as A } from "react";
2
+ import y from "prop-types";
3
3
  import v from "styled-components";
4
- const P = C(null), F = ({ children: e }) => {
5
- const [o, c] = w({}), n = L({}), f = y((t, r, s) => {
6
- c((i) => ({ ...i, [t]: { origin: r, frameRef: s } }));
4
+ const P = C(null), U = ({ children: t }) => {
5
+ const [o, a] = h({}), s = F({}), l = E((e, n, r) => {
6
+ a((i) => ({ ...i, [e]: { origin: n, frameRef: r } }));
7
+ }, []), u = E((e) => {
8
+ a((n) => {
9
+ const r = { ...n };
10
+ return delete r[e], r;
11
+ });
7
12
  }, []);
8
- u(() => {
9
- const t = (s = [], i) => {
10
- s.forEach((d) => d(i));
11
- }, r = (s) => {
12
- var M, p, R, l, E;
13
- if (!((M = s == null ? void 0 : s.data) != null && M.instanceId))
13
+ f(() => {
14
+ const e = (r = [], i) => {
15
+ r.forEach((d) => d(i));
16
+ }, n = (r) => {
17
+ var M, p, R, m, w;
18
+ if (!((M = r == null ? void 0 : r.data) != null && M.instanceId))
14
19
  return;
15
- const { instanceId: i, type: d } = s.data, g = n.current;
16
- t((p = g[i]) == null ? void 0 : p[d], s.data), t((R = g["*"]) == null ? void 0 : R[d], s.data), t((l = g[i]) == null ? void 0 : l["*"], s.data), t((E = g["*"]) == null ? void 0 : E["*"], s.data);
20
+ const { instanceId: i, type: d } = r.data, b = s.current;
21
+ e((p = b[i]) == null ? void 0 : p[d], r.data), e((R = b["*"]) == null ? void 0 : R[d], r.data), e((m = b[i]) == null ? void 0 : m["*"], r.data), e((w = b["*"]) == null ? void 0 : w["*"], r.data);
17
22
  };
18
- return window.addEventListener("message", r), () => {
19
- window.removeEventListener("message", r);
23
+ return window.addEventListener("message", n), () => {
24
+ window.removeEventListener("message", n);
20
25
  };
21
26
  }, [o]);
22
- const a = y(
23
- (t, r, s) => {
24
- const i = o[t];
27
+ const c = E(
28
+ (e, n, r) => {
29
+ console.log("sendMessage", e, n, r);
30
+ const i = o[e];
25
31
  if (!i) {
26
- console.warn(`Origin not set for id: ${t}. Unable to send message.`);
32
+ console.warn(`Origin not set for id: ${e}. Unable to send message.`);
27
33
  return;
28
34
  }
29
35
  i.frameRef.current.contentWindow.postMessage(
30
- { type: r, ...s, instanceId: t },
36
+ { type: n, ...r, instanceId: e },
31
37
  i.origin
32
38
  );
33
39
  },
34
40
  [o]
35
- ), b = y(
36
- (t, r) => {
37
- a(t, "load-options", r);
41
+ ), g = E(
42
+ (e, n) => {
43
+ c(e, "load-options", n);
38
44
  },
39
- [a]
40
- ), m = y(
41
- (t, r) => {
42
- a(t, "validate", r);
45
+ [c]
46
+ ), $ = E(
47
+ (e, n) => {
48
+ c(e, "validate", n);
43
49
  },
44
- [a]
45
- ), x = {
46
- registerFrame: f,
47
- sendMessage: a,
48
- loadOptions: b,
49
- validate: m,
50
- addListener: (t, r, s) => (n.current[t] || (n.current[t] = {}), n.current[t][r] || (n.current[t][r] = []), n.current[t][r].push(s), () => {
51
- n.current[t][r] = n.current[t][r].filter((i) => i !== s);
50
+ [c]
51
+ ), L = {
52
+ registerFrame: l,
53
+ unregisterFrame: u,
54
+ sendMessage: c,
55
+ loadOptions: g,
56
+ validate: $,
57
+ addListener: (e, n, r) => (s.current[e] || (s.current[e] = {}), s.current[e][n] || (s.current[e][n] = []), s.current[e][n].push(r), () => {
58
+ s.current[e][n] = s.current[e][n].filter((i) => i !== r);
52
59
  })
53
60
  };
54
- return /* @__PURE__ */ $.createElement(P.Provider, { value: x }, e);
55
- }, I = {}, U = async (e) => {
61
+ return /* @__PURE__ */ x.createElement(P.Provider, { value: L }, t);
62
+ }, I = {}, Z = async (t) => {
56
63
  try {
57
- const o = new URL(e).origin;
64
+ const o = new URL(t).origin;
58
65
  if (I[o] !== void 0)
59
66
  return I[o];
60
- const n = await (await fetch(`${o}/mm-app-manifest.json`)).json();
61
- if (!n.version)
67
+ const s = await (await fetch(`${o}/mm-app-manifest.json`)).json();
68
+ if (!s.version)
62
69
  throw new Error("Invalid manifest");
63
- return I[o] = n, n;
70
+ return I[o] = s, s;
64
71
  } catch {
65
72
  }
66
73
  return I[origin] = !1, !1;
67
- }, Z = async (e) => !!await U(e), j = (e, o, c) => {
68
- const n = A(P);
69
- if (n === null)
74
+ }, j = async (t) => !!await Z(t), O = (t, o, a) => {
75
+ const s = A(P);
76
+ if (s === null)
70
77
  throw new Error(
71
78
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
72
79
  );
73
- const f = L(c);
74
- return u(() => {
75
- f.current = c;
76
- }, [c]), u(() => {
77
- if (!(!e || !o))
78
- return n.addListener(
79
- e,
80
+ const l = F(a);
81
+ return f(() => {
82
+ l.current = a;
83
+ }, [a]), f(() => {
84
+ if (!(!t || !o))
85
+ return s.addListener(
86
+ t,
80
87
  o,
81
- (...a) => f.current(...a)
88
+ (...u) => l.current(...u)
82
89
  );
83
- }, [n, e, o]), y(
84
- (a, b) => n.sendMessage(e, a, b),
85
- [n, e]
90
+ }, [s, t, o]), E(
91
+ (u, c) => s.sendMessage(t, u, c),
92
+ [s, t]
86
93
  );
87
94
  };
88
- F.propTypes = {
89
- children: h.node.isRequired
95
+ U.propTypes = {
96
+ children: y.node.isRequired
90
97
  };
91
- const O = v.default || v, T = O.iframe`
98
+ const T = v.default || v, q = T.iframe`
92
99
  width: 100%;
93
100
  height: 100%;
94
101
  border: none;
95
- visibility: ${(e) => e.$isReady ? "visible" : "hidden"};
96
- `, q = ({
97
- id: e,
102
+ visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
103
+ `, S = ({
104
+ id: t,
98
105
  src: o,
99
- path: c,
100
- onInit: n = () => {
106
+ path: a,
107
+ onInit: s = () => {
101
108
  },
102
- context: f,
103
- isConfiguring: a,
104
- ...b
109
+ context: l,
110
+ isConfiguring: u,
111
+ ...c
105
112
  }) => {
106
- const m = L(), [x, t] = w(), [r, s] = w(0), { registerFrame: i, sendMessage: d } = A(P), [g, M] = w(!0), [p, R] = w();
107
- return u(() => {
108
- r && d(e, "set-context", { context: f });
109
- }, [f, e, r]), u(() => {
110
- c !== void 0 && R(c);
111
- }, [c]), u(() => {
112
- p === void 0 || !r || d(e, "navigate", { path: p });
113
- }, [p, r, e, d]), u(() => {
114
- r && d(e, "set-configuring", { isConfiguring: a });
115
- }, [r, a]), u(() => {
116
- !o || !e || !m || (async () => {
117
- const l = await Z(o);
118
- if (M(l), l) {
119
- const E = new URL(o);
120
- i(e, E.origin, m), n(), t(
121
- `${E.origin}/mm-embedded-interop?id=${e}&origin=${window.location.origin}`
122
- );
123
- return;
124
- }
125
- })();
126
- }, [o, e, i, m]), j(e, "ready", () => s((l) => l + 1)), g ? /* @__PURE__ */ $.createElement(T, { $isReady: r, ref: m, src: x, ...b }) : /* @__PURE__ */ $.createElement(
113
+ const g = F(), [$, L] = h(), [e, n] = h(0), { registerFrame: r, unregisterFrame: i, sendMessage: d } = A(
114
+ P
115
+ ), [b, M] = h(!0), [p, R] = h();
116
+ return f(() => {
117
+ e && d(t, "set-context", { context: l });
118
+ }, [l, t, e]), f(() => {
119
+ a !== void 0 && R(a);
120
+ }, [a]), f(() => {
121
+ p === void 0 || !e || d(t, "navigate", { path: p });
122
+ }, [p, e, t, d]), f(() => {
123
+ e && d(t, "set-configuring", { isConfiguring: u });
124
+ }, [e, u]), f(() => {
125
+ if (!(!o || !t || !g))
126
+ return (async () => {
127
+ const m = await j(o);
128
+ if (M(m), m) {
129
+ const w = new URL(o);
130
+ r(t, w.origin, g), s(), L(
131
+ `${w.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`
132
+ );
133
+ return;
134
+ }
135
+ })(), () => {
136
+ i(t);
137
+ };
138
+ }, [o, t, r, g]), O(t, "ready", () => n((m) => m + 1)), b ? /* @__PURE__ */ x.createElement(q, { $isReady: e, ref: g, src: $, ...c }) : /* @__PURE__ */ x.createElement(
127
139
  "div",
128
140
  {
129
141
  style: {
@@ -133,20 +145,20 @@ const O = v.default || v, T = O.iframe`
133
145
  justifyContent: "center"
134
146
  }
135
147
  },
136
- /* @__PURE__ */ $.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
148
+ /* @__PURE__ */ x.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
137
149
  );
138
150
  };
139
- q.propTypes = {
140
- id: h.string,
141
- src: h.string.isRequired,
142
- path: h.string,
143
- onInit: h.func
151
+ S.propTypes = {
152
+ id: y.string,
153
+ src: y.string.isRequired,
154
+ path: y.string,
155
+ onInit: y.func
144
156
  };
145
157
  export {
146
- q as MMEmbeddableZone,
158
+ S as MMEmbeddableZone,
147
159
  P as MMEmbeddableZoneContext,
148
- F as MMEmbeddableZoneProvider,
149
- U as getAppInfo,
150
- Z as testIsEmbeddable,
151
- j as useEmbeddedEffect
160
+ U as MMEmbeddableZoneProvider,
161
+ Z as getAppInfo,
162
+ j as testIsEmbeddable,
163
+ O as useEmbeddedEffect
152
164
  };
@@ -1,6 +1,6 @@
1
- (function(u,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(u=typeof globalThis<"u"?globalThis:u||self,e(u["mm-react-embeddable"]={},u.React,u.PropTypes,u.styled))})(this,function(u,e,p,L){"use strict";const M=e.createContext(null),S=({children:t})=>{const[o,f]=e.useState({}),s=e.useRef({}),l=e.useCallback((n,r,i)=>{f(d=>({...d,[n]:{origin:r,frameRef:i}}))},[]);e.useEffect(()=>{const n=(i=[],d)=>{i.forEach(c=>c(d))},r=i=>{var I,g,C,m,h;if(!((I=i==null?void 0:i.data)!=null&&I.instanceId))return;const{instanceId:d,type:c}=i.data,E=s.current;n((g=E[d])==null?void 0:g[c],i.data),n((C=E["*"])==null?void 0:C[c],i.data),n((m=E[d])==null?void 0:m["*"],i.data),n((h=E["*"])==null?void 0:h["*"],i.data)};return window.addEventListener("message",r),()=>{window.removeEventListener("message",r)}},[o]);const a=e.useCallback((n,r,i)=>{const d=o[n];if(!d){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}d.frameRef.current.contentWindow.postMessage({type:r,...i,instanceId:n},d.origin)},[o]),y=e.useCallback((n,r)=>{a(n,"load-options",r)},[a]),b=e.useCallback((n,r)=>{a(n,"validate",r)},[a]),$={registerFrame:l,sendMessage:a,loadOptions:y,validate:b,addListener:(n,r,i)=>(s.current[n]||(s.current[n]={}),s.current[n][r]||(s.current[n][r]=[]),s.current[n][r].push(i),()=>{s.current[n][r]=s.current[n][r].filter(d=>d!==i)})};return e.createElement(M.Provider,{value:$},t)},w={},Z=async t=>{try{const o=new URL(t).origin;if(w[o]!==void 0)return w[o];const s=await(await fetch(`${o}/mm-app-manifest.json`)).json();if(!s.version)throw new Error("Invalid manifest");return w[o]=s,s}catch{}return w[origin]=!1,!1},v=async t=>!!await Z(t),j=(t,o,f)=>{const s=e.useContext(M);if(s===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=e.useRef(f);return e.useEffect(()=>{l.current=f},[f]),e.useEffect(()=>{if(!(!t||!o))return s.addListener(t,o,(...a)=>l.current(...a))},[s,t,o]),e.useCallback((a,y)=>s.sendMessage(t,a,y),[s,t])};S.propTypes={children:p.node.isRequired};const x=(L.default||L).iframe`
1
+ (function(d,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(d=typeof globalThis<"u"?globalThis:d||self,e(d["mm-react-embeddable"]={},d.React,d.PropTypes,d.styled))})(this,function(d,e,y,S){"use strict";const h=e.createContext(null),Z=({children:n})=>{const[i,a]=e.useState({}),o=e.useRef({}),m=e.useCallback((t,s,r)=>{a(u=>({...u,[t]:{origin:s,frameRef:r}}))},[]),l=e.useCallback(t=>{a(s=>{const r={...s};return delete r[t],r})},[]);e.useEffect(()=>{const t=(r=[],u)=>{r.forEach(c=>c(u))},s=r=>{var I,p,C,b,M;if(!((I=r==null?void 0:r.data)!=null&&I.instanceId))return;const{instanceId:u,type:c}=r.data,E=o.current;t((p=E[u])==null?void 0:p[c],r.data),t((C=E["*"])==null?void 0:C[c],r.data),t((b=E[u])==null?void 0:b["*"],r.data),t((M=E["*"])==null?void 0:M["*"],r.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const f=e.useCallback((t,s,r)=>{console.log("sendMessage",t,s,r);const u=i[t];if(!u){console.warn(`Origin not set for id: ${t}. Unable to send message.`);return}u.frameRef.current.contentWindow.postMessage({type:s,...r,instanceId:t},u.origin)},[i]),g=e.useCallback((t,s)=>{f(t,"load-options",s)},[f]),$=e.useCallback((t,s)=>{f(t,"validate",s)},[f]),L={registerFrame:m,unregisterFrame:l,sendMessage:f,loadOptions:g,validate:$,addListener:(t,s,r)=>(o.current[t]||(o.current[t]={}),o.current[t][s]||(o.current[t][s]=[]),o.current[t][s].push(r),()=>{o.current[t][s]=o.current[t][s].filter(u=>u!==r)})};return e.createElement(h.Provider,{value:L},n)},w={},v=async n=>{try{const i=new URL(n).origin;if(w[i]!==void 0)return w[i];const o=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!o.version)throw new Error("Invalid manifest");return w[i]=o,o}catch{}return w[origin]=!1,!1},x=async n=>!!await v(n),F=(n,i,a)=>{const o=e.useContext(h);if(o===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=e.useRef(a);return e.useEffect(()=>{m.current=a},[a]),e.useEffect(()=>{if(!(!n||!i))return o.addListener(n,i,(...l)=>m.current(...l))},[o,n,i]),e.useCallback((l,f)=>o.sendMessage(n,l,f),[o,n])};Z.propTypes={children:y.node.isRequired};const q=(S.default||S).iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
- visibility: ${t=>t.$isReady?"visible":"hidden"};
6
- `,q=({id:t,src:o,path:f,onInit:s=()=>{},context:l,isConfiguring:a,...y})=>{const b=e.useRef(),[$,n]=e.useState(),[r,i]=e.useState(0),{registerFrame:d,sendMessage:c}=e.useContext(M),[E,I]=e.useState(!0),[g,C]=e.useState();return e.useEffect(()=>{r&&c(t,"set-context",{context:l})},[l,t,r]),e.useEffect(()=>{f!==void 0&&C(f)},[f]),e.useEffect(()=>{g===void 0||!r||c(t,"navigate",{path:g})},[g,r,t,c]),e.useEffect(()=>{r&&c(t,"set-configuring",{isConfiguring:a})},[r,a]),e.useEffect(()=>{!o||!t||!b||(async()=>{const m=await v(o);if(I(m),m){const h=new URL(o);d(t,h.origin,b),s(),n(`${h.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`);return}})()},[o,t,d,b]),j(t,"ready",()=>i(m=>m+1)),E?e.createElement(x,{$isReady:r,ref:b,src:$,...y}):e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};q.propTypes={id:p.string,src:p.string.isRequired,path:p.string,onInit:p.func},u.MMEmbeddableZone=q,u.MMEmbeddableZoneContext=M,u.MMEmbeddableZoneProvider=S,u.getAppInfo=Z,u.testIsEmbeddable=v,u.useEmbeddedEffect=j,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
5
+ visibility: ${n=>n.$isReady?"visible":"hidden"};
6
+ `,j=({id:n,src:i,path:a,onInit:o=()=>{},context:m,isConfiguring:l,...f})=>{const g=e.useRef(),[$,L]=e.useState(),[t,s]=e.useState(0),{registerFrame:r,unregisterFrame:u,sendMessage:c}=e.useContext(h),[E,I]=e.useState(!0),[p,C]=e.useState();return e.useEffect(()=>{t&&c(n,"set-context",{context:m})},[m,n,t]),e.useEffect(()=>{a!==void 0&&C(a)},[a]),e.useEffect(()=>{p===void 0||!t||c(n,"navigate",{path:p})},[p,t,n,c]),e.useEffect(()=>{t&&c(n,"set-configuring",{isConfiguring:l})},[t,l]),e.useEffect(()=>{if(!(!i||!n||!g))return(async()=>{const b=await x(i);if(I(b),b){const M=new URL(i);r(n,M.origin,g),o(),L(`${M.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`);return}})(),()=>{u(n)}},[i,n,r,g]),F(n,"ready",()=>s(b=>b+1)),E?e.createElement(q,{$isReady:t,ref:g,src:$,...f}):e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};j.propTypes={id:y.string,src:y.string.isRequired,path:y.string,onInit:y.func},d.MMEmbeddableZone=j,d.MMEmbeddableZoneContext=h,d.MMEmbeddableZoneProvider=Z,d.getAppInfo=v,d.testIsEmbeddable=x,d.useEmbeddedEffect=F,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "0.10.1",
3
+ "version": "0.10.2-1",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",