@machinemetrics/mm-react-embeddable 2.0.1 → 2.0.2

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,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):(g=typeof globalThis!="undefined"?globalThis:g||self,e(g["mm-react-embeddable"]={},g.React,g.PropTypes,g.styled))})(this,(function(g,e,me,Ee){"use strict";const R=t=>t&&t.__esModule?t:{default:t},O=R(e),C=R(me),te=R(Ee),J=e.createContext(null),G=e.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ne="access_token",V=t=>t?t.kind?t:t.accessToken?{kind:ne,accessToken:t.accessToken,expiresAt:t.expiresAt||null}:null:null,ge=60,ke=120,Ce=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),Q=t=>{try{return new URL(t).origin}catch{return null}},se=(t,o)=>{const c=o==null?void 0:o.origin;return!c||Q(t==null?void 0:t.authUrl)!==c?null:c},oe=t=>{if(!t||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(t)||t.includes(":"))return t;const o=t.split(".");return o.length<=2?t:o.slice(-2).join(".")},ee=(t,o)=>{try{const c=new URL(t),a=new URL(o);return c.protocol!==a.protocol?!0:oe(c.hostname)!==oe(a.hostname)}catch{return!0}},ue=(t,{onBlocked:o,onClosed:c})=>{const a=window.open(t.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!a)return o(),()=>{};const w=setInterval(()=>{a.closed&&(clearInterval(w),c())},1e3);return()=>clearInterval(w)},ie=({children:t,launcher:o,broker:c})=>{const a=e.useContext(J),[w,T]=e.useState({}),[F,B]=e.useState({}),[Z,H]=e.useState({}),[M,k]=e.useState({}),L=e.useRef(Z);L.current=Z;const j=e.useRef(w);j.current=w;const _=e.useRef(null),$=e.useRef({}),A=e.useRef({}),v=e.useRef(c);v.current=c;const x=e.useCallback(r=>{const s=$.current[r];if(!s)return;const n=i=>!i[r]||i[r].clientId?i:{...i,[r]:{...i[r],clientId:s}};T(n),k(n)},[]),q=e.useCallback((r,s)=>{r&&(k(n=>n[r]?n:{...n,[r]:{origin:r,reason:s,clientId:$.current[r]||null}}),x(r))},[x]),U=e.useCallback((r,{v2:s=!1,origin:n=null}={})=>{n&&(s&&L.current[n]||(T(i=>i[n]?i:{...i,[n]:{origin:n,authUrl:r.authUrl,status:s&&v.current?"broker":"interactive",v2:s,scope:r.scope||null,acceptsCredentials:A.current[n]||null,consent:null,error:null,clientId:r.clientId||$.current[n]||null,popupBlocked:!1}}),x(n)))},[x]),I=e.useRef({}),D=e.useCallback(r=>{I.current[r]&&(clearTimeout(I.current[r]),delete I.current[r])},[]),u=e.useCallback(r=>{D(r),T(s=>{if(!s[r])return s;const n={...s};return delete n[r],n})},[D]),d=e.useCallback(r=>{r&&(u(r),B(s=>({...s,[r]:(s[r]||0)+1})))},[u]),l=e.useCallback(r=>{u(r.origin)},[u]),p=e.useCallback((r,s)=>{const n=se(r,s);if(n){if(ee(n,window.location.origin)){q(n,"cross-site-legacy");return}U(r,{origin:n})}},[U,q]);e.useEffect(()=>{if(!a)return;const r=[a.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&($.current[n.origin]=s.clientId,x(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?A.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&ee(n.origin,window.location.origin)&&q(n.origin,"cross-site-legacy")}),a.addListener("*","authorize",p),a.addListener("*","login",p),a.addListener("*","credential-request",(s,n)=>U(s,{v2:!0,origin:se(s,n)}))];return()=>r.forEach(s=>s())},[a,p,U,x,q]),e.useEffect(()=>{const r=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const i=Q(n.authUrl);if(!i||s.origin!==i)return;const b=j.current[i];if(!(!b||b.status!=="interactive")){if(n.accessToken){H(z=>({...z,[i]:V({accessToken:n.accessToken})})),u(i);return}d(i)}};return window.addEventListener("message",r),()=>window.removeEventListener("message",r)},[u,d]);const h=e.useCallback((r,s)=>{const n=r==null?void 0:r.origin,i=V(s);!n||!i||(H(b=>({...b,[n]:i})),u(n))},[u]),m=e.useCallback((r,s)=>{T(n=>n[r]?{...n,[r]:{...n[r],...s}}:n)},[]),E=e.useRef({}),y=e.useCallback(async(r,{approve:s}={})=>{const n=v.current,{origin:i}=r;let b;try{b=await n({origin:i,clientId:r.clientId,scope:r.scope,authUrl:r.authUrl},{approve:s})}catch{s?m(i,{error:{reason:"broker_unavailable"}}):m(i,{status:"interactive"});return}const z=b==null?void 0:b.status,f=(b==null?void 0:b.body)||{};if(z===200&&f.credential){h(r,f.credential);return}if(z===409&&f.consentRequired){m(i,{status:"interactive",consent:{client:f.client||null,stepUp:!!f.stepUp,scope:f.scope||r.scope},error:null});return}const P=f.error||"broker_error";if(!s&&Ce.has(P)){m(i,{status:"interactive"});return}if(s&&P==="consent_denied"){m(i,{error:null});return}if(P==="rate_limited"&&!r.rateLimitRetried){const N=Math.min(Number(f.retryAfter)>0?Number(f.retryAfter):ge,ke),Le={...r,rateLimitRetried:!0};D(i),I.current[i]=setTimeout(()=>{delete I.current[i],E.current[i]=!0,S.current(Le,{approve:s}).finally(()=>{delete E.current[i]})},N*1e3),m(i,{rateLimitRetried:!0});return}u(i),k(N=>({...N,[i]:{origin:i,reason:`broker:${P}`,detail:{status:z!=null?z:null,reason:P},clientId:r.clientId||$.current[i]||null}}))},[h,m,u,D]),S=e.useRef(y);S.current=y,e.useEffect(()=>{Object.values(w).forEach(r=>{r.status!=="broker"||E.current[r.origin]||I.current[r.origin]||(E.current[r.origin]=!0,y(r).finally(()=>{delete E.current[r.origin]}))})},[w,y]);const X=e.useCallback(r=>!v.current||!(r!=null&&r.consent)||E.current[r.origin]?Promise.resolve():(E.current[r.origin]=!0,y(r,{approve:!0}).finally(()=>{delete E.current[r.origin]})),[y]);e.useEffect(()=>()=>{_.current&&_.current(),Object.values(I.current).forEach(clearTimeout)},[]);const W=e.useCallback(r=>{_.current&&(_.current(),_.current=null);const s=o||ue;_.current=s(r,{onBlocked:()=>{T(n=>n[r.origin]?{...n,[r.origin]:{...n[r.origin],popupBlocked:!0}}:n)},onClosed:()=>{const n=j.current[r.origin];!n||n.v2||d(r.origin)}})},[o,d]),K={pending:Object.values(w).filter(r=>r.status==="interactive"),authorize:W,approve:X,complete:h,dismiss:l,originEpochs:F,credentials:Z,unsupported:M};return O.default.createElement(G.Provider,{value:K},t)};ie.propTypes={children:C.default.node,launcher:C.default.func,broker:C.default.func};const we=()=>e.useContext(G),ce=t=>{const{originEpochs:o}=e.useContext(G),c=Q(t);return c&&o[c]||0},le=t=>{const{credentials:o}=e.useContext(G),c=Q(t);return c&&o[c]||null},de=t=>{const{unsupported:o}=e.useContext(G),c=Q(t);return c&&o[c]||null},ae="MMEmbeddableZoneProvider requires a boolean `ready` prop (true once the host holds the bearer its credentialBroker needs). Embedded apps will not load until it is provided.",fe=({children:t,authorizationLauncher:o,credentialBroker:c,ready:a})=>{const w=a===!0,T=e.useRef(!1);e.useEffect(()=>{a!==void 0||T.current||(T.current=!0,console.error(ae),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(ae))},[a]);const[F,B]=e.useState({}),[Z,H]=e.useState({}),M=e.useRef({}),k=e.useRef({}),L=e.useRef({}),j=e.useCallback((u,d)=>{var l;return!!((l=L.current[u])!=null&&l.has(d))},[]),_=e.useCallback((u,d,l)=>{B(p=>({...p,[u]:{origin:d,frameRef:l}}))},[]),$=e.useCallback(u=>{B(d=>{const l={...d};return delete l[u],l}),H(d=>{const l={...d};return delete l[u],l}),delete L.current[u],Object.keys(k.current).forEach(d=>{k.current[d].instanceId===u&&delete k.current[d]})},[]);e.useEffect(()=>()=>{k.current={}},[]),e.useEffect(()=>{const u=(l=[],p,h)=>{l.forEach(m=>m(p,h))},d=l=>{var X,W,K,r,s,n,i;if(!((X=l==null?void 0:l.data)!=null&&X.instanceId))return;const{instanceId:p,type:h}=l.data,m=F[p],E=(K=(W=m==null?void 0:m.frameRef)==null?void 0:W.current)==null?void 0:K.contentWindow;if(!E||l.source!==E||l.origin!==m.origin)return;const y={origin:l.origin},S=M.current;if(h==="response"){const{callbackId:b,response:z,error:f}=l.data,P=k.current[b];P&&P.instanceId===p&&(P.callback(z,f),delete k.current[b]);return}if(h==="mark-registered"){const{requestType:b}=l.data;L.current[p]||(L.current[p]=new Set),L.current[p].add(b);return}h==="ready"&&H(b=>({...b,[p]:!0})),u((r=S[p])==null?void 0:r[h],l.data,y),u((s=S["*"])==null?void 0:s[h],l.data,y),u((n=S[p])==null?void 0:n["*"],l.data,y),u((i=S["*"])==null?void 0:i["*"],l.data,y)};return window.addEventListener("message",d),()=>{window.removeEventListener("message",d)}},[F]);const A=e.useCallback((u,d,l,p)=>{var m,E;const h=F[u];if(!h){console.warn(`Origin not set for id: ${u}. Unable to send message.`);return}(E=(m=h==null?void 0:h.frameRef)==null?void 0:m.current)!=null&&E.contentWindow&&h.frameRef.current.contentWindow.postMessage({type:d,...l,instanceId:u},p||h.origin)},[F]),v=e.useRef(0),x=e.useCallback(()=>`${Date.now()}-${++v.current}`,[]),q=e.useCallback(async(u,d,l={})=>new Promise((p,h)=>{const m=x(),E=(y,S)=>{delete k.current[m],S?h(S):p(y)};k.current[m]={instanceId:u,callback:E},A(u,"request",{callbackId:m,requestType:d,data:l})}),[A]),U=e.useCallback((u,d)=>{A(u,"load-options",d)},[A]),I=e.useCallback((u,d)=>{A(u,"validate",d)},[A]),D=e.useMemo(()=>({registerFrame:_,unregisterFrame:$,sendMessage:A,sendRequest:q,loadOptions:U,validate:I,hasHandler:j,readyFrames:Z,ready:w,addListener:(u,d,l)=>(M.current[u]||(M.current[u]={}),M.current[u][d]||(M.current[u][d]=[]),M.current[u][d].push(l),()=>{M.current[u][d]=M.current[u][d].filter(p=>p!==l)})}),[_,$,A,q,U,I,j,Z,w]);return O.default.createElement(J.Provider,{value:D},O.default.createElement(ie,{launcher:o,broker:c},t))},Y=(t,o,c)=>{const a=e.useContext(J);if(a===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const w=e.useRef(c);e.useEffect(()=>{w.current=c},[c]),e.useEffect(()=>{if(!(!t||!o)){if(o.startsWith("key:")){const T=o.substring(4);return a.readyFrames[t]&&a.sendMessage(t,"subscribe-key",{keyChord:T}),a.addListener(t,`key-pressed:${T}`,(...B)=>w.current(...B))}return a.addListener(t,o,(...T)=>w.current(...T))}},[a,t,o])};fe.propTypes={children:C.default.node.isRequired,ready:C.default.bool.isRequired,authorizationLauncher:C.default.func,credentialBroker:C.default.func};const re=te.default.default||te.default,Te=1e4,_e="credentials",Ie=(t,o)=>`${t}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${_e}`,ye=re.iframe`
1
+ (function(b,r){typeof exports=="object"&&typeof module!="undefined"?r(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],r):(b=typeof globalThis!="undefined"?globalThis:b||self,r(b["mm-react-embeddable"]={},b.React,b.PropTypes,b.styled))})(this,(function(b,r,we,Te){"use strict";const te=t=>t&&t.__esModule?t:{default:t},x=te(r),k=te(we),oe=te(Te),J=r.createContext(null),V=r.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{},setDelivered:()=>{}}),ie="access_token",ne=t=>t?t.kind?t:t.accessToken?{kind:ie,accessToken:t.accessToken,expiresAt:t.expiresAt||null}:null:null,_e=60,Ie=120,Me=3e4,ye=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),X=t=>{try{return new URL(t).origin}catch{return null}},ce=(t,o)=>{const l=o==null?void 0:o.origin;return!l||X(t==null?void 0:t.authUrl)!==l?null:l},le=t=>{if(!t||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(t)||t.includes(":"))return t;const o=t.split(".");return o.length<=2?t:o.slice(-2).join(".")},se=(t,o)=>{try{const l=new URL(t),p=new URL(o);return l.protocol!==p.protocol?!0:le(l.hostname)!==le(p.hostname)}catch{return!0}},de=(t,{onBlocked:o,onClosed:l})=>{const p=window.open(t.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!p)return o(),()=>{};const m=setInterval(()=>{p.closed&&(clearInterval(m),l())},1e3);return()=>clearInterval(m)},ae=({children:t,launcher:o,broker:l,ready:p})=>{const m=r.useContext(J),[C,I]=r.useState({}),[j,Q]=r.useState({}),[B,w]=r.useState({}),[h,y]=r.useState({}),$=r.useRef(B);$.current=B;const M=r.useRef(C);M.current=C;const O=r.useRef(null),T=r.useRef({}),Z=r.useRef({}),H=r.useRef(l);H.current=l;const z=r.useRef({}),P=r.useRef({}),D=r.useCallback((e,s,n)=>{!e||!s||(z.current[e]||(z.current[e]=new Set),n?z.current[e].add(s):z.current[e].delete(s))},[]),v=r.useCallback(e=>{const s=T.current[e];if(!s)return;const n=u=>!u[e]||u[e].clientId?u:{...u,[e]:{...u[e],clientId:s}};I(n),y(n)},[]),c=r.useCallback((e,s)=>{e&&(y(n=>n[e]?n:{...n,[e]:{origin:e,reason:s,clientId:T.current[e]||null}}),v(e))},[v]),d=r.useCallback((e,{v2:s=!1,origin:n=null}={})=>{var u;if(n){if(s&&$.current[n]){if(!((u=z.current[n])!=null&&u.has(e.instanceId)))return;const g=Date.now();if(g-(P.current[n]||0)<Me)return;P.current[n]=g,z.current[n].clear();const W={...$.current};delete W[n],$.current=W,w(W)}I(g=>g[n]?g:{...g,[n]:{origin:n,authUrl:e.authUrl,status:s&&H.current?"broker":"interactive",v2:s,scope:e.scope||null,acceptsCredentials:Z.current[n]||null,consent:null,error:null,clientId:e.clientId||T.current[n]||null,popupBlocked:!1}}),v(n)}},[v]),i=r.useRef({}),f=r.useCallback(e=>{i.current[e]&&(clearTimeout(i.current[e]),delete i.current[e])},[]),a=r.useCallback(e=>{f(e),I(s=>{if(!s[e])return s;const n={...s};return delete n[e],n})},[f]),E=r.useCallback(e=>{e&&(a(e),Q(s=>({...s,[e]:(s[e]||0)+1})))},[a]),F=r.useCallback(e=>{a(e.origin)},[a]),_=r.useCallback((e,s)=>{const n=ce(e,s);if(n){if(se(n,window.location.origin)){c(n,"cross-site-legacy");return}d(e,{origin:n})}},[d,c]);r.useEffect(()=>{if(!m)return;const e=[m.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&(T.current[n.origin]=s.clientId,v(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?Z.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&se(n.origin,window.location.origin)&&c(n.origin,"cross-site-legacy")}),m.addListener("*","authorize",_),m.addListener("*","login",_),m.addListener("*","credential-request",(s,n)=>d(s,{v2:!0,origin:ce(s,n)}))];return()=>e.forEach(s=>s())},[m,_,d,v,c]),r.useEffect(()=>{const e=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const u=X(n.authUrl);if(!u||s.origin!==u)return;const g=M.current[u];if(!(!g||g.status!=="interactive")){if(n.accessToken){w(W=>({...W,[u]:ne({accessToken:n.accessToken})})),a(u);return}E(u)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[a,E]);const A=r.useCallback((e,s)=>{const n=e==null?void 0:e.origin,u=ne(s);!n||!u||(w(g=>({...g,[n]:u})),a(n))},[a]),S=r.useCallback((e,s)=>{I(n=>n[e]?{...n,[e]:{...n[e],...s}}:n)},[]),L=r.useRef({}),U=r.useCallback(async(e,{approve:s}={})=>{const n=H.current,{origin:u}=e;let g;try{g=await n({origin:u,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:s})}catch{s?S(u,{error:{reason:"broker_unavailable"}}):S(u,{status:"interactive"});return}const W=g==null?void 0:g.status,Y=(g==null?void 0:g.body)||{};if(W===200&&Y.credential){A(e,Y.credential);return}if(W===409&&Y.consentRequired){S(u,{status:"interactive",consent:{client:Y.client||null,stepUp:!!Y.stepUp,scope:Y.scope||e.scope},error:null});return}const ee=Y.error||"broker_error";if(!s&&ye.has(ee)){S(u,{status:"interactive"});return}if(s&&ee==="consent_denied"){S(u,{error:null});return}if(ee==="rate_limited"&&!e.rateLimitRetried){const re=Math.min(Number(Y.retryAfter)>0?Number(Y.retryAfter):_e,Ie),xe={...e,rateLimitRetried:!0};f(u),i.current[u]=setTimeout(()=>{delete i.current[u],L.current[u]=!0,K.current(xe,{approve:s}).finally(()=>{delete L.current[u]})},re*1e3),S(u,{rateLimitRetried:!0});return}a(u),y(re=>({...re,[u]:{origin:u,reason:`broker:${ee}`,detail:{status:W!=null?W:null,reason:ee},clientId:e.clientId||T.current[u]||null}}))},[A,S,a,f]),K=r.useRef(U);K.current=U,r.useEffect(()=>{Object.values(C).forEach(e=>{e.status!=="broker"||L.current[e.origin]||i.current[e.origin]||(L.current[e.origin]=!0,U(e).finally(()=>{delete L.current[e.origin]}))})},[C,U]);const R=r.useCallback(e=>!H.current||!(e!=null&&e.consent)||L.current[e.origin]?Promise.resolve():(L.current[e.origin]=!0,U(e,{approve:!0}).finally(()=>{delete L.current[e.origin]})),[U]);r.useEffect(()=>()=>{O.current&&O.current(),Object.values(i.current).forEach(clearTimeout)},[]),r.useEffect(()=>{p===!1&&(Object.values(i.current).forEach(clearTimeout),i.current={},L.current={},T.current={},Z.current={},z.current={},P.current={},$.current={},w({}),I({}),y({}))},[p]);const G=r.useCallback(e=>{O.current&&(O.current(),O.current=null);const s=o||de;O.current=s(e,{onBlocked:()=>{I(n=>n[e.origin]?{...n,[e.origin]:{...n[e.origin],popupBlocked:!0}}:n)},onClosed:()=>{const n=M.current[e.origin];!n||n.v2||E(e.origin)}})},[o,E]),q={pending:Object.values(C).filter(e=>e.status==="interactive"),authorize:G,approve:R,complete:A,dismiss:F,originEpochs:j,credentials:B,unsupported:h,setDelivered:D};return x.default.createElement(V.Provider,{value:q},t)};ae.propTypes={children:k.default.node,launcher:k.default.func,ready:k.default.bool,broker:k.default.func};const fe=()=>r.useContext(V),pe=t=>{const{originEpochs:o}=r.useContext(V),l=X(t);return l&&o[l]||0},be=t=>{const{credentials:o}=r.useContext(V),l=X(t);return l&&o[l]||null},he=t=>{const{unsupported:o}=r.useContext(V),l=X(t);return l&&o[l]||null},me="MMEmbeddableZoneProvider requires a boolean `ready` prop (true once the host holds the bearer its credentialBroker needs). Embedded apps will not load until it is provided.",Ee=({children:t,authorizationLauncher:o,credentialBroker:l,ready:p})=>{const m=p===!0,C=r.useRef(!1);r.useEffect(()=>{p!==void 0||C.current||(C.current=!0,console.error(me),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(me))},[p]);const[I,j]=r.useState({}),[Q,B]=r.useState({}),w=r.useRef({}),h=r.useRef({}),y=r.useRef({}),$=r.useCallback((c,d)=>{var i;return!!((i=y.current[c])!=null&&i.has(d))},[]),M=r.useCallback((c,d,i)=>{j(f=>({...f,[c]:{origin:d,frameRef:i}}))},[]),O=r.useCallback(c=>{j(d=>{const i={...d};return delete i[c],i}),B(d=>{const i={...d};return delete i[c],i}),delete y.current[c],Object.keys(h.current).forEach(d=>{h.current[d].instanceId===c&&delete h.current[d]})},[]);r.useEffect(()=>()=>{h.current={}},[]),r.useEffect(()=>{const c=(i=[],f,a)=>{i.forEach(E=>E(f,a))},d=i=>{var S,L,U,K,R,G,q;if(!((S=i==null?void 0:i.data)!=null&&S.instanceId))return;const{instanceId:f,type:a}=i.data,E=I[f],F=(U=(L=E==null?void 0:E.frameRef)==null?void 0:L.current)==null?void 0:U.contentWindow;if(!F||i.source!==F||i.origin!==E.origin)return;const _={origin:i.origin},A=w.current;if(a==="response"){const{callbackId:e,response:s,error:n}=i.data,u=h.current[e];u&&u.instanceId===f&&(u.callback(s,n),delete h.current[e]);return}if(a==="mark-registered"){const{requestType:e}=i.data;y.current[f]||(y.current[f]=new Set),y.current[f].add(e);return}a==="ready"&&B(e=>({...e,[f]:!0})),c((K=A[f])==null?void 0:K[a],i.data,_),c((R=A["*"])==null?void 0:R[a],i.data,_),c((G=A[f])==null?void 0:G["*"],i.data,_),c((q=A["*"])==null?void 0:q["*"],i.data,_)};return window.addEventListener("message",d),()=>{window.removeEventListener("message",d)}},[I]);const T=r.useCallback((c,d,i,f)=>{var E,F;const a=I[c];if(!a){console.warn(`Origin not set for id: ${c}. Unable to send message.`);return}(F=(E=a==null?void 0:a.frameRef)==null?void 0:E.current)!=null&&F.contentWindow&&a.frameRef.current.contentWindow.postMessage({type:d,...i,instanceId:c},f||a.origin)},[I]),Z=r.useRef(0),H=r.useCallback(()=>`${Date.now()}-${++Z.current}`,[]),z=r.useCallback(async(c,d,i={})=>new Promise((f,a)=>{const E=H(),F=(_,A)=>{delete h.current[E],A?a(A):f(_)};h.current[E]={instanceId:c,callback:F},T(c,"request",{callbackId:E,requestType:d,data:i})}),[T]),P=r.useCallback((c,d)=>{T(c,"load-options",d)},[T]),D=r.useCallback((c,d)=>{T(c,"validate",d)},[T]),v=r.useMemo(()=>({registerFrame:M,unregisterFrame:O,sendMessage:T,sendRequest:z,loadOptions:P,validate:D,hasHandler:$,readyFrames:Q,ready:m,addListener:(c,d,i)=>(w.current[c]||(w.current[c]={}),w.current[c][d]||(w.current[c][d]=[]),w.current[c][d].push(i),()=>{w.current[c][d]=w.current[c][d].filter(f=>f!==i)})}),[M,O,T,z,P,D,$,Q,m]);return x.default.createElement(J.Provider,{value:v},x.default.createElement(ae,{launcher:o,broker:l,ready:m},t))},N=(t,o,l)=>{const p=r.useContext(J);if(p===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=r.useRef(l);r.useEffect(()=>{m.current=l},[l]),r.useEffect(()=>{if(!(!t||!o)){if(o.startsWith("key:")){const C=o.substring(4);return p.readyFrames[t]&&p.sendMessage(t,"subscribe-key",{keyChord:C}),p.addListener(t,`key-pressed:${C}`,(...j)=>m.current(...j))}return p.addListener(t,o,(...C)=>m.current(...C))}},[p,t,o])};Ee.propTypes={children:k.default.node.isRequired,ready:k.default.bool.isRequired,authorizationLauncher:k.default.func,credentialBroker:k.default.func};const ue=oe.default.default||oe.default,Ae=1e4,Se="credentials",Le=(t,o)=>`${t}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Se}`,Ue=ue.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${t=>t.$isReady?"visible":"hidden"};
6
- `,Me=re.div`
6
+ `,ze=ue.div`
7
7
  width: 100%;
8
8
  height: 100%;
9
9
  display: flex;
@@ -16,10 +16,10 @@
16
16
  text-align: center;
17
17
  color: #555;
18
18
  font-size: 14px;
19
- `,pe=t=>{const o=t&&t.origin||"This app",c=t&&t.reason||"";switch(c){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 c.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."}}},be=({entry:t})=>{const o=pe(t);return O.default.createElement(Me,{"data-reason":t&&t.reason||void 0},O.default.createElement("div",{style:{fontWeight:600}},o.title),O.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};be.propTypes={entry:C.default.shape({origin:C.default.string,name:C.default.string,reason:C.default.string,detail:C.default.object})};const Se=re.div`
19
+ `,ge=t=>{const o=t&&t.origin||"This app",l=t&&t.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."}}},ke=({entry:t})=>{const o=ge(t);return x.default.createElement(ze,{"data-reason":t&&t.reason||void 0},x.default.createElement("div",{style:{fontWeight:600}},o.title),x.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};ke.propTypes={entry:k.default.shape({origin:k.default.string,name:k.default.string,reason:k.default.string,detail:k.default.object})};const Oe=ue.div`
20
20
  height: 100%;
21
21
  display: flex;
22
22
  flex: 1;
23
23
  align-items: center;
24
24
  justify-content: center;
25
- `,Ae=()=>O.default.createElement(Se,null,O.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),he=({id:t,src:o,path:c,onInit:a=()=>{},onTimeout:w,context:T={},params:F={},renderUnsupported:B,renderTimeout:Z,...H})=>{if("isConfiguring"in H)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in T)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in T)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const M=e.useRef(),k=Q(o),[L,j]=e.useState(),[_,$]=e.useState(0),[A,v]=e.useState(!1),[x,q]=e.useState(!1),U=ce(o),I=le(o),D=de(o),[u,d]=e.useState(0),[l,p]=e.useState(null),{registerFrame:h,unregisterFrame:m,sendMessage:E,ready:y}=e.useContext(J),[S,X]=e.useState(),W=e.useRef(),K=e.useRef(w),r=e.useRef(a),s=e.useRef(!1),n=e.useRef(),i=e.useCallback(()=>{s.current=!1,clearTimeout(W.current),clearTimeout(n.current),$(0),v(!1),q(!1),p(null)},[]);e.useEffect(()=>{(U>0||u>0)&&i()},[U,u,i]),e.useEffect(()=>{K.current=w},[w]),e.useEffect(()=>{r.current=a},[a]),e.useEffect(()=>{_&&E(t,"set-context",{context:{...T}},k)},[T,t,_,k]),e.useEffect(()=>{_&&E(t,"set-params",{params:F},k)},[F,t,_,k]),e.useEffect(()=>{c!==void 0&&X((c==null?void 0:c.indexOf("/"))===0?c:`/${c}`)},[c]),e.useEffect(()=>{S===void 0||!_||E(t,"navigate",{path:S},k)},[S,_,t,E,k]),e.useEffect(()=>{if(!o||!t||!M)return;if(!y){i(),j(void 0);return}i();const f=new URL(o);return h(t,f.origin,M),r.current(),j(Ie(f.origin,t)),()=>{m(t)}},[o,t,y,h,M,i]),e.useEffect(()=>{if(!(!L||s.current))return W.current=setTimeout(()=>{v(!0),K.current&&K.current()},Te),()=>clearTimeout(W.current)},[L,o,U,u]);const b=e.useCallback(()=>{s.current=!0,clearTimeout(W.current),v(!1)},[]);if(Y(t,"init",f=>{b(),Array.isArray(f==null?void 0:f.acceptsCredentials)&&p(f.acceptsCredentials)}),Y(t,"authorize",b),Y(t,"login",b),Y(t,"credential-request",()=>{b(),q(!0)}),e.useEffect(()=>{if(!x||!I)return;const f=l;if(Array.isArray(f)&&!f.includes(I.kind))return;const P=I.kind==="access_token"?{accessToken:I.accessToken}:{};E(t,"credential",{...P,credential:I},k)},[x,I,l,t,E,k]),Y(t,"ready",()=>{b(),$(f=>f+1)}),Y(t,"heartbeat",()=>{n.current&&clearTimeout(n.current),n.current=setTimeout(()=>{d(f=>f+1)},6e3)}),e.useEffect(()=>()=>{n.current&&clearTimeout(n.current)},[]),D)return B?B(D):O.default.createElement(be,{entry:D});if(A&&!_)return Z?Z():O.default.createElement(Ae,null);const z={...H};return delete z.skipManifestCheck,O.default.createElement(ye,{key:`auth-epoch-${U}-frame-${u}`,$isReady:_,ref:M,src:L,...z})};he.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},g.CREDENTIAL_KIND_ACCESS_TOKEN=ne,g.EmbeddedAuthorizationContext=G,g.MMEmbeddableZone=he,g.MMEmbeddableZoneContext=J,g.MMEmbeddableZoneProvider=fe,g.isCrossSite=ee,g.normalizeCredential=V,g.popupAuthorizationLauncher=ue,g.unsupportedCopy=pe,g.useEmbeddedAuthorizations=we,g.useEmbeddedCredential=le,g.useEmbeddedEffect=Y,g.useEmbeddedOriginEpoch=ce,g.useEmbeddedUnsupported=de,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
25
+ `,ve=()=>x.default.createElement(Oe,null,x.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),Ce=({id:t,src:o,path:l,onInit:p=()=>{},onTimeout:m,context:C={},params:I={},renderUnsupported:j,renderTimeout:Q,...B})=>{if("isConfiguring"in B)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in C)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in C)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const w=r.useRef(),h=X(o),[y,$]=r.useState(),[M,O]=r.useState(0),[T,Z]=r.useState(!1),[H,z]=r.useState(!1),P=pe(o),D=be(o),v=he(o),{setDelivered:c}=fe(),[d,i]=r.useState(0),[f,a]=r.useState(null),{registerFrame:E,unregisterFrame:F,sendMessage:_,ready:A}=r.useContext(J),[S,L]=r.useState(),U=r.useRef(),K=r.useRef(m),R=r.useRef(p),G=r.useRef(!1),q=r.useRef(),e=r.useCallback(()=>{G.current=!1,clearTimeout(U.current),clearTimeout(q.current),O(0),Z(!1),z(!1),a(null),c(h,t,!1)},[c,h,t]);r.useEffect(()=>{(P>0||d>0)&&e()},[P,d,e]),r.useEffect(()=>{K.current=m},[m]),r.useEffect(()=>{R.current=p},[p]),r.useEffect(()=>{M&&_(t,"set-context",{context:{...C}},h)},[C,t,M,h]),r.useEffect(()=>{M&&_(t,"set-params",{params:I},h)},[I,t,M,h]),r.useEffect(()=>{l!==void 0&&L((l==null?void 0:l.indexOf("/"))===0?l:`/${l}`)},[l]),r.useEffect(()=>{S===void 0||!M||_(t,"navigate",{path:S},h)},[S,M,t,_,h]),r.useEffect(()=>{if(!o||!t||!w)return;if(!A){e(),$(void 0);return}e();const u=new URL(o);return E(t,u.origin,w),R.current(),$(Le(u.origin,t)),()=>{F(t)}},[o,t,A,E,w,e]),r.useEffect(()=>{if(!(!y||G.current))return U.current=setTimeout(()=>{Z(!0),K.current&&K.current()},Ae),()=>clearTimeout(U.current)},[y,o,P,d]);const s=r.useCallback(()=>{G.current=!0,clearTimeout(U.current),Z(!1)},[]);if(N(t,"init",u=>{s(),Array.isArray(u==null?void 0:u.acceptsCredentials)&&a(u.acceptsCredentials)}),N(t,"authorize",s),N(t,"login",s),N(t,"credential-request",()=>{s(),z(!0)}),r.useEffect(()=>{if(!H||!D)return;const u=f;if(Array.isArray(u)&&!u.includes(D.kind))return;const g=D.kind==="access_token"?{accessToken:D.accessToken}:{};_(t,"credential",{...g,credential:D},h),c(h,t,!0)},[H,D,f,t,_,h,c]),N(t,"ready",()=>{s(),O(u=>u+1)}),N(t,"heartbeat",()=>{q.current&&clearTimeout(q.current),q.current=setTimeout(()=>{i(u=>u+1)},6e3)}),r.useEffect(()=>()=>{q.current&&clearTimeout(q.current)},[]),v)return j?j(v):x.default.createElement(ke,{entry:v});if(T&&!M)return Q?Q():x.default.createElement(ve,null);const n={...B};return delete n.skipManifestCheck,x.default.createElement(Ue,{key:`auth-epoch-${P}-frame-${d}`,$isReady:M,ref:w,src:y,...n})};Ce.propTypes={id:k.default.string,src:k.default.string.isRequired,path:k.default.string,onInit:k.default.func,onTimeout:k.default.func,renderUnsupported:k.default.func,renderTimeout:k.default.func},b.CREDENTIAL_KIND_ACCESS_TOKEN=ie,b.EmbeddedAuthorizationContext=V,b.MMEmbeddableZone=Ce,b.MMEmbeddableZoneContext=J,b.MMEmbeddableZoneProvider=Ee,b.isCrossSite=se,b.normalizeCredential=ne,b.popupAuthorizationLauncher=de,b.unsupportedCopy=ge,b.useEmbeddedAuthorizations=fe,b.useEmbeddedCredential=be,b.useEmbeddedEffect=N,b.useEmbeddedOriginEpoch=pe,b.useEmbeddedUnsupported=he,Object.defineProperty(b,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.1",
3
+ "version": "2.0.2",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",