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

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