@machinemetrics/mm-react-embeddable 1.4.0-8 → 2.0.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- (function(b,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):(b=typeof globalThis!="undefined"?globalThis:b||self,t(b["mm-react-embeddable"]={},b.React,b.PropTypes,b.styled))})(this,(function(b,t,he,me){"use strict";const Q=n=>n&&n.__esModule?n:{default:n},m=Q(t),w=Q(he),v=Q(me),N=t.createContext(null),H=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),R="access_token",X=n=>n?n.kind?n:n.accessToken?{kind:R,accessToken:n.accessToken,expiresAt:n.expiresAt||null}:null:null,ge=new Set(["host_not_allowed","invalid_token","provider_unavailable"]),J=typeof window!="undefined"&&window.location.protocol==="https:",Ee=8e3,V=n=>{try{return new URL(n).origin}catch{return null}},ee=(n,o)=>{const i=o==null?void 0:o.origin;return!i||V(n==null?void 0:n.authUrl)!==i?null:i},te=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 i=new URL(n),f=new URL(o);return i.protocol!==f.protocol?!0:te(i.hostname)!==te(f.hostname)}catch{return!0}},re=(n,{onBlocked:o,onClosed:i})=>{const f=window.open(n.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return o(),()=>{};const g=setInterval(()=>{f.closed&&(clearInterval(g),i())},1e3);return()=>clearInterval(g)},se=({children:n,launcher:o,broker:i})=>{const f=t.useContext(N),[g,C]=t.useState({}),[P,z]=t.useState({}),[I,_]=t.useState({}),[M,U]=t.useState({}),B=t.useRef(I);B.current=I;const p=t.useRef(null),W=t.useRef({}),L=t.useRef({}),F=t.useRef({}),y=t.useRef(i);y.current=i;const S=t.useCallback(e=>{const r=L.current[e];if(!r)return;const s=a=>!a[e]||a[e].clientId?a:{...a,[e]:{...a[e],clientId:r}};C(s),U(s)},[]),j=t.useCallback((e,r)=>{e&&(U(s=>s[e]?s:{...s,[e]:{origin:e,reason:r,clientId:L.current[e]||null}}),S(e))},[S]),u=t.useCallback((e,{v2:r=!1,origin:s=null}={})=>{s&&(r&&B.current[s]||(C(a=>a[s]?a:{...a,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:r&&y.current?"broker":r&&e.silentUrl&&J?"silent":"interactive",v2:r,scope:e.scope||null,acceptsCredentials:F.current[s]||null,consent:null,error:null,clientId:e.clientId||L.current[s]||null,popupBlocked:!1}}),S(s)))},[S]),c=t.useCallback(e=>{C(r=>{if(!r[e])return r;const s={...r};return delete s[e],s})},[]),l=t.useCallback(e=>{e&&(c(e),z(r=>({...r,[e]:(r[e]||0)+1})))},[c]),d=t.useCallback(e=>{C(r=>!r[e]||r[e].status==="interactive"?r:{...r,[e]:{...r[e],status:"interactive"}})},[]),E=t.useCallback(e=>{c(e.origin)},[c]),k=t.useCallback((e,r)=>{const s=ee(e,r);if(s){if(ne(s,window.location.origin)){j(s,"cross-site-legacy");return}u(e,{origin:s})}},[u,j]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(r,s)=>{s!=null&&s.origin&&(r!=null&&r.clientId)&&(L.current[s.origin]=r.clientId,S(s.origin)),s!=null&&s.origin&&Array.isArray(r==null?void 0:r.acceptsCredentials)&&(F.current[s.origin]=r.acceptsCredentials)}),f.addListener("*","authorize",k),f.addListener("*","login",k),f.addListener("*","credential-request",(r,s)=>u(r,{v2:!0,origin:ee(r,s)}))];return()=>e.forEach(r=>r())},[f,k,u,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 a=V(s.authUrl);if(!(!a||r.origin!==a)){if(s.type==="authorized"){s.accessToken?(_(h=>({...h,[a]:X({accessToken:s.accessToken})})),c(a)):l(a);return}d(a)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[c,l,d]);const A=t.useCallback((e,r)=>{const s=e==null?void 0:e.origin,a=X(r);!s||!a||(_(h=>({...h,[s]:a})),c(s))},[c]),T=t.useCallback((e,r)=>{C(s=>s[e]?{...s,[e]:{...s[e],...r}}:s)},[]),x=t.useCallback(async(e,{approve:r}={})=>{const s=y.current,{origin:a}=e;let h;try{h=await s({origin:a,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:r})}catch{r?T(a,{error:{reason:"broker_unavailable"}}):T(a,{status:e.silentUrl&&J?"silent":"interactive"});return}if((h==null?void 0:h.status)===200&&h.credential){A(e,h.credential);return}if((h==null?void 0:h.status)===409&&h.consentRequired){T(a,{status:"interactive",consent:{client:h.client||null,stepUp:!!h.stepUp,scope:h.scope||e.scope},error:null});return}const G=(h==null?void 0:h.error)||"broker_error";if(!r&&ge.has(G)){T(a,{status:e.silentUrl&&J?"silent":"interactive"});return}if(r&&G==="consent_denied"){T(a,{error:null});return}c(a),U(be=>({...be,[a]:{origin:a,reason:`broker:${G}`,detail:h||null,clientId:e.clientId||L.current[a]||null}}))},[A,T,c]),$=t.useRef({});t.useEffect(()=>{Object.values(g).forEach(e=>{e.status!=="broker"||$.current[e.origin]||($.current[e.origin]=!0,x(e).finally(()=>{delete $.current[e.origin]}))})},[g,x]);const Z=t.useCallback(e=>!y.current||!(e!=null&&e.consent)?Promise.resolve():x(e,{approve:!0}),[x]);t.useEffect(()=>{const e=W.current;Object.values(g).forEach(r=>{r.status==="silent"&&!e[r.origin]&&(e[r.origin]=setTimeout(()=>{delete e[r.origin],d(r.origin)},Ee))}),Object.keys(e).forEach(r=>{(!g[r]||g[r].status!=="silent")&&(clearTimeout(e[r]),delete e[r])})},[g,d]),t.useEffect(()=>()=>{p.current&&p.current(),Object.values(W.current).forEach(clearTimeout)},[]);const O=t.useCallback(e=>{p.current&&(p.current(),p.current=null);const r=o||re;p.current=r(e,{onBlocked:()=>{C(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){J&&C(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}l(e.origin)}})},[o,l]),D={pending:Object.values(g).filter(e=>e.status==="interactive"),authorize:O,approve:Z,complete:A,dismiss:E,originEpochs:P,credentials:I,unsupported:M},K=Object.values(g).filter(e=>e.status==="silent"&&e.silentUrl);return m.default.createElement(H.Provider,{value:D},n,K.map(e=>m.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"}})))};se.propTypes={children:w.default.node,launcher:w.default.func,broker:w.default.func};const oe=()=>t.useContext(H),ie=n=>{const{originEpochs:o}=t.useContext(H),i=V(n);return i&&o[i]||0},ue=n=>{const{credentials:o}=t.useContext(H),i=V(n);return i&&o[i]||null},le=n=>{const{unsupported:o}=t.useContext(H),i=V(n);return i&&o[i]||null},ce=({children:n,authorizationLauncher:o,credentialBroker:i})=>{const[f,g]=t.useState({}),[C,P]=t.useState({}),z=t.useRef({}),I=t.useRef({}),_=t.useRef({}),M=t.useCallback((u,c)=>{var l;return!!((l=_.current[u])!=null&&l.has(c))},[]),U=t.useCallback((u,c,l)=>{g(d=>({...d,[u]:{origin:c,frameRef:l}}))},[]),B=t.useCallback(u=>{g(c=>{const l={...c};return delete l[u],l}),P(c=>{const l={...c};return delete l[u],l}),delete _.current[u]},[]);t.useEffect(()=>{const u=(l=[],d,E)=>{l.forEach(k=>k(d,E))},c=l=>{var $,Z,O,D,K,e,r;if(!(($=l==null?void 0:l.data)!=null&&$.instanceId))return;const{instanceId:d,type:E}=l.data,k=f[d],A=(O=(Z=k==null?void 0:k.frameRef)==null?void 0:Z.current)==null?void 0:O.contentWindow;if(!A||l.source!==A||l.origin!==k.origin)return;const T={origin:l.origin},x=z.current;if(E==="response"){const{callbackId:s,response:a,error:h}=l.data,G=I.current[s];G&&(G(a,h),delete I.current[s]);return}if(E==="mark-registered"){const{requestType:s}=l.data;_.current[d]||(_.current[d]=new Set),_.current[d].add(s);return}E==="ready"&&P(s=>({...s,[d]:!0})),u((D=x[d])==null?void 0:D[E],l.data,T),u((K=x["*"])==null?void 0:K[E],l.data,T),u((e=x[d])==null?void 0:e["*"],l.data,T),u((r=x["*"])==null?void 0:r["*"],l.data,T)};return window.addEventListener("message",c),()=>{window.removeEventListener("message",c)}},[f]);const p=t.useCallback((u,c,l)=>{var E,k;const d=f[u];if(!d){console.warn(`Origin not set for id: ${u}. 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:c,...l,instanceId:u},d.origin)},[f]),W=t.useRef(0),L=t.useCallback(()=>`${Date.now()}-${++W.current}`,[]),F=t.useCallback(async(u,c,l={})=>new Promise((d,E)=>{const k=L(),A=(T,x)=>{delete I.current[k],x?E(x):d(T)};I.current[k]=A,p(u,"request",{callbackId:k,requestType:c,data:l})}),[p]),y=t.useCallback((u,c)=>{p(u,"load-options",c)},[p]),S=t.useCallback((u,c)=>{p(u,"validate",c)},[p]),j=t.useMemo(()=>({registerFrame:U,unregisterFrame:B,sendMessage:p,sendRequest:F,loadOptions:y,validate:S,hasHandler:M,readyFrames:C,addListener:(u,c,l)=>(z.current[u]||(z.current[u]={}),z.current[u][c]||(z.current[u][c]=[]),z.current[u][c].push(l),()=>{z.current[u][c]=z.current[u][c].filter(d=>d!==l)})}),[U,B,p,F,y,S,M,C]);return m.default.createElement(N.Provider,{value:j},m.default.createElement(se,{launcher:o,broker:i},n))},q=(n,o,i)=>{const f=t.useContext(N);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const g=t.useRef(i);t.useEffect(()=>{g.current=i},[i]),t.useEffect(()=>{if(!(!n||!o)){if(o.startsWith("key:")){const C=o.substring(4);return f.readyFrames[n]&&f.sendMessage(n,"subscribe-key",{keyChord:C}),f.addListener(n,`key-pressed:${C}`,(...z)=>g.current(...z))}return f.addListener(n,o,(...C)=>g.current(...C))}},[f,n,o])};ce.propTypes={children:w.default.node.isRequired,authorizationLauncher:w.default.func,credentialBroker:w.default.func};const Y=v.default.default||v.default,ke=1e4,Ce="credentials",ae=(n,o)=>`${n}/mm-embedded-interop?id=${o}&origin=${window.location.origin}&caps=${Ce}`,we=Y.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,he,me){"use strict";const Q=n=>n&&n.__esModule?n:{default:n},m=Q(t),w=Q(he),W=Q(me),N=t.createContext(null),K=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ee="access_token",X=n=>n?n.kind?n:n.accessToken?{kind:ee,accessToken:n.accessToken,expiresAt:n.expiresAt||null}:null:null,ge=new Set(["host_not_allowed","invalid_token","provider_unavailable"]),J=typeof window!="undefined"&&window.location.protocol==="https:",Ee=8e3,V=n=>{try{return new URL(n).origin}catch{return null}},te=(n,o)=>{const l=o==null?void 0:o.origin;return!l||V(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 o=n.split(".");return o.length<=2?n:o.slice(-2).join(".")},re=(n,o)=>{try{const l=new URL(n),f=new URL(o);return l.protocol!==f.protocol?!0:ne(l.hostname)!==ne(f.hostname)}catch{return!0}},se=(n,{onBlocked:o,onClosed:l})=>{const f=window.open(n.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return o(),()=>{};const b=setInterval(()=>{f.closed&&(clearInterval(b),l())},1e3);return()=>clearInterval(b)},oe=({children:n,launcher:o,broker:l})=>{const f=t.useContext(N),[b,E]=t.useState({}),[F,x]=t.useState({}),[M,S]=t.useState({}),[$,O]=t.useState({}),D=t.useRef(M);D.current=M;const k=t.useRef(b);k.current=b;const U=t.useRef(null),G=t.useRef({}),z=t.useRef({}),j=t.useRef({}),L=t.useRef(l);L.current=l;const I=t.useCallback(e=>{const r=z.current[e];if(!r)return;const s=u=>!u[e]||u[e].clientId?u:{...u,[e]:{...u[e],clientId:r}};E(s),O(s)},[]),c=t.useCallback((e,r)=>{e&&(O(s=>s[e]?s:{...s,[e]:{origin:e,reason:r,clientId:z.current[e]||null}}),I(e))},[I]),a=t.useCallback((e,{v2:r=!1,origin:s=null}={})=>{s&&(r&&D.current[s]||(E(u=>u[s]?u:{...u,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:r&&L.current?"broker":r&&e.silentUrl&&J?"silent":"interactive",v2:r,scope:e.scope||null,acceptsCredentials:j.current[s]||null,consent:null,error:null,clientId:e.clientId||z.current[s]||null,popupBlocked:!1}}),I(s)))},[I]),i=t.useCallback(e=>{E(r=>{if(!r[e])return r;const s={...r};return delete s[e],s})},[]),d=t.useCallback(e=>{e&&(i(e),x(r=>({...r,[e]:(r[e]||0)+1})))},[i]),C=t.useCallback(e=>{E(r=>!r[e]||r[e].status==="interactive"?r:{...r,[e]:{...r[e],status:"interactive"}})},[]),T=t.useCallback(e=>{i(e.origin)},[i]),y=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)&&(z.current[s.origin]=r.clientId,I(s.origin)),s!=null&&s.origin&&Array.isArray(r==null?void 0:r.acceptsCredentials)&&(j.current[s.origin]=r.acceptsCredentials)}),f.addListener("*","authorize",y),f.addListener("*","login",y),f.addListener("*","credential-request",(r,s)=>a(r,{v2:!0,origin:te(r,s)}))];return()=>e.forEach(r=>r())},[f,y,a,I]),t.useEffect(()=>{const e=r=>{const s=r==null?void 0:r.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const u=V(s.authUrl);if(!(!u||r.origin!==u)){if(s.type==="authorized"){if(s.accessToken){if(!k.current[u])return;S(p=>({...p,[u]:X({accessToken:s.accessToken})})),i(u)}else d(u);return}C(u)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[i,d,C]);const _=t.useCallback((e,r)=>{const s=e==null?void 0:e.origin,u=X(r);!s||!u||(S(p=>({...p,[s]:u})),i(s))},[i]),g=t.useCallback((e,r)=>{E(s=>s[e]?{...s,[e]:{...s[e],...r}}:s)},[]),A=t.useCallback(async(e,{approve:r}={})=>{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:r})}catch{r?g(u,{error:{reason:"broker_unavailable"}}):g(u,{status:e.silentUrl&&J?"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){g(u,{status:"interactive",consent:{client:p.client||null,stepUp:!!p.stepUp,scope:p.scope||e.scope},error:null});return}const R=(p==null?void 0:p.error)||"broker_error";if(!r&&ge.has(R)){g(u,{status:e.silentUrl&&J?"silent":"interactive"});return}if(r&&R==="consent_denied"){g(u,{error:null});return}i(u),O(be=>({...be,[u]:{origin:u,reason:`broker:${R}`,detail:p||null,clientId:e.clientId||z.current[u]||null}}))},[_,g,i]),v=t.useRef({});t.useEffect(()=>{Object.values(b).forEach(e=>{e.status!=="broker"||v.current[e.origin]||(v.current[e.origin]=!0,A(e).finally(()=>{delete v.current[e.origin]}))})},[b,A]);const P=t.useCallback(e=>!L.current||!(e!=null&&e.consent)?Promise.resolve():A(e,{approve:!0}),[A]);t.useEffect(()=>{const e=G.current;Object.values(b).forEach(r=>{r.status==="silent"&&!e[r.origin]&&(e[r.origin]=setTimeout(()=>{delete e[r.origin],C(r.origin)},Ee))}),Object.keys(e).forEach(r=>{(!b[r]||b[r].status!=="silent")&&(clearTimeout(e[r]),delete e[r])})},[b,C]),t.useEffect(()=>()=>{U.current&&U.current(),Object.values(G.current).forEach(clearTimeout)},[]);const q=t.useCallback(e=>{U.current&&(U.current(),U.current=null);const r=o||se;U.current=r(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}d(e.origin)}})},[o,d]),H={pending:Object.values(b).filter(e=>e.status==="interactive"),authorize:q,approve:P,complete:_,dismiss:T,originEpochs:F,credentials:M,unsupported:$},B=Object.values(b).filter(e=>e.status==="silent"&&e.silentUrl);return m.default.createElement(K.Provider,{value:H},n,B.map(e=>m.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:w.default.node,launcher:w.default.func,broker:w.default.func};const ie=()=>t.useContext(K),ue=n=>{const{originEpochs:o}=t.useContext(K),l=V(n);return l&&o[l]||0},le=n=>{const{credentials:o}=t.useContext(K),l=V(n);return l&&o[l]||null},ce=n=>{const{unsupported:o}=t.useContext(K),l=V(n);return l&&o[l]||null},ae=({children:n,authorizationLauncher:o,credentialBroker:l})=>{const[f,b]=t.useState({}),[E,F]=t.useState({}),x=t.useRef({}),M=t.useRef({}),S=t.useRef({}),$=t.useCallback((c,a)=>{var i;return!!((i=S.current[c])!=null&&i.has(a))},[]),O=t.useCallback((c,a,i)=>{b(d=>({...d,[c]:{origin:a,frameRef:i}}))},[]),D=t.useCallback(c=>{b(a=>{const i={...a};return delete i[c],i}),F(a=>{const i={...a};return delete i[c],i}),delete S.current[c]},[]);t.useEffect(()=>{const c=(i=[],d,C)=>{i.forEach(T=>T(d,C))},a=i=>{var A,v,P,q,H,B,e;if(!((A=i==null?void 0:i.data)!=null&&A.instanceId))return;const{instanceId:d,type:C}=i.data,T=f[d],y=(P=(v=T==null?void 0:T.frameRef)==null?void 0:v.current)==null?void 0:P.contentWindow;if(!y||i.source!==y||i.origin!==T.origin)return;const _={origin:i.origin},g=x.current;if(C==="response"){const{callbackId:r,response:s,error:u}=i.data,p=M.current[r];p&&(p(s,u),delete M.current[r]);return}if(C==="mark-registered"){const{requestType:r}=i.data;S.current[d]||(S.current[d]=new Set),S.current[d].add(r);return}C==="ready"&&F(r=>({...r,[d]:!0})),c((q=g[d])==null?void 0:q[C],i.data,_),c((H=g["*"])==null?void 0:H[C],i.data,_),c((B=g[d])==null?void 0:B["*"],i.data,_),c((e=g["*"])==null?void 0:e["*"],i.data,_)};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[f]);const k=t.useCallback((c,a,i)=>{var C,T;const d=f[c];if(!d){console.warn(`Origin not set for id: ${c}. Unable to send message.`);return}(T=(C=d==null?void 0:d.frameRef)==null?void 0:C.current)!=null&&T.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:a,...i,instanceId:c},d.origin)},[f]),U=t.useRef(0),G=t.useCallback(()=>`${Date.now()}-${++U.current}`,[]),z=t.useCallback(async(c,a,i={})=>new Promise((d,C)=>{const T=G(),y=(_,g)=>{delete M.current[T],g?C(g):d(_)};M.current[T]=y,k(c,"request",{callbackId:T,requestType:a,data:i})}),[k]),j=t.useCallback((c,a)=>{k(c,"load-options",a)},[k]),L=t.useCallback((c,a)=>{k(c,"validate",a)},[k]),I=t.useMemo(()=>({registerFrame:O,unregisterFrame:D,sendMessage:k,sendRequest:z,loadOptions:j,validate:L,hasHandler:$,readyFrames:E,addListener:(c,a,i)=>(x.current[c]||(x.current[c]={}),x.current[c][a]||(x.current[c][a]=[]),x.current[c][a].push(i),()=>{x.current[c][a]=x.current[c][a].filter(d=>d!==i)})}),[O,D,k,z,j,L,$,E]);return m.default.createElement(N.Provider,{value:I},m.default.createElement(oe,{launcher:o,broker:l},n))},Z=(n,o,l)=>{const f=t.useContext(N);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||!o)){if(o.startsWith("key:")){const E=o.substring(4);return f.readyFrames[n]&&f.sendMessage(n,"subscribe-key",{keyChord:E}),f.addListener(n,`key-pressed:${E}`,(...x)=>b.current(...x))}return f.addListener(n,o,(...E)=>b.current(...E))}},[f,n,o])};ae.propTypes={children:w.default.node.isRequired,authorizationLauncher:w.default.func,credentialBroker:w.default.func};const Y=W.default.default||W.default,ke=1e4,Ce="credentials",we=(n,o)=>`${n}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Ce}`,Te=Y.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${n=>n.$isReady?"visible":"hidden"};
6
- `,Te=Y.div`
6
+ `,xe=Y.div`
7
7
  width: 100%;
