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