@machinemetrics/mm-react-embeddable 2.0.0-2 → 2.0.0-3

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