8
8
  height: 100%;
9
9
  display: flex;
@@ -16,13 +16,13 @@
16
16
  text-align: center;
17
17
  color: #555;
18
18
  font-size: 14px;
19
- `,de=n=>{const o=n&&n.origin||"This app",i=n&&n.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."}}},fe=({entry:n})=>{const o=de(n);return m.default.createElement(Te,{"data-reason":n&&n.reason||void 0},m.default.createElement("div",{style:{fontWeight:600}},o.title),m.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};fe.propTypes={entry:w.default.shape({origin:w.default.string,name:w.default.string,reason:w.default.string,detail:w.default.object})};const xe=Y.div`
19
+ `,de=n=>{const o=n&&n.origin||"This app",l=n&&n.reason||"";switch(l){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 l.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."}}},fe=({entry:n})=>{const o=de(n);return m.default.createElement(xe,{"data-reason":n&&n.reason||void 0},m.default.createElement("div",{style:{fontWeight:600}},o.title),m.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};fe.propTypes={entry:w.default.shape({origin:w.default.string,name:w.default.string,reason:w.default.string,detail:w.default.object})};const Ie=Y.div`
20
20
  height: 100%;
21
21
  display: flex;
22
22
  flex: 1;
23
23
  align-items: center;
24
24
  justify-content: center;
