@machinemetrics/mm-react-embeddable 0.9.9 → 0.9.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),l=require("prop-types"),P=require("styled-components"),p=e.createContext(null),M=({children:n})=>{const[c,i]=e.useState({}),t=e.useRef({}),f=e.useCallback((s,r,a)=>{i(o=>({...o,[s]:{origin:r,frameRef:a}}))},[]);e.useEffect(()=>{const s=r=>{var o,m;if(!((o=r==null?void 0:r.data)!=null&&o.instanceId))return;(((m=t.current[r.data.instanceId])==null?void 0:m[r.data.type])||[]).forEach(E=>E(r.data))};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[c]);const u=e.useCallback((s,r,a)=>{const o=c[s];if(!o){console.warn(`Origin not set for id: ${s}. Unable to send message.`);return}o.frameRef.current.contentWindow.postMessage({type:r,...a,instanceId:s},o.origin)},[c]),d={registerFrame:f,sendMessage:u,addListener:(s,r,a)=>(t.current[s]||(t.current[s]={}),t.current[s][r]||(t.current[s][r]=[]),t.current[s][r].push(a),()=>{t.current[s][r]=t.current[s][r].filter(o=>o!==a)})};return e.createElement(p.Provider,{value:d},n)},y=(n,c,i)=>{const t=e.useContext(p);if(t===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const f=e.useRef(i);return e.useEffect(()=>{f.current=i},[i]),e.useEffect(()=>{if(!(!n||!c))return t.addListener(n,c,(...u)=>f.current(...u))},[t,n,c]),e.useCallback((u,d)=>t.sendMessage(n,u,d),[t,n])};M.propTypes={children:l.node.isRequired};const x=P.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),m=require("prop-types"),C=require("styled-components"),p=t.createContext(null),h=({children:n})=>{const[i,c]=t.useState({}),s=t.useRef({}),[l,d]=t.useState(!0),[f,E]=t.useState({}),b=t.useCallback((r,e,o)=>{c(a=>({...a,[r]:{origin:e,frameRef:o}}))},[]),y=t.useCallback(async r=>{try{const e=new URL(r).origin;if(f[e]!==void 0)return f[e];if(!(await(await fetch(`${e}/mm-app-manifest.json`)).json()).version)throw new Error("Invalid manifest");return E(u=>({...u,[e]:!0})),!0}catch{d(!1)}return E(e=>({...e,[origin]:!1})),!1},[f]);t.useEffect(()=>{const r=e=>{var a,u;if(!((a=e==null?void 0:e.data)!=null&&a.instanceId))return;(((u=s.current[e.data.instanceId])==null?void 0:u[e.data.type])||[]).forEach(I=>I(e.data))};return window.addEventListener("message",r),()=>{window.removeEventListener("message",r)}},[i]);const g=t.useCallback((r,e,o)=>{const a=i[r];if(!a){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}a.frameRef.current.contentWindow.postMessage({type:e,...o,instanceId:r},a.origin)},[i]),M={testIsEmbeddable:y,isEmbeddable:l,registerFrame:b,sendMessage:g,addListener:(r,e,o)=>(s.current[r]||(s.current[r]={}),s.current[r][e]||(s.current[r][e]=[]),s.current[r][e].push(o),()=>{s.current[r][e]=s.current[r][e].filter(a=>a!==o)})};return t.createElement(p.Provider,{value:M},n)},w=(n,i,c)=>{const s=t.useContext(p);if(s===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=t.useRef(c);return t.useEffect(()=>{l.current=c},[c]),t.useEffect(()=>{if(!(!n||!i))return s.addListener(n,i,(...d)=>l.current(...d))},[s,n,i]),t.useCallback((d,f)=>s.sendMessage(n,d,f),[s,n])};h.propTypes={children:m.node.isRequired};const v=C.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${n=>n.isready?"visible":"hidden"};
6
- `,h=({id:n,genericFrameFallback:c,src:i,path:t,onInit:f=()=>{},...u})=>{const d=e.useRef(),[s,r]=e.useState(),[a,o]=e.useState(0),{registerFrame:m,sendMessage:E}=e.useContext(p),[g,w]=e.useState(),[R,C]=e.useState(!1);return e.useEffect(()=>{t!==void 0&&(console.log(t),w(t))},[t]),e.useEffect(()=>{g===void 0||!a||E(n,"navigate",{path:g})},[g,a,n,E]),e.useEffect(()=>{!i||!n||!d||(async()=>{const b=new URL(i);m(n,b.origin,d),f();try{if((await(await fetch(`${b.origin}/mm-app-manifest.json`)).json()).version)r(`${b.origin}/mm-embedded-interop?id=${n}`);else throw Error()}catch{c?(r(i),o(1)):C(!0)}})()},[i,n,m,d]),y(n,"ready",()=>o(b=>b+1)),R?e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded.")):e.createElement(x,{isready:a,ref:d,src:s,...u})};h.propTypes={id:l.string,genericFrameFallback:l.bool,src:l.string.isRequired,path:l.string,onInit:l.func};exports.MMEmbeddableZone=h;exports.MMEmbeddableZoneProvider=M;exports.useEmbeddedEffect=y;
6
+ `,R=({id:n,src:i,path:c,onInit:s=()=>{},...l})=>{const d=t.useRef(),[f,E]=t.useState(),[b,y]=t.useState(0),{registerFrame:g,sendMessage:M,testIsEmbeddable:r,isEmbeddable:e}=t.useContext(p),[o,a]=t.useState();return t.useEffect(()=>{c!==void 0&&a(c)},[c]),t.useEffect(()=>{o===void 0||!b||M(n,"navigate",{path:o})},[o,b,n,M]),t.useEffect(()=>{!i||!n||!d||(async()=>{if(r(i)){const u=new URL(i);g(n,u.origin,d),s(),E(`${u.origin}/mm-embedded-interop?id=${n}`);return}})()},[i,n,g,d]),w(n,"ready",()=>y(u=>u+1)),e?t.createElement(v,{isready:b,ref:d,src:f,...l}):t.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},t.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};R.propTypes={id:m.string,src:m.string.isRequired,path:m.string,onInit:m.func};exports.MMEmbeddableZone=R;exports.MMEmbeddableZoneContext=p;exports.MMEmbeddableZoneProvider=h;exports.useEmbeddedEffect=w;
@@ -1,99 +1,104 @@
1
- import E, { createContext as P, useState as b, useRef as M, useCallback as y, useEffect as l, useContext as R } from "react";
2
- import f from "prop-types";
3
- import C from "styled-components";
4
- const w = P(null), $ = ({ children: n }) => {
5
- const [a, i] = b({}), e = M({}), u = y((r, t, o) => {
6
- i((s) => ({ ...s, [r]: { origin: t, frameRef: o } }));
7
- }, []);
1
+ import y, { createContext as v, useState as m, useRef as R, useCallback as w, useEffect as l, useContext as L } from "react";
2
+ import g from "prop-types";
3
+ import P from "styled-components";
4
+ const I = v(null), C = ({ children: r }) => {
5
+ const [i, d] = m({}), n = R({}), [f, a] = m(!0), [u, p] = m({}), b = w((t, e, o) => {
6
+ d((s) => ({ ...s, [t]: { origin: e, frameRef: o } }));
7
+ }, []), M = w(
8
+ async (t) => {
9
+ try {
10
+ const e = new URL(t).origin;
11
+ if (u[e] !== void 0)
12
+ return u[e];
13
+ if (!(await (await fetch(`${e}/mm-app-manifest.json`)).json()).version)
14
+ throw new Error("Invalid manifest");
15
+ return p((c) => ({ ...c, [e]: !0 })), !0;
16
+ } catch {
17
+ a(!1);
18
+ }
19
+ return p((e) => ({ ...e, [origin]: !1 })), !1;
20
+ },
21
+ [u]
22
+ );
8
23
  l(() => {
9
- const r = (t) => {
10
- var s, m;
11
- if (!((s = t == null ? void 0 : t.data) != null && s.instanceId))
24
+ const t = (e) => {
25
+ var s, c;
26
+ if (!((s = e == null ? void 0 : e.data) != null && s.instanceId))
12
27
  return;
13
- (((m = e.current[t.data.instanceId]) == null ? void 0 : m[t.data.type]) || []).forEach((p) => p(t.data));
28
+ (((c = n.current[e.data.instanceId]) == null ? void 0 : c[e.data.type]) || []).forEach((x) => x(e.data));
14
29
  };
15
- return window.addEventListener("message", r), () => {
16
- window.removeEventListener("message", r);
30
+ return window.addEventListener("message", t), () => {
31
+ window.removeEventListener("message", t);
17
32
  };
18
- }, [a]);
19
- const d = y(
20
- (r, t, o) => {
21
- const s = a[r];
33
+ }, [i]);
34
+ const E = w(
35
+ (t, e, o) => {
36
+ const s = i[t];
22
37
  if (!s) {
23
- console.warn(`Origin not set for id: ${r}. Unable to send message.`);
38
+ console.warn(`Origin not set for id: ${t}. Unable to send message.`);
24
39
  return;
25
40
  }
26
41
  s.frameRef.current.contentWindow.postMessage(
27
- { type: t, ...o, instanceId: r },
42
+ { type: e, ...o, instanceId: t },
28
43
  s.origin
29
44
  );
30
45
  },
31
- [a]
32
- ), c = {
33
- registerFrame: u,
34
- sendMessage: d,
35
- addListener: (r, t, o) => (e.current[r] || (e.current[r] = {}), e.current[r][t] || (e.current[r][t] = []), e.current[r][t].push(o), () => {
36
- e.current[r][t] = e.current[r][t].filter((s) => s !== o);
46
+ [i]
47
+ ), h = {
48
+ testIsEmbeddable: M,
49
+ isEmbeddable: f,
50
+ registerFrame: b,
51
+ sendMessage: E,
52
+ addListener: (t, e, o) => (n.current[t] || (n.current[t] = {}), n.current[t][e] || (n.current[t][e] = []), n.current[t][e].push(o), () => {
53
+ n.current[t][e] = n.current[t][e].filter((s) => s !== o);
37
54
  })
38
55
  };
39
- return /* @__PURE__ */ E.createElement(w.Provider, { value: c }, n);
40
- }, v = (n, a, i) => {
41
- const e = R(w);
42
- if (e === null)
56
+ return /* @__PURE__ */ y.createElement(I.Provider, { value: h }, r);
57
+ }, $ = (r, i, d) => {
58
+ const n = L(I);
59
+ if (n === null)
43
60
  throw new Error(
44
61
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
45
62
  );
46
- const u = M(i);
63
+ const f = R(d);
47
64
  return l(() => {
48
- u.current = i;
49
- }, [i]), l(() => {
50
- if (!(!n || !a))
51
- return e.addListener(
52
- n,
53
- a,
54
- (...d) => u.current(...d)
65
+ f.current = d;
66
+ }, [d]), l(() => {
67
+ if (!(!r || !i))
68
+ return n.addListener(
69
+ r,
70
+ i,
71
+ (...a) => f.current(...a)
55
72
  );
56
- }, [e, n, a]), y(
57
- (d, c) => e.sendMessage(n, d, c),
58
- [e, n]
73
+ }, [n, r, i]), w(
74
+ (a, u) => n.sendMessage(r, a, u),
75
+ [n, r]
59
76
  );
60
77
  };
61
- $.propTypes = {
62
- children: f.node.isRequired
78
+ C.propTypes = {
79
+ children: g.node.isRequired
63
80
  };
64
- const Z = C.iframe`
81
+ const F = P.iframe`
65
82
  width: 100%;
66
83
  height: 100%;
67
84
  border: none;
68
- visibility: ${(n) => n.isready ? "visible" : "hidden"};
69
- `, j = ({
70
- id: n,
71
- genericFrameFallback: a,
72
- src: i,
73
- path: e,
74
- onInit: u = () => {
75
- },
76
- ...d
77
- }) => {
78
- const c = M(), [r, t] = b(), [o, s] = b(0), { registerFrame: m, sendMessage: p } = R(w), [h, I] = b(), [x, L] = b(!1);
85
+ visibility: ${(r) => r.isready ? "visible" : "hidden"};
86
+ `, U = ({ id: r, src: i, path: d, onInit: n = () => {
87
+ }, ...f }) => {
88
+ const a = R(), [u, p] = m(), [b, M] = m(0), { registerFrame: E, sendMessage: h, testIsEmbeddable: t, isEmbeddable: e } = L(I), [o, s] = m();
79
89
  return l(() => {
80
- e !== void 0 && (console.log(e), I(e));
81
- }, [e]), l(() => {
82
- h === void 0 || !o || p(n, "navigate", { path: h });
83
- }, [h, o, n, p]), l(() => {
84
- !i || !n || !c || (async () => {
85
- const g = new URL(i);
86
- m(n, g.origin, c), u();
87
- try {
88
- if ((await (await fetch(`${g.origin}/mm-app-manifest.json`)).json()).version)
89
- t(`${g.origin}/mm-embedded-interop?id=${n}`);
90
- else
91
- throw Error();
92
- } catch {
93
- a ? (t(i), s(1)) : L(!0);
90
+ d !== void 0 && s(d);
91
+ }, [d]), l(() => {
92
+ o === void 0 || !b || h(r, "navigate", { path: o });
93
+ }, [o, b, r, h]), l(() => {
94
+ !i || !r || !a || (async () => {
95
+ if (t(i)) {
96
+ const c = new URL(i);
97
+ E(r, c.origin, a), n(), p(`${c.origin}/mm-embedded-interop?id=${r}`);
98
+ return;
94
99
  }
95
100
  })();
96
- }, [i, n, m, c]), v(n, "ready", () => s((g) => g + 1)), x ? /* @__PURE__ */ E.createElement(
101
+ }, [i, r, E, a]), $(r, "ready", () => M((c) => c + 1)), e ? /* @__PURE__ */ y.createElement(F, { isready: b, ref: a, src: u, ...f }) : /* @__PURE__ */ y.createElement(
97
102
  "div",
98
103
  {
99
104
  style: {
@@ -103,18 +108,18 @@ const Z = C.iframe`
103
108
  justifyContent: "center"
104
109
  }
105
110
  },
106
- /* @__PURE__ */ E.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
107
- ) : /* @__PURE__ */ E.createElement(Z, { isready: o, ref: c, src: r, ...d });
111
+ /* @__PURE__ */ y.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
112
+ );
108
113
  };
109
- j.propTypes = {
110
- id: f.string,
111
- genericFrameFallback: f.bool,
112
- src: f.string.isRequired,
113
- path: f.string,
114
- onInit: f.func
114
+ U.propTypes = {
115
+ id: g.string,
116
+ src: g.string.isRequired,
117
+ path: g.string,
118
+ onInit: g.func
115
119
  };
116
120
  export {
117
- j as MMEmbeddableZone,
118
- $ as MMEmbeddableZoneProvider,
119
- v as useEmbeddedEffect
121
+ U as MMEmbeddableZone,
122
+ I as MMEmbeddableZoneContext,
123
+ C as MMEmbeddableZoneProvider,
124
+ $ as useEmbeddedEffect
120
125
  };
@@ -1,6 +1,6 @@
1
- (function(i,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(i=typeof globalThis<"u"?globalThis:i||self,e(i["mm-react-embeddable"]={},i.React,i.PropTypes,i.styled))})(this,function(i,e,l,C){"use strict";const p=e.createContext(null),y=({children:s})=>{const[f,u]=e.useState({}),t=e.useRef({}),m=e.useCallback((r,n,d)=>{u(o=>({...o,[r]:{origin:n,frameRef:d}}))},[]);e.useEffect(()=>{const r=n=>{var o,b;if(!((o=n==null?void 0:n.data)!=null&&o.instanceId))return;(((b=t.current[n.data.instanceId])==null?void 0:b[n.data.type])||[]).forEach(g=>g(n.data))};return window.addEventListener("message",r),()=>{window.removeEventListener("message",r)}},[f]);const c=e.useCallback((r,n,d)=>{const o=f[r];if(!o){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}o.frameRef.current.contentWindow.postMessage({type:n,...d,instanceId:r},o.origin)},[f]),a={registerFrame:m,sendMessage:c,addListener:(r,n,d)=>(t.current[r]||(t.current[r]={}),t.current[r][n]||(t.current[r][n]=[]),t.current[r][n].push(d),()=>{t.current[r][n]=t.current[r][n].filter(o=>o!==d)})};return e.createElement(p.Provider,{value:a},s)},M=(s,f,u)=>{const t=e.useContext(p);if(t===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=e.useRef(u);return e.useEffect(()=>{m.current=u},[u]),e.useEffect(()=>{if(!(!s||!f))return t.addListener(s,f,(...c)=>m.current(...c))},[t,s,f]),e.useCallback((c,a)=>t.sendMessage(s,c,a),[t,s])};y.propTypes={children:l.node.isRequired};const I=C.iframe`
1
+ (function(o,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(o=typeof globalThis<"u"?globalThis:o||self,e(o["mm-react-embeddable"]={},o.React,o.PropTypes,o.styled))})(this,function(o,e,b,v){"use strict";const g=e.createContext(null),w=({children:r})=>{const[d,f]=e.useState({}),s=e.useRef({}),[m,a]=e.useState(!0),[l,p]=e.useState({}),E=e.useCallback((n,t,u)=>{f(i=>({...i,[n]:{origin:t,frameRef:u}}))},[]),M=e.useCallback(async n=>{try{const t=new URL(n).origin;if(l[t]!==void 0)return l[t];if(!(await(await fetch(`${t}/mm-app-manifest.json`)).json()).version)throw new Error("Invalid manifest");return p(c=>({...c,[t]:!0})),!0}catch{a(!1)}return p(t=>({...t,[origin]:!1})),!1},[l]);e.useEffect(()=>{const n=t=>{var i,c;if(!((i=t==null?void 0:t.data)!=null&&i.instanceId))return;(((c=s.current[t.data.instanceId])==null?void 0:c[t.data.type])||[]).forEach(S=>S(t.data))};return window.addEventListener("message",n),()=>{window.removeEventListener("message",n)}},[d]);const h=e.useCallback((n,t,u)=>{const i=d[n];if(!i){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}i.frameRef.current.contentWindow.postMessage({type:t,...u,instanceId:n},i.origin)},[d]),y={testIsEmbeddable:M,isEmbeddable:m,registerFrame:E,sendMessage:h,addListener:(n,t,u)=>(s.current[n]||(s.current[n]={}),s.current[n][t]||(s.current[n][t]=[]),s.current[n][t].push(u),()=>{s.current[n][t]=s.current[n][t].filter(i=>i!==u)})};return e.createElement(g.Provider,{value:y},r)},I=(r,d,f)=>{const s=e.useContext(g);if(s===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=e.useRef(f);return e.useEffect(()=>{m.current=f},[f]),e.useEffect(()=>{if(!(!r||!d))return s.addListener(r,d,(...a)=>m.current(...a))},[s,r,d]),e.useCallback((a,l)=>s.sendMessage(r,a,l),[s,r])};w.propTypes={children:b.node.isRequired};const L=v.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
- visibility: ${s=>s.isready?"visible":"hidden"};
6
- `,w=({id:s,genericFrameFallback:f,src:u,path:t,onInit:m=()=>{},...c})=>{const a=e.useRef(),[r,n]=e.useState(),[d,o]=e.useState(0),{registerFrame:b,sendMessage:g}=e.useContext(p),[h,x]=e.useState(),[L,S]=e.useState(!1);return e.useEffect(()=>{t!==void 0&&(console.log(t),x(t))},[t]),e.useEffect(()=>{h===void 0||!d||g(s,"navigate",{path:h})},[h,d,s,g]),e.useEffect(()=>{!u||!s||!a||(async()=>{const E=new URL(u);b(s,E.origin,a),m();try{if((await(await fetch(`${E.origin}/mm-app-manifest.json`)).json()).version)n(`${E.origin}/mm-embedded-interop?id=${s}`);else throw Error()}catch{f?(n(u),o(1)):S(!0)}})()},[u,s,b,a]),M(s,"ready",()=>o(E=>E+1)),L?e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded.")):e.createElement(I,{isready:d,ref:a,src:r,...c})};w.propTypes={id:l.string,genericFrameFallback:l.bool,src:l.string.isRequired,path:l.string,onInit:l.func},i.MMEmbeddableZone=w,i.MMEmbeddableZoneProvider=y,i.useEmbeddedEffect=M,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
5
+ visibility: ${r=>r.isready?"visible":"hidden"};
6
+ `,C=({id:r,src:d,path:f,onInit:s=()=>{},...m})=>{const a=e.useRef(),[l,p]=e.useState(),[E,M]=e.useState(0),{registerFrame:h,sendMessage:y,testIsEmbeddable:n,isEmbeddable:t}=e.useContext(g),[u,i]=e.useState();return e.useEffect(()=>{f!==void 0&&i(f)},[f]),e.useEffect(()=>{u===void 0||!E||y(r,"navigate",{path:u})},[u,E,r,y]),e.useEffect(()=>{!d||!r||!a||(async()=>{if(n(d)){const c=new URL(d);h(r,c.origin,a),s(),p(`${c.origin}/mm-embedded-interop?id=${r}`);return}})()},[d,r,h,a]),I(r,"ready",()=>M(c=>c+1)),t?e.createElement(L,{isready:E,ref:a,src:l,...m}):e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};C.propTypes={id:b.string,src:b.string.isRequired,path:b.string,onInit:b.func},o.MMEmbeddableZone=C,o.MMEmbeddableZoneContext=g,o.MMEmbeddableZoneProvider=w,o.useEmbeddedEffect=I,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "0.9.9",
3
+ "version": "0.9.10",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",