@machinemetrics/mm-react-embeddable 1.4.0-4 → 1.4.0-6
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.
- package/dist/mm-react-embeddable.cjs.js +10 -10
- package/dist/mm-react-embeddable.es.js +456 -336
- package/dist/mm-react-embeddable.umd.js +10 -10
- package/package.json +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(p,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):(p=typeof globalThis!="undefined"?globalThis:p||self,e(p["mm-react-embeddable"]={},p.React,p.PropTypes,p.styled))})(this,(function(p,e,C,v){"use strict";const G=e.createContext(null),H=e.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),X="access_token",J=s=>s?s.kind?s:s.accessToken?{kind:X,accessToken:s.accessToken,expiresAt:s.expiresAt||null}:null:null,fe=new Set(["host_not_allowed","invalid_token","provider_unavailable"]),V=typeof window!="undefined"&&window.location.protocol==="https:",be=8e3,N=s=>{try{return new URL(s).origin}catch{return null}},Y=(s,u)=>{const c=u==null?void 0:u.origin;return!c||N(s==null?void 0:s.authUrl)!==c?null:c},R=s=>{if(!s||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(s)||s.includes(":"))return s;const u=s.split(".");return u.length<=2?s:u.slice(-2).join(".")},ee=(s,u)=>{try{const c=new URL(s),f=new URL(u);return c.protocol!==f.protocol?!0:R(c.hostname)!==R(f.hostname)}catch{return!0}},ne=(s,{onBlocked:u,onClosed:c})=>{const f=window.open(s.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return u(),()=>{};const g=setInterval(()=>{f.closed&&(clearInterval(g),c())},1e3);return()=>clearInterval(g)},te=({children:s,launcher:u,broker:c})=>{const f=e.useContext(G),[g,k]=e.useState({}),[j,I]=e.useState({}),[M,S]=e.useState({}),[U,T]=e.useState({}),Z=e.useRef(M);Z.current=M;const b=e.useRef(null),y=e.useRef({}),_=e.useRef({}),q=e.useRef({}),A=e.useRef(c);A.current=c;const z=e.useCallback(n=>{const t=_.current[n];if(!t)return;const r=d=>!d[n]||d[n].clientId?d:{...d,[n]:{...d[n],clientId:t}};k(r),T(r)},[]),B=e.useCallback((n,t)=>{n&&(T(r=>r[n]?r:{...r,[n]:{origin:n,reason:t,clientId:_.current[n]||null}}),z(n))},[z]),i=e.useCallback((n,{v2:t=!1,origin:r=null}={})=>{r&&(t&&Z.current[r]||(k(d=>d[r]?d:{...d,[r]:{origin:r,authUrl:n.authUrl,silentUrl:n.silentUrl||null,status:t&&A.current?"broker":t&&n.silentUrl&&V?"silent":"interactive",v2:t,scope:n.scope||null,acceptsCredentials:q.current[r]||null,consent:null,error:null,clientId:n.clientId||_.current[r]||null,popupBlocked:!1}}),z(r)))},[z]),l=e.useCallback(n=>{k(t=>{if(!t[n])return t;const r={...t};return delete r[n],r})},[]),o=e.useCallback(n=>{n&&(l(n),I(t=>({...t,[n]:(t[n]||0)+1})))},[l]),a=e.useCallback(n=>{k(t=>!t[n]||t[n].status==="interactive"?t:{...t,[n]:{...t[n],status:"interactive"}})},[]),h=e.useCallback(n=>{l(n.origin)},[l]),E=e.useCallback((n,t)=>{const r=Y(n,t);if(r){if(ee(r,window.location.origin)){B(r,"cross-site-legacy");return}i(n,{origin:r})}},[i,B]);e.useEffect(()=>{if(!f)return;const n=[f.addListener("*","init",(t,r)=>{r!=null&&r.origin&&(t!=null&&t.clientId)&&(_.current[r.origin]=t.clientId,z(r.origin)),r!=null&&r.origin&&Array.isArray(t==null?void 0:t.acceptsCredentials)&&(q.current[r.origin]=t.acceptsCredentials)}),f.addListener("*","authorize",E),f.addListener("*","login",E),f.addListener("*","credential-request",(t,r)=>i(t,{v2:!0,origin:Y(t,r)}))];return()=>n.forEach(t=>t())},[f,E,i,z]),e.useEffect(()=>{const n=t=>{const r=t==null?void 0:t.data;if(!r||r.type!=="authorized"&&r.type!=="authorize-failed")return;const d=N(r.authUrl);if(!(!d||t.origin!==d)){if(r.type==="authorized"){r.accessToken?(S(m=>({...m,[d]:J({accessToken:r.accessToken})})),l(d)):o(d);return}a(d)}};return window.addEventListener("message",n),()=>window.removeEventListener("message",n)},[l,o,a]);const O=e.useCallback((n,t)=>{const r=n==null?void 0:n.origin,d=J(t);!r||!d||(S(m=>({...m,[r]:d})),l(r))},[l]),w=e.useCallback((n,t)=>{k(r=>r[n]?{...r,[n]:{...r[n],...t}}:r)},[]),x=e.useCallback(async(n,{approve:t}={})=>{const r=A.current,{origin:d}=n;let m;try{m=await r({origin:d,clientId:n.clientId,scope:n.scope,authUrl:n.authUrl},{approve:t})}catch{t?w(d,{error:{reason:"broker_unavailable"}}):w(d,{status:n.silentUrl&&V?"silent":"interactive"});return}if((m==null?void 0:m.status)===200&&m.credential){O(n,m.credential);return}if((m==null?void 0:m.status)===409&&m.consentRequired){w(d,{status:"interactive",consent:{client:m.client||null,stepUp:!!m.stepUp,scope:m.scope||n.scope},error:null});return}const P=(m==null?void 0:m.error)||"broker_error";if(!t&&fe.has(P)){w(d,{status:n.silentUrl&&V?"silent":"interactive"});return}if(t&&P==="consent_denied"){w(d,{error:null});return}l(d),T(ae=>({...ae,[d]:{origin:d,reason:`broker:${P}`,detail:m||null,clientId:n.clientId||_.current[d]||null}}))},[O,w,l]),F=e.useRef({});e.useEffect(()=>{Object.values(g).forEach(n=>{n.status!=="broker"||F.current[n.origin]||(F.current[n.origin]=!0,x(n).finally(()=>{delete F.current[n.origin]}))})},[g,x]);const W=e.useCallback(n=>!A.current||!(n!=null&&n.consent)?Promise.resolve():x(n,{approve:!0}),[x]);e.useEffect(()=>{const n=y.current;Object.values(g).forEach(t=>{t.status==="silent"&&!n[t.origin]&&(n[t.origin]=setTimeout(()=>{delete n[t.origin],a(t.origin)},be))}),Object.keys(n).forEach(t=>{(!g[t]||g[t].status!=="silent")&&(clearTimeout(n[t]),delete n[t])})},[g,a]),e.useEffect(()=>()=>{b.current&&b.current(),Object.values(y.current).forEach(clearTimeout)},[]);const L=e.useCallback(n=>{b.current&&(b.current(),b.current=null);const t=u||ne;b.current=t(n,{onBlocked:()=>{k(r=>r[n.origin]?{...r,[n.origin]:{...r[n.origin],popupBlocked:!0}}:r)},onClosed:()=>{if(n.v2){V&&k(r=>!r[n.origin]||!r[n.origin].silentUrl?r:{...r,[n.origin]:{...r[n.origin],status:"silent"}});return}o(n.origin)}})},[u,o]),D={pending:Object.values(g).filter(n=>n.status==="interactive"),authorize:L,approve:W,complete:O,dismiss:h,originEpochs:j,credentials:M,unsupported:U},K=Object.values(g).filter(n=>n.status==="silent"&&n.silentUrl);return e.createElement(H.Provider,{value:D},s,K.map(n=>e.createElement("iframe",{key:`mm-silent-auth-${n.origin}`,src:n.silentUrl,title:`MachineMetrics silent authorization (${n.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};te.propTypes={children:C.node,launcher:C.func,broker:C.func};const re=()=>e.useContext(H),se=s=>{const{originEpochs:u}=e.useContext(H),c=N(s);return c&&u[c]||0},ie=s=>{const{credentials:u}=e.useContext(H),c=N(s);return c&&u[c]||null},oe=s=>{const{unsupported:u}=e.useContext(H),c=N(s);return c&&u[c]||null},ue=({children:s,authorizationLauncher:u,credentialBroker:c})=>{const[f,g]=e.useState({}),[k,j]=e.useState({}),I=e.useRef({}),M=e.useRef({}),S=e.useRef({}),U=e.useCallback((i,l)=>{var o;return!!((o=S.current[i])!=null&&o.has(l))},[]),T=e.useCallback((i,l,o)=>{g(a=>({...a,[i]:{origin:l,frameRef:o}}))},[]),Z=e.useCallback(i=>{g(l=>{const o={...l};return delete o[i],o}),j(l=>{const o={...l};return delete o[i],o}),delete S.current[i]},[]);e.useEffect(()=>{const i=(o=[],a,h)=>{o.forEach(E=>E(a,h))},l=o=>{var F,W,L,D,K,n,t;if(!((F=o==null?void 0:o.data)!=null&&F.instanceId))return;const{instanceId:a,type:h}=o.data,E=f[a],O=(L=(W=E==null?void 0:E.frameRef)==null?void 0:W.current)==null?void 0:L.contentWindow;if(!O||o.source!==O||o.origin!==E.origin)return;const w={origin:o.origin},x=I.current;if(h==="response"){const{callbackId:r,response:d,error:m}=o.data,P=M.current[r];P&&(P(d,m),delete M.current[r]);return}if(h==="mark-registered"){const{requestType:r}=o.data;S.current[a]||(S.current[a]=new Set),S.current[a].add(r);return}h==="ready"&&j(r=>({...r,[a]:!0})),i((D=x[a])==null?void 0:D[h],o.data,w),i((K=x["*"])==null?void 0:K[h],o.data,w),i((n=x[a])==null?void 0:n["*"],o.data,w),i((t=x["*"])==null?void 0:t["*"],o.data,w)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[f]);const b=e.useCallback((i,l,o)=>{var h,E;const a=f[i];if(!a){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}(E=(h=a==null?void 0:a.frameRef)==null?void 0:h.current)!=null&&E.contentWindow&&a.frameRef.current.contentWindow.postMessage({type:l,...o,instanceId:i},a.origin)},[f]),y=e.useRef(0),_=e.useCallback(()=>`${Date.now()}-${++y.current}`,[]),q=e.useCallback(async(i,l,o={})=>new Promise((a,h)=>{const E=_(),O=(w,x)=>{delete M.current[E],x?h(x):a(w)};M.current[E]=O,b(i,"request",{callbackId:E,requestType:l,data:o})}),[b]),A=e.useCallback((i,l)=>{b(i,"load-options",l)},[b]),z=e.useCallback((i,l)=>{b(i,"validate",l)},[b]),B=e.useMemo(()=>({registerFrame:T,unregisterFrame:Z,sendMessage:b,sendRequest:q,loadOptions:A,validate:z,hasHandler:U,readyFrames:k,addListener:(i,l,o)=>(I.current[i]||(I.current[i]={}),I.current[i][l]||(I.current[i][l]=[]),I.current[i][l].push(o),()=>{I.current[i][l]=I.current[i][l].filter(a=>a!==o)})}),[T,Z,b,q,A,z,U,k]);return e.createElement(G.Provider,{value:B},e.createElement(te,{launcher:u,broker:c},s))},$=(s,u,c)=>{const f=e.useContext(G);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const g=e.useRef(c);e.useEffect(()=>{g.current=c},[c]),e.useEffect(()=>{if(!(!s||!u)){if(u.startsWith("key:")){const k=u.substring(4);return f.readyFrames[s]&&f.sendMessage(s,"subscribe-key",{keyChord:k}),f.addListener(s,`key-pressed:${k}`,(...I)=>g.current(...I))}return f.addListener(s,u,(...k)=>g.current(...k))}},[f,s,u])};ue.propTypes={children:C.node.isRequired,authorizationLauncher:C.func,credentialBroker:C.func};const Q=v.default||v,me=1e4,pe="credentials",ce=(s,u)=>`${s}/mm-embedded-interop?id=${u}&origin=${window.location.origin}&caps=${pe}`,ge=Q.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
|
-
visibility: ${
|
|
6
|
-
`,
|
|
5
|
+
visibility: ${s=>s.$isReady?"visible":"hidden"};
|
|
6
|
+
`,he=Q.div`
|
|
7
7
|
width: 100%;
|
|
8
8
|
height: 100%;
|
|
9
9
|
display: flex;
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
text-align: center;
|
|
17
17
|
color: #555;
|
|
18
18
|
font-size: 14px;
|
|
19
|
-
`,
|
|
19
|
+
`,le=({entry:s})=>e.createElement(he,null,e.createElement("div",{style:{fontWeight:600}},s&&s.origin||"This app"," can’t be embedded here"),e.createElement("div",{style:{fontSize:12,color:"#888"}},"It needs an update to support embedding from a different domain."));le.propTypes={entry:C.shape({origin:C.string,name:C.string,reason:C.string})};const Ee=Q.div`
|
|
20
20
|
height: 100%;
|
|
21
21
|
display: flex;
|
|
22
22
|
flex: 1;
|
|
23
23
|
align-items: center;
|
|
24
24
|
justify-content: center;
|
|
25
|
-
`,
|
|
25
|
+
`,ke=()=>e.createElement(Ee,null,e.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),de=({id:s,src:u,path:c,onInit:f=()=>{},onTimeout:g,context:k={},params:j={},renderUnsupported:I,renderTimeout:M,...S})=>{if("isConfiguring"in S)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in k)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in k)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const U=e.useRef(),[T,Z]=e.useState(),[b,y]=e.useState(0),[_,q]=e.useState(!1),[A,z]=e.useState(!1),B=se(u),i=ie(u),l=oe(u);e.useEffect(()=>{B>0&&y(0)},[B]);const{registerFrame:o,unregisterFrame:a,sendMessage:h}=e.useContext(G),[E,O]=e.useState(),w=e.useRef(),x=e.useRef(g),F=e.useRef(f),W=e.useRef(!1);e.useEffect(()=>{x.current=g},[g]),e.useEffect(()=>{F.current=f},[f]),e.useEffect(()=>{b&&h(s,"set-context",{context:{...k}})},[k,s,b]),e.useEffect(()=>{b&&h(s,"set-params",{params:j})},[j,s,b]),e.useEffect(()=>{c!==void 0&&O((c==null?void 0:c.indexOf("/"))===0?c:`/${c}`)},[c]),e.useEffect(()=>{E===void 0||!b||h(s,"navigate",{path:E})},[E,b,s,h]),e.useEffect(()=>{if(!u||!s||!U)return;const t=new URL(u);return o(s,t.origin,U),F.current(),Z(ce(t.origin,s)),()=>{a(s)}},[u,s,o,U]),e.useEffect(()=>{if(!(!T||W.current))return w.current=setTimeout(()=>{q(!0),x.current&&x.current()},me),()=>clearTimeout(w.current)},[T]);const L=e.useCallback(()=>{W.current=!0,clearTimeout(w.current),q(!1)},[]),[D,K]=e.useState(null);$(s,"init",t=>{L(),Array.isArray(t==null?void 0:t.acceptsCredentials)&&K(t.acceptsCredentials)}),$(s,"authorize",L),$(s,"login",L),$(s,"credential-request",()=>{L(),z(!0)}),e.useEffect(()=>{if(!A||!i)return;const t=D;if(Array.isArray(t)&&!t.includes(i.kind))return;const r=i.kind==="access_token"?{accessToken:i.accessToken}:{};h(s,"credential",{...r,credential:i})},[A,i,D,s,h]),$(s,"ready",()=>{L(),y(t=>t+1)});const n=e.useRef();return $(s,"heartbeat",()=>{n.current&&clearTimeout(n.current),n.current=setTimeout(()=>{if(U.current){const t=new URL(u);U.current.src=ce(t.origin,s)}},6e3)}),e.useEffect(()=>()=>{n.current&&clearTimeout(n.current)},[]),l?I?I(l):e.createElement(le,{entry:l}):_&&!b?M?M():e.createElement(ke,null):e.createElement(ge,{key:`auth-epoch-${B}`,$isReady:b,ref:U,src:T,...S})};de.propTypes={id:C.string,src:C.string.isRequired,path:C.string,onInit:C.func,onTimeout:C.func,renderUnsupported:C.func,renderTimeout:C.func};const Ce=v.div`
|
|
26
26
|
position: fixed;
|
|
27
27
|
inset: 0;
|
|
28
28
|
background: rgba(0, 0, 0, 0.45);
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
align-items: center;
|
|
31
31
|
justify-content: center;
|
|
32
32
|
z-index: 10000;
|
|
33
|
-
`,
|
|
33
|
+
`,we=v.div`
|
|
34
34
|
background: #fff;
|
|
35
35
|
border-radius: 8px;
|
|
36
36
|
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
max-width: calc(100vw - 32px);
|
|
40
40
|
font-family: sans-serif;
|
|
41
41
|
color: #222;
|
|
42
|
-
`,
|
|
42
|
+
`,xe=v.div`
|
|
43
43
|
display: flex;
|
|
44
44
|
align-items: center;
|
|
45
45
|
justify-content: space-between;
|
|
46
46
|
padding: 10px 0;
|
|
47
47
|
gap: 16px;
|
|
48
|
-
`,
|
|
48
|
+
`,Ie=v.button`
|
|
49
49
|
background: #16a500;
|
|
50
50
|
color: #fff;
|
|
51
51
|
border: none;
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
font-size: 14px;
|
|
55
55
|
cursor: pointer;
|
|
56
56
|
white-space: nowrap;
|
|
57
|
-
`,
|
|
57
|
+
`,Me=v.p`
|
|
58
58
|
color: #b3261e;
|
|
59
59
|
font-size: 13px;
|
|
60
60
|
margin: 4px 0 0;
|
|
61
|
-
`,
|
|
61
|
+
`,Se=()=>{const{pending:s,authorize:u}=re();return s.length===0?null:e.createElement(Ce,null,e.createElement(we,null,e.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),e.createElement("p",{style:{margin:"0 0 12px",fontSize:14,color:"#555"}},"The following apps need you to sign in and authorize them before they can load."),s.map(c=>e.createElement("div",{key:c.origin},e.createElement(xe,null,e.createElement("div",null,e.createElement("div",{style:{fontWeight:600}},c.origin)),e.createElement(Ie,{onClick:()=>u(c)},"Authorize")),c.popupBlocked&&e.createElement(Me,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};p.CREDENTIAL_KIND_ACCESS_TOKEN=X,p.EmbeddedAuthorizationContext=H,p.EmbeddedAuthorizationPrompt=Se,p.MMEmbeddableZone=de,p.MMEmbeddableZoneContext=G,p.MMEmbeddableZoneProvider=ue,p.isCrossSite=ee,p.normalizeCredential=J,p.popupAuthorizationLauncher=ne,p.useEmbeddedAuthorizations=re,p.useEmbeddedCredential=ie,p.useEmbeddedEffect=$,p.useEmbeddedOriginEpoch=se,p.useEmbeddedUnsupported=oe,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
|