25
- `,ze=()=>m.default.createElement(xe,null,m.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),pe=({id:n,src:o,path:i,onInit:f=()=>{},onTimeout:g,context:C={},params:P={},renderUnsupported:z,renderTimeout:I,..._})=>{if("isConfiguring"in _)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in C)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in C)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const M=t.useRef(),[U,B]=t.useState(),[p,W]=t.useState(0),[L,F]=t.useState(!1),[y,S]=t.useState(!1),j=ie(o),u=ue(o),c=le(o);t.useEffect(()=>{j>0&&W(0)},[j]);const{registerFrame:l,unregisterFrame:d,sendMessage:E}=t.useContext(N),[k,A]=t.useState(),T=t.useRef(),x=t.useRef(g),$=t.useRef(f),Z=t.useRef(!1);t.useEffect(()=>{x.current=g},[g]),t.useEffect(()=>{$.current=f},[f]),t.useEffect(()=>{p&&E(n,"set-context",{context:{...C}})},[C,n,p]),t.useEffect(()=>{p&&E(n,"set-params",{params:P})},[P,n,p]),t.useEffect(()=>{i!==void 0&&A((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),t.useEffect(()=>{k===void 0||!p||E(n,"navigate",{path:k})},[k,p,n,E]),t.useEffect(()=>{if(!o||!n||!M)return;const r=new URL(o);return l(n,r.origin,M),$.current(),B(ae(r.origin,n)),()=>{d(n)}},[o,n,l,M]),t.useEffect(()=>{if(!(!U||Z.current))return T.current=setTimeout(()=>{F(!0),x.current&&x.current()},ke),()=>clearTimeout(T.current)},[U]);const O=t.useCallback(()=>{Z.current=!0,clearTimeout(T.current),F(!1)},[]),[D,K]=t.useState(null);q(n,"init",r=>{O(),Array.isArray(r==null?void 0:r.acceptsCredentials)&&K(r.acceptsCredentials)}),q(n,"authorize",O),q(n,"login",O),q(n,"credential-request",()=>{O(),S(!0)}),t.useEffect(()=>{if(!y||!u)return;const r=D;if(Array.isArray(r)&&!r.includes(u.kind))return;const s=u.kind==="access_token"?{accessToken:u.accessToken}:{};E(n,"credential",{...s,credential:u})},[y,u,D,n,E]),q(n,"ready",()=>{O(),W(r=>r+1)});const e=t.useRef();return q(n,"heartbeat",()=>{e.current&&clearTimeout(e.current),e.current=setTimeout(()=>{if(M.current){const r=new URL(o);M.current.src=ae(r.origin,n)}},6e3)}),t.useEffect(()=>()=>{e.current&&clearTimeout(e.current)},[]),c?z?z(c):m.default.createElement(fe,{entry:c}):L&&!p?I?I():m.default.createElement(ze,null):m.default.createElement(we,{key:`auth-epoch-${j}`,$isReady:p,ref:M,src:U,..._})};pe.propTypes={id:w.default.string,src:w.default.string.isRequired,path:w.default.string,onInit:w.default.func,onTimeout:w.default.func,renderUnsupported:w.default.func,renderTimeout:w.default.func};const Ie=v.default.div`
25
+ `,ze=()=>m.default.createElement(Ie,null,m.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),pe=({id:n,src:o,path:l,onInit:f=()=>{},onTimeout:b,context:E={},params:F={},renderUnsupported:x,renderTimeout:M,...S})=>{if("isConfiguring"in S)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 $=t.useRef(),[O,D]=t.useState(),[k,U]=t.useState(0),[G,z]=t.useState(!1),[j,L]=t.useState(!1),I=ue(o),c=le(o),a=ce(o),[i,d]=t.useState(0),[C,T]=t.useState(null),{registerFrame:y,unregisterFrame:_,sendMessage:g}=t.useContext(N),[A,v]=t.useState(),P=t.useRef(),q=t.useRef(b),H=t.useRef(f),B=t.useRef(!1),e=t.useCallback(()=>{B.current=!1,clearTimeout(P.current),U(0),z(!1),L(!1),T(null)},[]);t.useEffect(()=>{(I>0||i>0)&&e()},[I,i,e]),t.useEffect(()=>{q.current=b},[b]),t.useEffect(()=>{H.current=f},[f]),t.useEffect(()=>{k&&g(n,"set-context",{context:{...E}})},[E,n,k]),t.useEffect(()=>{k&&g(n,"set-params",{params:F})},[F,n,k]),t.useEffect(()=>{l!==void 0&&v((l==null?void 0:l.indexOf("/"))===0?l:`/${l}`)},[l]),t.useEffect(()=>{A===void 0||!k||g(n,"navigate",{path:A})},[A,k,n,g]),t.useEffect(()=>{if(!o||!n||!$)return;e();const u=new URL(o);return y(n,u.origin,$),H.current(),D(we(u.origin,n)),()=>{_(n)}},[o,n,y,$,e]),t.useEffect(()=>{if(!(!O||B.current))return P.current=setTimeout(()=>{z(!0),q.current&&q.current()},ke),()=>clearTimeout(P.current)},[O,o,I,i]);const r=t.useCallback(()=>{B.current=!0,clearTimeout(P.current),z(!1)},[]);Z(n,"init",u=>{r(),Array.isArray(u==null?void 0:u.acceptsCredentials)&&T(u.acceptsCredentials)}),Z(n,"authorize",r),Z(n,"login",r),Z(n,"credential-request",()=>{r(),L(!0)}),t.useEffect(()=>{if(!j||!c)return;const u=C;if(Array.isArray(u)&&!u.includes(c.kind))return;const p=c.kind==="access_token"?{accessToken:c.accessToken}:{};g(n,"credential",{...p,credential:c})},[j,c,C,n,g]),Z(n,"ready",()=>{r(),U(u=>u+1)});const s=t.useRef();return Z(n,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{d(u=>u+1)},6e3)}),t.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),a?x?x(a):m.default.createElement(fe,{entry:a}):G&&!k?M?M():m.default.createElement(ze,null):m.default.createElement(Te,{key:`auth-epoch-${I}-frame-${i}`,$isReady:k,ref:$,src:O,...S})};pe.propTypes={id:w.default.string,src:w.default.string.isRequired,path:w.default.string,onInit:w.default.func,onTimeout:w.default.func,renderUnsupported:w.default.func,renderTimeout:w.default.func};const _e=W.default.div`
26
26
  position: fixed;
