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