@machinemetrics/mm-react-embeddable 2.0.1-0 → 2.0.1-1

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,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),me=require("prop-types"),Ee=require("styled-components"),V=r=>r&&r.__esModule?r:{default:r},O=V(e),m=V(me),te=V(Ee),G=e.createContext(null),Y=e.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),oe="access_token",J=r=>r?r.kind?r:r.accessToken?{kind:oe,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,ge=60,Re=120,ke=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),Q=r=>{try{return new URL(r).origin}catch{return null}},re=(r,o)=>{const i=o==null?void 0:o.origin;return!i||Q(r==null?void 0:r.authUrl)!==i?null:i},ne=r=>{if(!r||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(r)||r.includes(":"))return r;const o=r.split(".");return o.length<=2?r:o.slice(-2).join(".")},N=(r,o)=>{try{const i=new URL(r),b=new URL(o);return i.protocol!==b.protocol?!0:ne(i.hostname)!==ne(b.hostname)}catch{return!0}},ue=(r,{onBlocked:o,onClosed:i})=>{const b=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!b)return o(),()=>{};const h=setInterval(()=>{b.closed&&(clearInterval(h),i())},1e3);return()=>clearInterval(h)},ce=({children:r,launcher:o,broker:i})=>{const b=e.useContext(G),[h,E]=e.useState({}),[$,P]=e.useState({}),[q,B]=e.useState({}),[k,M]=e.useState({}),L=e.useRef(q);L.current=q;const w=e.useRef(h);w.current=h;const y=e.useRef(null),F=e.useRef({}),C=e.useRef({}),v=e.useRef(i);v.current=i;const x=e.useCallback(t=>{const s=F.current[t];if(!s)return;const n=u=>!u[t]||u[t].clientId?u:{...u,[t]:{...u[t],clientId:s}};E(n),M(n)},[]),S=e.useCallback((t,s)=>{t&&(M(n=>n[t]?n:{...n,[t]:{origin:t,reason:s,clientId:F.current[t]||null}}),x(t))},[x]),_=e.useCallback((t,{v2:s=!1,origin:n=null}={})=>{n&&(s&&L.current[n]||(E(u=>u[n]?u:{...u,[n]:{origin:n,authUrl:t.authUrl,status:s&&v.current?"broker":"interactive",v2:s,scope:t.scope||null,acceptsCredentials:C.current[n]||null,consent:null,error:null,clientId:t.clientId||F.current[n]||null,popupBlocked:!1}}),x(n)))},[x]),T=e.useRef({}),U=e.useCallback(t=>{T.current[t]&&(clearTimeout(T.current[t]),delete T.current[t])},[]),c=e.useCallback(t=>{U(t),E(s=>{if(!s[t])return s;const n={...s};return delete n[t],n})},[U]),d=e.useCallback(t=>{t&&(c(t),P(s=>({...s,[t]:(s[t]||0)+1})))},[c]),a=e.useCallback(t=>{c(t.origin)},[c]),f=e.useCallback((t,s)=>{const n=re(t,s);if(n){if(N(n,window.location.origin)){S(n,"cross-site-legacy");return}_(t,{origin:n})}},[_,S]);e.useEffect(()=>{if(!b)return;const t=[b.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&(F.current[n.origin]=s.clientId,x(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?C.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&N(n.origin,window.location.origin)&&S(n.origin,"cross-site-legacy")}),b.addListener("*","authorize",f),b.addListener("*","login",f),b.addListener("*","credential-request",(s,n)=>_(s,{v2:!0,origin:re(s,n)}))];return()=>t.forEach(s=>s())},[b,f,_,x,S]),e.useEffect(()=>{const t=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const u=Q(n.authUrl);if(!(!u||s.origin!==u)){if(n.accessToken){if(!w.current[u])return;B(l=>({...l,[u]:J({accessToken:n.accessToken})})),c(u);return}d(u)}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},[c,d]);const g=e.useCallback((t,s)=>{const n=t==null?void 0:t.origin,u=J(s);!n||!u||(B(l=>({...l,[n]:u})),c(n))},[c]),p=e.useCallback((t,s)=>{E(n=>n[t]?{...n,[t]:{...n[t],...s}}:n)},[]),I=e.useRef({}),R=e.useCallback(async(t,{approve:s}={})=>{const n=v.current,{origin:u}=t;let l;try{l=await n({origin:u,clientId:t.clientId,scope:t.scope,authUrl:t.authUrl},{approve:s})}catch{s?p(u,{error:{reason:"broker_unavailable"}}):p(u,{status:"interactive"});return}const K=l==null?void 0:l.status,z=(l==null?void 0:l.body)||{};if(K===200&&z.credential){g(t,z.credential);return}if(K===409&&z.consentRequired){p(u,{status:"interactive",consent:{client:z.client||null,stepUp:!!z.stepUp,scope:z.scope||t.scope},error:null});return}const j=z.error||"broker_error";if(!s&&ke.has(j)){p(u,{status:"interactive"});return}if(s&&j==="consent_denied"){p(u,{error:null});return}if(j==="rate_limited"&&!t.rateLimitRetried){const X=Math.min(Number(z.retryAfter)>0?Number(z.retryAfter):ge,Re),he={...t,rateLimitRetried:!0};U(u),T.current[u]=setTimeout(()=>{delete T.current[u],I.current[u]=!0,A.current(he,{approve:s}).finally(()=>{delete I.current[u]})},X*1e3),p(u,{rateLimitRetried:!0});return}c(u),M(X=>({...X,[u]:{origin:u,reason:`broker:${j}`,detail:l||null,clientId:t.clientId||F.current[u]||null}}))},[g,p,c,U]),A=e.useRef(R);A.current=R,e.useEffect(()=>{Object.values(h).forEach(t=>{t.status!=="broker"||I.current[t.origin]||T.current[t.origin]||(I.current[t.origin]=!0,R(t).finally(()=>{delete I.current[t.origin]}))})},[h,R]);const D=e.useCallback(t=>!v.current||!(t!=null&&t.consent)?Promise.resolve():R(t,{approve:!0}),[R]);e.useEffect(()=>()=>{y.current&&y.current(),Object.values(T.current).forEach(clearTimeout)},[]);const Z=e.useCallback(t=>{y.current&&(y.current(),y.current=null);const s=o||ue;y.current=s(t,{onBlocked:()=>{E(n=>n[t.origin]?{...n,[t.origin]:{...n[t.origin],popupBlocked:!0}}:n)},onClosed:()=>d(t.origin)})},[o,d]),W={pending:Object.values(h).filter(t=>t.status==="interactive"),authorize:Z,approve:D,complete:g,dismiss:a,originEpochs:$,credentials:q,unsupported:k};return O.default.createElement(Y.Provider,{value:W},r)};ce.propTypes={children:m.default.node,launcher:m.default.func,broker:m.default.func};const Ce=()=>e.useContext(Y),ie=r=>{const{originEpochs:o}=e.useContext(Y),i=Q(r);return i&&o[i]||0},ae=r=>{const{credentials:o}=e.useContext(Y),i=Q(r);return i&&o[i]||null},le=r=>{const{unsupported:o}=e.useContext(Y),i=Q(r);return i&&o[i]||null},se="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.",de=({children:r,authorizationLauncher:o,credentialBroker:i,ready:b})=>{const h=b===!0,E=e.useRef(!1);e.useEffect(()=>{b!==void 0||E.current||(E.current=!0,console.error(se),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(se))},[b]);const[$,P]=e.useState({}),[q,B]=e.useState({}),k=e.useRef({}),M=e.useRef({}),L=e.useRef({}),w=e.useCallback((c,d)=>{var a;return!!((a=L.current[c])!=null&&a.has(d))},[]),y=e.useCallback((c,d,a)=>{P(f=>({...f,[c]:{origin:d,frameRef:a}}))},[]),F=e.useCallback(c=>{P(d=>{const a={...d};return delete a[c],a}),B(d=>{const a={...d};return delete a[c],a}),delete L.current[c]},[]);e.useEffect(()=>{const c=(a=[],f,g)=>{a.forEach(p=>p(f,g))},d=a=>{var D,Z,W,t,s,n,u;if(!((D=a==null?void 0:a.data)!=null&&D.instanceId))return;const{instanceId:f,type:g}=a.data,p=$[f],I=(W=(Z=p==null?void 0:p.frameRef)==null?void 0:Z.current)==null?void 0:W.contentWindow;if(!I||a.source!==I||a.origin!==p.origin)return;const R={origin:a.origin},A=k.current;if(g==="response"){const{callbackId:l,response:K,error:z}=a.data,j=M.current[l];j&&(j(K,z),delete M.current[l]);return}if(g==="mark-registered"){const{requestType:l}=a.data;L.current[f]||(L.current[f]=new Set),L.current[f].add(l);return}g==="ready"&&B(l=>({...l,[f]:!0})),c((t=A[f])==null?void 0:t[g],a.data,R),c((s=A["*"])==null?void 0:s[g],a.data,R),c((n=A[f])==null?void 0:n["*"],a.data,R),c((u=A["*"])==null?void 0:u["*"],a.data,R)};return window.addEventListener("message",d),()=>{window.removeEventListener("message",d)}},[$]);const C=e.useCallback((c,d,a)=>{var g,p;const f=$[c];if(!f){console.warn(`Origin not set for id: ${c}. Unable to send message.`);return}(p=(g=f==null?void 0:f.frameRef)==null?void 0:g.current)!=null&&p.contentWindow&&f.frameRef.current.contentWindow.postMessage({type:d,...a,instanceId:c},f.origin)},[$]),v=e.useRef(0),x=e.useCallback(()=>`${Date.now()}-${++v.current}`,[]),S=e.useCallback(async(c,d,a={})=>new Promise((f,g)=>{const p=x(),I=(R,A)=>{delete M.current[p],A?g(A):f(R)};M.current[p]=I,C(c,"request",{callbackId:p,requestType:d,data:a})}),[C]),_=e.useCallback((c,d)=>{C(c,"load-options",d)},[C]),T=e.useCallback((c,d)=>{C(c,"validate",d)},[C]),U=e.useMemo(()=>({registerFrame:y,unregisterFrame:F,sendMessage:C,sendRequest:S,loadOptions:_,validate:T,hasHandler:w,readyFrames:q,ready:h,addListener:(c,d,a)=>(k.current[c]||(k.current[c]={}),k.current[c][d]||(k.current[c][d]=[]),k.current[c][d].push(a),()=>{k.current[c][d]=k.current[c][d].filter(f=>f!==a)})}),[y,F,C,S,_,T,w,q,h]);return O.default.createElement(G.Provider,{value:U},O.default.createElement(ce,{launcher:o,broker:i},r))},H=(r,o,i)=>{const b=e.useContext(G);if(b===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const h=e.useRef(i);e.useEffect(()=>{h.current=i},[i]),e.useEffect(()=>{if(!(!r||!o)){if(o.startsWith("key:")){const E=o.substring(4);return b.readyFrames[r]&&b.sendMessage(r,"subscribe-key",{keyChord:E}),b.addListener(r,`key-pressed:${E}`,(...P)=>h.current(...P))}return b.addListener(r,o,(...E)=>h.current(...E))}},[b,r,o])};de.propTypes={children:m.default.node.isRequired,ready:m.default.bool.isRequired,authorizationLauncher:m.default.func,credentialBroker:m.default.func};const ee=te.default.default||te.default,we=1e4,_e="credentials",Te=(r,o)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${_e}`,Ie=ee.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),ge=require("prop-types"),me=require("styled-components"),V=r=>r&&r.__esModule?r:{default:r},O=V(e),k=V(ge),te=V(me),Q=e.createContext(null),Y=e.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),oe="access_token",J=r=>r?r.kind?r:r.accessToken?{kind:oe,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Ee=60,Re=120,ke=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),G=r=>{try{return new URL(r).origin}catch{return null}},re=(r,o)=>{const i=o==null?void 0:o.origin;return!i||G(r==null?void 0:r.authUrl)!==i?null:i},ne=r=>{if(!r||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(r)||r.includes(":"))return r;const o=r.split(".");return o.length<=2?r:o.slice(-2).join(".")},N=(r,o)=>{try{const i=new URL(r),d=new URL(o);return i.protocol!==d.protocol?!0:ne(i.hostname)!==ne(d.hostname)}catch{return!0}},ue=(r,{onBlocked:o,onClosed:i})=>{const d=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!d)return o(),()=>{};const R=setInterval(()=>{d.closed&&(clearInterval(R),i())},1e3);return()=>clearInterval(R)},ce=({children:r,launcher:o,broker:i})=>{const d=e.useContext(Q),[R,C]=e.useState({}),[$,v]=e.useState({}),[D,Z]=e.useState({}),[I,E]=e.useState({}),S=e.useRef(D);S.current=D;const W=e.useRef(R);W.current=R;const w=e.useRef(null),x=e.useRef({}),y=e.useRef({}),P=e.useRef(i);P.current=i;const U=e.useCallback(t=>{const s=x.current[t];if(!s)return;const n=c=>!c[t]||c[t].clientId?c:{...c,[t]:{...c[t],clientId:s}};C(n),E(n)},[]),F=e.useCallback((t,s)=>{t&&(E(n=>n[t]?n:{...n,[t]:{origin:t,reason:s,clientId:x.current[t]||null}}),U(t))},[U]),A=e.useCallback((t,{v2:s=!1,origin:n=null}={})=>{n&&(s&&S.current[n]||(C(c=>c[n]?c:{...c,[n]:{origin:n,authUrl:t.authUrl,status:s&&P.current?"broker":"interactive",v2:s,scope:t.scope||null,acceptsCredentials:y.current[n]||null,consent:null,error:null,clientId:t.clientId||x.current[n]||null,popupBlocked:!1}}),U(n)))},[U]),T=e.useRef({}),q=e.useCallback(t=>{T.current[t]&&(clearTimeout(T.current[t]),delete T.current[t])},[]),u=e.useCallback(t=>{q(t),C(s=>{if(!s[t])return s;const n={...s};return delete n[t],n})},[q]),l=e.useCallback(t=>{t&&(u(t),v(s=>({...s,[t]:(s[t]||0)+1})))},[u]),a=e.useCallback(t=>{u(t.origin)},[u]),p=e.useCallback((t,s)=>{const n=re(t,s);if(n){if(N(n,window.location.origin)){F(n,"cross-site-legacy");return}A(t,{origin:n})}},[A,F]);e.useEffect(()=>{if(!d)return;const t=[d.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&(x.current[n.origin]=s.clientId,U(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?y.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&N(n.origin,window.location.origin)&&F(n.origin,"cross-site-legacy")}),d.addListener("*","authorize",p),d.addListener("*","login",p),d.addListener("*","credential-request",(s,n)=>A(s,{v2:!0,origin:re(s,n)}))];return()=>t.forEach(s=>s())},[d,p,A,U,F]),e.useEffect(()=>{const t=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const c=G(n.authUrl);if(!c||s.origin!==c)return;const b=W.current[c];if(!(!b||b.status!=="interactive")){if(n.accessToken){Z(L=>({...L,[c]:J({accessToken:n.accessToken})})),u(c);return}l(c)}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},[u,l]);const h=e.useCallback((t,s)=>{const n=t==null?void 0:t.origin,c=J(s);!n||!c||(Z(b=>({...b,[n]:c})),u(n))},[u]),g=e.useCallback((t,s)=>{C(n=>n[t]?{...n,[t]:{...n[t],...s}}:n)},[]),m=e.useRef({}),_=e.useCallback(async(t,{approve:s}={})=>{const n=P.current,{origin:c}=t;let b;try{b=await n({origin:c,clientId:t.clientId,scope:t.scope,authUrl:t.authUrl},{approve:s})}catch{s?g(c,{error:{reason:"broker_unavailable"}}):g(c,{status:"interactive"});return}const L=b==null?void 0:b.status,f=(b==null?void 0:b.body)||{};if(L===200&&f.credential){h(t,f.credential);return}if(L===409&&f.consentRequired){g(c,{status:"interactive",consent:{client:f.client||null,stepUp:!!f.stepUp,scope:f.scope||t.scope},error:null});return}const z=f.error||"broker_error";if(!s&&ke.has(z)){g(c,{status:"interactive"});return}if(s&&z==="consent_denied"){g(c,{error:null});return}if(z==="rate_limited"&&!t.rateLimitRetried){const X=Math.min(Number(f.retryAfter)>0?Number(f.retryAfter):Ee,Re),he={...t,rateLimitRetried:!0};q(c),T.current[c]=setTimeout(()=>{delete T.current[c],m.current[c]=!0,M.current(he,{approve:s}).finally(()=>{delete m.current[c]})},X*1e3),g(c,{rateLimitRetried:!0});return}u(c),E(X=>({...X,[c]:{origin:c,reason:`broker:${z}`,detail:{status:L!=null?L:null,reason:z},clientId:t.clientId||x.current[c]||null}}))},[h,g,u,q]),M=e.useRef(_);M.current=_,e.useEffect(()=>{Object.values(R).forEach(t=>{t.status!=="broker"||m.current[t.origin]||T.current[t.origin]||(m.current[t.origin]=!0,_(t).finally(()=>{delete m.current[t.origin]}))})},[R,_]);const K=e.useCallback(t=>!P.current||!(t!=null&&t.consent)||m.current[t.origin]?Promise.resolve():(m.current[t.origin]=!0,_(t,{approve:!0}).finally(()=>{delete m.current[t.origin]})),[_]);e.useEffect(()=>()=>{w.current&&w.current(),Object.values(T.current).forEach(clearTimeout)},[]);const B=e.useCallback(t=>{w.current&&(w.current(),w.current=null);const s=o||ue;w.current=s(t,{onBlocked:()=>{C(n=>n[t.origin]?{...n,[t.origin]:{...n[t.origin],popupBlocked:!0}}:n)},onClosed:()=>l(t.origin)})},[o,l]),j={pending:Object.values(R).filter(t=>t.status==="interactive"),authorize:B,approve:K,complete:h,dismiss:a,originEpochs:$,credentials:D,unsupported:I};return O.default.createElement(Y.Provider,{value:j},r)};ce.propTypes={children:k.default.node,launcher:k.default.func,broker:k.default.func};const Ce=()=>e.useContext(Y),ie=r=>{const{originEpochs:o}=e.useContext(Y),i=G(r);return i&&o[i]||0},ae=r=>{const{credentials:o}=e.useContext(Y),i=G(r);return i&&o[i]||null},le=r=>{const{unsupported:o}=e.useContext(Y),i=G(r);return i&&o[i]||null},se="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.",de=({children:r,authorizationLauncher:o,credentialBroker:i,ready:d})=>{const R=d===!0,C=e.useRef(!1);e.useEffect(()=>{d!==void 0||C.current||(C.current=!0,console.error(se),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(se))},[d]);const[$,v]=e.useState({}),[D,Z]=e.useState({}),I=e.useRef({}),E=e.useRef({}),S=e.useRef({}),W=e.useCallback((u,l)=>{var a;return!!((a=S.current[u])!=null&&a.has(l))},[]),w=e.useCallback((u,l,a)=>{v(p=>({...p,[u]:{origin:l,frameRef:a}}))},[]),x=e.useCallback(u=>{v(l=>{const a={...l};return delete a[u],a}),Z(l=>{const a={...l};return delete a[u],a}),delete S.current[u],Object.keys(E.current).forEach(l=>{E.current[l].instanceId===u&&delete E.current[l]})},[]);e.useEffect(()=>()=>{E.current={}},[]),e.useEffect(()=>{const u=(a=[],p,h)=>{a.forEach(g=>g(p,h))},l=a=>{var K,B,j,t,s,n,c;if(!((K=a==null?void 0:a.data)!=null&&K.instanceId))return;const{instanceId:p,type:h}=a.data,g=$[p],m=(j=(B=g==null?void 0:g.frameRef)==null?void 0:B.current)==null?void 0:j.contentWindow;if(!m||a.source!==m||a.origin!==g.origin)return;const _={origin:a.origin},M=I.current;if(h==="response"){const{callbackId:b,response:L,error:f}=a.data,z=E.current[b];z&&z.instanceId===p&&(z.callback(L,f),delete E.current[b]);return}if(h==="mark-registered"){const{requestType:b}=a.data;S.current[p]||(S.current[p]=new Set),S.current[p].add(b);return}h==="ready"&&Z(b=>({...b,[p]:!0})),u((t=M[p])==null?void 0:t[h],a.data,_),u((s=M["*"])==null?void 0:s[h],a.data,_),u((n=M[p])==null?void 0:n["*"],a.data,_),u((c=M["*"])==null?void 0:c["*"],a.data,_)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[$]);const y=e.useCallback((u,l,a,p)=>{var g,m;const h=$[u];if(!h){console.warn(`Origin not set for id: ${u}. Unable to send message.`);return}(m=(g=h==null?void 0:h.frameRef)==null?void 0:g.current)!=null&&m.contentWindow&&h.frameRef.current.contentWindow.postMessage({type:l,...a,instanceId:u},p||h.origin)},[$]),P=e.useRef(0),U=e.useCallback(()=>`${Date.now()}-${++P.current}`,[]),F=e.useCallback(async(u,l,a={})=>new Promise((p,h)=>{const g=U(),m=(_,M)=>{delete E.current[g],M?h(M):p(_)};E.current[g]={instanceId:u,callback:m},y(u,"request",{callbackId:g,requestType:l,data:a})}),[y]),A=e.useCallback((u,l)=>{y(u,"load-options",l)},[y]),T=e.useCallback((u,l)=>{y(u,"validate",l)},[y]),q=e.useMemo(()=>({registerFrame:w,unregisterFrame:x,sendMessage:y,sendRequest:F,loadOptions:A,validate:T,hasHandler:W,readyFrames:D,ready:R,addListener:(u,l,a)=>(I.current[u]||(I.current[u]={}),I.current[u][l]||(I.current[u][l]=[]),I.current[u][l].push(a),()=>{I.current[u][l]=I.current[u][l].filter(p=>p!==a)})}),[w,x,y,F,A,T,W,D,R]);return O.default.createElement(Q.Provider,{value:q},O.default.createElement(ce,{launcher:o,broker:i},r))},H=(r,o,i)=>{const d=e.useContext(Q);if(d===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const R=e.useRef(i);e.useEffect(()=>{R.current=i},[i]),e.useEffect(()=>{if(!(!r||!o)){if(o.startsWith("key:")){const C=o.substring(4);return d.readyFrames[r]&&d.sendMessage(r,"subscribe-key",{keyChord:C}),d.addListener(r,`key-pressed:${C}`,(...v)=>R.current(...v))}return d.addListener(r,o,(...C)=>R.current(...C))}},[d,r,o])};de.propTypes={children:k.default.node.isRequired,ready:k.default.bool.isRequired,authorizationLauncher:k.default.func,credentialBroker:k.default.func};const ee=te.default.default||te.default,we=1e4,Te="credentials",_e=(r,o)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Te}`,Ie=ee.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
@@ -16,10 +16,10 @@
16
16
  text-align: center;
17
17
  color: #555;
18
18
  font-size: 14px;
19
- `,fe=r=>{const o=r&&r.origin||"This app",i=r&&r.reason||"";switch(i){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 i.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."}}},pe=({entry:r})=>{const o=fe(r);return O.default.createElement(Me,{"data-reason":r&&r.reason||void 0},O.default.createElement("div",{style:{fontWeight:600}},o.title),O.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};pe.propTypes={entry:m.default.shape({origin:m.default.string,name:m.default.string,reason:m.default.string,detail:m.default.object})};const ye=ee.div`
19
+ `,fe=r=>{const o=r&&r.origin||"This app",i=r&&r.reason||"";switch(i){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 i.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."}}},pe=({entry:r})=>{const o=fe(r);return O.default.createElement(Me,{"data-reason":r&&r.reason||void 0},O.default.createElement("div",{style:{fontWeight:600}},o.title),O.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};pe.propTypes={entry:k.default.shape({origin:k.default.string,name:k.default.string,reason:k.default.string,detail:k.default.object})};const ye=ee.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=()=>O.default.createElement(ye,null,O.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),be=({id:r,src:o,path:i,onInit:b=()=>{},onTimeout:h,context:E={},params:$={},renderUnsupported:P,renderTimeout:q,...B})=>{if("isConfiguring"in B)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 k=e.useRef(),[M,L]=e.useState(),[w,y]=e.useState(0),[F,C]=e.useState(!1),[v,x]=e.useState(!1),S=ie(o),_=ae(o),T=le(o),[U,c]=e.useState(0),[d,a]=e.useState(null),{registerFrame:f,unregisterFrame:g,sendMessage:p,ready:I}=e.useContext(G),[R,A]=e.useState(),D=e.useRef(),Z=e.useRef(h),W=e.useRef(b),t=e.useRef(!1),s=e.useCallback(()=>{t.current=!1,clearTimeout(D.current),y(0),C(!1),x(!1),a(null)},[]);e.useEffect(()=>{(S>0||U>0)&&s()},[S,U,s]),e.useEffect(()=>{Z.current=h},[h]),e.useEffect(()=>{W.current=b},[b]),e.useEffect(()=>{w&&p(r,"set-context",{context:{...E}})},[E,r,w]),e.useEffect(()=>{w&&p(r,"set-params",{params:$})},[$,r,w]),e.useEffect(()=>{i!==void 0&&A((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),e.useEffect(()=>{R===void 0||!w||p(r,"navigate",{path:R})},[R,w,r,p]),e.useEffect(()=>{if(!o||!r||!k)return;if(!I){s(),L(void 0);return}s();const l=new URL(o);return f(r,l.origin,k),W.current(),L(Te(l.origin,r)),()=>{g(r)}},[o,r,I,f,k,s]),e.useEffect(()=>{if(!(!M||t.current))return D.current=setTimeout(()=>{C(!0),Z.current&&Z.current()},we),()=>clearTimeout(D.current)},[M,o,S,U]);const n=e.useCallback(()=>{t.current=!0,clearTimeout(D.current),C(!1)},[]);H(r,"init",l=>{n(),Array.isArray(l==null?void 0:l.acceptsCredentials)&&a(l.acceptsCredentials)}),H(r,"authorize",n),H(r,"login",n),H(r,"credential-request",()=>{n(),x(!0)}),e.useEffect(()=>{if(!v||!_)return;const l=d;if(Array.isArray(l)&&!l.includes(_.kind))return;const K=_.kind==="access_token"?{accessToken:_.accessToken}:{};p(r,"credential",{...K,credential:_})},[v,_,d,r,p]),H(r,"ready",()=>{n(),y(l=>l+1)});const u=e.useRef();return H(r,"heartbeat",()=>{u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{c(l=>l+1)},6e3)}),e.useEffect(()=>()=>{u.current&&clearTimeout(u.current)},[]),T?P?P(T):O.default.createElement(pe,{entry:T}):F&&!w?q?q():O.default.createElement(Se,null):O.default.createElement(Ie,{key:`auth-epoch-${S}-frame-${U}`,$isReady:w,ref:k,src:M,...B})};be.propTypes={id:m.default.string,src:m.default.string.isRequired,path:m.default.string,onInit:m.default.func,onTimeout:m.default.func,renderUnsupported:m.default.func,renderTimeout:m.default.func};exports.CREDENTIAL_KIND_ACCESS_TOKEN=oe;exports.EmbeddedAuthorizationContext=Y;exports.MMEmbeddableZone=be;exports.MMEmbeddableZoneContext=G;exports.MMEmbeddableZoneProvider=de;exports.isCrossSite=N;exports.normalizeCredential=J;exports.popupAuthorizationLauncher=ue;exports.unsupportedCopy=fe;exports.useEmbeddedAuthorizations=Ce;exports.useEmbeddedCredential=ae;exports.useEmbeddedEffect=H;exports.useEmbeddedOriginEpoch=ie;exports.useEmbeddedUnsupported=le;
25
+ `,Se=()=>O.default.createElement(ye,null,O.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),be=({id:r,src:o,path:i,onInit:d=()=>{},onTimeout:R,context:C={},params:$={},renderUnsupported:v,renderTimeout:D,...Z})=>{if("isConfiguring"in Z)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 I=e.useRef(),E=G(o),[S,W]=e.useState(),[w,x]=e.useState(0),[y,P]=e.useState(!1),[U,F]=e.useState(!1),A=ie(o),T=ae(o),q=le(o),[u,l]=e.useState(0),[a,p]=e.useState(null),{registerFrame:h,unregisterFrame:g,sendMessage:m,ready:_}=e.useContext(Q),[M,K]=e.useState(),B=e.useRef(),j=e.useRef(R),t=e.useRef(d),s=e.useRef(!1),n=e.useRef(),c=e.useCallback(()=>{s.current=!1,clearTimeout(B.current),clearTimeout(n.current),x(0),P(!1),F(!1),p(null)},[]);e.useEffect(()=>{(A>0||u>0)&&c()},[A,u,c]),e.useEffect(()=>{j.current=R},[R]),e.useEffect(()=>{t.current=d},[d]),e.useEffect(()=>{w&&m(r,"set-context",{context:{...C}},E)},[C,r,w,E]),e.useEffect(()=>{w&&m(r,"set-params",{params:$},E)},[$,r,w,E]),e.useEffect(()=>{i!==void 0&&K((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),e.useEffect(()=>{M===void 0||!w||m(r,"navigate",{path:M},E)},[M,w,r,m,E]),e.useEffect(()=>{if(!o||!r||!I)return;if(!_){c(),W(void 0);return}c();const f=new URL(o);return h(r,f.origin,I),t.current(),W(_e(f.origin,r)),()=>{g(r)}},[o,r,_,h,I,c]),e.useEffect(()=>{if(!(!S||s.current))return B.current=setTimeout(()=>{P(!0),j.current&&j.current()},we),()=>clearTimeout(B.current)},[S,o,A,u]);const b=e.useCallback(()=>{s.current=!0,clearTimeout(B.current),P(!1)},[]);if(H(r,"init",f=>{b(),Array.isArray(f==null?void 0:f.acceptsCredentials)&&p(f.acceptsCredentials)}),H(r,"authorize",b),H(r,"login",b),H(r,"credential-request",()=>{b(),F(!0)}),e.useEffect(()=>{if(!U||!T)return;const f=a;if(Array.isArray(f)&&!f.includes(T.kind))return;const z=T.kind==="access_token"?{accessToken:T.accessToken}:{};m(r,"credential",{...z,credential:T},E)},[U,T,a,r,m,E]),H(r,"ready",()=>{b(),x(f=>f+1)}),H(r,"heartbeat",()=>{n.current&&clearTimeout(n.current),n.current=setTimeout(()=>{l(f=>f+1)},6e3)}),e.useEffect(()=>()=>{n.current&&clearTimeout(n.current)},[]),q)return v?v(q):O.default.createElement(pe,{entry:q});if(y&&!w)return D?D():O.default.createElement(Se,null);const L={...Z};return delete L.skipManifestCheck,O.default.createElement(Ie,{key:`auth-epoch-${A}-frame-${u}`,$isReady:w,ref:I,src:S,...L})};be.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};exports.CREDENTIAL_KIND_ACCESS_TOKEN=oe;exports.EmbeddedAuthorizationContext=Y;exports.MMEmbeddableZone=be;exports.MMEmbeddableZoneContext=Q;exports.MMEmbeddableZoneProvider=de;exports.isCrossSite=N;exports.normalizeCredential=J;exports.popupAuthorizationLauncher=ue;exports.unsupportedCopy=fe;exports.useEmbeddedAuthorizations=Ce;exports.useEmbeddedCredential=ae;exports.useEmbeddedEffect=H;exports.useEmbeddedOriginEpoch=ie;exports.useEmbeddedUnsupported=le;
@@ -1,7 +1,7 @@
1
- import P, { createContext as ue, useContext as G, useState as L, useRef as g, useCallback as h, useEffect as b, useMemo as fe } from "react";
2
- import E from "prop-types";
1
+ import P, { createContext as ue, useContext as G, useState as U, useRef as R, useCallback as b, useEffect as k, useMemo as fe } from "react";
2
+ import T from "prop-types";
3
3
  import te from "styled-components";
4
- const ee = ue(null), J = ue({
4
+ const ee = ue(null), V = ue({
5
5
  pending: [],
6
6
  authorize: () => {
7
7
  },
@@ -23,175 +23,176 @@ const ee = ue(null), J = ue({
23
23
  "unbound_host_bound_guest",
24
24
  "invalid_token",
25
25
  "provider_unavailable"
26
- ]), V = (r) => {
26
+ ]), J = (r) => {
27
27
  try {
28
28
  return new URL(r).origin;
29
29
  } catch {
30
30
  return null;
31
31
  }
32
- }, oe = (r, o) => {
33
- const i = o == null ? void 0 : o.origin;
34
- return !i || V(r == null ? void 0 : r.authUrl) !== i ? null : i;
35
- }, se = (r) => {
32
+ }, se = (r, s) => {
33
+ const i = s == null ? void 0 : s.origin;
34
+ return !i || J(r == null ? void 0 : r.authUrl) !== i ? null : i;
35
+ }, oe = (r) => {
36
36
  if (!r || /^\d{1,3}(?:\.\d{1,3}){3}$/.test(r) || r.includes(":"))
37
37
  return r;
38
- const o = r.split(".");
39
- return o.length <= 2 ? r : o.slice(-2).join(".");
40
- }, ce = (r, o) => {
38
+ const s = r.split(".");
39
+ return s.length <= 2 ? r : s.slice(-2).join(".");
40
+ }, ce = (r, s) => {
41
41
  try {
42
- const i = new URL(r), p = new URL(o);
43
- return i.protocol !== p.protocol ? !0 : se(i.hostname) !== se(p.hostname);
42
+ const i = new URL(r), l = new URL(s);
43
+ return i.protocol !== l.protocol ? !0 : oe(i.hostname) !== oe(l.hostname);
44
44
  } catch {
45
45
  return !0;
46
46
  }
47
- }, be = (r, { onBlocked: o, onClosed: i }) => {
48
- const p = window.open(
47
+ }, be = (r, { onBlocked: s, onClosed: i }) => {
48
+ const l = window.open(
49
49
  r.authUrl,
50
50
  "mm-embed-auth",
51
51
  "popup,width=600,height=700"
52
52
  );
53
- if (!p)
54
- return o(), () => {
53
+ if (!l)
54
+ return s(), () => {
55
55
  };
56
- const m = setInterval(() => {
57
- p.closed && (clearInterval(m), i());
56
+ const w = setInterval(() => {
57
+ l.closed && (clearInterval(w), i());
58
58
  }, 1e3);
59
- return () => clearInterval(m);
60
- }, ae = ({ children: r, launcher: o, broker: i }) => {
61
- const p = G(ee), [m, R] = L({}), [v, B] = L({}), [W, j] = L({}), [T, A] = L({}), $ = g(W);
62
- $.current = W;
63
- const _ = g(m);
64
- _.current = m;
65
- const U = g(null), q = g({}), I = g({}), D = g(i);
66
- D.current = i;
67
- const O = h((e) => {
68
- const n = q.current[e];
59
+ return () => clearInterval(w);
60
+ }, ae = ({ children: r, launcher: s, broker: i }) => {
61
+ const l = G(ee), [w, I] = U({}), [v, D] = U({}), [j, H] = U({}), [y, E] = U({}), x = R(j);
62
+ x.current = j;
63
+ const K = R(w);
64
+ K.current = w;
65
+ const _ = R(null), $ = R({}), A = R({}), q = R(i);
66
+ q.current = i;
67
+ const F = b((e) => {
68
+ const n = $.current[e];
69
69
  if (!n)
70
70
  return;
71
- const t = (s) => !s[e] || s[e].clientId ? s : { ...s, [e]: { ...s[e], clientId: n } };
72
- R(t), A(t);
73
- }, []), x = h(
71
+ const t = (c) => !c[e] || c[e].clientId ? c : { ...c, [e]: { ...c[e], clientId: n } };
72
+ I(t), E(t);
73
+ }, []), B = b(
74
74
  (e, n) => {
75
- e && (A((t) => t[e] ? t : {
75
+ e && (E((t) => t[e] ? t : {
76
76
  ...t,
77
77
  [e]: {
78
78
  origin: e,
79
79
  reason: n,
80
- clientId: q.current[e] || null
80
+ clientId: $.current[e] || null
81
81
  }
82
- }), O(e));
82
+ }), F(e));
83
83
  },
84
- [O]
85
- ), M = h(
84
+ [F]
85
+ ), z = b(
86
86
  (e, { v2: n = !1, origin: t = null } = {}) => {
87
- t && (n && $.current[t] || (R((s) => s[t] ? s : {
88
- ...s,
87
+ t && (n && x.current[t] || (I((c) => c[t] ? c : {
88
+ ...c,
89
89
  [t]: {
90
90
  origin: t,
91
91
  authUrl: e.authUrl,
92
- status: n && D.current ? "broker" : "interactive",
92
+ status: n && q.current ? "broker" : "interactive",
93
93
  v2: n,
94
94
  scope: e.scope || null,
95
- acceptsCredentials: I.current[t] || null,
95
+ acceptsCredentials: A.current[t] || null,
96
96
  consent: null,
97
97
  error: null,
98
- clientId: e.clientId || q.current[t] || null,
98
+ clientId: e.clientId || $.current[t] || null,
99
99
  popupBlocked: !1
100
100
  }
101
- }), O(t)));
101
+ }), F(t)));
102
102
  },
103
- [O]
104
- ), y = g({}), F = h((e) => {
105
- y.current[e] && (clearTimeout(y.current[e]), delete y.current[e]);
106
- }, []), c = h(
103
+ [F]
104
+ ), M = R({}), W = b((e) => {
105
+ M.current[e] && (clearTimeout(M.current[e]), delete M.current[e]);
106
+ }, []), o = b(
107
107
  (e) => {
108
- F(e), R((n) => {
108
+ W(e), I((n) => {
109
109
  if (!n[e])
110
110
  return n;
111
111
  const t = { ...n };
112
112
  return delete t[e], t;
113
113
  });
114
114
  },
115
- [F]
116
- ), l = h(
115
+ [W]
116
+ ), a = b(
117
117
  (e) => {
118
- e && (c(e), B((n) => ({
118
+ e && (o(e), D((n) => ({
119
119
  ...n,
120
120
  [e]: (n[e] || 0) + 1
121
121
  })));
122
122
  },
123
- [c]
124
- ), u = h(
123
+ [o]
124
+ ), u = b(
125
125
  (e) => {
126
- c(e.origin);
126
+ o(e.origin);
127
127
  },
128
- [c]
129
- ), d = h(
128
+ [o]
129
+ ), f = b(
130
130
  (e, n) => {
131
- const t = oe(e, n);
131
+ const t = se(e, n);
132
132
  if (t) {
133
133
  if (ce(t, window.location.origin)) {
134
- x(t, "cross-site-legacy");
134
+ B(t, "cross-site-legacy");
135
135
  return;
136
136
  }
137
- M(e, { origin: t });
137
+ z(e, { origin: t });
138
138
  }
139
139
  },
140
- [M, x]
140
+ [z, B]
141
141
  );
142
- b(() => {
143
- if (!p)
142
+ k(() => {
143
+ if (!l)
144
144
  return;
145
145
  const e = [
146
- p.addListener("*", "init", (n, t) => {
147
- t != null && t.origin && (n != null && n.clientId) && (q.current[t.origin] = n.clientId, O(t.origin)), t != null && t.origin && Array.isArray(n == null ? void 0 : n.acceptsCredentials) ? I.current[t.origin] = n.acceptsCredentials : t != null && t.origin && ce(t.origin, window.location.origin) && x(t.origin, "cross-site-legacy");
146
+ l.addListener("*", "init", (n, t) => {
147
+ t != null && t.origin && (n != null && n.clientId) && ($.current[t.origin] = n.clientId, F(t.origin)), t != null && t.origin && Array.isArray(n == null ? void 0 : n.acceptsCredentials) ? A.current[t.origin] = n.acceptsCredentials : t != null && t.origin && ce(t.origin, window.location.origin) && B(t.origin, "cross-site-legacy");
148
148
  }),
149
- p.addListener("*", "authorize", d),
150
- p.addListener("*", "login", d),
151
- p.addListener(
149
+ l.addListener("*", "authorize", f),
150
+ l.addListener("*", "login", f),
151
+ l.addListener(
152
152
  "*",
153
153
  "credential-request",
154
- (n, t) => M(n, { v2: !0, origin: oe(n, t) })
154
+ (n, t) => z(n, { v2: !0, origin: se(n, t) })
155
155
  )
156
156
  ];
157
157
  return () => e.forEach((n) => n());
158
- }, [p, d, M, O, x]), b(() => {
158
+ }, [l, f, z, F, B]), k(() => {
159
159
  const e = (n) => {
160
160
  const t = n == null ? void 0 : n.data;
161
161
  if (!t || t.type !== "authorized")
162
162
  return;
163
- const s = V(t.authUrl);
164
- if (!(!s || n.origin !== s)) {
163
+ const c = J(t.authUrl);
164
+ if (!c || n.origin !== c)
165
+ return;
166
+ const p = K.current[c];
167
+ if (!(!p || p.status !== "interactive")) {
165
168
  if (t.accessToken) {
166
- if (!_.current[s])
167
- return;
168
- j((a) => ({
169
- ...a,
170
- [s]: ne({ accessToken: t.accessToken })
171
- })), c(s);
169
+ H((O) => ({
170
+ ...O,
171
+ [c]: ne({ accessToken: t.accessToken })
172
+ })), o(c);
172
173
  return;
173
174
  }
174
- l(s);
175
+ a(c);
175
176
  }
176
177
  };
177
178
  return window.addEventListener("message", e), () => window.removeEventListener("message", e);
178
- }, [c, l]);
179
- const w = h(
179
+ }, [o, a]);
180
+ const h = b(
180
181
  (e, n) => {
181
- const t = e == null ? void 0 : e.origin, s = ne(n);
182
- !t || !s || (j((a) => ({ ...a, [t]: s })), c(t));
182
+ const t = e == null ? void 0 : e.origin, c = ne(n);
183
+ !t || !c || (H((p) => ({ ...p, [t]: c })), o(t));
183
184
  },
184
- [c]
185
- ), f = h((e, n) => {
186
- R((t) => t[e] ? { ...t, [e]: { ...t[e], ...n } } : t);
187
- }, []), C = g({}), k = h(
185
+ [o]
186
+ ), g = b((e, n) => {
187
+ I((t) => t[e] ? { ...t, [e]: { ...t[e], ...n } } : t);
188
+ }, []), m = R({}), C = b(
188
189
  async (e, { approve: n } = {}) => {
189
- const t = D.current, { origin: s } = e;
190
- let a;
190
+ const t = q.current, { origin: c } = e;
191
+ let p;
191
192
  try {
192
- a = await t(
193
+ p = await t(
193
194
  {
194
- origin: s,
195
+ origin: c,
195
196
  clientId: e.clientId,
196
197
  scope: e.scope,
197
198
  authUrl: e.authUrl
@@ -199,106 +200,108 @@ const ee = ue(null), J = ue({
199
200
  { approve: n }
200
201
  );
201
202
  } catch {
202
- n ? f(s, { error: { reason: "broker_unavailable" } }) : f(s, { status: "interactive" });
203
+ n ? g(c, { error: { reason: "broker_unavailable" } }) : g(c, { status: "interactive" });
203
204
  return;
204
205
  }
205
- const Q = a == null ? void 0 : a.status, S = (a == null ? void 0 : a.body) || {};
206
- if (Q === 200 && S.credential) {
207
- w(e, S.credential);
206
+ const O = p == null ? void 0 : p.status, d = (p == null ? void 0 : p.body) || {};
207
+ if (O === 200 && d.credential) {
208
+ h(e, d.credential);
208
209
  return;
209
210
  }
210
- if (Q === 409 && S.consentRequired) {
211
- f(s, {
211
+ if (O === 409 && d.consentRequired) {
212
+ g(c, {
212
213
  status: "interactive",
213
214
  consent: {
214
- client: S.client || null,
215
- stepUp: !!S.stepUp,
216
- scope: S.scope || e.scope
215
+ client: d.client || null,
216
+ stepUp: !!d.stepUp,
217
+ scope: d.scope || e.scope
217
218
  },
218
219
  error: null
219
220
  });
220
221
  return;
221
222
  }
222
- const Y = S.error || "broker_error";
223
- if (!n && me.has(Y)) {
224
- f(s, { status: "interactive" });
223
+ const S = d.error || "broker_error";
224
+ if (!n && me.has(S)) {
225
+ g(c, { status: "interactive" });
225
226
  return;
226
227
  }
227
- if (n && Y === "consent_denied") {
228
- f(s, { error: null });
228
+ if (n && S === "consent_denied") {
229
+ g(c, { error: null });
229
230
  return;
230
231
  }
231
- if (Y === "rate_limited" && !e.rateLimitRetried) {
232
+ if (S === "rate_limited" && !e.rateLimitRetried) {
232
233
  const N = Math.min(
233
- Number(S.retryAfter) > 0 ? Number(S.retryAfter) : he,
234
+ Number(d.retryAfter) > 0 ? Number(d.retryAfter) : he,
234
235
  ge
235
236
  ), de = { ...e, rateLimitRetried: !0 };
236
- F(s), y.current[s] = setTimeout(() => {
237
- delete y.current[s], C.current[s] = !0, z.current(de, { approve: n }).finally(() => {
238
- delete C.current[s];
237
+ W(c), M.current[c] = setTimeout(() => {
238
+ delete M.current[c], m.current[c] = !0, L.current(de, { approve: n }).finally(() => {
239
+ delete m.current[c];
239
240
  });
240
- }, N * 1e3), f(s, { rateLimitRetried: !0 });
241
+ }, N * 1e3), g(c, { rateLimitRetried: !0 });
241
242
  return;
242
243
  }
243
- c(s), A((N) => ({
244
+ o(c), E((N) => ({
244
245
  ...N,
245
- [s]: {
246
- origin: s,
247
- reason: `broker:${Y}`,
248
- detail: a || null,
249
- clientId: e.clientId || q.current[s] || null
246
+ [c]: {
247
+ origin: c,
248
+ reason: `broker:${S}`,
249
+ detail: { status: O != null ? O : null, reason: S },
250
+ clientId: e.clientId || $.current[c] || null
250
251
  }
251
252
  }));
252
253
  },
253
- [w, f, c, F]
254
- ), z = g(k);
255
- z.current = k, b(() => {
256
- Object.values(m).forEach((e) => {
257
- e.status !== "broker" || C.current[e.origin] || y.current[e.origin] || (C.current[e.origin] = !0, k(e).finally(() => {
258
- delete C.current[e.origin];
254
+ [h, g, o, W]
255
+ ), L = R(C);
256
+ L.current = C, k(() => {
257
+ Object.values(w).forEach((e) => {
258
+ e.status !== "broker" || m.current[e.origin] || M.current[e.origin] || (m.current[e.origin] = !0, C(e).finally(() => {
259
+ delete m.current[e.origin];
259
260
  }));
260
261
  });
261
- }, [m, k]);
262
- const Z = h(
263
- (e) => !D.current || !(e != null && e.consent) ? Promise.resolve() : k(e, { approve: !0 }),
264
- [k]
262
+ }, [w, C]);
263
+ const Q = b(
264
+ (e) => !q.current || !(e != null && e.consent) || m.current[e.origin] ? Promise.resolve() : (m.current[e.origin] = !0, C(e, { approve: !0 }).finally(() => {
265
+ delete m.current[e.origin];
266
+ })),
267
+ [C]
265
268
  );
266
- b(() => () => {
267
- U.current && U.current(), Object.values(y.current).forEach(clearTimeout);
269
+ k(() => () => {
270
+ _.current && _.current(), Object.values(M.current).forEach(clearTimeout);
268
271
  }, []);
269
- const H = h(
272
+ const Z = b(
270
273
  (e) => {
271
- U.current && (U.current(), U.current = null);
272
- const n = o || be;
273
- U.current = n(e, {
274
+ _.current && (_.current(), _.current = null);
275
+ const n = s || be;
276
+ _.current = n(e, {
274
277
  onBlocked: () => {
275
- R((t) => t[e.origin] ? {
278
+ I((t) => t[e.origin] ? {
276
279
  ...t,
277
280
  [e.origin]: { ...t[e.origin], popupBlocked: !0 }
278
281
  } : t);
279
282
  },
280
283
  // A popup closed without posting may still have completed login; the remounted guest re-requests.
281
- onClosed: () => l(e.origin)
284
+ onClosed: () => a(e.origin)
282
285
  });
283
286
  },
284
- [o, l]
285
- ), K = {
286
- pending: Object.values(m).filter(
287
+ [s, a]
288
+ ), Y = {
289
+ pending: Object.values(w).filter(
287
290
  (e) => e.status === "interactive"
288
291
  ),
289
- authorize: H,
290
- approve: Z,
291
- complete: w,
292
+ authorize: Z,
293
+ approve: Q,
294
+ complete: h,
292
295
  dismiss: u,
293
296
  originEpochs: v,
294
- credentials: W,
295
- unsupported: T
297
+ credentials: j,
298
+ unsupported: y
296
299
  };
297
- return /* @__PURE__ */ P.createElement(J.Provider, { value: K }, r);
300
+ return /* @__PURE__ */ P.createElement(V.Provider, { value: Y }, r);
298
301
  };
299
302
  ae.propTypes = {
300
- children: E.node,
301
- launcher: E.func,
303
+ children: T.node,
304
+ launcher: T.func,
302
305
  /**
303
306
  * `async (request, { approve }) => result` where request =
304
307
  * `{ origin, clientId, scope, authUrl }` (origin is the zone-attested frame origin) and result
@@ -307,192 +310,196 @@ ae.propTypes = {
307
310
  * on 200, `{ consentRequired: true, client: { name, hostname }, stepUp }` on 409, `{ error,
308
311
  * … }` otherwise. Provided only by hosts that are themselves first-party clients.
309
312
  */
310
- broker: E.func
313
+ broker: T.func
311
314
  };
312
- const Oe = () => G(J), Ee = (r) => {
313
- const { originEpochs: o } = G(J), i = V(r);
314
- return i && o[i] || 0;
315
+ const $e = () => G(V), Ee = (r) => {
316
+ const { originEpochs: s } = G(V), i = J(r);
317
+ return i && s[i] || 0;
315
318
  }, Re = (r) => {
316
- const { credentials: o } = G(J), i = V(r);
317
- return i && o[i] || null;
318
- }, we = (r) => {
319
- const { unsupported: o } = G(J), i = V(r);
320
- return i && o[i] || null;
321
- }, ie = "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.", ke = ({
319
+ const { credentials: s } = G(V), i = J(r);
320
+ return i && s[i] || null;
321
+ }, ke = (r) => {
322
+ const { unsupported: s } = G(V), i = J(r);
323
+ return i && s[i] || null;
324
+ }, ie = "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.", we = ({
322
325
  children: r,
323
- authorizationLauncher: o,
326
+ authorizationLauncher: s,
324
327
  credentialBroker: i,
325
- ready: p
328
+ ready: l
326
329
  }) => {
327
- const m = p === !0, R = g(!1);
328
- b(() => {
329
- p !== void 0 || R.current || (R.current = !0, console.error(ie), typeof window != "undefined" && typeof window.alert == "function" && window.alert(ie));
330
- }, [p]);
331
- const [v, B] = L({}), [W, j] = L({}), T = g({}), A = g({}), $ = g({}), _ = h((c, l) => {
330
+ const w = l === !0, I = R(!1);
331
+ k(() => {
332
+ l !== void 0 || I.current || (I.current = !0, console.error(ie), typeof window != "undefined" && typeof window.alert == "function" && window.alert(ie));
333
+ }, [l]);
334
+ const [v, D] = U({}), [j, H] = U({}), y = R({}), E = R({}), x = R({}), K = b((o, a) => {
332
335
  var u;
333
- return !!((u = $.current[c]) != null && u.has(l));
334
- }, []), U = h((c, l, u) => {
335
- B((d) => ({ ...d, [c]: { origin: l, frameRef: u } }));
336
- }, []), q = h((c) => {
337
- B((l) => {
338
- const u = { ...l };
339
- return delete u[c], u;
340
- }), j((l) => {
341
- const u = { ...l };
342
- return delete u[c], u;
343
- }), delete $.current[c];
336
+ return !!((u = x.current[o]) != null && u.has(a));
337
+ }, []), _ = b((o, a, u) => {
338
+ D((f) => ({ ...f, [o]: { origin: a, frameRef: u } }));
339
+ }, []), $ = b((o) => {
340
+ D((a) => {
341
+ const u = { ...a };
342
+ return delete u[o], u;
343
+ }), H((a) => {
344
+ const u = { ...a };
345
+ return delete u[o], u;
346
+ }), delete x.current[o], Object.keys(E.current).forEach((a) => {
347
+ E.current[a].instanceId === o && delete E.current[a];
348
+ });
344
349
  }, []);
345
- b(() => {
346
- const c = (u = [], d, w) => {
347
- u.forEach((f) => f(d, w));
348
- }, l = (u) => {
349
- var Z, H, K, e, n, t, s;
350
- if (!((Z = u == null ? void 0 : u.data) != null && Z.instanceId))
350
+ k(() => () => {
351
+ E.current = {};
352
+ }, []), k(() => {
353
+ const o = (u = [], f, h) => {
354
+ u.forEach((g) => g(f, h));
355
+ }, a = (u) => {
356
+ var Q, Z, Y, e, n, t, c;
357
+ if (!((Q = u == null ? void 0 : u.data) != null && Q.instanceId))
351
358
  return;
352
- const { instanceId: d, type: w } = u.data, f = v[d], C = (K = (H = f == null ? void 0 : f.frameRef) == null ? void 0 : H.current) == null ? void 0 : K.contentWindow;
353
- if (!C || u.source !== C || u.origin !== f.origin)
359
+ const { instanceId: f, type: h } = u.data, g = v[f], m = (Y = (Z = g == null ? void 0 : g.frameRef) == null ? void 0 : Z.current) == null ? void 0 : Y.contentWindow;
360
+ if (!m || u.source !== m || u.origin !== g.origin)
354
361
  return;
355
- const k = { origin: u.origin }, z = T.current;
356
- if (w === "response") {
357
- const { callbackId: a, response: Q, error: S } = u.data, Y = A.current[a];
358
- Y && (Y(Q, S), delete A.current[a]);
362
+ const C = { origin: u.origin }, L = y.current;
363
+ if (h === "response") {
364
+ const { callbackId: p, response: O, error: d } = u.data, S = E.current[p];
365
+ S && S.instanceId === f && (S.callback(O, d), delete E.current[p]);
359
366
  return;
360
367
  }
361
- if (w === "mark-registered") {
362
- const { requestType: a } = u.data;
363
- $.current[d] || ($.current[d] = /* @__PURE__ */ new Set()), $.current[d].add(a);
368
+ if (h === "mark-registered") {
369
+ const { requestType: p } = u.data;
370
+ x.current[f] || (x.current[f] = /* @__PURE__ */ new Set()), x.current[f].add(p);
364
371
  return;
365
372
  }
366
- w === "ready" && j((a) => ({ ...a, [d]: !0 })), c((e = z[d]) == null ? void 0 : e[w], u.data, k), c((n = z["*"]) == null ? void 0 : n[w], u.data, k), c((t = z[d]) == null ? void 0 : t["*"], u.data, k), c((s = z["*"]) == null ? void 0 : s["*"], u.data, k);
373
+ h === "ready" && H((p) => ({ ...p, [f]: !0 })), o((e = L[f]) == null ? void 0 : e[h], u.data, C), o((n = L["*"]) == null ? void 0 : n[h], u.data, C), o((t = L[f]) == null ? void 0 : t["*"], u.data, C), o((c = L["*"]) == null ? void 0 : c["*"], u.data, C);
367
374
  };
368
- return window.addEventListener("message", l), () => {
369
- window.removeEventListener("message", l);
375
+ return window.addEventListener("message", a), () => {
376
+ window.removeEventListener("message", a);
370
377
  };
371
378
  }, [v]);
372
- const I = h(
373
- (c, l, u) => {
374
- var w, f;
375
- const d = v[c];
376
- if (!d) {
377
- console.warn(`Origin not set for id: ${c}. Unable to send message.`);
379
+ const A = b(
380
+ (o, a, u, f) => {
381
+ var g, m;
382
+ const h = v[o];
383
+ if (!h) {
384
+ console.warn(`Origin not set for id: ${o}. Unable to send message.`);
378
385
  return;
379
386
  }
380
- (f = (w = d == null ? void 0 : d.frameRef) == null ? void 0 : w.current) != null && f.contentWindow && d.frameRef.current.contentWindow.postMessage(
381
- { type: l, ...u, instanceId: c },
382
- d.origin
387
+ (m = (g = h == null ? void 0 : h.frameRef) == null ? void 0 : g.current) != null && m.contentWindow && h.frameRef.current.contentWindow.postMessage(
388
+ { type: a, ...u, instanceId: o },
389
+ f || h.origin
383
390
  );
384
391
  },
385
392
  [v]
386
- ), D = g(0), O = h(() => `${Date.now()}-${++D.current}`, []), x = h(
387
- async (c, l, u = {}) => new Promise((d, w) => {
388
- const f = O(), C = (k, z) => {
389
- delete A.current[f], z ? w(z) : d(k);
393
+ ), q = R(0), F = b(() => `${Date.now()}-${++q.current}`, []), B = b(
394
+ async (o, a, u = {}) => new Promise((f, h) => {
395
+ const g = F(), m = (C, L) => {
396
+ delete E.current[g], L ? h(L) : f(C);
390
397
  };
391
- A.current[f] = C, I(c, "request", { callbackId: f, requestType: l, data: u });
398
+ E.current[g] = { instanceId: o, callback: m }, A(o, "request", { callbackId: g, requestType: a, data: u });
392
399
  }),
393
- [I]
394
- ), M = h(
395
- (c, l) => {
396
- I(c, "load-options", l);
400
+ [A]
401
+ ), z = b(
402
+ (o, a) => {
403
+ A(o, "load-options", a);
397
404
  },
398
- [I]
399
- ), y = h(
400
- (c, l) => {
401
- I(c, "validate", l);
405
+ [A]
406
+ ), M = b(
407
+ (o, a) => {
408
+ A(o, "validate", a);
402
409
  },
403
- [I]
404
- ), F = fe(
410
+ [A]
411
+ ), W = fe(
405
412
  () => ({
406
- registerFrame: U,
407
- unregisterFrame: q,
408
- sendMessage: I,
409
- sendRequest: x,
410
- loadOptions: M,
411
- validate: y,
412
- hasHandler: _,
413
- readyFrames: W,
414
- ready: m,
415
- addListener: (c, l, u) => (T.current[c] || (T.current[c] = {}), T.current[c][l] || (T.current[c][l] = []), T.current[c][l].push(u), () => {
416
- T.current[c][l] = T.current[c][l].filter((d) => d !== u);
413
+ registerFrame: _,
414
+ unregisterFrame: $,
415
+ sendMessage: A,
416
+ sendRequest: B,
417
+ loadOptions: z,
418
+ validate: M,
419
+ hasHandler: K,
420
+ readyFrames: j,
421
+ ready: w,
422
+ addListener: (o, a, u) => (y.current[o] || (y.current[o] = {}), y.current[o][a] || (y.current[o][a] = []), y.current[o][a].push(u), () => {
423
+ y.current[o][a] = y.current[o][a].filter((f) => f !== u);
417
424
  })
418
425
  }),
419
426
  [
420
- U,
421
- q,
422
- I,
423
- x,
424
- M,
425
- y,
426
427
  _,
427
- W,
428
- m
428
+ $,
429
+ A,
430
+ B,
431
+ z,
432
+ M,
433
+ K,
434
+ j,
435
+ w
429
436
  ]
430
437
  );
431
- return /* @__PURE__ */ P.createElement(ee.Provider, { value: F }, /* @__PURE__ */ P.createElement(
438
+ return /* @__PURE__ */ P.createElement(ee.Provider, { value: W }, /* @__PURE__ */ P.createElement(
432
439
  ae,
433
440
  {
434
- launcher: o,
441
+ launcher: s,
435
442
  broker: i
436
443
  },
437
444
  r
438
445
  ));
439
- }, X = (r, o, i) => {
440
- const p = G(ee);
441
- if (p === null)
446
+ }, X = (r, s, i) => {
447
+ const l = G(ee);
448
+ if (l === null)
442
449
  throw new Error(
443
450
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
444
451
  );
445
- const m = g(i);
446
- b(() => {
447
- m.current = i;
448
- }, [i]), b(() => {
449
- if (!(!r || !o)) {
450
- if (o.startsWith("key:")) {
451
- const R = o.substring(4);
452
- return p.readyFrames[r] && p.sendMessage(r, "subscribe-key", { keyChord: R }), p.addListener(
452
+ const w = R(i);
453
+ k(() => {
454
+ w.current = i;
455
+ }, [i]), k(() => {
456
+ if (!(!r || !s)) {
457
+ if (s.startsWith("key:")) {
458
+ const I = s.substring(4);
459
+ return l.readyFrames[r] && l.sendMessage(r, "subscribe-key", { keyChord: I }), l.addListener(
453
460
  r,
454
- `key-pressed:${R}`,
455
- (...B) => m.current(...B)
461
+ `key-pressed:${I}`,
462
+ (...D) => w.current(...D)
456
463
  );
457
464
  }
458
- return p.addListener(
465
+ return l.addListener(
459
466
  r,
460
- o,
461
- (...R) => m.current(...R)
467
+ s,
468
+ (...I) => w.current(...I)
462
469
  );
463
470
  }
464
- }, [p, r, o]);
471
+ }, [l, r, s]);
465
472
  };
466
- ke.propTypes = {
467
- children: E.node.isRequired,
473
+ we.propTypes = {
474
+ children: T.node.isRequired,
468
475
  /**
469
476
  * Required. True once the host holds its bearer; zones mount no iframe until then. Missing =
470
477
  * not ready, plus a one-time alert.
471
478
  */
472
- ready: E.bool.isRequired,
479
+ ready: T.bool.isRequired,
473
480
  /**
474
481
  * Optional interactive-step launcher for embedded authorizations,
475
482
  * `(entry, { onBlocked, onClosed }) => cleanup`. Defaults to a top-level
476
483
  * popup. Hosts that cannot open windows (Android WebView shells) drive
477
484
  * their own UI from useEmbeddedAuthorizations() instead.
478
485
  */
479
- authorizationLauncher: E.func,
486
+ authorizationLauncher: T.func,
480
487
  /**
481
488
  * Optional credential broker for hosts that are first-party MachineMetrics clients
482
489
  * (`async (request, { approve }) => result`, see EmbeddedAuthorizationManager). When present,
483
490
  * v2 credential requests are brokered server-side (no popup) and consent is
484
491
  * rendered natively by the host from the broker's `client` identity.
485
492
  */
486
- credentialBroker: E.func
493
+ credentialBroker: T.func
487
494
  };
488
- const re = te.default || te, Te = 1e4, Ie = "credentials", _e = (r, o) => `${r}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(
495
+ const re = te.default || te, Te = 1e4, Ie = "credentials", _e = (r, s) => `${r}/mm-embedded-interop?id=${encodeURIComponent(s)}&origin=${encodeURIComponent(
489
496
  window.location.origin
490
497
  )}&caps=${Ie}`, Me = re.iframe`
491
498
  width: 100%;
492
499
  height: 100%;
493
500
  border: none;
494
501
  visibility: ${(r) => r.$isReady ? "visible" : "hidden"};
495
- `, ye = re.div`
502
+ `, Ce = re.div`
496
503
  width: 100%;
497
504
  height: 100%;
498
505
  display: flex;
@@ -505,44 +512,44 @@ const re = te.default || te, Te = 1e4, Ie = "credentials", _e = (r, o) => `${r}/
505
512
  text-align: center;
506
513
  color: #555;
507
514
  font-size: 14px;
508
- `, Ce = (r) => {
509
- const o = r && r.origin || "This app", i = r && r.reason || "";
515
+ `, ye = (r) => {
516
+ const s = r && r.origin || "This app", i = r && r.reason || "";
510
517
  switch (i) {
511
518
  case "broker:scope_above_ceiling":
512
519
  return {
513
- title: `${o} can’t be authorized here`,
520
+ title: `${s} can’t be authorized here`,
514
521
  detail: "It asks for permissions your role doesn’t have."
515
522
  };
516
523
  case "broker:origin_mismatch":
517
524
  case "broker:unknown_client":
518
525
  return {
519
- title: `${o} can’t be authorized here`,
526
+ title: `${s} can’t be authorized here`,
520
527
  detail: "It isn’t registered to run at this address."
521
528
  };
522
529
  case "broker:cross_company_principal":
523
530
  return {
524
- title: `${o} can’t be authorized here`,
531
+ title: `${s} can’t be authorized here`,
525
532
  detail: "It can’t be authorized for this account."
526
533
  };
527
534
  default:
528
535
  return i.startsWith("broker:") ? {
529
- title: `${o} can’t be authorized here`,
536
+ title: `${s} can’t be authorized here`,
530
537
  detail: "Authorization was refused."
531
538
  } : {
532
- title: `${o} can’t be embedded here`,
539
+ title: `${s} can’t be embedded here`,
533
540
  detail: "It needs an update to support embedding from a different domain."
534
541
  };
535
542
  }
536
543
  }, le = ({ entry: r }) => {
537
- const o = Ce(r);
538
- return /* @__PURE__ */ P.createElement(ye, { "data-reason": r && r.reason || void 0 }, /* @__PURE__ */ P.createElement("div", { style: { fontWeight: 600 } }, o.title), /* @__PURE__ */ P.createElement("div", { style: { fontSize: 12, color: "#888" } }, o.detail));
544
+ const s = ye(r);
545
+ return /* @__PURE__ */ P.createElement(Ce, { "data-reason": r && r.reason || void 0 }, /* @__PURE__ */ P.createElement("div", { style: { fontWeight: 600 } }, s.title), /* @__PURE__ */ P.createElement("div", { style: { fontSize: 12, color: "#888" } }, s.detail));
539
546
  };
540
547
  le.propTypes = {
541
- entry: E.shape({
542
- origin: E.string,
543
- name: E.string,
544
- reason: E.string,
545
- detail: E.object
548
+ entry: T.shape({
549
+ origin: T.string,
550
+ name: T.string,
551
+ reason: T.string,
552
+ detail: T.object
546
553
  })
547
554
  };
548
555
  const Le = re.div`
@@ -553,138 +560,153 @@ const Le = re.div`
553
560
  justify-content: center;
554
561
  `, Ae = () => /* @__PURE__ */ P.createElement(Le, null, /* @__PURE__ */ P.createElement("div", { style: { textAlign: "center" } }, "This app couldn’t be loaded as an embeddable widget.")), Ue = ({
555
562
  id: r,
556
- src: o,
563
+ src: s,
557
564
  path: i,
558
- onInit: p = () => {
565
+ onInit: l = () => {
559
566
  },
560
- onTimeout: m,
561
- context: R = {},
567
+ onTimeout: w,
568
+ context: I = {},
562
569
  params: v = {},
563
- renderUnsupported: B,
564
- renderTimeout: W,
565
- ...j
570
+ renderUnsupported: D,
571
+ renderTimeout: j,
572
+ ...H
566
573
  }) => {
567
- if ("isConfiguring" in j)
574
+ if ("isConfiguring" in H)
568
575
  return console.error(
569
576
  "isConfiguring must be passed via the params prop to MMEmbeddableZone"
570
577
  ), null;
571
- if ("colorMode" in R)
578
+ if ("colorMode" in I)
572
579
  return console.error(
573
580
  "colorMode must be passed via the params prop to MMEmbeddableZone"
574
581
  ), null;
575
- if ("isFullScreen" in R)
582
+ if ("isFullScreen" in I)
576
583
  return console.error(
577
584
  "isFullScreen must be passed via the params prop to MMEmbeddableZone"
578
585
  ), null;
579
- const T = g(), [A, $] = L(), [_, U] = L(0), [q, I] = L(!1), [D, O] = L(!1), x = Ee(o), M = Re(o), y = we(o), [F, c] = L(0), [l, u] = L(null), { registerFrame: d, unregisterFrame: w, sendMessage: f, ready: C } = G(
586
+ const y = R(), E = J(s), [x, K] = U(), [_, $] = U(0), [A, q] = U(!1), [F, B] = U(!1), z = Ee(s), M = Re(s), W = ke(s), [o, a] = U(0), [u, f] = U(null), { registerFrame: h, unregisterFrame: g, sendMessage: m, ready: C } = G(
580
587
  ee
581
- ), [k, z] = L(), Z = g(), H = g(m), K = g(p), e = g(!1), n = h(() => {
582
- e.current = !1, clearTimeout(Z.current), U(0), I(!1), O(!1), u(null);
588
+ ), [L, Q] = U(), Z = R(), Y = R(w), e = R(l), n = R(!1), t = R(), c = b(() => {
589
+ n.current = !1, clearTimeout(Z.current), clearTimeout(t.current), $(0), q(!1), B(!1), f(null);
583
590
  }, []);
584
- b(() => {
585
- (x > 0 || F > 0) && n();
586
- }, [x, F, n]), b(() => {
587
- H.current = m;
588
- }, [m]), b(() => {
589
- K.current = p;
590
- }, [p]), b(() => {
591
- _ && f(r, "set-context", {
592
- context: {
593
- ...R
594
- }
595
- });
596
- }, [R, r, _]), b(() => {
597
- _ && f(r, "set-params", { params: v });
598
- }, [v, r, _]), b(() => {
599
- i !== void 0 && z((i == null ? void 0 : i.indexOf("/")) === 0 ? i : `/${i}`);
600
- }, [i]), b(() => {
601
- k === void 0 || !_ || f(r, "navigate", { path: k });
602
- }, [k, _, r, f]), b(() => {
603
- if (!o || !r || !T)
591
+ k(() => {
592
+ (z > 0 || o > 0) && c();
593
+ }, [z, o, c]), k(() => {
594
+ Y.current = w;
595
+ }, [w]), k(() => {
596
+ e.current = l;
597
+ }, [l]), k(() => {
598
+ _ && m(
599
+ r,
600
+ "set-context",
601
+ {
602
+ context: {
603
+ ...I
604
+ }
605
+ },
606
+ E
607
+ );
608
+ }, [I, r, _, E]), k(() => {
609
+ _ && m(r, "set-params", { params: v }, E);
610
+ }, [v, r, _, E]), k(() => {
611
+ i !== void 0 && Q((i == null ? void 0 : i.indexOf("/")) === 0 ? i : `/${i}`);
612
+ }, [i]), k(() => {
613
+ L === void 0 || !_ || m(r, "navigate", { path: L }, E);
614
+ }, [L, _, r, m, E]), k(() => {
615
+ if (!s || !r || !y)
604
616
  return;
605
617
  if (!C) {
606
- n(), $(void 0);
618
+ c(), K(void 0);
607
619
  return;
608
620
  }
609
- n();
610
- const a = new URL(o);
611
- return d(r, a.origin, T), K.current(), $(_e(a.origin, r)), () => {
612
- w(r);
621
+ c();
622
+ const d = new URL(s);
623
+ return h(r, d.origin, y), e.current(), K(_e(d.origin, r)), () => {
624
+ g(r);
613
625
  };
614
- }, [o, r, C, d, T, n]), b(() => {
615
- if (!(!A || e.current))
626
+ }, [s, r, C, h, y, c]), k(() => {
627
+ if (!(!x || n.current))
616
628
  return Z.current = setTimeout(() => {
617
- I(!0), H.current && H.current();
629
+ q(!0), Y.current && Y.current();
618
630
  }, Te), () => clearTimeout(Z.current);
619
- }, [A, o, x, F]);
620
- const t = h(() => {
621
- e.current = !0, clearTimeout(Z.current), I(!1);
631
+ }, [x, s, z, o]);
632
+ const p = b(() => {
633
+ n.current = !0, clearTimeout(Z.current), q(!1);
622
634
  }, []);
623
- X(r, "init", (a) => {
624
- t(), Array.isArray(a == null ? void 0 : a.acceptsCredentials) && u(a.acceptsCredentials);
625
- }), X(r, "authorize", t), X(r, "login", t), X(r, "credential-request", () => {
626
- t(), O(!0);
627
- }), b(() => {
628
- if (!D || !M)
635
+ if (X(r, "init", (d) => {
636
+ p(), Array.isArray(d == null ? void 0 : d.acceptsCredentials) && f(d.acceptsCredentials);
637
+ }), X(r, "authorize", p), X(r, "login", p), X(r, "credential-request", () => {
638
+ p(), B(!0);
639
+ }), k(() => {
640
+ if (!F || !M)
629
641
  return;
630
- const a = l;
631
- if (Array.isArray(a) && !a.includes(M.kind))
642
+ const d = u;
643
+ if (Array.isArray(d) && !d.includes(M.kind))
632
644
  return;
633
- const Q = M.kind === "access_token" ? { accessToken: M.accessToken } : {};
634
- f(r, "credential", { ...Q, credential: M });
635
- }, [D, M, l, r, f]), X(r, "ready", () => {
636
- t(), U((a) => a + 1);
637
- });
638
- const s = g();
639
- return X(r, "heartbeat", () => {
640
- s.current && clearTimeout(s.current), s.current = setTimeout(() => {
641
- c((a) => a + 1);
645
+ const S = M.kind === "access_token" ? { accessToken: M.accessToken } : {};
646
+ m(r, "credential", { ...S, credential: M }, E);
647
+ }, [
648
+ F,
649
+ M,
650
+ u,
651
+ r,
652
+ m,
653
+ E
654
+ ]), X(r, "ready", () => {
655
+ p(), $((d) => d + 1);
656
+ }), X(r, "heartbeat", () => {
657
+ t.current && clearTimeout(t.current), t.current = setTimeout(() => {
658
+ a((d) => d + 1);
642
659
  }, 6e3);
643
- }), b(() => () => {
644
- s.current && clearTimeout(s.current);
645
- }, []), y ? B ? B(y) : /* @__PURE__ */ P.createElement(le, { entry: y }) : q && !_ ? W ? W() : /* @__PURE__ */ P.createElement(Ae, null) : /* @__PURE__ */ P.createElement(
660
+ }), k(() => () => {
661
+ t.current && clearTimeout(t.current);
662
+ }, []), W)
663
+ return D ? D(W) : /* @__PURE__ */ P.createElement(le, { entry: W });
664
+ if (A && !_)
665
+ return j ? j() : /* @__PURE__ */ P.createElement(Ae, null);
666
+ const O = { ...H };
667
+ return delete O.skipManifestCheck, /* @__PURE__ */ P.createElement(
646
668
  Me,
647
669
  {
648
- key: `auth-epoch-${x}-frame-${F}`,
670
+ key: `auth-epoch-${z}-frame-${o}`,
649
671
  $isReady: _,
650
- ref: T,
651
- src: A,
652
- ...j
672
+ ref: y,
673
+ src: x,
674
+ ...O
653
675
  }
654
676
  );
655
677
  };
656
678
  Ue.propTypes = {
657
- id: E.string,
658
- src: E.string.isRequired,
659
- path: E.string,
660
- onInit: E.func,
661
- onTimeout: E.func,
679
+ id: T.string,
680
+ src: T.string.isRequired,
681
+ path: T.string,
682
+ onInit: T.func,
683
+ onTimeout: T.func,
662
684
  /**
663
685
  * Optional host-styled renderer for the structurally-unsupported embed
664
686
  * state (legacy app on a cross-site origin), `(entry) => ReactNode`. Falls
665
687
  * back to a minimal built-in placeholder.
666
688
  */
667
- renderUnsupported: E.func,
689
+ renderUnsupported: T.func,
668
690
  /**
669
691
  * Optional host-styled renderer for the handshake-timeout failure state
670
692
  * (the app is not embeddable / could not load), `() => ReactNode`. Falls
671
693
  * back to a minimal centered message.
672
694
  */
673
- renderTimeout: E.func
695
+ renderTimeout: T.func
674
696
  };
675
697
  export {
676
698
  pe as CREDENTIAL_KIND_ACCESS_TOKEN,
677
- J as EmbeddedAuthorizationContext,
699
+ V as EmbeddedAuthorizationContext,
678
700
  Ue as MMEmbeddableZone,
679
701
  ee as MMEmbeddableZoneContext,
680
- ke as MMEmbeddableZoneProvider,
702
+ we as MMEmbeddableZoneProvider,
681
703
  ce as isCrossSite,
682
704
  ne as normalizeCredential,
683
705
  be as popupAuthorizationLauncher,
684
- Ce as unsupportedCopy,
685
- Oe as useEmbeddedAuthorizations,
706
+ ye as unsupportedCopy,
707
+ $e as useEmbeddedAuthorizations,
686
708
  Re as useEmbeddedCredential,
687
709
  X as useEmbeddedEffect,
688
710
  Ee as useEmbeddedOriginEpoch,
689
- we as useEmbeddedUnsupported
711
+ ke as useEmbeddedUnsupported
690
712
  };
@@ -1,4 +1,4 @@
1
- (function(h,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):(h=typeof globalThis!="undefined"?globalThis:h||self,e(h["mm-react-embeddable"]={},h.React,h.PropTypes,h.styled))})(this,(function(h,e,me,Ee){"use strict";const R=t=>t&&t.__esModule?t:{default:t},z=R(e),m=R(me),te=R(Ee),X=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"]),J=t=>{try{return new URL(t).origin}catch{return null}},se=(t,o)=>{const c=o==null?void 0:o.origin;return!c||J(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),b=new URL(o);return c.protocol!==b.protocol?!0:oe(c.hostname)!==oe(b.hostname)}catch{return!0}},ue=(t,{onBlocked:o,onClosed:c})=>{const b=window.open(t.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!b)return o(),()=>{};const E=setInterval(()=>{b.closed&&(clearInterval(E),c())},1e3);return()=>clearInterval(E)},ie=({children:t,launcher:o,broker:c})=>{const b=e.useContext(X),[E,g]=e.useState({}),[P,v]=e.useState({}),[D,W]=e.useState({}),[w,S]=e.useState({}),O=e.useRef(D);O.current=D;const _=e.useRef(E);_.current=E;const A=e.useRef(null),q=e.useRef({}),T=e.useRef({}),B=e.useRef(c);B.current=c;const $=e.useCallback(r=>{const s=q.current[r];if(!s)return;const n=u=>!u[r]||u[r].clientId?u:{...u,[r]:{...u[r],clientId:s}};g(n),S(n)},[]),L=e.useCallback((r,s)=>{r&&(S(n=>n[r]?n:{...n,[r]:{origin:r,reason:s,clientId:q.current[r]||null}}),$(r))},[$]),y=e.useCallback((r,{v2:s=!1,origin:n=null}={})=>{n&&(s&&O.current[n]||(g(u=>u[n]?u:{...u,[n]:{origin:n,authUrl:r.authUrl,status:s&&B.current?"broker":"interactive",v2:s,scope:r.scope||null,acceptsCredentials:T.current[n]||null,consent:null,error:null,clientId:r.clientId||q.current[n]||null,popupBlocked:!1}}),$(n)))},[$]),I=e.useRef({}),x=e.useCallback(r=>{I.current[r]&&(clearTimeout(I.current[r]),delete I.current[r])},[]),i=e.useCallback(r=>{x(r),g(s=>{if(!s[r])return s;const n={...s};return delete n[r],n})},[x]),a=e.useCallback(r=>{r&&(i(r),v(s=>({...s,[r]:(s[r]||0)+1})))},[i]),l=e.useCallback(r=>{i(r.origin)},[i]),f=e.useCallback((r,s)=>{const n=se(r,s);if(n){if(ee(n,window.location.origin)){L(n,"cross-site-legacy");return}y(r,{origin:n})}},[y,L]);e.useEffect(()=>{if(!b)return;const r=[b.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&(q.current[n.origin]=s.clientId,$(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?T.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&ee(n.origin,window.location.origin)&&L(n.origin,"cross-site-legacy")}),b.addListener("*","authorize",f),b.addListener("*","login",f),b.addListener("*","credential-request",(s,n)=>y(s,{v2:!0,origin:se(s,n)}))];return()=>r.forEach(s=>s())},[b,f,y,$,L]),e.useEffect(()=>{const r=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const u=J(n.authUrl);if(!(!u||s.origin!==u)){if(n.accessToken){if(!_.current[u])return;W(d=>({...d,[u]:V({accessToken:n.accessToken})})),i(u);return}a(u)}};return window.addEventListener("message",r),()=>window.removeEventListener("message",r)},[i,a]);const k=e.useCallback((r,s)=>{const n=r==null?void 0:r.origin,u=V(s);!n||!u||(W(d=>({...d,[n]:u})),i(n))},[i]),p=e.useCallback((r,s)=>{g(n=>n[r]?{...n,[r]:{...n[r],...s}}:n)},[]),M=e.useRef({}),C=e.useCallback(async(r,{approve:s}={})=>{const n=B.current,{origin:u}=r;let d;try{d=await n({origin:u,clientId:r.clientId,scope:r.scope,authUrl:r.authUrl},{approve:s})}catch{s?p(u,{error:{reason:"broker_unavailable"}}):p(u,{status:"interactive"});return}const Q=d==null?void 0:d.status,F=(d==null?void 0:d.body)||{};if(Q===200&&F.credential){k(r,F.credential);return}if(Q===409&&F.consentRequired){p(u,{status:"interactive",consent:{client:F.client||null,stepUp:!!F.stepUp,scope:F.scope||r.scope},error:null});return}const Y=F.error||"broker_error";if(!s&&Ce.has(Y)){p(u,{status:"interactive"});return}if(s&&Y==="consent_denied"){p(u,{error:null});return}if(Y==="rate_limited"&&!r.rateLimitRetried){const N=Math.min(Number(F.retryAfter)>0?Number(F.retryAfter):ge,ke),Le={...r,rateLimitRetried:!0};x(u),I.current[u]=setTimeout(()=>{delete I.current[u],M.current[u]=!0,U.current(Le,{approve:s}).finally(()=>{delete M.current[u]})},N*1e3),p(u,{rateLimitRetried:!0});return}i(u),S(N=>({...N,[u]:{origin:u,reason:`broker:${Y}`,detail:d||null,clientId:r.clientId||q.current[u]||null}}))},[k,p,i,x]),U=e.useRef(C);U.current=C,e.useEffect(()=>{Object.values(E).forEach(r=>{r.status!=="broker"||M.current[r.origin]||I.current[r.origin]||(M.current[r.origin]=!0,C(r).finally(()=>{delete M.current[r.origin]}))})},[E,C]);const Z=e.useCallback(r=>!B.current||!(r!=null&&r.consent)?Promise.resolve():C(r,{approve:!0}),[C]);e.useEffect(()=>()=>{A.current&&A.current(),Object.values(I.current).forEach(clearTimeout)},[]);const j=e.useCallback(r=>{A.current&&(A.current(),A.current=null);const s=o||ue;A.current=s(r,{onBlocked:()=>{g(n=>n[r.origin]?{...n,[r.origin]:{...n[r.origin],popupBlocked:!0}}:n)},onClosed:()=>a(r.origin)})},[o,a]),K={pending:Object.values(E).filter(r=>r.status==="interactive"),authorize:j,approve:Z,complete:k,dismiss:l,originEpochs:P,credentials:D,unsupported:w};return z.default.createElement(G.Provider,{value:K},t)};ie.propTypes={children:m.default.node,launcher:m.default.func,broker:m.default.func};const we=()=>e.useContext(G),ce=t=>{const{originEpochs:o}=e.useContext(G),c=J(t);return c&&o[c]||0},le=t=>{const{credentials:o}=e.useContext(G),c=J(t);return c&&o[c]||null},de=t=>{const{unsupported:o}=e.useContext(G),c=J(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:b})=>{const E=b===!0,g=e.useRef(!1);e.useEffect(()=>{b!==void 0||g.current||(g.current=!0,console.error(ae),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(ae))},[b]);const[P,v]=e.useState({}),[D,W]=e.useState({}),w=e.useRef({}),S=e.useRef({}),O=e.useRef({}),_=e.useCallback((i,a)=>{var l;return!!((l=O.current[i])!=null&&l.has(a))},[]),A=e.useCallback((i,a,l)=>{v(f=>({...f,[i]:{origin:a,frameRef:l}}))},[]),q=e.useCallback(i=>{v(a=>{const l={...a};return delete l[i],l}),W(a=>{const l={...a};return delete l[i],l}),delete O.current[i]},[]);e.useEffect(()=>{const i=(l=[],f,k)=>{l.forEach(p=>p(f,k))},a=l=>{var Z,j,K,r,s,n,u;if(!((Z=l==null?void 0:l.data)!=null&&Z.instanceId))return;const{instanceId:f,type:k}=l.data,p=P[f],M=(K=(j=p==null?void 0:p.frameRef)==null?void 0:j.current)==null?void 0:K.contentWindow;if(!M||l.source!==M||l.origin!==p.origin)return;const C={origin:l.origin},U=w.current;if(k==="response"){const{callbackId:d,response:Q,error:F}=l.data,Y=S.current[d];Y&&(Y(Q,F),delete S.current[d]);return}if(k==="mark-registered"){const{requestType:d}=l.data;O.current[f]||(O.current[f]=new Set),O.current[f].add(d);return}k==="ready"&&W(d=>({...d,[f]:!0})),i((r=U[f])==null?void 0:r[k],l.data,C),i((s=U["*"])==null?void 0:s[k],l.data,C),i((n=U[f])==null?void 0:n["*"],l.data,C),i((u=U["*"])==null?void 0:u["*"],l.data,C)};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[P]);const T=e.useCallback((i,a,l)=>{var k,p;const f=P[i];if(!f){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}(p=(k=f==null?void 0:f.frameRef)==null?void 0:k.current)!=null&&p.contentWindow&&f.frameRef.current.contentWindow.postMessage({type:a,...l,instanceId:i},f.origin)},[P]),B=e.useRef(0),$=e.useCallback(()=>`${Date.now()}-${++B.current}`,[]),L=e.useCallback(async(i,a,l={})=>new Promise((f,k)=>{const p=$(),M=(C,U)=>{delete S.current[p],U?k(U):f(C)};S.current[p]=M,T(i,"request",{callbackId:p,requestType:a,data:l})}),[T]),y=e.useCallback((i,a)=>{T(i,"load-options",a)},[T]),I=e.useCallback((i,a)=>{T(i,"validate",a)},[T]),x=e.useMemo(()=>({registerFrame:A,unregisterFrame:q,sendMessage:T,sendRequest:L,loadOptions:y,validate:I,hasHandler:_,readyFrames:D,ready:E,addListener:(i,a,l)=>(w.current[i]||(w.current[i]={}),w.current[i][a]||(w.current[i][a]=[]),w.current[i][a].push(l),()=>{w.current[i][a]=w.current[i][a].filter(f=>f!==l)})}),[A,q,T,L,y,I,_,D,E]);return z.default.createElement(X.Provider,{value:x},z.default.createElement(ie,{launcher:o,broker:c},t))},H=(t,o,c)=>{const b=e.useContext(X);if(b===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const E=e.useRef(c);e.useEffect(()=>{E.current=c},[c]),e.useEffect(()=>{if(!(!t||!o)){if(o.startsWith("key:")){const g=o.substring(4);return b.readyFrames[t]&&b.sendMessage(t,"subscribe-key",{keyChord:g}),b.addListener(t,`key-pressed:${g}`,(...v)=>E.current(...v))}return b.addListener(t,o,(...g)=>E.current(...g))}},[b,t,o])};fe.propTypes={children:m.default.node.isRequired,ready:m.default.bool.isRequired,authorizationLauncher:m.default.func,credentialBroker:m.default.func};const re=te.default.default||te.default,Te=1e4,_e="credentials",ye=(t,o)=>`${t}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${_e}`,Ie=re.iframe`
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,W]=e.useState({}),[M,k]=e.useState({}),L=e.useRef(Z);L.current=Z;const H=e.useRef(w);H.current=w;const _=e.useRef(null),$=e.useRef({}),A=e.useRef({}),q=e.useRef(c);q.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)},[]),v=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&&q.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)){v(n,"cross-site-legacy");return}U(r,{origin:n})}},[U,v]);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)&&v(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,v]),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=H.current[i];if(!(!b||b.status!=="interactive")){if(n.accessToken){W(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||(W(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=q.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=>!q.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 j=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:()=>d(r.origin)})},[o,d]),K={pending:Object.values(w).filter(r=>r.status==="interactive"),authorize:j,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,W]=e.useState({}),M=e.useRef({}),k=e.useRef({}),L=e.useRef({}),H=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}),W(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,j,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=(j=m==null?void 0:m.frameRef)==null?void 0:j.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"&&W(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]),q=e.useRef(0),x=e.useCallback(()=>`${Date.now()}-${++q.current}`,[]),v=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:v,loadOptions:U,validate:I,hasHandler:H,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,v,U,I,H,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`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
@@ -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 z.default.createElement(Me,{"data-reason":t&&t.reason||void 0},z.default.createElement("div",{style:{fontWeight:600}},o.title),z.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};be.propTypes={entry:m.default.shape({origin:m.default.string,name:m.default.string,reason:m.default.string,detail:m.default.object})};const Se=re.div`
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`
20
20
  height: 100%;
21
21
  display: flex;
22
22
  flex: 1;
23
23
  align-items: center;
24
24
  justify-content: center;
25
- `,Ae=()=>z.default.createElement(Se,null,z.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),he=({id:t,src:o,path:c,onInit:b=()=>{},onTimeout:E,context:g={},params:P={},renderUnsupported:v,renderTimeout:D,...W})=>{if("isConfiguring"in W)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in g)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in g)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const w=e.useRef(),[S,O]=e.useState(),[_,A]=e.useState(0),[q,T]=e.useState(!1),[B,$]=e.useState(!1),L=ce(o),y=le(o),I=de(o),[x,i]=e.useState(0),[a,l]=e.useState(null),{registerFrame:f,unregisterFrame:k,sendMessage:p,ready:M}=e.useContext(X),[C,U]=e.useState(),Z=e.useRef(),j=e.useRef(E),K=e.useRef(b),r=e.useRef(!1),s=e.useCallback(()=>{r.current=!1,clearTimeout(Z.current),A(0),T(!1),$(!1),l(null)},[]);e.useEffect(()=>{(L>0||x>0)&&s()},[L,x,s]),e.useEffect(()=>{j.current=E},[E]),e.useEffect(()=>{K.current=b},[b]),e.useEffect(()=>{_&&p(t,"set-context",{context:{...g}})},[g,t,_]),e.useEffect(()=>{_&&p(t,"set-params",{params:P})},[P,t,_]),e.useEffect(()=>{c!==void 0&&U((c==null?void 0:c.indexOf("/"))===0?c:`/${c}`)},[c]),e.useEffect(()=>{C===void 0||!_||p(t,"navigate",{path:C})},[C,_,t,p]),e.useEffect(()=>{if(!o||!t||!w)return;if(!M){s(),O(void 0);return}s();const d=new URL(o);return f(t,d.origin,w),K.current(),O(ye(d.origin,t)),()=>{k(t)}},[o,t,M,f,w,s]),e.useEffect(()=>{if(!(!S||r.current))return Z.current=setTimeout(()=>{T(!0),j.current&&j.current()},Te),()=>clearTimeout(Z.current)},[S,o,L,x]);const n=e.useCallback(()=>{r.current=!0,clearTimeout(Z.current),T(!1)},[]);H(t,"init",d=>{n(),Array.isArray(d==null?void 0:d.acceptsCredentials)&&l(d.acceptsCredentials)}),H(t,"authorize",n),H(t,"login",n),H(t,"credential-request",()=>{n(),$(!0)}),e.useEffect(()=>{if(!B||!y)return;const d=a;if(Array.isArray(d)&&!d.includes(y.kind))return;const Q=y.kind==="access_token"?{accessToken:y.accessToken}:{};p(t,"credential",{...Q,credential:y})},[B,y,a,t,p]),H(t,"ready",()=>{n(),A(d=>d+1)});const u=e.useRef();return H(t,"heartbeat",()=>{u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{i(d=>d+1)},6e3)}),e.useEffect(()=>()=>{u.current&&clearTimeout(u.current)},[]),I?v?v(I):z.default.createElement(be,{entry:I}):q&&!_?D?D():z.default.createElement(Ae,null):z.default.createElement(Ie,{key:`auth-epoch-${L}-frame-${x}`,$isReady:_,ref:w,src:S,...W})};he.propTypes={id:m.default.string,src:m.default.string.isRequired,path:m.default.string,onInit:m.default.func,onTimeout:m.default.func,renderUnsupported:m.default.func,renderTimeout:m.default.func},h.CREDENTIAL_KIND_ACCESS_TOKEN=ne,h.EmbeddedAuthorizationContext=G,h.MMEmbeddableZone=he,h.MMEmbeddableZoneContext=X,h.MMEmbeddableZoneProvider=fe,h.isCrossSite=ee,h.normalizeCredential=V,h.popupAuthorizationLauncher=ue,h.unsupportedCopy=pe,h.useEmbeddedAuthorizations=we,h.useEmbeddedCredential=le,h.useEmbeddedEffect=H,h.useEmbeddedOriginEpoch=ce,h.useEmbeddedUnsupported=de,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})}));
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,...W})=>{if("isConfiguring"in W)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,H]=e.useState(),[_,$]=e.useState(0),[A,q]=e.useState(!1),[x,v]=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(),j=e.useRef(),K=e.useRef(w),r=e.useRef(a),s=e.useRef(!1),n=e.useRef(),i=e.useCallback(()=>{s.current=!1,clearTimeout(j.current),clearTimeout(n.current),$(0),q(!1),v(!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(),H(void 0);return}i();const f=new URL(o);return h(t,f.origin,M),r.current(),H(Ie(f.origin,t)),()=>{m(t)}},[o,t,y,h,M,i]),e.useEffect(()=>{if(!(!L||s.current))return j.current=setTimeout(()=>{q(!0),K.current&&K.current()},Te),()=>clearTimeout(j.current)},[L,o,U,u]);const b=e.useCallback(()=>{s.current=!0,clearTimeout(j.current),q(!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(),v(!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={...W};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"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "2.0.1-0",
3
+ "version": "2.0.1-1",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",