27
27
  inset: 0;
28
28
  background: rgba(0, 0, 0, 0.45);
@@ -30,7 +30,7 @@
30
30
  align-items: center;
31
31
  justify-content: center;
32
32
  z-index: 10000;
33
- `,_e=v.default.div`
33
+ `,Me=W.default.div`
34
34
  background: #fff;
35
35
  border-radius: 8px;
36
36
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
@@ -39,13 +39,13 @@
39
39
  max-width: calc(100vw - 32px);
40
40
  font-family: sans-serif;
41
41
  color: #222;
42
- `,Me=v.default.div`
42
+ `,Se=W.default.div`
43
43
  display: flex;
44
44
  align-items: center;
45
45
  justify-content: space-between;
46
46
  padding: 10px 0;
47
47
  gap: 16px;
48
- `,Se=v.default.button`
48
+ `,Ue=W.default.button`
49
49
  background: #16a500;
50
50
  color: #fff;
51
51
  border: none;
@@ -54,8 +54,8 @@
54
54
  font-size: 14px;
55
55
  cursor: pointer;
56
56
  white-space: nowrap;
57
- `,Ue=v.default.p`
57
+ `,ye=W.default.p`
58
58
  color: #b3261e;
59
59
  font-size: 13px;
60
60
  margin: 4px 0 0;
