@machinemetrics/mm-react-embeddable 1.0.11-0 → 1.0.12

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