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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),pe=require("prop-types"),be=require("styled-components"),J=r=>r&&r.__esModule?r:{default:r},I=J(t),C=J(pe),Y=J(be),v=t.createContext(null),H=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),re="access_token",V=r=>r?r.kind?r:r.accessToken?{kind:re,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,he=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),G=typeof window!="undefined"&&window.location.protocol==="https:",me=8e3,K=r=>{try{return new URL(r).origin}catch{return null}},ee=(r,c)=>{const a=c==null?void 0:c.origin;return!a||K(r==null?void 0:r.authUrl)!==a?null:a},te=r=>{if(!r||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(r)||r.includes(":"))return r;const c=r.split(".");return c.length<=2?r:c.slice(-2).join(".")},ne=(r,c)=>{try{const a=new URL(r),f=new URL(c);return a.protocol!==f.protocol?!0:te(a.hostname)!==te(f.hostname)}catch{return!0}},se=(r,{onBlocked:c,onClosed:a})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return c(),()=>{};const b=setInterval(()=>{f.closed&&(clearInterval(b),a())},1e3);return()=>clearInterval(b)},oe=({children:r,launcher:c,broker:a})=>{const f=t.useContext(v),[b,m]=t.useState({}),[y,R]=t.useState({}),[M,U]=t.useState({}),[A,O]=t.useState({}),Z=t.useRef(M);Z.current=M;const g=t.useRef(b);g.current=b;const S=t.useRef(null),W=t.useRef({}),T=t.useRef({}),F=t.useRef({}),L=t.useRef(a);L.current=a;const w=t.useCallback(e=>{const n=T.current[e];if(!n)return;const s=u=>!u[e]||u[e].clientId?u:{...u,[e]:{...u[e],clientId:n}};m(s),O(s)},[]),i=t.useCallback((e,n)=>{e&&(O(s=>s[e]?s:{...s,[e]:{origin:e,reason:n,clientId:T.current[e]||null}}),w(e))},[w]),l=t.useCallback((e,{v2:n=!1,origin:s=null}={})=>{s&&(n&&Z.current[s]||(m(u=>u[s]?u:{...u,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:n&&L.current?"broker":n&&e.silentUrl&&G?"silent":"interactive",v2:n,scope:e.scope||null,acceptsCredentials:F.current[s]||null,consent:null,error:null,clientId:e.clientId||T.current[s]||null,popupBlocked:!1}}),w(s)))},[w]),o=t.useCallback(e=>{m(n=>{if(!n[e])return n;const s={...n};return delete s[e],s})},[]),d=t.useCallback(e=>{e&&(o(e),R(n=>({...n,[e]:(n[e]||0)+1})))},[o]),E=t.useCallback(e=>{m(n=>!n[e]||n[e].status==="interactive"?n:{...n,[e]:{...n[e],status:"interactive"}})},[]),k=t.useCallback(e=>{o(e.origin)},[o]),x=t.useCallback((e,n)=>{const s=ee(e,n);if(s){if(ne(s,window.location.origin)){i(s,"cross-site-legacy");return}l(e,{origin:s})}},[l,i]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(n,s)=>{s!=null&&s.origin&&(n!=null&&n.clientId)&&(T.current[s.origin]=n.clientId,w(s.origin)),s!=null&&s.origin&&Array.isArray(n==null?void 0:n.acceptsCredentials)&&(F.current[s.origin]=n.acceptsCredentials)}),f.addListener("*","authorize",x),f.addListener("*","login",x),f.addListener("*","credential-request",(n,s)=>l(n,{v2:!0,origin:ee(n,s)}))];return()=>e.forEach(n=>n())},[f,x,l,w]),t.useEffect(()=>{const e=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const u=K(s.authUrl);if(!(!u||n.origin!==u)){if(s.type==="authorized"){if(s.accessToken){if(!g.current[u])return;U(p=>({...p,[u]:V({accessToken:s.accessToken})})),o(u)}else d(u);return}E(u)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[o,d,E]);const _=t.useCallback((e,n)=>{const s=e==null?void 0:e.origin,u=V(n);!s||!u||(U(p=>({...p,[s]:u})),o(s))},[o]),h=t.useCallback((e,n)=>{m(s=>s[e]?{...s,[e]:{...s[e],...n}}:s)},[]),z=t.useCallback(async(e,{approve:n}={})=>{const s=L.current,{origin:u}=e;let p;try{p=await s({origin:u,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:n})}catch{n?h(u,{error:{reason:"broker_unavailable"}}):h(u,{status:e.silentUrl&&G?"silent":"interactive"});return}if((p==null?void 0:p.status)===200&&p.credential){_(e,p.credential);return}if((p==null?void 0:p.status)===409&&p.consentRequired){h(u,{status:"interactive",consent:{client:p.client||null,stepUp:!!p.stepUp,scope:p.scope||e.scope},error:null});return}const N=(p==null?void 0:p.error)||"broker_error";if(!n&&he.has(N)){h(u,{status:e.silentUrl&&G?"silent":"interactive"});return}if(n&&N==="consent_denied"){h(u,{error:null});return}o(u),O(X=>({...X,[u]:{origin:u,reason:`broker:${N}`,detail:p||null,clientId:e.clientId||T.current[u]||null}}))},[_,h,o]),P=t.useRef({});t.useEffect(()=>{Object.values(b).forEach(e=>{e.status!=="broker"||P.current[e.origin]||(P.current[e.origin]=!0,z(e).finally(()=>{delete P.current[e.origin]}))})},[b,z]);const $=t.useCallback(e=>!L.current||!(e!=null&&e.consent)?Promise.resolve():z(e,{approve:!0}),[z]);t.useEffect(()=>{const e=W.current;Object.values(b).forEach(n=>{n.status==="silent"&&!e[n.origin]&&(e[n.origin]=setTimeout(()=>{delete e[n.origin],E(n.origin)},me))}),Object.keys(e).forEach(n=>{(!b[n]||b[n].status!=="silent")&&(clearTimeout(e[n]),delete e[n])})},[b,E]),t.useEffect(()=>()=>{S.current&&S.current(),Object.values(W.current).forEach(clearTimeout)},[]);const q=t.useCallback(e=>{S.current&&(S.current(),S.current=null);const n=c||se;S.current=n(e,{onBlocked:()=>{m(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){G&&m(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}d(e.origin)}})},[c,d]),B={pending:Object.values(b).filter(e=>e.status==="interactive"),authorize:q,approve:$,complete:_,dismiss:k,originEpochs:y,credentials:M,unsupported:A},j=Object.values(b).filter(e=>e.status==="silent"&&e.silentUrl);return I.default.createElement(H.Provider,{value:B},r,j.map(e=>I.default.createElement("iframe",{key:`mm-silent-auth-${e.origin}`,src:e.silentUrl,title:`MachineMetrics silent authorization (${e.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};oe.propTypes={children:C.default.node,launcher:C.default.func,broker:C.default.func};const ge=()=>t.useContext(H),ue=r=>{const{originEpochs:c}=t.useContext(H),a=K(r);return a&&c[a]||0},ce=r=>{const{credentials:c}=t.useContext(H),a=K(r);return a&&c[a]||null},ie=r=>{const{unsupported:c}=t.useContext(H),a=K(r);return a&&c[a]||null},ae=({children:r,authorizationLauncher:c,credentialBroker:a})=>{const[f,b]=t.useState({}),[m,y]=t.useState({}),R=t.useRef({}),M=t.useRef({}),U=t.useRef({}),A=t.useCallback((i,l)=>{var o;return!!((o=U.current[i])!=null&&o.has(l))},[]),O=t.useCallback((i,l,o)=>{b(d=>({...d,[i]:{origin:l,frameRef:o}}))},[]),Z=t.useCallback(i=>{b(l=>{const o={...l};return delete o[i],o}),y(l=>{const o={...l};return delete o[i],o}),delete U.current[i]},[]);t.useEffect(()=>{const i=(o=[],d,E)=>{o.forEach(k=>k(d,E))},l=o=>{var z,P,$,q,B,j,e;if(!((z=o==null?void 0:o.data)!=null&&z.instanceId))return;const{instanceId:d,type:E}=o.data,k=f[d],x=($=(P=k==null?void 0:k.frameRef)==null?void 0:P.current)==null?void 0:$.contentWindow;if(!x||o.source!==x||o.origin!==k.origin)return;const _={origin:o.origin},h=R.current;if(E==="response"){const{callbackId:n,response:s,error:u}=o.data,p=M.current[n];p&&(p(s,u),delete M.current[n]);return}if(E==="mark-registered"){const{requestType:n}=o.data;U.current[d]||(U.current[d]=new Set),U.current[d].add(n);return}E==="ready"&&y(n=>({...n,[d]:!0})),i((q=h[d])==null?void 0:q[E],o.data,_),i((B=h["*"])==null?void 0:B[E],o.data,_),i((j=h[d])==null?void 0:j["*"],o.data,_),i((e=h["*"])==null?void 0:e["*"],o.data,_)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[f]);const g=t.useCallback((i,l,o)=>{var E,k;const d=f[i];if(!d){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}(k=(E=d==null?void 0:d.frameRef)==null?void 0:E.current)!=null&&k.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...o,instanceId:i},d.origin)},[f]),S=t.useRef(0),W=t.useCallback(()=>`${Date.now()}-${++S.current}`,[]),T=t.useCallback(async(i,l,o={})=>new Promise((d,E)=>{const k=W(),x=(_,h)=>{delete M.current[k],h?E(h):d(_)};M.current[k]=x,g(i,"request",{callbackId:k,requestType:l,data:o})}),[g]),F=t.useCallback((i,l)=>{g(i,"load-options",l)},[g]),L=t.useCallback((i,l)=>{g(i,"validate",l)},[g]),w=t.useMemo(()=>({registerFrame:O,unregisterFrame:Z,sendMessage:g,sendRequest:T,loadOptions:F,validate:L,hasHandler:A,readyFrames:m,addListener:(i,l,o)=>(R.current[i]||(R.current[i]={}),R.current[i][l]||(R.current[i][l]=[]),R.current[i][l].push(o),()=>{R.current[i][l]=R.current[i][l].filter(d=>d!==o)})}),[O,Z,g,T,F,L,A,m]);return I.default.createElement(v.Provider,{value:w},I.default.createElement(oe,{launcher:c,broker:a},r))},D=(r,c,a)=>{const f=t.useContext(v);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=t.useRef(a);t.useEffect(()=>{b.current=a},[a]),t.useEffect(()=>{if(!(!r||!c)){if(c.startsWith("key:")){const m=c.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:m}),f.addListener(r,`key-pressed:${m}`,(...R)=>b.current(...R))}return f.addListener(r,c,(...m)=>b.current(...m))}},[f,r,c])};ae.propTypes={children:C.default.node.isRequired,authorizationLauncher:C.default.func,credentialBroker:C.default.func};const Q=Y.default.default||Y.default,Ee=1e4,ke="credentials",Ce=(r,c)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(c)}&origin=${encodeURIComponent(window.location.origin)}&caps=${ke}`,Re=Q.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),Ee=require("prop-types"),ge=require("styled-components"),te=r=>r&&r.__esModule?r:{default:r},L=te(t),k=te(Ee),ne=te(ge),Y=t.createContext(null),N=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ue="access_token",Q=r=>r?r.kind?r:r.accessToken?{kind:ue,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Re=60,ke=120,Ce=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),J=typeof window!="undefined"&&window.location.protocol==="https:",Te=8e3,G=r=>{try{return new URL(r).origin}catch{return null}},se=(r,o)=>{const i=o==null?void 0:o.origin;return!i||G(r==null?void 0:r.authUrl)!==i?null:i},oe=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(".")},ee=(r,o)=>{try{const i=new URL(r),f=new URL(o);return i.protocol!==f.protocol?!0:oe(i.hostname)!==oe(f.hostname)}catch{return!0}},ce=(r,{onBlocked:o,onClosed:i})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return o(),()=>{};const h=setInterval(()=>{f.closed&&(clearInterval(h),i())},1e3);return()=>clearInterval(h)},ie=({children:r,launcher:o,broker:i})=>{const f=t.useContext(Y),[h,E]=t.useState({}),[B,C]=t.useState({}),[x,A]=t.useState({}),[P,O]=t.useState({}),v=t.useRef(x);v.current=x;const g=t.useRef(h);g.current=h;const z=t.useRef(null),K=t.useRef({}),M=t.useRef({}),D=t.useRef({}),$=t.useRef(i);$.current=i;const _=t.useCallback(e=>{const n=M.current[e];if(!n)return;const s=a=>!a[e]||a[e].clientId?a:{...a,[e]:{...a[e],clientId:n}};E(s),O(s)},[]),c=t.useCallback((e,n)=>{e&&(O(s=>s[e]?s:{...s,[e]:{origin:e,reason:n,clientId:M.current[e]||null}}),_(e))},[_]),l=t.useCallback((e,{v2:n=!1,origin:s=null}={})=>{s&&(n&&v.current[s]||(E(a=>a[s]?a:{...a,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:n&&$.current?"broker":n&&e.silentUrl&&J?"silent":"interactive",v2:n,scope:e.scope||null,acceptsCredentials:D.current[s]||null,consent:null,error:null,clientId:e.clientId||M.current[s]||null,popupBlocked:!1}}),_(s)))},[_]),u=t.useRef({}),d=t.useCallback(e=>{u.current[e]&&(clearTimeout(u.current[e]),delete u.current[e])},[]),p=t.useCallback(e=>{d(e),E(n=>{if(!n[e])return n;const s={...n};return delete s[e],s})},[d]),m=t.useCallback(e=>{e&&(p(e),C(n=>({...n,[e]:(n[e]||0)+1})))},[p]),S=t.useCallback(e=>{E(n=>!n[e]||n[e].status==="interactive"?n:{...n,[e]:{...n[e],status:"interactive"}})},[]),y=t.useCallback(e=>{p(e.origin)},[p]),R=t.useCallback((e,n)=>{const s=se(e,n);if(s){if(ee(s,window.location.origin)){c(s,"cross-site-legacy");return}l(e,{origin:s})}},[l,c]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(n,s)=>{s!=null&&s.origin&&(n!=null&&n.clientId)&&(M.current[s.origin]=n.clientId,_(s.origin)),s!=null&&s.origin&&Array.isArray(n==null?void 0:n.acceptsCredentials)?D.current[s.origin]=n.acceptsCredentials:s!=null&&s.origin&&ee(s.origin,window.location.origin)&&c(s.origin,"cross-site-legacy")}),f.addListener("*","authorize",R),f.addListener("*","login",R),f.addListener("*","credential-request",(n,s)=>l(n,{v2:!0,origin:se(n,s)}))];return()=>e.forEach(n=>n())},[f,R,l,_,c]),t.useEffect(()=>{const e=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const a=G(s.authUrl);if(!(!a||n.origin!==a)){if(s.type==="authorized"){if(s.accessToken){if(!g.current[a])return;A(b=>({...b,[a]:Q({accessToken:s.accessToken})})),p(a)}else m(a);return}S(a)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[p,m,S]);const F=t.useCallback((e,n)=>{const s=e==null?void 0:e.origin,a=Q(n);!s||!a||(A(b=>({...b,[s]:a})),p(s))},[p]),U=t.useCallback((e,n)=>{E(s=>s[e]?{...s,[e]:{...s[e],...n}}:s)},[]),w=t.useRef({}),I=t.useCallback(async(e,{approve:n}={})=>{const s=$.current,{origin:a}=e;let b;try{b=await s({origin:a,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:n})}catch{n?U(a,{error:{reason:"broker_unavailable"}}):U(a,{status:e.silentUrl&&J?"silent":"interactive"});return}if((b==null?void 0:b.status)===200&&b.credential){F(e,b.credential);return}if((b==null?void 0:b.status)===409&&b.consentRequired){U(a,{status:"interactive",consent:{client:b.client||null,stepUp:!!b.stepUp,scope:b.scope||e.scope},error:null});return}const V=(b==null?void 0:b.error)||"broker_error";if(!n&&Ce.has(V)){U(a,{status:e.silentUrl&&J?"silent":"interactive"});return}if(n&&V==="consent_denied"){U(a,{error:null});return}if(V==="rate_limited"&&!e.rateLimitRetried){const X=Math.min(Number(b==null?void 0:b.retryAfter)>0?Number(b.retryAfter):Re,ke),me={...e,rateLimitRetried:!0};d(a),u.current[a]=setTimeout(()=>{delete u.current[a],w.current[a]=!0,Z.current(me,{approve:n}).finally(()=>{delete w.current[a]})},X*1e3),U(a,{rateLimitRetried:!0});return}p(a),O(X=>({...X,[a]:{origin:a,reason:`broker:${V}`,detail:b||null,clientId:e.clientId||M.current[a]||null}}))},[F,U,p,d]),Z=t.useRef(I);Z.current=I,t.useEffect(()=>{Object.values(h).forEach(e=>{e.status!=="broker"||w.current[e.origin]||u.current[e.origin]||(w.current[e.origin]=!0,I(e).finally(()=>{delete w.current[e.origin]}))})},[h,I]);const W=t.useCallback(e=>!$.current||!(e!=null&&e.consent)?Promise.resolve():I(e,{approve:!0}),[I]);t.useEffect(()=>{const e=K.current;Object.values(h).forEach(n=>{n.status==="silent"&&!e[n.origin]&&(e[n.origin]=setTimeout(()=>{delete e[n.origin],S(n.origin)},Te))}),Object.keys(e).forEach(n=>{(!h[n]||h[n].status!=="silent")&&(clearTimeout(e[n]),delete e[n])})},[h,S]),t.useEffect(()=>()=>{z.current&&z.current(),Object.values(K.current).forEach(clearTimeout),Object.values(u.current).forEach(clearTimeout)},[]);const j=t.useCallback(e=>{z.current&&(z.current(),z.current=null);const n=o||ce;z.current=n(e,{onBlocked:()=>{E(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){J&&E(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}m(e.origin)}})},[o,m]),T={pending:Object.values(h).filter(e=>e.status==="interactive"),authorize:j,approve:W,complete:F,dismiss:y,originEpochs:B,credentials:x,unsupported:P},q=Object.values(h).filter(e=>e.status==="silent"&&e.silentUrl);return L.default.createElement(N.Provider,{value:T},r,q.map(e=>L.default.createElement("iframe",{key:`mm-silent-auth-${e.origin}`,src:e.silentUrl,title:`MachineMetrics silent authorization (${e.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};ie.propTypes={children:k.default.node,launcher:k.default.func,broker:k.default.func};const _e=()=>t.useContext(N),ae=r=>{const{originEpochs:o}=t.useContext(N),i=G(r);return i&&o[i]||0},le=r=>{const{credentials:o}=t.useContext(N),i=G(r);return i&&o[i]||null},de=r=>{const{unsupported:o}=t.useContext(N),i=G(r);return i&&o[i]||null},fe=({children:r,authorizationLauncher:o,credentialBroker:i})=>{const[f,h]=t.useState({}),[E,B]=t.useState({}),C=t.useRef({}),x=t.useRef({}),A=t.useRef({}),P=t.useCallback((c,l)=>{var u;return!!((u=A.current[c])!=null&&u.has(l))},[]),O=t.useCallback((c,l,u)=>{h(d=>({...d,[c]:{origin:l,frameRef:u}}))},[]),v=t.useCallback(c=>{h(l=>{const u={...l};return delete u[c],u}),B(l=>{const u={...l};return delete u[c],u}),delete A.current[c]},[]);t.useEffect(()=>{const c=(u=[],d,p)=>{u.forEach(m=>m(d,p))},l=u=>{var F,U,w,I,Z,W,j;if(!((F=u==null?void 0:u.data)!=null&&F.instanceId))return;const{instanceId:d,type:p}=u.data,m=f[d],S=(w=(U=m==null?void 0:m.frameRef)==null?void 0:U.current)==null?void 0:w.contentWindow;if(!S||u.source!==S||u.origin!==m.origin)return;const y={origin:u.origin},R=C.current;if(p==="response"){const{callbackId:T,response:q,error:e}=u.data,n=x.current[T];n&&(n(q,e),delete x.current[T]);return}if(p==="mark-registered"){const{requestType:T}=u.data;A.current[d]||(A.current[d]=new Set),A.current[d].add(T);return}p==="ready"&&B(T=>({...T,[d]:!0})),c((I=R[d])==null?void 0:I[p],u.data,y),c((Z=R["*"])==null?void 0:Z[p],u.data,y),c((W=R[d])==null?void 0:W["*"],u.data,y),c((j=R["*"])==null?void 0:j["*"],u.data,y)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[f]);const g=t.useCallback((c,l,u)=>{var p,m;const d=f[c];if(!d){console.warn(`Origin not set for id: ${c}. Unable to send message.`);return}(m=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&m.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...u,instanceId:c},d.origin)},[f]),z=t.useRef(0),K=t.useCallback(()=>`${Date.now()}-${++z.current}`,[]),M=t.useCallback(async(c,l,u={})=>new Promise((d,p)=>{const m=K(),S=(y,R)=>{delete x.current[m],R?p(R):d(y)};x.current[m]=S,g(c,"request",{callbackId:m,requestType:l,data:u})}),[g]),D=t.useCallback((c,l)=>{g(c,"load-options",l)},[g]),$=t.useCallback((c,l)=>{g(c,"validate",l)},[g]),_=t.useMemo(()=>({registerFrame:O,unregisterFrame:v,sendMessage:g,sendRequest:M,loadOptions:D,validate:$,hasHandler:P,readyFrames:E,addListener:(c,l,u)=>(C.current[c]||(C.current[c]={}),C.current[c][l]||(C.current[c][l]=[]),C.current[c][l].push(u),()=>{C.current[c][l]=C.current[c][l].filter(d=>d!==u)})}),[O,v,g,M,D,$,P,E]);return L.default.createElement(Y.Provider,{value:_},L.default.createElement(ie,{launcher:o,broker:i},r))},H=(r,o,i)=>{const f=t.useContext(Y);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const h=t.useRef(i);t.useEffect(()=>{h.current=i},[i]),t.useEffect(()=>{if(!(!r||!o)){if(o.startsWith("key:")){const E=o.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:E}),f.addListener(r,`key-pressed:${E}`,(...C)=>h.current(...C))}return f.addListener(r,o,(...E)=>h.current(...E))}},[f,r,o])};fe.propTypes={children:k.default.node.isRequired,authorizationLauncher:k.default.func,credentialBroker:k.default.func};const re=ne.default.default||ne.default,we=1e4,Ie="credentials",Me=(r,o)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Ie}`,Se=re.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${r=>r.$isReady?"visible":"hidden"};
6
- `,we=Q.div`
6
+ `,Ue=re.div`
7
7
  width: 100%;
8
8
  height: 100%;
9
9
  display: flex;
@@ -16,10 +16,10 @@
16
16
  text-align: center;
17
17
  color: #555;
18
18
  font-size: 14px;
19
- `,le=r=>{const c=r&&r.origin||"This app",a=r&&r.reason||"";switch(a){case"broker:scope_above_ceiling":return{title:`${c} 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:`${c} can’t be authorized here`,detail:"It isn’t registered to run at this address."};case"broker:cross_company_principal":return{title:`${c} can’t be authorized here`,detail:"It can’t be authorized for this account."};default:return a.startsWith("broker:")?{title:`${c} can’t be authorized here`,detail:"Authorization was refused."}:{title:`${c} can’t be embedded here`,detail:"It needs an update to support embedding from a different domain."}}},de=({entry:r})=>{const c=le(r);return I.default.createElement(we,{"data-reason":r&&r.reason||void 0},I.default.createElement("div",{style:{fontWeight:600}},c.title),I.default.createElement("div",{style:{fontSize:12,color:"#888"}},c.detail))};de.propTypes={entry:C.default.shape({origin:C.default.string,name:C.default.string,reason:C.default.string,detail:C.default.object})};const Te=Q.div`
19
+ `,pe=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."}}},be=({entry:r})=>{const o=pe(r);return L.default.createElement(Ue,{"data-reason":r&&r.reason||void 0},L.default.createElement("div",{style:{fontWeight:600}},o.title),L.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};be.propTypes={entry:k.default.shape({origin:k.default.string,name:k.default.string,reason:k.default.string,detail:k.default.object})};const Le=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
- `,_e=()=>I.default.createElement(Te,null,I.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),fe=({id:r,src:c,path:a,onInit:f=()=>{},onTimeout:b,context:m={},params:y={},renderUnsupported:R,renderTimeout:M,...U})=>{if("isConfiguring"in U)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in m)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in m)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const A=t.useRef(),[O,Z]=t.useState(),[g,S]=t.useState(0),[W,T]=t.useState(!1),[F,L]=t.useState(!1),w=ue(c),i=ce(c),l=ie(c),[o,d]=t.useState(0),[E,k]=t.useState(null),{registerFrame:x,unregisterFrame:_,sendMessage:h}=t.useContext(v),[z,P]=t.useState(),$=t.useRef(),q=t.useRef(b),B=t.useRef(f),j=t.useRef(!1),e=t.useCallback(()=>{j.current=!1,clearTimeout($.current),S(0),T(!1),L(!1),k(null)},[]);t.useEffect(()=>{(w>0||o>0)&&e()},[w,o,e]),t.useEffect(()=>{q.current=b},[b]),t.useEffect(()=>{B.current=f},[f]),t.useEffect(()=>{g&&h(r,"set-context",{context:{...m}})},[m,r,g]),t.useEffect(()=>{g&&h(r,"set-params",{params:y})},[y,r,g]),t.useEffect(()=>{a!==void 0&&P((a==null?void 0:a.indexOf("/"))===0?a:`/${a}`)},[a]),t.useEffect(()=>{z===void 0||!g||h(r,"navigate",{path:z})},[z,g,r,h]),t.useEffect(()=>{if(!c||!r||!A)return;e();const u=new URL(c);return x(r,u.origin,A),B.current(),Z(Ce(u.origin,r)),()=>{_(r)}},[c,r,x,A,e]),t.useEffect(()=>{if(!(!O||j.current))return $.current=setTimeout(()=>{T(!0),q.current&&q.current()},Ee),()=>clearTimeout($.current)},[O,c,w,o]);const n=t.useCallback(()=>{j.current=!0,clearTimeout($.current),T(!1)},[]);D(r,"init",u=>{n(),Array.isArray(u==null?void 0:u.acceptsCredentials)&&k(u.acceptsCredentials)}),D(r,"authorize",n),D(r,"login",n),D(r,"credential-request",()=>{n(),L(!0)}),t.useEffect(()=>{if(!F||!i)return;const u=E;if(Array.isArray(u)&&!u.includes(i.kind))return;const p=i.kind==="access_token"?{accessToken:i.accessToken}:{};h(r,"credential",{...p,credential:i})},[F,i,E,r,h]),D(r,"ready",()=>{n(),S(u=>u+1)});const s=t.useRef();return D(r,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{d(u=>u+1)},6e3)}),t.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),l?R?R(l):I.default.createElement(de,{entry:l}):W&&!g?M?M():I.default.createElement(_e,null):I.default.createElement(Re,{key:`auth-epoch-${w}-frame-${o}`,$isReady:g,ref:A,src:O,...U})};fe.propTypes={id:C.default.string,src:C.default.string.isRequired,path:C.default.string,onInit:C.default.func,onTimeout:C.default.func,renderUnsupported:C.default.func,renderTimeout:C.default.func};exports.CREDENTIAL_KIND_ACCESS_TOKEN=re;exports.EmbeddedAuthorizationContext=H;exports.MMEmbeddableZone=fe;exports.MMEmbeddableZoneContext=v;exports.MMEmbeddableZoneProvider=ae;exports.isCrossSite=ne;exports.normalizeCredential=V;exports.popupAuthorizationLauncher=se;exports.unsupportedCopy=le;exports.useEmbeddedAuthorizations=ge;exports.useEmbeddedCredential=ce;exports.useEmbeddedEffect=D;exports.useEmbeddedOriginEpoch=ue;exports.useEmbeddedUnsupported=ie;
25
+ `,xe=()=>L.default.createElement(Le,null,L.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),he=({id:r,src:o,path:i,onInit:f=()=>{},onTimeout:h,context:E={},params:B={},renderUnsupported:C,renderTimeout:x,...A})=>{if("isConfiguring"in A)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 P=t.useRef(),[O,v]=t.useState(),[g,z]=t.useState(0),[K,M]=t.useState(!1),[D,$]=t.useState(!1),_=ae(o),c=le(o),l=de(o),[u,d]=t.useState(0),[p,m]=t.useState(null),{registerFrame:S,unregisterFrame:y,sendMessage:R}=t.useContext(Y),[F,U]=t.useState(),w=t.useRef(),I=t.useRef(h),Z=t.useRef(f),W=t.useRef(!1),j=t.useCallback(()=>{W.current=!1,clearTimeout(w.current),z(0),M(!1),$(!1),m(null)},[]);t.useEffect(()=>{(_>0||u>0)&&j()},[_,u,j]),t.useEffect(()=>{I.current=h},[h]),t.useEffect(()=>{Z.current=f},[f]),t.useEffect(()=>{g&&R(r,"set-context",{context:{...E}})},[E,r,g]),t.useEffect(()=>{g&&R(r,"set-params",{params:B})},[B,r,g]),t.useEffect(()=>{i!==void 0&&U((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),t.useEffect(()=>{F===void 0||!g||R(r,"navigate",{path:F})},[F,g,r,R]),t.useEffect(()=>{if(!o||!r||!P)return;j();const e=new URL(o);return S(r,e.origin,P),Z.current(),v(Me(e.origin,r)),()=>{y(r)}},[o,r,S,P,j]),t.useEffect(()=>{if(!(!O||W.current))return w.current=setTimeout(()=>{M(!0),I.current&&I.current()},we),()=>clearTimeout(w.current)},[O,o,_,u]);const T=t.useCallback(()=>{W.current=!0,clearTimeout(w.current),M(!1)},[]);H(r,"init",e=>{T(),Array.isArray(e==null?void 0:e.acceptsCredentials)&&m(e.acceptsCredentials)}),H(r,"authorize",T),H(r,"login",T),H(r,"credential-request",()=>{T(),$(!0)}),t.useEffect(()=>{if(!D||!c)return;const e=p;if(Array.isArray(e)&&!e.includes(c.kind))return;const n=c.kind==="access_token"?{accessToken:c.accessToken}:{};R(r,"credential",{...n,credential:c})},[D,c,p,r,R]),H(r,"ready",()=>{T(),z(e=>e+1)});const q=t.useRef();return H(r,"heartbeat",()=>{q.current&&clearTimeout(q.current),q.current=setTimeout(()=>{d(e=>e+1)},6e3)}),t.useEffect(()=>()=>{q.current&&clearTimeout(q.current)},[]),l?C?C(l):L.default.createElement(be,{entry:l}):K&&!g?x?x():L.default.createElement(xe,null):L.default.createElement(Se,{key:`auth-epoch-${_}-frame-${u}`,$isReady:g,ref:P,src:O,...A})};he.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=ue;exports.EmbeddedAuthorizationContext=N;exports.MMEmbeddableZone=he;exports.MMEmbeddableZoneContext=Y;exports.MMEmbeddableZoneProvider=fe;exports.isCrossSite=ee;exports.normalizeCredential=Q;exports.popupAuthorizationLauncher=ce;exports.unsupportedCopy=pe;exports.useEmbeddedAuthorizations=_e;exports.useEmbeddedCredential=le;exports.useEmbeddedEffect=H;exports.useEmbeddedOriginEpoch=ae;exports.useEmbeddedUnsupported=de;