61
- `,ye=()=>{const{pending:n,authorize:o}=oe();return n.length===0?null:m.default.createElement(Ie,null,m.default.createElement(_e,null,m.default.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),m.default.createElement("p",{style:{margin:"0 0 12px",fontSize:14,color:"#555"}},"The following apps need you to sign in and authorize them before they can load."),n.map(i=>m.default.createElement("div",{key:i.origin},m.default.createElement(Me,null,m.default.createElement("div",null,m.default.createElement("div",{style:{fontWeight:600}},i.origin)),m.default.createElement(Se,{onClick:()=>o(i)},"Authorize")),i.popupBlocked&&m.default.createElement(Ue,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};b.CREDENTIAL_KIND_ACCESS_TOKEN=R,b.EmbeddedAuthorizationContext=H,b.EmbeddedAuthorizationPrompt=ye,b.MMEmbeddableZone=pe,b.MMEmbeddableZoneContext=N,b.MMEmbeddableZoneProvider=ce,b.isCrossSite=ne,b.normalizeCredential=X,b.popupAuthorizationLauncher=re,b.unsupportedCopy=de,b.useEmbeddedAuthorizations=oe,b.useEmbeddedCredential=ue,b.useEmbeddedEffect=q,b.useEmbeddedOriginEpoch=ie,b.useEmbeddedUnsupported=le,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})}));
61
+ `,Ae=()=>{const{pending:n,authorize:o}=ie();return n.length===0?null:m.default.createElement(_e,null,m.default.createElement(Me,null,m.default.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),m.default.createElement("p",{style:{margin:"0 0 12px",fontSize:14,color:"#555"}},"The following apps need you to sign in and authorize them before they can load."),n.map(l=>m.default.createElement("div",{key:l.origin},m.default.createElement(Se,null,m.default.createElement("div",null,m.default.createElement("div",{style:{fontWeight:600}},l.origin)),m.default.createElement(Ue,{onClick:()=>o(l)},"Authorize")),l.popupBlocked&&m.default.createElement(ye,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};h.CREDENTIAL_KIND_ACCESS_TOKEN=ee,h.EmbeddedAuthorizationContext=K,h.EmbeddedAuthorizationPrompt=Ae,h.MMEmbeddableZone=pe,h.MMEmbeddableZoneContext=N,h.MMEmbeddableZoneProvider=ae,h.isCrossSite=re,h.normalizeCredential=X,h.popupAuthorizationLauncher=se,h.unsupportedCopy=de,h.useEmbeddedAuthorizations=ie,h.useEmbeddedCredential=le,h.useEmbeddedEffect=Z,h.useEmbeddedOriginEpoch=ue,h.useEmbeddedUnsupported=ce,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": "1.4.0-8",
3
+ "version": "2.0.0-1",
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
+ }