@machinemetrics/mm-react-embeddable 1.1.2-0 → 1.1.2-2

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