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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,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,H]=e.useState({}),[M,k]=e.useState({}),L=e.useRef(Z);L.current=Z;const j=e.useRef(w);j.current=w;const _=e.useRef(null),$=e.useRef({}),A=e.useRef({}),v=e.useRef(c);v.current=c;const x=e.useCallback(r=>{const s=$.current[r];if(!s)return;const n=i=>!i[r]||i[r].clientId?i:{...i,[r]:{...i[r],clientId:s}};T(n),k(n)},[]),q=e.useCallback((r,s)=>{r&&(k(n=>n[r]?n:{...n,[r]:{origin:r,reason:s,clientId:$.current[r]||null}}),x(r))},[x]),U=e.useCallback((r,{v2:s=!1,origin:n=null}={})=>{n&&(s&&L.current[n]||(T(i=>i[n]?i:{...i,[n]:{origin:n,authUrl:r.authUrl,status:s&&v.current?"broker":"interactive",v2:s,scope:r.scope||null,acceptsCredentials:A.current[n]||null,consent:null,error:null,clientId:r.clientId||$.current[n]||null,popupBlocked:!1}}),x(n)))},[x]),I=e.useRef({}),D=e.useCallback(r=>{I.current[r]&&(clearTimeout(I.current[r]),delete I.current[r])},[]),u=e.useCallback(r=>{D(r),T(s=>{if(!s[r])return s;const n={...s};return delete n[r],n})},[D]),d=e.useCallback(r=>{r&&(u(r),B(s=>({...s,[r]:(s[r]||0)+1})))},[u]),l=e.useCallback(r=>{u(r.origin)},[u]),p=e.useCallback((r,s)=>{const n=se(r,s);if(n){if(ee(n,window.location.origin)){q(n,"cross-site-legacy");return}U(r,{origin:n})}},[U,q]);e.useEffect(()=>{if(!a)return;const r=[a.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&($.current[n.origin]=s.clientId,x(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?A.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&ee(n.origin,window.location.origin)&&q(n.origin,"cross-site-legacy")}),a.addListener("*","authorize",p),a.addListener("*","login",p),a.addListener("*","credential-request",(s,n)=>U(s,{v2:!0,origin:se(s,n)}))];return()=>r.forEach(s=>s())},[a,p,U,x,q]),e.useEffect(()=>{const r=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const i=Q(n.authUrl);if(!i||s.origin!==i)return;const b=j.current[i];if(!(!b||b.status!=="interactive")){if(n.accessToken){H(z=>({...z,[i]:V({accessToken:n.accessToken})})),u(i);return}d(i)}};return window.addEventListener("message",r),()=>window.removeEventListener("message",r)},[u,d]);const h=e.useCallback((r,s)=>{const n=r==null?void 0:r.origin,i=V(s);!n||!i||(H(b=>({...b,[n]:i})),u(n))},[u]),m=e.useCallback((r,s)=>{T(n=>n[r]?{...n,[r]:{...n[r],...s}}:n)},[]),E=e.useRef({}),y=e.useCallback(async(r,{approve:s}={})=>{const n=v.current,{origin:i}=r;let b;try{b=await n({origin:i,clientId:r.clientId,scope:r.scope,authUrl:r.authUrl},{approve:s})}catch{s?m(i,{error:{reason:"broker_unavailable"}}):m(i,{status:"interactive"});return}const z=b==null?void 0:b.status,f=(b==null?void 0:b.body)||{};if(z===200&&f.credential){h(r,f.credential);return}if(z===409&&f.consentRequired){m(i,{status:"interactive",consent:{client:f.client||null,stepUp:!!f.stepUp,scope:f.scope||r.scope},error:null});return}const P=f.error||"broker_error";if(!s&&Ce.has(P)){m(i,{status:"interactive"});return}if(s&&P==="consent_denied"){m(i,{error:null});return}if(P==="rate_limited"&&!r.rateLimitRetried){const N=Math.min(Number(f.retryAfter)>0?Number(f.retryAfter):ge,ke),Le={...r,rateLimitRetried:!0};D(i),I.current[i]=setTimeout(()=>{delete I.current[i],E.current[i]=!0,S.current(Le,{approve:s}).finally(()=>{delete E.current[i]})},N*1e3),m(i,{rateLimitRetried:!0});return}u(i),k(N=>({...N,[i]:{origin:i,reason:`broker:${P}`,detail:{status:z!=null?z:null,reason:P},clientId:r.clientId||$.current[i]||null}}))},[h,m,u,D]),S=e.useRef(y);S.current=y,e.useEffect(()=>{Object.values(w).forEach(r=>{r.status!=="broker"||E.current[r.origin]||I.current[r.origin]||(E.current[r.origin]=!0,y(r).finally(()=>{delete E.current[r.origin]}))})},[w,y]);const X=e.useCallback(r=>!v.current||!(r!=null&&r.consent)||E.current[r.origin]?Promise.resolve():(E.current[r.origin]=!0,y(r,{approve:!0}).finally(()=>{delete E.current[r.origin]})),[y]);e.useEffect(()=>()=>{_.current&&_.current(),Object.values(I.current).forEach(clearTimeout)},[]);const W=e.useCallback(r=>{_.current&&(_.current(),_.current=null);const s=o||ue;_.current=s(r,{onBlocked:()=>{T(n=>n[r.origin]?{...n,[r.origin]:{...n[r.origin],popupBlocked:!0}}:n)},onClosed:()=>{const n=j.current[r.origin];!n||n.v2||d(r.origin)}})},[o,d]),K={pending:Object.values(w).filter(r=>r.status==="interactive"),authorize:W,approve:X,complete:h,dismiss:l,originEpochs:F,credentials:Z,unsupported:M};return O.default.createElement(G.Provider,{value:K},t)};ie.propTypes={children:C.default.node,launcher:C.default.func,broker:C.default.func};const we=()=>e.useContext(G),ce=t=>{const{originEpochs:o}=e.useContext(G),c=Q(t);return c&&o[c]||0},le=t=>{const{credentials:o}=e.useContext(G),c=Q(t);return c&&o[c]||null},de=t=>{const{unsupported:o}=e.useContext(G),c=Q(t);return c&&o[c]||null},ae="MMEmbeddableZoneProvider requires a boolean `ready` prop (true once the host holds the bearer its credentialBroker needs). Embedded apps will not load until it is provided.",fe=({children:t,authorizationLauncher:o,credentialBroker:c,ready:a})=>{const w=a===!0,T=e.useRef(!1);e.useEffect(()=>{a!==void 0||T.current||(T.current=!0,console.error(ae),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(ae))},[a]);const[F,B]=e.useState({}),[Z,H]=e.useState({}),M=e.useRef({}),k=e.useRef({}),L=e.useRef({}),j=e.useCallback((u,d)=>{var l;return!!((l=L.current[u])!=null&&l.has(d))},[]),_=e.useCallback((u,d,l)=>{B(p=>({...p,[u]:{origin:d,frameRef:l}}))},[]),$=e.useCallback(u=>{B(d=>{const l={...d};return delete l[u],l}),H(d=>{const l={...d};return delete l[u],l}),delete L.current[u],Object.keys(k.current).forEach(d=>{k.current[d].instanceId===u&&delete k.current[d]})},[]);e.useEffect(()=>()=>{k.current={}},[]),e.useEffect(()=>{const u=(l=[],p,h)=>{l.forEach(m=>m(p,h))},d=l=>{var X,W,K,r,s,n,i;if(!((X=l==null?void 0:l.data)!=null&&X.instanceId))return;const{instanceId:p,type:h}=l.data,m=F[p],E=(K=(W=m==null?void 0:m.frameRef)==null?void 0:W.current)==null?void 0:K.contentWindow;if(!E||l.source!==E||l.origin!==m.origin)return;const y={origin:l.origin},S=M.current;if(h==="response"){const{callbackId:b,response:z,error:f}=l.data,P=k.current[b];P&&P.instanceId===p&&(P.callback(z,f),delete k.current[b]);return}if(h==="mark-registered"){const{requestType:b}=l.data;L.current[p]||(L.current[p]=new Set),L.current[p].add(b);return}h==="ready"&&H(b=>({...b,[p]:!0})),u((r=S[p])==null?void 0:r[h],l.data,y),u((s=S["*"])==null?void 0:s[h],l.data,y),u((n=S[p])==null?void 0:n["*"],l.data,y),u((i=S["*"])==null?void 0:i["*"],l.data,y)};return window.addEventListener("message",d),()=>{window.removeEventListener("message",d)}},[F]);const A=e.useCallback((u,d,l,p)=>{var m,E;const h=F[u];if(!h){console.warn(`Origin not set for id: ${u}. Unable to send message.`);return}(E=(m=h==null?void 0:h.frameRef)==null?void 0:m.current)!=null&&E.contentWindow&&h.frameRef.current.contentWindow.postMessage({type:d,...l,instanceId:u},p||h.origin)},[F]),v=e.useRef(0),x=e.useCallback(()=>`${Date.now()}-${++v.current}`,[]),q=e.useCallback(async(u,d,l={})=>new Promise((p,h)=>{const m=x(),E=(y,S)=>{delete k.current[m],S?h(S):p(y)};k.current[m]={instanceId:u,callback:E},A(u,"request",{callbackId:m,requestType:d,data:l})}),[A]),U=e.useCallback((u,d)=>{A(u,"load-options",d)},[A]),I=e.useCallback((u,d)=>{A(u,"validate",d)},[A]),D=e.useMemo(()=>({registerFrame:_,unregisterFrame:$,sendMessage:A,sendRequest:q,loadOptions:U,validate:I,hasHandler:j,readyFrames:Z,ready:w,addListener:(u,d,l)=>(M.current[u]||(M.current[u]={}),M.current[u][d]||(M.current[u][d]=[]),M.current[u][d].push(l),()=>{M.current[u][d]=M.current[u][d].filter(p=>p!==l)})}),[_,$,A,q,U,I,j,Z,w]);return O.default.createElement(J.Provider,{value:D},O.default.createElement(ie,{launcher:o,broker:c},t))},Y=(t,o,c)=>{const a=e.useContext(J);if(a===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const w=e.useRef(c);e.useEffect(()=>{w.current=c},[c]),e.useEffect(()=>{if(!(!t||!o)){if(o.startsWith("key:")){const T=o.substring(4);return a.readyFrames[t]&&a.sendMessage(t,"subscribe-key",{keyChord:T}),a.addListener(t,`key-pressed:${T}`,(...B)=>w.current(...B))}return a.addListener(t,o,(...T)=>w.current(...T))}},[a,t,o])};fe.propTypes={children:C.default.node.isRequired,ready:C.default.bool.isRequired,authorizationLauncher:C.default.func,credentialBroker:C.default.func};const re=te.default.default||te.default,Te=1e4,_e="credentials",Ie=(t,o)=>`${t}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${_e}`,ye=re.iframe`
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,...H})=>{if("isConfiguring"in H)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in T)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in T)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const M=e.useRef(),k=Q(o),[L,j]=e.useState(),[_,$]=e.useState(0),[A,v]=e.useState(!1),[x,q]=e.useState(!1),U=ce(o),I=le(o),D=de(o),[u,d]=e.useState(0),[l,p]=e.useState(null),{registerFrame:h,unregisterFrame:m,sendMessage:E,ready:y}=e.useContext(J),[S,X]=e.useState(),W=e.useRef(),K=e.useRef(w),r=e.useRef(a),s=e.useRef(!1),n=e.useRef(),i=e.useCallback(()=>{s.current=!1,clearTimeout(W.current),clearTimeout(n.current),$(0),v(!1),q(!1),p(null)},[]);e.useEffect(()=>{(U>0||u>0)&&i()},[U,u,i]),e.useEffect(()=>{K.current=w},[w]),e.useEffect(()=>{r.current=a},[a]),e.useEffect(()=>{_&&E(t,"set-context",{context:{...T}},k)},[T,t,_,k]),e.useEffect(()=>{_&&E(t,"set-params",{params:F},k)},[F,t,_,k]),e.useEffect(()=>{c!==void 0&&X((c==null?void 0:c.indexOf("/"))===0?c:`/${c}`)},[c]),e.useEffect(()=>{S===void 0||!_||E(t,"navigate",{path:S},k)},[S,_,t,E,k]),e.useEffect(()=>{if(!o||!t||!M)return;if(!y){i(),j(void 0);return}i();const f=new URL(o);return h(t,f.origin,M),r.current(),j(Ie(f.origin,t)),()=>{m(t)}},[o,t,y,h,M,i]),e.useEffect(()=>{if(!(!L||s.current))return W.current=setTimeout(()=>{v(!0),K.current&&K.current()},Te),()=>clearTimeout(W.current)},[L,o,U,u]);const b=e.useCallback(()=>{s.current=!0,clearTimeout(W.current),v(!1)},[]);if(Y(t,"init",f=>{b(),Array.isArray(f==null?void 0:f.acceptsCredentials)&&p(f.acceptsCredentials)}),Y(t,"authorize",b),Y(t,"login",b),Y(t,"credential-request",()=>{b(),q(!0)}),e.useEffect(()=>{if(!x||!I)return;const f=l;if(Array.isArray(f)&&!f.includes(I.kind))return;const P=I.kind==="access_token"?{accessToken:I.accessToken}:{};E(t,"credential",{...P,credential:I},k)},[x,I,l,t,E,k]),Y(t,"ready",()=>{b(),$(f=>f+1)}),Y(t,"heartbeat",()=>{n.current&&clearTimeout(n.current),n.current=setTimeout(()=>{d(f=>f+1)},6e3)}),e.useEffect(()=>()=>{n.current&&clearTimeout(n.current)},[]),D)return B?B(D):O.default.createElement(be,{entry:D});if(A&&!_)return Z?Z():O.default.createElement(Ae,null);const z={...H};return delete z.skipManifestCheck,O.default.createElement(ye,{key:`auth-epoch-${U}-frame-${u}`,$isReady:_,ref:M,src:L,...z})};he.propTypes={id:C.default.string,src:C.default.string.isRequired,path:C.default.string,onInit:C.default.func,onTimeout:C.default.func,renderUnsupported:C.default.func,renderTimeout:C.default.func},g.CREDENTIAL_KIND_ACCESS_TOKEN=ne,g.EmbeddedAuthorizationContext=G,g.MMEmbeddableZone=he,g.MMEmbeddableZoneContext=J,g.MMEmbeddableZoneProvider=fe,g.isCrossSite=ee,g.normalizeCredential=V,g.popupAuthorizationLauncher=ue,g.unsupportedCopy=pe,g.useEmbeddedAuthorizations=we,g.useEmbeddedCredential=le,g.useEmbeddedEffect=Y,g.useEmbeddedOriginEpoch=ce,g.useEmbeddedUnsupported=de,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
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-2",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",