@machinemetrics/mm-react-embeddable 1.2.0 → 1.2.1-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.
@@ -1,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),w=require("prop-types"),v=require("styled-components"),Z=e.createContext(null),A=({children:n})=>{const[i,a]=e.useState({}),[u,f]=e.useState({}),c=e.useRef({}),E=e.useRef({}),d=e.useRef({}),h=e.useCallback((t,s)=>{var r;return!!((r=d.current[t])!=null&&r.has(s))},[]),C=e.useCallback((t,s,r)=>{a(o=>({...o,[t]:{origin:s,frameRef:r}}))},[]),m=e.useCallback(t=>{a(s=>{const r={...s};return delete r[t],r}),f(s=>{const r={...s};return delete r[t],r}),delete d.current[t]},[]);e.useEffect(()=>{const t=(r=[],o)=>{r.forEach(l=>l(o))},s=r=>{var F,S,R,P,T;if(!((F=r==null?void 0:r.data)!=null&&F.instanceId))return;const{instanceId:o,type:l}=r.data,b=c.current;if(l==="response"){const{callbackId:M,response:H,error:B}=r.data,U=E.current[M];U&&(U(H,B),delete E.current[M]);return}if(l==="mark-registered"){const{requestType:M}=r.data;d.current[o]||(d.current[o]=new Set),d.current[o].add(M);return}l==="ready"&&f(M=>({...M,[o]:!0})),t((S=b[o])==null?void 0:S[l],r.data),t((R=b["*"])==null?void 0:R[l],r.data),t((P=b[o])==null?void 0:P["*"],r.data),t((T=b["*"])==null?void 0:T["*"],r.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const g=e.useCallback((t,s,r)=>{var l,b;const o=i[t];if(!o){console.warn(`Origin not set for id: ${t}. Unable to send message.`);return}(b=(l=o==null?void 0:o.frameRef)==null?void 0:l.current)!=null&&b.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...r,instanceId:t},o.origin)},[i]),k=e.useRef(0),q=e.useCallback(()=>`${Date.now()}-${++k.current}`,[]),y=e.useCallback(async(t,s,r={})=>new Promise((o,l)=>{const b=q(),F=(S,R)=>{delete E.current[b],R?l(R):o(S)};E.current[b]=F,g(t,"request",{callbackId:b,requestType:s,data:r})}),[g]),I=e.useCallback((t,s)=>{g(t,"load-options",s)},[g]),$=e.useCallback((t,s)=>{g(t,"validate",s)},[g]),p=e.useMemo(()=>({registerFrame:C,unregisterFrame:m,sendMessage:g,sendRequest:y,loadOptions:I,validate:$,hasHandler:h,readyFrames:u,addListener:(t,s,r)=>(c.current[t]||(c.current[t]={}),c.current[t][s]||(c.current[t][s]=[]),c.current[t][s].push(r),()=>{c.current[t][s]=c.current[t][s].filter(o=>o!==r)})}),[C,m,g,y,I,$,h,u]);return e.createElement(Z.Provider,{value:p},n)},x={},O=async n=>{try{const i=new URL(n).origin;if(x[i]!==void 0)return x[i];const u=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!u.version)throw new Error("Invalid manifest");return x[i]=u,u}catch{}return x[origin]=!1,!1},j=async n=>!!await O(n),L=(n,i,a)=>{const u=e.useContext(Z);if(u===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const f=e.useRef(a);e.useEffect(()=>{f.current=a},[a]),e.useEffect(()=>{if(!(!n||!i)){if(i.startsWith("key:")){const c=i.substring(4);return u.readyFrames[n]&&u.sendMessage(n,"subscribe-key",{keyChord:c}),u.addListener(n,`key-pressed:${c}`,(...d)=>f.current(...d))}return u.addListener(n,i,(...c)=>f.current(...c))}},[u,n,i])};A.propTypes={children:w.node.isRequired};const D=v.default||v,_=D.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),R=require("prop-types"),v=require("styled-components"),Z=e.createContext(null),A=({children:n,manifestProxyBaseUrl:c=null})=>{const[i,u]=e.useState({}),[l,a]=e.useState({}),f=e.useRef({}),d=e.useRef({}),p=e.useRef({}),$=e.useCallback((r,s)=>{var t;return!!((t=p.current[r])!=null&&t.has(s))},[]),E=e.useCallback((r,s,t)=>{u(o=>({...o,[r]:{origin:s,frameRef:t}}))},[]),I=e.useCallback(r=>{u(s=>{const t={...s};return delete t[r],t}),a(s=>{const t={...s};return delete t[r],t}),delete p.current[r]},[]);e.useEffect(()=>{const r=(t=[],o)=>{t.forEach(b=>b(o))},s=t=>{var S,x,C,T,U;if(!((S=t==null?void 0:t.data)!=null&&S.instanceId))return;const{instanceId:o,type:b}=t.data,g=f.current;if(b==="response"){const{callbackId:M,response:B,error:D}=t.data,j=d.current[M];j&&(j(B,D),delete d.current[M]);return}if(b==="mark-registered"){const{requestType:M}=t.data;p.current[o]||(p.current[o]=new Set),p.current[o].add(M);return}b==="ready"&&a(M=>({...M,[o]:!0})),r((x=g[o])==null?void 0:x[b],t.data),r((C=g["*"])==null?void 0:C[b],t.data),r((T=g[o])==null?void 0:T["*"],t.data),r((U=g["*"])==null?void 0:U["*"],t.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const m=e.useCallback((r,s,t)=>{var b,g;const o=i[r];if(!o){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}(g=(b=o==null?void 0:o.frameRef)==null?void 0:b.current)!=null&&g.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...t,instanceId:r},o.origin)},[i]),q=e.useRef(0),y=e.useCallback(()=>`${Date.now()}-${++q.current}`,[]),w=e.useCallback(async(r,s,t={})=>new Promise((o,b)=>{const g=y(),S=(x,C)=>{delete d.current[g],C?b(C):o(x)};d.current[g]=S,m(r,"request",{callbackId:g,requestType:s,data:t})}),[m]),k=e.useCallback((r,s)=>{m(r,"load-options",s)},[m]),F=e.useCallback((r,s)=>{m(r,"validate",s)},[m]),h=e.useMemo(()=>({registerFrame:E,unregisterFrame:I,sendMessage:m,sendRequest:w,loadOptions:k,validate:F,hasHandler:$,readyFrames:l,manifestProxyBaseUrl:c,addListener:(r,s,t)=>(f.current[r]||(f.current[r]={}),f.current[r][s]||(f.current[r][s]=[]),f.current[r][s].push(t),()=>{f.current[r][s]=f.current[r][s].filter(o=>o!==t)})}),[E,I,m,w,k,F,$,l,c]);return e.createElement(Z.Provider,{value:h},n)},L={},O=async(n,c=null)=>{try{const i=new URL(n).origin;if(L[i]!==void 0)return L[i];const u=c?`${c}?url=${encodeURIComponent(`${i}/mm-app-manifest.json`)}`:`${i}/mm-app-manifest.json`,a=await(await fetch(u)).json();if(!a.version)throw new Error("Invalid manifest");return L[i]=a,a}catch{}return L[new URL(n).origin]=!1,!1},W=async(n,c=null)=>!!await O(n,c),P=(n,c,i)=>{const u=e.useContext(Z);if(u===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=e.useRef(i);e.useEffect(()=>{l.current=i},[i]),e.useEffect(()=>{if(!(!n||!c)){if(c.startsWith("key:")){const a=c.substring(4);return u.readyFrames[n]&&u.sendMessage(n,"subscribe-key",{keyChord:a}),u.addListener(n,`key-pressed:${a}`,(...d)=>l.current(...d))}return u.addListener(n,c,(...a)=>l.current(...a))}},[u,n,c])};A.propTypes={children:R.node.isRequired,manifestProxyBaseUrl:R.string};const _=v.default||v,z=_.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${n=>n.$isReady?"visible":"hidden"};
6
- `,W=({id:n,src:i,path:a,onInit:u=()=>{},context:f={},params:c={},...E})=>{if("isConfiguring"in E)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in f)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in f)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const d=e.useRef(),[h,C]=e.useState(),[m,g]=e.useState(0),{registerFrame:k,unregisterFrame:q,sendMessage:y}=e.useContext(Z),[I,$]=e.useState(!0),[p,t]=e.useState();e.useEffect(()=>{m&&y(n,"set-context",{context:{...f,colorMode:c.colorMode,isFullScreen:c.isFullScreen}})},[f,c.colorMode,c.isFullScreen,n,m]),e.useEffect(()=>{m&&(y(n,"set-params",{params:c}),y(n,"set-configuring",{isConfiguring:c.isConfiguring}))},[c,n,m]),e.useEffect(()=>{a!==void 0&&t((a==null?void 0:a.indexOf("/"))===0?a:`/${a}`)},[a]),e.useEffect(()=>{p===void 0||!m||y(n,"navigate",{path:p})},[p,m,n,y]),e.useEffect(()=>{if(!(!i||!n||!d))return(async()=>{const r=await j(i);if($(r),r){const o=new URL(i);k(n,o.origin,d),u(),C(`${o.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`);return}})(),()=>{q(n)}},[i,n,k,d]),L(n,"ready",()=>g(r=>r+1));const s=e.useRef();return L(n,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(d.current){const r=new URL(i);d.current.src=`${r.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),I?e.createElement(_,{$isReady:m,ref:d,src:h,...E}):e.createElement("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};W.propTypes={id:w.string,src:w.string.isRequired,path:w.string,onInit:w.func};exports.MMEmbeddableZone=W;exports.MMEmbeddableZoneContext=Z;exports.MMEmbeddableZoneProvider=A;exports.getAppInfo=O;exports.testIsEmbeddable=j;exports.useEmbeddedEffect=L;
6
+ `,H=({id:n,src:c,path:i,onInit:u=()=>{},context:l={},params:a={},...f})=>{if("isConfiguring"in f)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in l)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in l)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const d=e.useRef(),[p,$]=e.useState(),[E,I]=e.useState(0),{registerFrame:m,unregisterFrame:q,sendMessage:y,manifestProxyBaseUrl:w}=e.useContext(Z),[k,F]=e.useState(!0),[h,r]=e.useState();e.useEffect(()=>{E&&y(n,"set-context",{context:{...l,colorMode:a.colorMode,isFullScreen:a.isFullScreen}})},[l,a.colorMode,a.isFullScreen,n,E]),e.useEffect(()=>{E&&(y(n,"set-params",{params:a}),y(n,"set-configuring",{isConfiguring:a.isConfiguring}))},[a,n,E]),e.useEffect(()=>{i!==void 0&&r((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),e.useEffect(()=>{h===void 0||!E||y(n,"navigate",{path:h})},[h,E,n,y]),e.useEffect(()=>{if(!(!c||!n||!d))return(async()=>{const t=await W(c,w);if(F(t),t){const o=new URL(c);m(n,o.origin,d),u(),$(`${o.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`);return}})(),()=>{q(n)}},[c,n,m,d,w]),P(n,"ready",()=>I(t=>t+1));const s=e.useRef();return P(n,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(d.current){const t=new URL(c);d.current.src=`${t.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),k?e.createElement(z,{$isReady:E,ref:d,src:p,...f}):e.createElement("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};H.propTypes={id:R.string,src:R.string.isRequired,path:R.string,onInit:R.func};exports.MMEmbeddableZone=H;exports.MMEmbeddableZoneContext=Z;exports.MMEmbeddableZoneProvider=A;exports.getAppInfo=O;exports.testIsEmbeddable=W;exports.useEmbeddedEffect=P;
@@ -1,216 +1,219 @@
1
- import Z, { createContext as _, useState as R, useRef as M, useCallback as w, useEffect as b, useMemo as z, useContext as H } from "react";
2
- import F from "prop-types";
3
- import v from "styled-components";
4
- const U = _(null), G = ({ children: t }) => {
5
- const [o, c] = R({}), [a, l] = R({}), i = M({}), p = M({}), u = M({}), I = w((r, n) => {
1
+ import Z, { createContext as z, useState as h, useRef as R, useCallback as y, useEffect as p, useMemo as G, useContext as B } from "react";
2
+ import $ from "prop-types";
3
+ import W from "styled-components";
4
+ const j = z(null), J = ({ children: n, manifestProxyBaseUrl: i = null }) => {
5
+ const [o, a] = h({}), [d, c] = h({}), l = R({}), u = R({}), w = R({}), x = y((r, t) => {
6
6
  var e;
7
- return !!((e = u.current[r]) != null && e.has(n));
8
- }, []), C = w((r, n, e) => {
9
- c((s) => ({ ...s, [r]: { origin: n, frameRef: e } }));
10
- }, []), m = w((r) => {
11
- c((n) => {
12
- const e = { ...n };
7
+ return !!((e = w.current[r]) != null && e.has(t));
8
+ }, []), b = y((r, t, e) => {
9
+ a((s) => ({ ...s, [r]: { origin: t, frameRef: e } }));
10
+ }, []), L = y((r) => {
11
+ a((t) => {
12
+ const e = { ...t };
13
13
  return delete e[r], e;
14
- }), l((n) => {
15
- const e = { ...n };
14
+ }), c((t) => {
15
+ const e = { ...t };
16
16
  return delete e[r], e;
17
- }), delete u.current[r];
17
+ }), delete w.current[r];
18
18
  }, []);
19
- b(() => {
19
+ p(() => {
20
20
  const r = (e = [], s) => {
21
- e.forEach((d) => d(s));
22
- }, n = (e) => {
23
- var T, P, $, A, O;
21
+ e.forEach((f) => f(s));
22
+ }, t = (e) => {
23
+ var T, P, C, A, O;
24
24
  if (!((T = e == null ? void 0 : e.data) != null && T.instanceId))
25
25
  return;
26
- const { instanceId: s, type: d } = e.data, f = i.current;
27
- if (d === "response") {
28
- const { callbackId: E, response: B, error: D } = e.data, j = p.current[E];
29
- j && (j(B, D), delete p.current[E]);
26
+ const { instanceId: s, type: f } = e.data, g = l.current;
27
+ if (f === "response") {
28
+ const { callbackId: E, response: D, error: _ } = e.data, v = u.current[E];
29
+ v && (v(D, _), delete u.current[E]);
30
30
  return;
31
31
  }
32
- if (d === "mark-registered") {
32
+ if (f === "mark-registered") {
33
33
  const { requestType: E } = e.data;
34
- u.current[s] || (u.current[s] = /* @__PURE__ */ new Set()), u.current[s].add(E);
34
+ w.current[s] || (w.current[s] = /* @__PURE__ */ new Set()), w.current[s].add(E);
35
35
  return;
36
36
  }
37
- d === "ready" && l((E) => ({ ...E, [s]: !0 })), r((P = f[s]) == null ? void 0 : P[d], e.data), r(($ = f["*"]) == null ? void 0 : $[d], e.data), r((A = f[s]) == null ? void 0 : A["*"], e.data), r((O = f["*"]) == null ? void 0 : O["*"], e.data);
37
+ f === "ready" && c((E) => ({ ...E, [s]: !0 })), r((P = g[s]) == null ? void 0 : P[f], e.data), r((C = g["*"]) == null ? void 0 : C[f], e.data), r((A = g[s]) == null ? void 0 : A["*"], e.data), r((O = g["*"]) == null ? void 0 : O["*"], e.data);
38
38
  };
39
- return window.addEventListener("message", n), () => {
40
- window.removeEventListener("message", n);
39
+ return window.addEventListener("message", t), () => {
40
+ window.removeEventListener("message", t);
41
41
  };
42
42
  }, [o]);
43
- const g = w(
44
- (r, n, e) => {
45
- var d, f;
43
+ const m = y(
44
+ (r, t, e) => {
45
+ var f, g;
46
46
  const s = o[r];
47
47
  if (!s) {
48
48
  console.warn(`Origin not set for id: ${r}. Unable to send message.`);
49
49
  return;
50
50
  }
51
- (f = (d = s == null ? void 0 : s.frameRef) == null ? void 0 : d.current) != null && f.contentWindow && s.frameRef.current.contentWindow.postMessage(
52
- { type: n, ...e, instanceId: r },
51
+ (g = (f = s == null ? void 0 : s.frameRef) == null ? void 0 : f.current) != null && g.contentWindow && s.frameRef.current.contentWindow.postMessage(
52
+ { type: t, ...e, instanceId: r },
53
53
  s.origin
54
54
  );
55
55
  },
56
56
  [o]
57
- ), x = M(0), q = w(() => `${Date.now()}-${++x.current}`, []), y = w(
58
- async (r, n, e = {}) => new Promise((s, d) => {
59
- const f = q(), T = (P, $) => {
60
- delete p.current[f], $ ? d($) : s(P);
57
+ ), q = R(0), M = y(() => `${Date.now()}-${++q.current}`, []), I = y(
58
+ async (r, t, e = {}) => new Promise((s, f) => {
59
+ const g = M(), T = (P, C) => {
60
+ delete u.current[g], C ? f(C) : s(P);
61
61
  };
62
- p.current[f] = T, g(r, "request", { callbackId: f, requestType: n, data: e });
62
+ u.current[g] = T, m(r, "request", { callbackId: g, requestType: t, data: e });
63
63
  }),
64
- [g]
65
- ), k = w(
66
- (r, n) => {
67
- g(r, "load-options", n);
64
+ [m]
65
+ ), k = y(
66
+ (r, t) => {
67
+ m(r, "load-options", t);
68
68
  },
69
- [g]
70
- ), L = w(
71
- (r, n) => {
72
- g(r, "validate", n);
69
+ [m]
70
+ ), U = y(
71
+ (r, t) => {
72
+ m(r, "validate", t);
73
73
  },
74
- [g]
75
- ), h = z(
74
+ [m]
75
+ ), F = G(
76
76
  () => ({
77
- registerFrame: C,
78
- unregisterFrame: m,
79
- sendMessage: g,
80
- sendRequest: y,
77
+ registerFrame: b,
78
+ unregisterFrame: L,
79
+ sendMessage: m,
80
+ sendRequest: I,
81
81
  loadOptions: k,
82
- validate: L,
83
- hasHandler: I,
84
- readyFrames: a,
85
- addListener: (r, n, e) => (i.current[r] || (i.current[r] = {}), i.current[r][n] || (i.current[r][n] = []), i.current[r][n].push(e), () => {
86
- i.current[r][n] = i.current[r][n].filter((s) => s !== e);
82
+ validate: U,
83
+ hasHandler: x,
84
+ readyFrames: d,
85
+ manifestProxyBaseUrl: i,
86
+ addListener: (r, t, e) => (l.current[r] || (l.current[r] = {}), l.current[r][t] || (l.current[r][t] = []), l.current[r][t].push(e), () => {
87
+ l.current[r][t] = l.current[r][t].filter((s) => s !== e);
87
88
  })
88
89
  }),
89
90
  [
90
- C,
91
- m,
92
- g,
93
- y,
94
- k,
91
+ b,
95
92
  L,
93
+ m,
96
94
  I,
97
- a
95
+ k,
96
+ U,
97
+ x,
98
+ d,
99
+ i
98
100
  ]
99
101
  );
100
- return /* @__PURE__ */ Z.createElement(U.Provider, { value: h }, t);
101
- }, S = {}, J = async (t) => {
102
+ return /* @__PURE__ */ Z.createElement(j.Provider, { value: F }, n);
103
+ }, S = {}, K = async (n, i = null) => {
102
104
  try {
103
- const o = new URL(t).origin;
105
+ const o = new URL(n).origin;
104
106
  if (S[o] !== void 0)
105
107
  return S[o];
106
- const a = await (await fetch(`${o}/mm-app-manifest.json`)).json();
107
- if (!a.version)
108
+ const a = i ? `${i}?url=${encodeURIComponent(`${o}/mm-app-manifest.json`)}` : `${o}/mm-app-manifest.json`, c = await (await fetch(a)).json();
109
+ if (!c.version)
108
110
  throw new Error("Invalid manifest");
109
- return S[o] = a, a;
111
+ return S[o] = c, c;
110
112
  } catch {
111
113
  }
112
- return S[origin] = !1, !1;
113
- }, K = async (t) => !!await J(t), W = (t, o, c) => {
114
- const a = H(U);
114
+ return S[new URL(n).origin] = !1, !1;
115
+ }, Q = async (n, i = null) => !!await K(n, i), H = (n, i, o) => {
116
+ const a = B(j);
115
117
  if (a === null)
116
118
  throw new Error(
117
119
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
118
120
  );
119
- const l = M(c);
120
- b(() => {
121
- l.current = c;
122
- }, [c]), b(() => {
123
- if (!(!t || !o)) {
124
- if (o.startsWith("key:")) {
125
- const i = o.substring(4);
126
- return a.readyFrames[t] && a.sendMessage(t, "subscribe-key", { keyChord: i }), a.addListener(
127
- t,
128
- `key-pressed:${i}`,
129
- (...u) => l.current(...u)
121
+ const d = R(o);
122
+ p(() => {
123
+ d.current = o;
124
+ }, [o]), p(() => {
125
+ if (!(!n || !i)) {
126
+ if (i.startsWith("key:")) {
127
+ const c = i.substring(4);
128
+ return a.readyFrames[n] && a.sendMessage(n, "subscribe-key", { keyChord: c }), a.addListener(
129
+ n,
130
+ `key-pressed:${c}`,
131
+ (...u) => d.current(...u)
130
132
  );
131
133
  }
132
134
  return a.addListener(
133
- t,
134
- o,
135
- (...i) => l.current(...i)
135
+ n,
136
+ i,
137
+ (...c) => d.current(...c)
136
138
  );
137
139
  }
138
- }, [a, t, o]);
140
+ }, [a, n, i]);
139
141
  };
140
- G.propTypes = {
141
- children: F.node.isRequired
142
+ J.propTypes = {
143
+ children: $.node.isRequired,
144
+ manifestProxyBaseUrl: $.string
142
145
  };
143
- const Q = v.default || v, V = Q.iframe`
146
+ const V = W.default || W, X = V.iframe`
144
147
  width: 100%;
145
148
  height: 100%;
146
149
  border: none;
147
- visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
148
- `, X = ({
149
- id: t,
150
- src: o,
151
- path: c,
150
+ visibility: ${(n) => n.$isReady ? "visible" : "hidden"};
151
+ `, Y = ({
152
+ id: n,
153
+ src: i,
154
+ path: o,
152
155
  onInit: a = () => {
153
156
  },
154
- context: l = {},
155
- params: i = {},
156
- ...p
157
+ context: d = {},
158
+ params: c = {},
159
+ ...l
157
160
  }) => {
158
- if ("isConfiguring" in p)
161
+ if ("isConfiguring" in l)
159
162
  return console.error(
160
163
  "isConfiguring must be passed via the params prop to MMEmbeddableZone"
161
164
  ), null;
162
- if ("colorMode" in l)
165
+ if ("colorMode" in d)
163
166
  return console.error(
164
167
  "colorMode must be passed via the params prop to MMEmbeddableZone"
165
168
  ), null;
166
- if ("isFullScreen" in l)
169
+ if ("isFullScreen" in d)
167
170
  return console.error(
168
171
  "isFullScreen must be passed via the params prop to MMEmbeddableZone"
169
172
  ), null;
170
- const u = M(), [I, C] = R(), [m, g] = R(0), { registerFrame: x, unregisterFrame: q, sendMessage: y } = H(
171
- U
172
- ), [k, L] = R(!0), [h, r] = R();
173
- b(() => {
174
- m && y(t, "set-context", {
173
+ const u = R(), [w, x] = h(), [b, L] = h(0), { registerFrame: m, unregisterFrame: q, sendMessage: M, manifestProxyBaseUrl: I } = B(
174
+ j
175
+ ), [k, U] = h(!0), [F, r] = h();
176
+ p(() => {
177
+ b && M(n, "set-context", {
175
178
  context: {
176
- ...l,
179
+ ...d,
177
180
  // TODO: Remove this once everything is migrated to params
178
- colorMode: i.colorMode,
179
- isFullScreen: i.isFullScreen
181
+ colorMode: c.colorMode,
182
+ isFullScreen: c.isFullScreen
180
183
  }
181
184
  });
182
- }, [l, i.colorMode, i.isFullScreen, t, m]), b(() => {
183
- m && (y(t, "set-params", { params: i }), y(t, "set-configuring", { isConfiguring: i.isConfiguring }));
184
- }, [i, t, m]), b(() => {
185
- c !== void 0 && r((c == null ? void 0 : c.indexOf("/")) === 0 ? c : `/${c}`);
186
- }, [c]), b(() => {
187
- h === void 0 || !m || y(t, "navigate", { path: h });
188
- }, [h, m, t, y]), b(() => {
189
- if (!(!o || !t || !u))
185
+ }, [d, c.colorMode, c.isFullScreen, n, b]), p(() => {
186
+ b && (M(n, "set-params", { params: c }), M(n, "set-configuring", { isConfiguring: c.isConfiguring }));
187
+ }, [c, n, b]), p(() => {
188
+ o !== void 0 && r((o == null ? void 0 : o.indexOf("/")) === 0 ? o : `/${o}`);
189
+ }, [o]), p(() => {
190
+ F === void 0 || !b || M(n, "navigate", { path: F });
191
+ }, [F, b, n, M]), p(() => {
192
+ if (!(!i || !n || !u))
190
193
  return (async () => {
191
- const e = await K(o);
192
- if (L(e), e) {
193
- const s = new URL(o);
194
- x(t, s.origin, u), a(), C(
195
- `${s.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`
194
+ const e = await Q(i, I);
195
+ if (U(e), e) {
196
+ const s = new URL(i);
197
+ m(n, s.origin, u), a(), x(
198
+ `${s.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`
196
199
  );
197
200
  return;
198
201
  }
199
202
  })(), () => {
200
- q(t);
203
+ q(n);
201
204
  };
202
- }, [o, t, x, u]), W(t, "ready", () => g((e) => e + 1));
203
- const n = M();
204
- return W(t, "heartbeat", () => {
205
- n.current && clearTimeout(n.current), n.current = setTimeout(() => {
205
+ }, [i, n, m, u, I]), H(n, "ready", () => L((e) => e + 1));
206
+ const t = R();
207
+ return H(n, "heartbeat", () => {
208
+ t.current && clearTimeout(t.current), t.current = setTimeout(() => {
206
209
  if (u.current) {
207
- const e = new URL(o);
208
- u.current.src = `${e.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`;
210
+ const e = new URL(i);
211
+ u.current.src = `${e.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`;
209
212
  }
210
213
  }, 6e3);
211
- }), b(() => () => {
212
- n.current && clearTimeout(n.current);
213
- }, []), k ? /* @__PURE__ */ Z.createElement(V, { $isReady: m, ref: u, src: I, ...p }) : /* @__PURE__ */ Z.createElement(
214
+ }), p(() => () => {
215
+ t.current && clearTimeout(t.current);
216
+ }, []), k ? /* @__PURE__ */ Z.createElement(X, { $isReady: b, ref: u, src: w, ...l }) : /* @__PURE__ */ Z.createElement(
214
217
  "div",
215
218
  {
216
219
  style: {
@@ -224,17 +227,17 @@ const Q = v.default || v, V = Q.iframe`
224
227
  /* @__PURE__ */ Z.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
225
228
  );
226
229
  };
227
- X.propTypes = {
228
- id: F.string,
229
- src: F.string.isRequired,
230
- path: F.string,
231
- onInit: F.func
230
+ Y.propTypes = {
231
+ id: $.string,
232
+ src: $.string.isRequired,
233
+ path: $.string,
234
+ onInit: $.func
232
235
  };
233
236
  export {
234
- X as MMEmbeddableZone,
235
- U as MMEmbeddableZoneContext,
236
- G as MMEmbeddableZoneProvider,
237
- J as getAppInfo,
238
- K as testIsEmbeddable,
239
- W as useEmbeddedEffect
237
+ Y as MMEmbeddableZone,
238
+ j as MMEmbeddableZoneContext,
239
+ J as MMEmbeddableZoneProvider,
240
+ K as getAppInfo,
241
+ Q as testIsEmbeddable,
242
+ H as useEmbeddedEffect
240
243
  };
@@ -1,6 +1,6 @@
1
- (function(a,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(a=typeof globalThis!="undefined"?globalThis:a||self,e(a["mm-react-embeddable"]={},a.React,a.PropTypes,a.styled))})(this,(function(a,e,w,j){"use strict";const k=e.createContext(null),v=({children:t})=>{const[i,d]=e.useState({}),[c,b]=e.useState({}),u=e.useRef({}),y=e.useRef({}),l=e.useRef({}),$=e.useCallback((n,s)=>{var r;return!!((r=l.current[n])!=null&&r.has(s))},[]),F=e.useCallback((n,s,r)=>{d(o=>({...o,[n]:{origin:s,frameRef:r}}))},[]),g=e.useCallback(n=>{d(s=>{const r={...s};return delete r[n],r}),b(s=>{const r={...s};return delete r[n],r}),delete l.current[n]},[]);e.useEffect(()=>{const n=(r=[],o)=>{r.forEach(f=>f(o))},s=r=>{var L,x,C,W,H;if(!((L=r==null?void 0:r.data)!=null&&L.instanceId))return;const{instanceId:o,type:f}=r.data,m=u.current;if(f==="response"){const{callbackId:M,response:z,error:G}=r.data,B=y.current[M];B&&(B(z,G),delete y.current[M]);return}if(f==="mark-registered"){const{requestType:M}=r.data;l.current[o]||(l.current[o]=new Set),l.current[o].add(M);return}f==="ready"&&b(M=>({...M,[o]:!0})),n((x=m[o])==null?void 0:x[f],r.data),n((C=m["*"])==null?void 0:C[f],r.data),n((W=m[o])==null?void 0:W["*"],r.data),n((H=m["*"])==null?void 0:H["*"],r.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const E=e.useCallback((n,s,r)=>{var f,m;const o=i[n];if(!o){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}(m=(f=o==null?void 0:o.frameRef)==null?void 0:f.current)!=null&&m.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...r,instanceId:n},o.origin)},[i]),S=e.useRef(0),U=e.useCallback(()=>`${Date.now()}-${++S.current}`,[]),p=e.useCallback(async(n,s,r={})=>new Promise((o,f)=>{const m=U(),L=(x,C)=>{delete y.current[m],C?f(C):o(x)};y.current[m]=L,E(n,"request",{callbackId:m,requestType:s,data:r})}),[E]),Z=e.useCallback((n,s)=>{E(n,"load-options",s)},[E]),q=e.useCallback((n,s)=>{E(n,"validate",s)},[E]),h=e.useMemo(()=>({registerFrame:F,unregisterFrame:g,sendMessage:E,sendRequest:p,loadOptions:Z,validate:q,hasHandler:$,readyFrames:c,addListener:(n,s,r)=>(u.current[n]||(u.current[n]={}),u.current[n][s]||(u.current[n][s]=[]),u.current[n][s].push(r),()=>{u.current[n][s]=u.current[n][s].filter(o=>o!==r)})}),[F,g,E,p,Z,q,$,c]);return e.createElement(k.Provider,{value:h},t)},I={},A=async t=>{try{const i=new URL(t).origin;if(I[i]!==void 0)return I[i];const c=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!c.version)throw new Error("Invalid manifest");return I[i]=c,c}catch{}return I[origin]=!1,!1},O=async t=>!!await A(t),T=(t,i,d)=>{const c=e.useContext(k);if(c===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=e.useRef(d);e.useEffect(()=>{b.current=d},[d]),e.useEffect(()=>{if(!(!t||!i)){if(i.startsWith("key:")){const u=i.substring(4);return c.readyFrames[t]&&c.sendMessage(t,"subscribe-key",{keyChord:u}),c.addListener(t,`key-pressed:${u}`,(...l)=>b.current(...l))}return c.addListener(t,i,(...u)=>b.current(...u))}},[c,t,i])};v.propTypes={children:w.node.isRequired};const D=(j.default||j).iframe`
1
+ (function(c,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(c=typeof globalThis!="undefined"?globalThis:c||self,e(c["mm-react-embeddable"]={},c.React,c.PropTypes,c.styled))})(this,(function(c,e,w,v){"use strict";const k=e.createContext(null),A=({children:t,manifestProxyBaseUrl:u=null})=>{const[i,l]=e.useState({}),[f,d]=e.useState({}),m=e.useRef({}),a=e.useRef({}),y=e.useRef({}),S=e.useCallback((r,s)=>{var n;return!!((n=y.current[r])!=null&&n.has(s))},[]),p=e.useCallback((r,s,n)=>{l(o=>({...o,[r]:{origin:s,frameRef:n}}))},[]),L=e.useCallback(r=>{l(s=>{const n={...s};return delete n[r],n}),d(s=>{const n={...s};return delete n[r],n}),delete y.current[r]},[]);e.useEffect(()=>{const r=(n=[],o)=>{n.forEach(b=>b(o))},s=n=>{var U,x,I,H,B;if(!((U=n==null?void 0:n.data)!=null&&U.instanceId))return;const{instanceId:o,type:b}=n.data,E=m.current;if(b==="response"){const{callbackId:h,response:G,error:J}=n.data,D=a.current[h];D&&(D(G,J),delete a.current[h]);return}if(b==="mark-registered"){const{requestType:h}=n.data;y.current[o]||(y.current[o]=new Set),y.current[o].add(h);return}b==="ready"&&d(h=>({...h,[o]:!0})),r((x=E[o])==null?void 0:x[b],n.data),r((I=E["*"])==null?void 0:I[b],n.data),r((H=E[o])==null?void 0:H["*"],n.data),r((B=E["*"])==null?void 0:B["*"],n.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const g=e.useCallback((r,s,n)=>{var b,E;const o=i[r];if(!o){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}(E=(b=o==null?void 0:o.frameRef)==null?void 0:b.current)!=null&&E.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...n,instanceId:r},o.origin)},[i]),T=e.useRef(0),M=e.useCallback(()=>`${Date.now()}-${++T.current}`,[]),C=e.useCallback(async(r,s,n={})=>new Promise((o,b)=>{const E=M(),U=(x,I)=>{delete a.current[E],I?b(I):o(x)};a.current[E]=U,g(r,"request",{callbackId:E,requestType:s,data:n})}),[g]),Z=e.useCallback((r,s)=>{g(r,"load-options",s)},[g]),q=e.useCallback((r,s)=>{g(r,"validate",s)},[g]),$=e.useMemo(()=>({registerFrame:p,unregisterFrame:L,sendMessage:g,sendRequest:C,loadOptions:Z,validate:q,hasHandler:S,readyFrames:f,manifestProxyBaseUrl:u,addListener:(r,s,n)=>(m.current[r]||(m.current[r]={}),m.current[r][s]||(m.current[r][s]=[]),m.current[r][s].push(n),()=>{m.current[r][s]=m.current[r][s].filter(o=>o!==n)})}),[p,L,g,C,Z,q,S,f,u]);return e.createElement(k.Provider,{value:$},t)},F={},O=async(t,u=null)=>{try{const i=new URL(t).origin;if(F[i]!==void 0)return F[i];const l=u?`${u}?url=${encodeURIComponent(`${i}/mm-app-manifest.json`)}`:`${i}/mm-app-manifest.json`,d=await(await fetch(l)).json();if(!d.version)throw new Error("Invalid manifest");return F[i]=d,d}catch{}return F[new URL(t).origin]=!1,!1},P=async(t,u=null)=>!!await O(t,u),j=(t,u,i)=>{const l=e.useContext(k);if(l===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const f=e.useRef(i);e.useEffect(()=>{f.current=i},[i]),e.useEffect(()=>{if(!(!t||!u)){if(u.startsWith("key:")){const d=u.substring(4);return l.readyFrames[t]&&l.sendMessage(t,"subscribe-key",{keyChord:d}),l.addListener(t,`key-pressed:${d}`,(...a)=>f.current(...a))}return l.addListener(t,u,(...d)=>f.current(...d))}},[l,t,u])};A.propTypes={children:w.node.isRequired,manifestProxyBaseUrl:w.string};const z=(v.default||v).iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${t=>t.$isReady?"visible":"hidden"};
6
- `,P=({id:t,src:i,path:d,onInit:c=()=>{},context:b={},params:u={},...y})=>{if("isConfiguring"in y)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in b)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in b)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const l=e.useRef(),[$,F]=e.useState(),[g,E]=e.useState(0),{registerFrame:S,unregisterFrame:U,sendMessage:p}=e.useContext(k),[Z,q]=e.useState(!0),[h,n]=e.useState();e.useEffect(()=>{g&&p(t,"set-context",{context:{...b,colorMode:u.colorMode,isFullScreen:u.isFullScreen}})},[b,u.colorMode,u.isFullScreen,t,g]),e.useEffect(()=>{g&&(p(t,"set-params",{params:u}),p(t,"set-configuring",{isConfiguring:u.isConfiguring}))},[u,t,g]),e.useEffect(()=>{d!==void 0&&n((d==null?void 0:d.indexOf("/"))===0?d:`/${d}`)},[d]),e.useEffect(()=>{h===void 0||!g||p(t,"navigate",{path:h})},[h,g,t,p]),e.useEffect(()=>{if(!(!i||!t||!l))return(async()=>{const r=await O(i);if(q(r),r){const o=new URL(i);S(t,o.origin,l),c(),F(`${o.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`);return}})(),()=>{U(t)}},[i,t,S,l]),T(t,"ready",()=>E(r=>r+1));const s=e.useRef();return T(t,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(l.current){const r=new URL(i);l.current.src=`${r.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),Z?e.createElement(D,{$isReady:g,ref:l,src:$,...y}):e.createElement("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};P.propTypes={id:w.string,src:w.string.isRequired,path:w.string,onInit:w.func},a.MMEmbeddableZone=P,a.MMEmbeddableZoneContext=k,a.MMEmbeddableZoneProvider=v,a.getAppInfo=A,a.testIsEmbeddable=O,a.useEmbeddedEffect=T,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
6
+ `,W=({id:t,src:u,path:i,onInit:l=()=>{},context:f={},params:d={},...m})=>{if("isConfiguring"in m)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in f)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in f)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const a=e.useRef(),[y,S]=e.useState(),[p,L]=e.useState(0),{registerFrame:g,unregisterFrame:T,sendMessage:M,manifestProxyBaseUrl:C}=e.useContext(k),[Z,q]=e.useState(!0),[$,r]=e.useState();e.useEffect(()=>{p&&M(t,"set-context",{context:{...f,colorMode:d.colorMode,isFullScreen:d.isFullScreen}})},[f,d.colorMode,d.isFullScreen,t,p]),e.useEffect(()=>{p&&(M(t,"set-params",{params:d}),M(t,"set-configuring",{isConfiguring:d.isConfiguring}))},[d,t,p]),e.useEffect(()=>{i!==void 0&&r((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),e.useEffect(()=>{$===void 0||!p||M(t,"navigate",{path:$})},[$,p,t,M]),e.useEffect(()=>{if(!(!u||!t||!a))return(async()=>{const n=await P(u,C);if(q(n),n){const o=new URL(u);g(t,o.origin,a),l(),S(`${o.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`);return}})(),()=>{T(t)}},[u,t,g,a,C]),j(t,"ready",()=>L(n=>n+1));const s=e.useRef();return j(t,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(a.current){const n=new URL(u);a.current.src=`${n.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),Z?e.createElement(z,{$isReady:p,ref:a,src:y,...m}):e.createElement("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};W.propTypes={id:w.string,src:w.string.isRequired,path:w.string,onInit:w.func},c.MMEmbeddableZone=W,c.MMEmbeddableZoneContext=k,c.MMEmbeddableZoneProvider=A,c.getAppInfo=O,c.testIsEmbeddable=P,c.useEmbeddedEffect=j,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "1.2.0",
3
+ "version": "1.2.1-0",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",