@machinemetrics/mm-react-embeddable 1.4.0-1 → 1.4.0-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),E=require("prop-types"),j=require("styled-components"),v=e.createContext(null),Z=e.createContext({pending:[],authorize:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ee=2e4,F=r=>{try{return new URL(r).origin}catch{return null}},W=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(".")},D=(r,u)=>{try{const s=new URL(r),d=new URL(u);return s.protocol!==d.protocol?!0:W(s.hostname)!==W(d.hostname)}catch{return!0}},H=(r,{onBlocked:u,onClosed:s})=>{const d=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!d)return u(),()=>{};const g=setInterval(()=>{d.closed&&(clearInterval(g),s())},1e3);return()=>clearInterval(g)},K=({children:r,launcher:u})=>{const s=e.useContext(v),[d,g]=e.useState({}),[R,p]=e.useState({}),[x,T]=e.useState({}),[I,w]=e.useState({}),y=e.useRef(x);y.current=x;const b=e.useRef(null),k=e.useRef({}),P=e.useRef({}),C=e.useCallback(t=>{const n=P.current[t];if(!n)return;const o=f=>f[t]?{...f,[t]:{...f[t],name:n}}:f;g(o),w(o)},[]),O=e.useCallback((t,n)=>{const o=F(t==null?void 0:t.authUrl);o&&(w(f=>f[o]?f:{...f,[o]:{origin:o,reason:n,name:null}}),C(o))},[C]),z=e.useCallback((t,{v2:n=!1}={})=>{const o=F(t==null?void 0:t.authUrl);o&&(n&&y.current[o]||(g(f=>f[o]?f:{...f,[o]:{origin:o,authUrl:t.authUrl,silentUrl:t.silentUrl||null,status:n&&t.silentUrl?"silent":"interactive",v2:n,name:null,popupBlocked:!1}}),C(o)))},[C]),S=e.useCallback(t=>{g(n=>{if(!n[t])return n;const o={...n};return delete o[t],o})},[]),i=e.useCallback(t=>{t&&(S(t),p(n=>({...n,[t]:(n[t]||0)+1})))},[S]),l=e.useCallback(t=>{g(n=>!n[t]||n[t].status==="interactive"?n:{...n,[t]:{...n[t],status:"interactive"}})},[]),c=e.useCallback(t=>{S(t.origin)},[S]),a=e.useCallback(t=>{const n=F(t==null?void 0:t.authUrl);if(n&&D(n,window.location.origin)){O(t,"cross-site-legacy");return}z(t)},[z,O]);e.useEffect(()=>{if(!s)return;const t=[s.addListener("*","init",n=>{n!=null&&n.origin&&n.name&&(P.current[n.origin]=n.name,C(n.origin))}),s.addListener("*","authorize",a),s.addListener("*","login",a),s.addListener("*","credential-request",n=>z(n,{v2:!0})),s.addListener("*","authorized",n=>{n!=null&&n.accessToken||i(F(n==null?void 0:n.authUrl))})];return()=>t.forEach(n=>n())},[s,a,z,i,C]),e.useEffect(()=>{const t=n=>{const o=n==null?void 0:n.data;if(!o||o.type!=="authorized"&&o.type!=="authorize-failed")return;const f=F(o.authUrl);if(!(!f||n.origin!==f)){if(o.type==="authorized"&&o.accessToken){T(M=>({...M,[f]:{accessToken:o.accessToken}})),S(f);return}o.type==="authorize-failed"&&l(f)}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},[S,l]),e.useEffect(()=>{const t=k.current;Object.values(d).forEach(n=>{n.status==="silent"&&!t[n.origin]&&(t[n.origin]=setTimeout(()=>{delete t[n.origin],l(n.origin)},ee))}),Object.keys(t).forEach(n=>{(!d[n]||d[n].status!=="silent")&&(clearTimeout(t[n]),delete t[n])})},[d,l]),e.useEffect(()=>()=>{b.current&&b.current(),Object.values(k.current).forEach(clearTimeout)},[]);const h=e.useCallback(t=>{b.current&&(b.current(),b.current=null);const n=u||H;b.current=n(t,{onBlocked:()=>{g(o=>o[t.origin]?{...o,[t.origin]:{...o[t.origin],popupBlocked:!0}}:o)},onClosed:()=>{if(t.v2){g(o=>!o[t.origin]||!o[t.origin].silentUrl?o:{...o,[t.origin]:{...o[t.origin],status:"silent"}});return}i(t.origin)}})},[u,i]),m={pending:Object.values(d).filter(t=>t.status==="interactive"),authorize:h,dismiss:c,originEpochs:R,credentials:x,unsupported:I},U=Object.values(d).filter(t=>t.status==="silent"&&t.silentUrl);return e.createElement(Z.Provider,{value:m},r,U.map(t=>e.createElement("iframe",{key:`mm-silent-auth-${t.origin}`,src:t.silentUrl,title:`MachineMetrics silent authorization (${t.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};K.propTypes={children:E.node,launcher:E.func};const V=()=>e.useContext(Z),G=r=>{const{originEpochs:u}=e.useContext(Z),s=F(r);return s&&u[s]||0},J=r=>{const{credentials:u}=e.useContext(Z),s=F(r);return s&&u[s]||null},Q=r=>{const{unsupported:u}=e.useContext(Z),s=F(r);return s&&u[s]||null},X=({children:r,authorizationLauncher:u})=>{const[s,d]=e.useState({}),[g,R]=e.useState({}),p=e.useRef({}),x=e.useRef({}),T=e.useRef({}),I=e.useCallback((i,l)=>{var c;return!!((c=T.current[i])!=null&&c.has(l))},[]),w=e.useCallback((i,l,c)=>{d(a=>({...a,[i]:{origin:l,frameRef:c}}))},[]),y=e.useCallback(i=>{d(l=>{const c={...l};return delete c[i],c}),R(l=>{const c={...l};return delete c[i],c}),delete T.current[i]},[]);e.useEffect(()=>{const i=(c=[],a)=>{c.forEach(h=>h(a))},l=c=>{var U,t,n,o,f;if(!((U=c==null?void 0:c.data)!=null&&U.instanceId))return;const{instanceId:a,type:h}=c.data,m=p.current;if(h==="response"){const{callbackId:M,response:$,error:A}=c.data,L=x.current[M];L&&(L($,A),delete x.current[M]);return}if(h==="mark-registered"){const{requestType:M}=c.data;T.current[a]||(T.current[a]=new Set),T.current[a].add(M);return}h==="ready"&&R(M=>({...M,[a]:!0})),i((t=m[a])==null?void 0:t[h],c.data),i((n=m["*"])==null?void 0:n[h],c.data),i((o=m[a])==null?void 0:o["*"],c.data),i((f=m["*"])==null?void 0:f["*"],c.data)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[s]);const b=e.useCallback((i,l,c)=>{var h,m;const a=s[i];if(!a){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}(m=(h=a==null?void 0:a.frameRef)==null?void 0:h.current)!=null&&m.contentWindow&&a.frameRef.current.contentWindow.postMessage({type:l,...c,instanceId:i},a.origin)},[s]),k=e.useRef(0),P=e.useCallback(()=>`${Date.now()}-${++k.current}`,[]),C=e.useCallback(async(i,l,c={})=>new Promise((a,h)=>{const m=P(),U=(t,n)=>{delete x.current[m],n?h(n):a(t)};x.current[m]=U,b(i,"request",{callbackId:m,requestType:l,data:c})}),[b]),O=e.useCallback((i,l)=>{b(i,"load-options",l)},[b]),z=e.useCallback((i,l)=>{b(i,"validate",l)},[b]),S=e.useMemo(()=>({registerFrame:w,unregisterFrame:y,sendMessage:b,sendRequest:C,loadOptions:O,validate:z,hasHandler:I,readyFrames:g,addListener:(i,l,c)=>(p.current[i]||(p.current[i]={}),p.current[i][l]||(p.current[i][l]=[]),p.current[i][l].push(c),()=>{p.current[i][l]=p.current[i][l].filter(a=>a!==c)})}),[w,y,b,C,O,z,I,g]);return e.createElement(v.Provider,{value:S},e.createElement(K,{launcher:u},r))},q=(r,u,s)=>{const d=e.useContext(v);if(d===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const g=e.useRef(s);e.useEffect(()=>{g.current=s},[s]),e.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const R=u.substring(4);return d.readyFrames[r]&&d.sendMessage(r,"subscribe-key",{keyChord:R}),d.addListener(r,`key-pressed:${R}`,(...x)=>g.current(...x))}return d.addListener(r,u,(...R)=>g.current(...R))}},[d,r,u])};X.propTypes={children:E.node.isRequired,authorizationLauncher:E.func};const B=j.default||j,te=1e4,ne="credentials",_=(r,u)=>`${r}/mm-embedded-interop?id=${u}&origin=${window.location.origin}&caps=${ne}`,re=B.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),E=require("prop-types"),j=require("styled-components"),B=e.createContext(null),Z=e.createContext({pending:[],authorize:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ee=2e4,F=r=>{try{return new URL(r).origin}catch{return null}},W=r=>{if(!r||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(r)||r.includes(":"))return r;const c=r.split(".");return c.length<=2?r:c.slice(-2).join(".")},D=(r,c)=>{try{const i=new URL(r),d=new URL(c);return i.protocol!==d.protocol?!0:W(i.hostname)!==W(d.hostname)}catch{return!0}},H=(r,{onBlocked:c,onClosed:i})=>{const d=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!d)return c(),()=>{};const m=setInterval(()=>{d.closed&&(clearInterval(m),i())},1e3);return()=>clearInterval(m)},K=({children:r,launcher:c})=>{const i=e.useContext(B),[d,m]=e.useState({}),[R,p]=e.useState({}),[x,T]=e.useState({}),[P,k]=e.useState({}),S=e.useRef(x);S.current=x;const b=e.useRef(null),C=e.useRef({}),I=e.useRef({}),w=e.useCallback(t=>{const n=I.current[t];if(!n)return;const s=f=>!f[t]||f[t].clientId?f:{...f,[t]:{...f[t],clientId:n}};m(s),k(s)},[]),O=e.useCallback((t,n)=>{const s=F(t==null?void 0:t.authUrl);s&&(k(f=>f[s]?f:{...f,[s]:{origin:s,reason:n,clientId:I.current[s]||null}}),w(s))},[w]),z=e.useCallback((t,{v2:n=!1}={})=>{const s=F(t==null?void 0:t.authUrl);s&&(n&&S.current[s]||(m(f=>f[s]?f:{...f,[s]:{origin:s,authUrl:t.authUrl,silentUrl:t.silentUrl||null,status:n&&t.silentUrl?"silent":"interactive",v2:n,clientId:t.clientId||I.current[s]||null,popupBlocked:!1}}),w(s)))},[w]),y=e.useCallback(t=>{m(n=>{if(!n[t])return n;const s={...n};return delete s[t],s})},[]),o=e.useCallback(t=>{t&&(y(t),p(n=>({...n,[t]:(n[t]||0)+1})))},[y]),l=e.useCallback(t=>{m(n=>!n[t]||n[t].status==="interactive"?n:{...n,[t]:{...n[t],status:"interactive"}})},[]),u=e.useCallback(t=>{y(t.origin)},[y]),a=e.useCallback(t=>{const n=F(t==null?void 0:t.authUrl);if(n&&D(n,window.location.origin)){O(t,"cross-site-legacy");return}z(t)},[z,O]);e.useEffect(()=>{if(!i)return;const t=[i.addListener("*","init",n=>{n!=null&&n.origin&&n.clientId&&(I.current[n.origin]=n.clientId,w(n.origin))}),i.addListener("*","authorize",a),i.addListener("*","login",a),i.addListener("*","credential-request",n=>z(n,{v2:!0})),i.addListener("*","authorized",n=>{n!=null&&n.accessToken||o(F(n==null?void 0:n.authUrl))})];return()=>t.forEach(n=>n())},[i,a,z,o,w]),e.useEffect(()=>{const t=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const f=F(s.authUrl);if(!(!f||n.origin!==f)){if(s.type==="authorized"&&s.accessToken){T(M=>({...M,[f]:{accessToken:s.accessToken}})),y(f);return}s.type==="authorize-failed"&&l(f)}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},[y,l]),e.useEffect(()=>{const t=C.current;Object.values(d).forEach(n=>{n.status==="silent"&&!t[n.origin]&&(t[n.origin]=setTimeout(()=>{delete t[n.origin],l(n.origin)},ee))}),Object.keys(t).forEach(n=>{(!d[n]||d[n].status!=="silent")&&(clearTimeout(t[n]),delete t[n])})},[d,l]),e.useEffect(()=>()=>{b.current&&b.current(),Object.values(C.current).forEach(clearTimeout)},[]);const h=e.useCallback(t=>{b.current&&(b.current(),b.current=null);const n=c||H;b.current=n(t,{onBlocked:()=>{m(s=>s[t.origin]?{...s,[t.origin]:{...s[t.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(t.v2){m(s=>!s[t.origin]||!s[t.origin].silentUrl?s:{...s,[t.origin]:{...s[t.origin],status:"silent"}});return}o(t.origin)}})},[c,o]),g={pending:Object.values(d).filter(t=>t.status==="interactive"),authorize:h,dismiss:u,originEpochs:R,credentials:x,unsupported:P},U=Object.values(d).filter(t=>t.status==="silent"&&t.silentUrl);return e.createElement(Z.Provider,{value:g},r,U.map(t=>e.createElement("iframe",{key:`mm-silent-auth-${t.origin}`,src:t.silentUrl,title:`MachineMetrics silent authorization (${t.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};K.propTypes={children:E.node,launcher:E.func};const V=()=>e.useContext(Z),G=r=>{const{originEpochs:c}=e.useContext(Z),i=F(r);return i&&c[i]||0},J=r=>{const{credentials:c}=e.useContext(Z),i=F(r);return i&&c[i]||null},Q=r=>{const{unsupported:c}=e.useContext(Z),i=F(r);return i&&c[i]||null},X=({children:r,authorizationLauncher:c})=>{const[i,d]=e.useState({}),[m,R]=e.useState({}),p=e.useRef({}),x=e.useRef({}),T=e.useRef({}),P=e.useCallback((o,l)=>{var u;return!!((u=T.current[o])!=null&&u.has(l))},[]),k=e.useCallback((o,l,u)=>{d(a=>({...a,[o]:{origin:l,frameRef:u}}))},[]),S=e.useCallback(o=>{d(l=>{const u={...l};return delete u[o],u}),R(l=>{const u={...l};return delete u[o],u}),delete T.current[o]},[]);e.useEffect(()=>{const o=(u=[],a)=>{u.forEach(h=>h(a))},l=u=>{var U,t,n,s,f;if(!((U=u==null?void 0:u.data)!=null&&U.instanceId))return;const{instanceId:a,type:h}=u.data,g=p.current;if(h==="response"){const{callbackId:M,response:$,error:A}=u.data,L=x.current[M];L&&(L($,A),delete x.current[M]);return}if(h==="mark-registered"){const{requestType:M}=u.data;T.current[a]||(T.current[a]=new Set),T.current[a].add(M);return}h==="ready"&&R(M=>({...M,[a]:!0})),o((t=g[a])==null?void 0:t[h],u.data),o((n=g["*"])==null?void 0:n[h],u.data),o((s=g[a])==null?void 0:s["*"],u.data),o((f=g["*"])==null?void 0:f["*"],u.data)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[i]);const b=e.useCallback((o,l,u)=>{var h,g;const a=i[o];if(!a){console.warn(`Origin not set for id: ${o}. Unable to send message.`);return}(g=(h=a==null?void 0:a.frameRef)==null?void 0:h.current)!=null&&g.contentWindow&&a.frameRef.current.contentWindow.postMessage({type:l,...u,instanceId:o},a.origin)},[i]),C=e.useRef(0),I=e.useCallback(()=>`${Date.now()}-${++C.current}`,[]),w=e.useCallback(async(o,l,u={})=>new Promise((a,h)=>{const g=I(),U=(t,n)=>{delete x.current[g],n?h(n):a(t)};x.current[g]=U,b(o,"request",{callbackId:g,requestType:l,data:u})}),[b]),O=e.useCallback((o,l)=>{b(o,"load-options",l)},[b]),z=e.useCallback((o,l)=>{b(o,"validate",l)},[b]),y=e.useMemo(()=>({registerFrame:k,unregisterFrame:S,sendMessage:b,sendRequest:w,loadOptions:O,validate:z,hasHandler:P,readyFrames:m,addListener:(o,l,u)=>(p.current[o]||(p.current[o]={}),p.current[o][l]||(p.current[o][l]=[]),p.current[o][l].push(u),()=>{p.current[o][l]=p.current[o][l].filter(a=>a!==u)})}),[k,S,b,w,O,z,P,m]);return e.createElement(B.Provider,{value:y},e.createElement(K,{launcher:c},r))},q=(r,c,i)=>{const d=e.useContext(B);if(d===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=e.useRef(i);e.useEffect(()=>{m.current=i},[i]),e.useEffect(()=>{if(!(!r||!c)){if(c.startsWith("key:")){const R=c.substring(4);return d.readyFrames[r]&&d.sendMessage(r,"subscribe-key",{keyChord:R}),d.addListener(r,`key-pressed:${R}`,(...x)=>m.current(...x))}return d.addListener(r,c,(...R)=>m.current(...R))}},[d,r,c])};X.propTypes={children:E.node.isRequired,authorizationLauncher:E.func};const v=j.default||j,te=1e4,ne="credentials",_=(r,c)=>`${r}/mm-embedded-interop?id=${c}&origin=${window.location.origin}&caps=${ne}`,re=v.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${r=>r.$isReady?"visible":"hidden"};
6
- `,se=B.div`
6
+ `,se=v.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
- `,Y=({entry:r})=>e.createElement(se,null,e.createElement("div",{style:{fontWeight:600}},r&&(r.name||r.origin)||"This app"," can’t be embedded here"),e.createElement("div",{style:{fontSize:12,color:"#888"}},"It needs an update to support embedding from a different domain."));Y.propTypes={entry:E.shape({origin:E.string,name:E.string,reason:E.string})};const oe=B.div`
19
+ `,Y=({entry:r})=>e.createElement(se,null,e.createElement("div",{style:{fontWeight:600}},r&&r.origin||"This app"," can’t be embedded here"),e.createElement("div",{style:{fontSize:12,color:"#888"}},"It needs an update to support embedding from a different domain."));Y.propTypes={entry:E.shape({origin:E.string,name:E.string,reason:E.string})};const ie=v.div`
20
20
  height: 100%;
21
21
  display: flex;
22
22
  flex: 1;
23
23
  align-items: center;
24
24
  justify-content: center;
25
- `,ie=()=>e.createElement(oe,null,e.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),N=({id:r,src:u,path:s,onInit:d=()=>{},onTimeout:g,context:R={},params:p={},renderUnsupported:x,renderTimeout:T,...I})=>{if("isConfiguring"in I)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in R)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in R)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const w=e.useRef(),[y,b]=e.useState(),[k,P]=e.useState(0),[C,O]=e.useState(!1),[z,S]=e.useState(!1),i=G(u),l=J(u),c=Q(u);e.useEffect(()=>{i>0&&P(0)},[i]);const{registerFrame:a,unregisterFrame:h,sendMessage:m}=e.useContext(v),[U,t]=e.useState(),n=e.useRef(),o=e.useRef(g),f=e.useRef(d),M=e.useRef(!1);e.useEffect(()=>{o.current=g},[g]),e.useEffect(()=>{f.current=d},[d]),e.useEffect(()=>{k&&m(r,"set-context",{context:{...R,colorMode:p.colorMode,isFullScreen:p.isFullScreen}})},[R,p.colorMode,p.isFullScreen,r,k]),e.useEffect(()=>{k&&(m(r,"set-params",{params:p}),m(r,"set-configuring",{isConfiguring:p.isConfiguring}))},[p,r,k]),e.useEffect(()=>{s!==void 0&&t((s==null?void 0:s.indexOf("/"))===0?s:`/${s}`)},[s]),e.useEffect(()=>{U===void 0||!k||m(r,"navigate",{path:U})},[U,k,r,m]),e.useEffect(()=>{if(!u||!r||!w)return;const L=new URL(u);return a(r,L.origin,w),f.current(),b(_(L.origin,r)),()=>{h(r)}},[u,r,a,w]),e.useEffect(()=>{if(!(!y||M.current))return n.current=setTimeout(()=>{O(!0),o.current&&o.current()},te),()=>clearTimeout(n.current)},[y]);const $=e.useCallback(()=>{M.current=!0,clearTimeout(n.current),O(!1)},[]);q(r,"init",$),q(r,"authorize",$),q(r,"login",$),q(r,"credential-request",()=>{$(),S(!0)}),e.useEffect(()=>{!z||!l||m(r,"credential",{accessToken:l.accessToken})},[z,l,r,m]),q(r,"ready",()=>{$(),P(L=>L+1)});const A=e.useRef();return q(r,"heartbeat",()=>{A.current&&clearTimeout(A.current),A.current=setTimeout(()=>{if(w.current){const L=new URL(u);w.current.src=_(L.origin,r)}},6e3)}),e.useEffect(()=>()=>{A.current&&clearTimeout(A.current)},[]),c?x?x(c):e.createElement(Y,{entry:c}):C&&!k?T?T():e.createElement(ie,null):e.createElement(re,{key:`auth-epoch-${i}`,$isReady:k,ref:w,src:y,...I})};N.propTypes={id:E.string,src:E.string.isRequired,path:E.string,onInit:E.func,onTimeout:E.func,renderUnsupported:E.func,renderTimeout:E.func};const ue=j.div`
25
+ `,oe=()=>e.createElement(ie,null,e.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),N=({id:r,src:c,path:i,onInit:d=()=>{},onTimeout:m,context:R={},params:p={},renderUnsupported:x,renderTimeout:T,...P})=>{if("isConfiguring"in P)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in R)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in R)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const k=e.useRef(),[S,b]=e.useState(),[C,I]=e.useState(0),[w,O]=e.useState(!1),[z,y]=e.useState(!1),o=G(c),l=J(c),u=Q(c);e.useEffect(()=>{o>0&&I(0)},[o]);const{registerFrame:a,unregisterFrame:h,sendMessage:g}=e.useContext(B),[U,t]=e.useState(),n=e.useRef(),s=e.useRef(m),f=e.useRef(d),M=e.useRef(!1);e.useEffect(()=>{s.current=m},[m]),e.useEffect(()=>{f.current=d},[d]),e.useEffect(()=>{C&&g(r,"set-context",{context:{...R,colorMode:p.colorMode,isFullScreen:p.isFullScreen}})},[R,p.colorMode,p.isFullScreen,r,C]),e.useEffect(()=>{C&&(g(r,"set-params",{params:p}),g(r,"set-configuring",{isConfiguring:p.isConfiguring}))},[p,r,C]),e.useEffect(()=>{i!==void 0&&t((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),e.useEffect(()=>{U===void 0||!C||g(r,"navigate",{path:U})},[U,C,r,g]),e.useEffect(()=>{if(!c||!r||!k)return;const L=new URL(c);return a(r,L.origin,k),f.current(),b(_(L.origin,r)),()=>{h(r)}},[c,r,a,k]),e.useEffect(()=>{if(!(!S||M.current))return n.current=setTimeout(()=>{O(!0),s.current&&s.current()},te),()=>clearTimeout(n.current)},[S]);const $=e.useCallback(()=>{M.current=!0,clearTimeout(n.current),O(!1)},[]);q(r,"init",$),q(r,"authorize",$),q(r,"login",$),q(r,"credential-request",()=>{$(),y(!0)}),e.useEffect(()=>{!z||!l||g(r,"credential",{accessToken:l.accessToken})},[z,l,r,g]),q(r,"ready",()=>{$(),I(L=>L+1)});const A=e.useRef();return q(r,"heartbeat",()=>{A.current&&clearTimeout(A.current),A.current=setTimeout(()=>{if(k.current){const L=new URL(c);k.current.src=_(L.origin,r)}},6e3)}),e.useEffect(()=>()=>{A.current&&clearTimeout(A.current)},[]),u?x?x(u):e.createElement(Y,{entry:u}):w&&!C?T?T():e.createElement(oe,null):e.createElement(re,{key:`auth-epoch-${o}`,$isReady:C,ref:k,src:S,...P})};N.propTypes={id:E.string,src:E.string.isRequired,path:E.string,onInit:E.func,onTimeout:E.func,renderUnsupported:E.func,renderTimeout:E.func};const ce=j.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
- `,ce=j.div`
33
+ `,ue=j.div`
34
34
  background: #fff;
35
35
  border-radius: 8px;
36
36
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
@@ -58,4 +58,4 @@
58
58
  color: #b3261e;
59
59
  font-size: 13px;
60
60
  margin: 4px 0 0;
61
- `,fe=()=>{const{pending:r,authorize:u}=V();return r.length===0?null:e.createElement(ue,null,e.createElement(ce,null,e.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),e.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."),r.map(s=>e.createElement("div",{key:s.origin},e.createElement(le,null,e.createElement("div",null,e.createElement("div",{style:{fontWeight:600}},s.name||s.origin),s.name&&e.createElement("div",{style:{fontSize:12,color:"#888"}},s.origin)),e.createElement(ae,{onClick:()=>u(s)},"Authorize")),s.popupBlocked&&e.createElement(de,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};exports.EmbeddedAuthorizationContext=Z;exports.EmbeddedAuthorizationPrompt=fe;exports.MMEmbeddableZone=N;exports.MMEmbeddableZoneContext=v;exports.MMEmbeddableZoneProvider=X;exports.isCrossSite=D;exports.popupAuthorizationLauncher=H;exports.useEmbeddedAuthorizations=V;exports.useEmbeddedCredential=J;exports.useEmbeddedEffect=q;exports.useEmbeddedOriginEpoch=G;exports.useEmbeddedUnsupported=Q;
61
+ `,fe=()=>{const{pending:r,authorize:c}=V();return r.length===0?null:e.createElement(ce,null,e.createElement(ue,null,e.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),e.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."),r.map(i=>e.createElement("div",{key:i.origin},e.createElement(le,null,e.createElement("div",null,e.createElement("div",{style:{fontWeight:600}},i.origin)),e.createElement(ae,{onClick:()=>c(i)},"Authorize")),i.popupBlocked&&e.createElement(de,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};exports.EmbeddedAuthorizationContext=Z;exports.EmbeddedAuthorizationPrompt=fe;exports.MMEmbeddableZone=N;exports.MMEmbeddableZoneContext=B;exports.MMEmbeddableZoneProvider=X;exports.isCrossSite=D;exports.popupAuthorizationLauncher=H;exports.useEmbeddedAuthorizations=V;exports.useEmbeddedCredential=J;exports.useEmbeddedEffect=q;exports.useEmbeddedOriginEpoch=G;exports.useEmbeddedUnsupported=Q;
@@ -1,6 +1,6 @@
1
- import f, { createContext as J, useContext as Z, useState as L, useRef as R, useCallback as b, useEffect as h, useMemo as Y } from "react";
1
+ import f, { createContext as J, useContext as _, useState as I, useRef as R, useCallback as b, useEffect as h, useMemo as Y } from "react";
2
2
  import x from "prop-types";
3
- import W from "styled-components";
3
+ import Z from "styled-components";
4
4
  const H = J(null), D = J({
5
5
  pending: [],
6
6
  authorize: () => {
@@ -10,7 +10,7 @@ const H = J(null), D = J({
10
10
  originEpochs: {},
11
11
  credentials: {},
12
12
  unsupported: {}
13
- }), N = 2e4, A = (n) => {
13
+ }), N = 2e4, j = (n) => {
14
14
  try {
15
15
  return new URL(n).origin;
16
16
  } catch {
@@ -23,12 +23,12 @@ const H = J(null), D = J({
23
23
  return o.length <= 2 ? n : o.slice(-2).join(".");
24
24
  }, ee = (n, o) => {
25
25
  try {
26
- const r = new URL(n), a = new URL(o);
27
- return r.protocol !== a.protocol ? !0 : V(r.hostname) !== V(a.hostname);
26
+ const i = new URL(n), a = new URL(o);
27
+ return i.protocol !== a.protocol ? !0 : V(i.hostname) !== V(a.hostname);
28
28
  } catch {
29
29
  return !0;
30
30
  }
31
- }, te = (n, { onBlocked: o, onClosed: r }) => {
31
+ }, te = (n, { onBlocked: o, onClosed: i }) => {
32
32
  const a = window.open(
33
33
  n.authUrl,
34
34
  "mm-embed-auth",
@@ -37,125 +37,128 @@ const H = J(null), D = J({
37
37
  if (!a)
38
38
  return o(), () => {
39
39
  };
40
- const g = setInterval(() => {
41
- a.closed && (clearInterval(g), r());
40
+ const m = setInterval(() => {
41
+ a.closed && (clearInterval(m), i());
42
42
  }, 1e3);
43
- return () => clearInterval(g);
43
+ return () => clearInterval(m);
44
44
  }, Q = ({ children: n, launcher: o }) => {
45
- const r = Z(H), [a, g] = L({}), [T, p] = L({}), [M, C] = L({}), [j, z] = L({}), I = R(M);
46
- I.current = M;
47
- const E = R(null), k = R({}), q = R({}), U = b((e) => {
48
- const t = q.current[e];
45
+ const i = _(H), [a, m] = I({}), [T, p] = I({}), [M, L] = I({}), [q, U] = I({}), S = R(M);
46
+ S.current = M;
47
+ const E = R(null), k = R({}), $ = R({}), y = b((e) => {
48
+ const t = $.current[e];
49
49
  if (!t)
50
50
  return;
51
- const s = (d) => d[e] ? { ...d, [e]: { ...d[e], name: t } } : d;
52
- g(s), z(s);
53
- }, []), P = b(
51
+ const r = (d) => !d[e] || d[e].clientId ? d : { ...d, [e]: { ...d[e], clientId: t } };
52
+ m(r), U(r);
53
+ }, []), A = b(
54
54
  (e, t) => {
55
- const s = A(e == null ? void 0 : e.authUrl);
56
- s && (z((d) => d[s] ? d : {
55
+ const r = j(e == null ? void 0 : e.authUrl);
56
+ r && (U((d) => d[r] ? d : {
57
57
  ...d,
58
- [s]: { origin: s, reason: t, name: null }
59
- }), U(s));
58
+ [r]: { origin: r, reason: t, clientId: $.current[r] || null }
59
+ }), y(r));
60
60
  },
61
- [U]
62
- ), O = b(
61
+ [y]
62
+ ), C = b(
63
63
  (e, { v2: t = !1 } = {}) => {
64
- const s = A(e == null ? void 0 : e.authUrl);
65
- s && (t && I.current[s] || (g((d) => d[s] ? d : {
64
+ const r = j(e == null ? void 0 : e.authUrl);
65
+ r && (t && S.current[r] || (m((d) => d[r] ? d : {
66
66
  ...d,
67
- [s]: {
68
- origin: s,
67
+ [r]: {
68
+ origin: r,
69
69
  authUrl: e.authUrl,
70
70
  silentUrl: e.silentUrl || null,
71
71
  // v2 entries try the invisible silent resolver first; legacy
72
72
  // signals go straight to the host's interactive UI.
73
73
  status: t && e.silentUrl ? "silent" : "interactive",
74
74
  v2: t,
75
- name: null,
75
+ // clientId from the request payload (v2) or the cached init (legacy);
76
+ // the host resolves the display name from it.
77
+ clientId: e.clientId || $.current[r] || null,
76
78
  popupBlocked: !1
77
79
  }
78
- }), U(s)));
80
+ }), y(r)));
79
81
  },
80
- [U]
81
- ), S = b((e) => {
82
- g((t) => {
82
+ [y]
83
+ ), O = b((e) => {
84
+ m((t) => {
83
85
  if (!t[e])
84
86
  return t;
85
- const s = { ...t };
86
- return delete s[e], s;
87
+ const r = { ...t };
88
+ return delete r[e], r;
87
89
  });
88
- }, []), i = b(
90
+ }, []), s = b(
89
91
  (e) => {
90
- e && (S(e), p((t) => ({
92
+ e && (O(e), p((t) => ({
91
93
  ...t,
92
94
  [e]: (t[e] || 0) + 1
93
95
  })));
94
96
  },
95
- [S]
97
+ [O]
96
98
  ), u = b((e) => {
97
- g((t) => !t[e] || t[e].status === "interactive" ? t : {
99
+ m((t) => !t[e] || t[e].status === "interactive" ? t : {
98
100
  ...t,
99
101
  [e]: { ...t[e], status: "interactive" }
100
102
  });
101
103
  }, []), c = b(
102
104
  (e) => {
103
- S(e.origin);
105
+ O(e.origin);
104
106
  },
105
- [S]
107
+ [O]
106
108
  ), l = b(
107
109
  (e) => {
108
- const t = A(e == null ? void 0 : e.authUrl);
110
+ const t = j(e == null ? void 0 : e.authUrl);
109
111
  if (t && ee(t, window.location.origin)) {
110
- P(e, "cross-site-legacy");
112
+ A(e, "cross-site-legacy");
111
113
  return;
112
114
  }
113
- O(e);
115
+ C(e);
114
116
  },
115
- [O, P]
117
+ [C, A]
116
118
  );
117
119
  h(() => {
118
- if (!r)
120
+ if (!i)
119
121
  return;
120
122
  const e = [
121
- // `init` carries the app's display name (v2 apps). Cache it by app origin
122
- // and enrich any entry already showing for that origin. `init` is posted
123
- // before the authorize/login/credential-request signals, so the name is
124
- // usually cached by the time an entry is created.
125
- r.addListener("*", "init", (t) => {
126
- t != null && t.origin && t.name && (q.current[t.origin] = t.name, U(t.origin));
123
+ // `init` carries the app's OAuth clientId. Cache it by app origin and
124
+ // attach it to any entry already showing. `init` is posted before the
125
+ // authorize/login/credential-request signals, so it is usually cached by
126
+ // the time an entry is created. The host resolves the display name from
127
+ // the clientId.
128
+ i.addListener("*", "init", (t) => {
129
+ t != null && t.origin && t.clientId && ($.current[t.origin] = t.clientId, y(t.origin));
127
130
  }),
128
- r.addListener("*", "authorize", l),
129
- r.addListener("*", "login", l),
130
- r.addListener(
131
+ i.addListener("*", "authorize", l),
132
+ i.addListener("*", "login", l),
133
+ i.addListener(
131
134
  "*",
132
135
  "credential-request",
133
- (t) => O(t, { v2: !0 })
136
+ (t) => C(t, { v2: !0 })
134
137
  ),
135
- r.addListener("*", "authorized", (t) => {
136
- t != null && t.accessToken || i(A(t == null ? void 0 : t.authUrl));
138
+ i.addListener("*", "authorized", (t) => {
139
+ t != null && t.accessToken || s(j(t == null ? void 0 : t.authUrl));
137
140
  })
138
141
  ];
139
142
  return () => e.forEach((t) => t());
140
- }, [r, l, O, i, U]), h(() => {
143
+ }, [i, l, C, s, y]), h(() => {
141
144
  const e = (t) => {
142
- const s = t == null ? void 0 : t.data;
143
- if (!s || s.type !== "authorized" && s.type !== "authorize-failed")
145
+ const r = t == null ? void 0 : t.data;
146
+ if (!r || r.type !== "authorized" && r.type !== "authorize-failed")
144
147
  return;
145
- const d = A(s.authUrl);
148
+ const d = j(r.authUrl);
146
149
  if (!(!d || t.origin !== d)) {
147
- if (s.type === "authorized" && s.accessToken) {
148
- C((y) => ({
149
- ...y,
150
- [d]: { accessToken: s.accessToken }
151
- })), S(d);
150
+ if (r.type === "authorized" && r.accessToken) {
151
+ L((z) => ({
152
+ ...z,
153
+ [d]: { accessToken: r.accessToken }
154
+ })), O(d);
152
155
  return;
153
156
  }
154
- s.type === "authorize-failed" && u(d);
157
+ r.type === "authorize-failed" && u(d);
155
158
  }
156
159
  };
157
160
  return window.addEventListener("message", e), () => window.removeEventListener("message", e);
158
- }, [S, u]), h(() => {
161
+ }, [O, u]), h(() => {
159
162
  const e = k.current;
160
163
  Object.values(a).forEach((t) => {
161
164
  t.status === "silent" && !e[t.origin] && (e[t.origin] = setTimeout(() => {
@@ -173,25 +176,25 @@ const H = J(null), D = J({
173
176
  const t = o || te;
174
177
  E.current = t(e, {
175
178
  onBlocked: () => {
176
- g((s) => s[e.origin] ? {
177
- ...s,
178
- [e.origin]: { ...s[e.origin], popupBlocked: !0 }
179
- } : s);
179
+ m((r) => r[e.origin] ? {
180
+ ...r,
181
+ [e.origin]: { ...r[e.origin], popupBlocked: !0 }
182
+ } : r);
180
183
  },
181
184
  onClosed: () => {
182
185
  if (e.v2) {
183
- g((s) => !s[e.origin] || !s[e.origin].silentUrl ? s : {
184
- ...s,
185
- [e.origin]: { ...s[e.origin], status: "silent" }
186
+ m((r) => !r[e.origin] || !r[e.origin].silentUrl ? r : {
187
+ ...r,
188
+ [e.origin]: { ...r[e.origin], status: "silent" }
186
189
  });
187
190
  return;
188
191
  }
189
- i(e.origin);
192
+ s(e.origin);
190
193
  }
191
194
  });
192
195
  },
193
- [o, i]
194
- ), m = {
196
+ [o, s]
197
+ ), g = {
195
198
  // Hosts render UI for interactive entries only; silent resolution is
196
199
  // invisible by design.
197
200
  pending: Object.values(a).filter(
@@ -201,11 +204,11 @@ const H = J(null), D = J({
201
204
  dismiss: c,
202
205
  originEpochs: T,
203
206
  credentials: M,
204
- unsupported: j
207
+ unsupported: q
205
208
  }, F = Object.values(a).filter(
206
209
  (e) => e.status === "silent" && e.silentUrl
207
210
  );
208
- return /* @__PURE__ */ f.createElement(D.Provider, { value: m }, n, F.map((e) => /* @__PURE__ */ f.createElement(
211
+ return /* @__PURE__ */ f.createElement(D.Provider, { value: g }, n, F.map((e) => /* @__PURE__ */ f.createElement(
209
212
  "iframe",
210
213
  {
211
214
  key: `mm-silent-auth-${e.origin}`,
@@ -221,135 +224,135 @@ Q.propTypes = {
221
224
  children: x.node,
222
225
  launcher: x.func
223
226
  };
224
- const ne = () => Z(D), re = (n) => {
225
- const { originEpochs: o } = Z(D), r = A(n);
226
- return r && o[r] || 0;
227
- }, se = (n) => {
228
- const { credentials: o } = Z(D), r = A(n);
229
- return r && o[r] || null;
227
+ const ne = () => _(D), re = (n) => {
228
+ const { originEpochs: o } = _(D), i = j(n);
229
+ return i && o[i] || 0;
230
230
  }, ie = (n) => {
231
- const { unsupported: o } = Z(D), r = A(n);
232
- return r && o[r] || null;
231
+ const { credentials: o } = _(D), i = j(n);
232
+ return i && o[i] || null;
233
+ }, se = (n) => {
234
+ const { unsupported: o } = _(D), i = j(n);
235
+ return i && o[i] || null;
233
236
  }, oe = ({ children: n, authorizationLauncher: o }) => {
234
- const [r, a] = L({}), [g, T] = L({}), p = R({}), M = R({}), C = R({}), j = b((i, u) => {
237
+ const [i, a] = I({}), [m, T] = I({}), p = R({}), M = R({}), L = R({}), q = b((s, u) => {
235
238
  var c;
236
- return !!((c = C.current[i]) != null && c.has(u));
237
- }, []), z = b((i, u, c) => {
238
- a((l) => ({ ...l, [i]: { origin: u, frameRef: c } }));
239
- }, []), I = b((i) => {
239
+ return !!((c = L.current[s]) != null && c.has(u));
240
+ }, []), U = b((s, u, c) => {
241
+ a((l) => ({ ...l, [s]: { origin: u, frameRef: c } }));
242
+ }, []), S = b((s) => {
240
243
  a((u) => {
241
244
  const c = { ...u };
242
- return delete c[i], c;
245
+ return delete c[s], c;
243
246
  }), T((u) => {
244
247
  const c = { ...u };
245
- return delete c[i], c;
246
- }), delete C.current[i];
248
+ return delete c[s], c;
249
+ }), delete L.current[s];
247
250
  }, []);
248
251
  h(() => {
249
- const i = (c = [], l) => {
252
+ const s = (c = [], l) => {
250
253
  c.forEach((w) => w(l));
251
254
  }, u = (c) => {
252
- var F, e, t, s, d;
255
+ var F, e, t, r, d;
253
256
  if (!((F = c == null ? void 0 : c.data) != null && F.instanceId))
254
257
  return;
255
- const { instanceId: l, type: w } = c.data, m = p.current;
258
+ const { instanceId: l, type: w } = c.data, g = p.current;
256
259
  if (w === "response") {
257
- const { callbackId: y, response: B, error: v } = c.data, $ = M.current[y];
258
- $ && ($(B, v), delete M.current[y]);
260
+ const { callbackId: z, response: B, error: W } = c.data, P = M.current[z];
261
+ P && (P(B, W), delete M.current[z]);
259
262
  return;
260
263
  }
261
264
  if (w === "mark-registered") {
262
- const { requestType: y } = c.data;
263
- C.current[l] || (C.current[l] = /* @__PURE__ */ new Set()), C.current[l].add(y);
265
+ const { requestType: z } = c.data;
266
+ L.current[l] || (L.current[l] = /* @__PURE__ */ new Set()), L.current[l].add(z);
264
267
  return;
265
268
  }
266
- w === "ready" && T((y) => ({ ...y, [l]: !0 })), i((e = m[l]) == null ? void 0 : e[w], c.data), i((t = m["*"]) == null ? void 0 : t[w], c.data), i((s = m[l]) == null ? void 0 : s["*"], c.data), i((d = m["*"]) == null ? void 0 : d["*"], c.data);
269
+ w === "ready" && T((z) => ({ ...z, [l]: !0 })), s((e = g[l]) == null ? void 0 : e[w], c.data), s((t = g["*"]) == null ? void 0 : t[w], c.data), s((r = g[l]) == null ? void 0 : r["*"], c.data), s((d = g["*"]) == null ? void 0 : d["*"], c.data);
267
270
  };
268
271
  return window.addEventListener("message", u), () => {
269
272
  window.removeEventListener("message", u);
270
273
  };
271
- }, [r]);
274
+ }, [i]);
272
275
  const E = b(
273
- (i, u, c) => {
274
- var w, m;
275
- const l = r[i];
276
+ (s, u, c) => {
277
+ var w, g;
278
+ const l = i[s];
276
279
  if (!l) {
277
- console.warn(`Origin not set for id: ${i}. Unable to send message.`);
280
+ console.warn(`Origin not set for id: ${s}. Unable to send message.`);
278
281
  return;
279
282
  }
280
- (m = (w = l == null ? void 0 : l.frameRef) == null ? void 0 : w.current) != null && m.contentWindow && l.frameRef.current.contentWindow.postMessage(
281
- { type: u, ...c, instanceId: i },
283
+ (g = (w = l == null ? void 0 : l.frameRef) == null ? void 0 : w.current) != null && g.contentWindow && l.frameRef.current.contentWindow.postMessage(
284
+ { type: u, ...c, instanceId: s },
282
285
  l.origin
283
286
  );
284
287
  },
285
- [r]
286
- ), k = R(0), q = b(() => `${Date.now()}-${++k.current}`, []), U = b(
287
- async (i, u, c = {}) => new Promise((l, w) => {
288
- const m = q(), F = (e, t) => {
289
- delete M.current[m], t ? w(t) : l(e);
288
+ [i]
289
+ ), k = R(0), $ = b(() => `${Date.now()}-${++k.current}`, []), y = b(
290
+ async (s, u, c = {}) => new Promise((l, w) => {
291
+ const g = $(), F = (e, t) => {
292
+ delete M.current[g], t ? w(t) : l(e);
290
293
  };
291
- M.current[m] = F, E(i, "request", { callbackId: m, requestType: u, data: c });
294
+ M.current[g] = F, E(s, "request", { callbackId: g, requestType: u, data: c });
292
295
  }),
293
296
  [E]
294
- ), P = b(
295
- (i, u) => {
296
- E(i, "load-options", u);
297
+ ), A = b(
298
+ (s, u) => {
299
+ E(s, "load-options", u);
297
300
  },
298
301
  [E]
299
- ), O = b(
300
- (i, u) => {
301
- E(i, "validate", u);
302
+ ), C = b(
303
+ (s, u) => {
304
+ E(s, "validate", u);
302
305
  },
303
306
  [E]
304
- ), S = Y(
307
+ ), O = Y(
305
308
  () => ({
306
- registerFrame: z,
307
- unregisterFrame: I,
309
+ registerFrame: U,
310
+ unregisterFrame: S,
308
311
  sendMessage: E,
309
- sendRequest: U,
310
- loadOptions: P,
311
- validate: O,
312
- hasHandler: j,
313
- readyFrames: g,
314
- addListener: (i, u, c) => (p.current[i] || (p.current[i] = {}), p.current[i][u] || (p.current[i][u] = []), p.current[i][u].push(c), () => {
315
- p.current[i][u] = p.current[i][u].filter((l) => l !== c);
312
+ sendRequest: y,
313
+ loadOptions: A,
314
+ validate: C,
315
+ hasHandler: q,
316
+ readyFrames: m,
317
+ addListener: (s, u, c) => (p.current[s] || (p.current[s] = {}), p.current[s][u] || (p.current[s][u] = []), p.current[s][u].push(c), () => {
318
+ p.current[s][u] = p.current[s][u].filter((l) => l !== c);
316
319
  })
317
320
  }),
318
321
  [
319
- z,
320
- I,
321
- E,
322
322
  U,
323
- P,
324
- O,
325
- j,
326
- g
323
+ S,
324
+ E,
325
+ y,
326
+ A,
327
+ C,
328
+ q,
329
+ m
327
330
  ]
328
331
  );
329
- return /* @__PURE__ */ f.createElement(H.Provider, { value: S }, /* @__PURE__ */ f.createElement(Q, { launcher: o }, n));
330
- }, _ = (n, o, r) => {
331
- const a = Z(H);
332
+ return /* @__PURE__ */ f.createElement(H.Provider, { value: O }, /* @__PURE__ */ f.createElement(Q, { launcher: o }, n));
333
+ }, v = (n, o, i) => {
334
+ const a = _(H);
332
335
  if (a === null)
333
336
  throw new Error(
334
337
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
335
338
  );
336
- const g = R(r);
339
+ const m = R(i);
337
340
  h(() => {
338
- g.current = r;
339
- }, [r]), h(() => {
341
+ m.current = i;
342
+ }, [i]), h(() => {
340
343
  if (!(!n || !o)) {
341
344
  if (o.startsWith("key:")) {
342
345
  const T = o.substring(4);
343
346
  return a.readyFrames[n] && a.sendMessage(n, "subscribe-key", { keyChord: T }), a.addListener(
344
347
  n,
345
348
  `key-pressed:${T}`,
346
- (...M) => g.current(...M)
349
+ (...M) => m.current(...M)
347
350
  );
348
351
  }
349
352
  return a.addListener(
350
353
  n,
351
354
  o,
352
- (...T) => g.current(...T)
355
+ (...T) => m.current(...T)
353
356
  );
354
357
  }
355
358
  }, [a, n, o]);
@@ -364,7 +367,7 @@ oe.propTypes = {
364
367
  */
365
368
  authorizationLauncher: x.func
366
369
  };
367
- const K = W.default || W, ce = 1e4, ue = "credentials", G = (n, o) => `${n}/mm-embedded-interop?id=${o}&origin=${window.location.origin}&caps=${ue}`, le = K.iframe`
370
+ const K = Z.default || Z, ce = 1e4, ue = "credentials", G = (n, o) => `${n}/mm-embedded-interop?id=${o}&origin=${window.location.origin}&caps=${ue}`, le = K.iframe`
368
371
  width: 100%;
369
372
  height: 100%;
370
373
  border: none;
@@ -382,7 +385,7 @@ const K = W.default || W, ce = 1e4, ue = "credentials", G = (n, o) => `${n}/mm-e
382
385
  text-align: center;
383
386
  color: #555;
384
387
  font-size: 14px;
385
- `, X = ({ entry: n }) => /* @__PURE__ */ f.createElement(ae, null, /* @__PURE__ */ f.createElement("div", { style: { fontWeight: 600 } }, n && (n.name || n.origin) || "This app", " can’t be embedded here"), /* @__PURE__ */ f.createElement("div", { style: { fontSize: 12, color: "#888" } }, "It needs an update to support embedding from a different domain."));
388
+ `, X = ({ entry: n }) => /* @__PURE__ */ f.createElement(ae, null, /* @__PURE__ */ f.createElement("div", { style: { fontWeight: 600 } }, n && n.origin || "This app", " can’t be embedded here"), /* @__PURE__ */ f.createElement("div", { style: { fontSize: 12, color: "#888" } }, "It needs an update to support embedding from a different domain."));
386
389
  X.propTypes = {
387
390
  entry: x.shape({
388
391
  origin: x.string,
@@ -396,20 +399,20 @@ const de = K.div`
396
399
  flex: 1;
397
400
  align-items: center;
398
401
  justify-content: center;
399
- `, fe = () => /* @__PURE__ */ f.createElement(de, null, /* @__PURE__ */ f.createElement("div", { style: { textAlign: "center" } }, "This app couldn’t be loaded as an embeddable widget.")), me = ({
402
+ `, fe = () => /* @__PURE__ */ f.createElement(de, null, /* @__PURE__ */ f.createElement("div", { style: { textAlign: "center" } }, "This app couldn’t be loaded as an embeddable widget.")), ge = ({
400
403
  id: n,
401
404
  src: o,
402
- path: r,
405
+ path: i,
403
406
  onInit: a = () => {
404
407
  },
405
- onTimeout: g,
408
+ onTimeout: m,
406
409
  context: T = {},
407
410
  params: p = {},
408
411
  renderUnsupported: M,
409
- renderTimeout: C,
410
- ...j
412
+ renderTimeout: L,
413
+ ...q
411
414
  }) => {
412
- if ("isConfiguring" in j)
415
+ if ("isConfiguring" in q)
413
416
  return console.error(
414
417
  "isConfiguring must be passed via the params prop to MMEmbeddableZone"
415
418
  ), null;
@@ -421,19 +424,19 @@ const de = K.div`
421
424
  return console.error(
422
425
  "isFullScreen must be passed via the params prop to MMEmbeddableZone"
423
426
  ), null;
424
- const z = R(), [I, E] = L(), [k, q] = L(0), [U, P] = L(!1), [O, S] = L(!1), i = re(o), u = se(o), c = ie(o);
427
+ const U = R(), [S, E] = I(), [k, $] = I(0), [y, A] = I(!1), [C, O] = I(!1), s = re(o), u = ie(o), c = se(o);
425
428
  h(() => {
426
- i > 0 && q(0);
427
- }, [i]);
428
- const { registerFrame: l, unregisterFrame: w, sendMessage: m } = Z(
429
+ s > 0 && $(0);
430
+ }, [s]);
431
+ const { registerFrame: l, unregisterFrame: w, sendMessage: g } = _(
429
432
  H
430
- ), [F, e] = L(), t = R(), s = R(g), d = R(a), y = R(!1);
433
+ ), [F, e] = I(), t = R(), r = R(m), d = R(a), z = R(!1);
431
434
  h(() => {
432
- s.current = g;
433
- }, [g]), h(() => {
435
+ r.current = m;
436
+ }, [m]), h(() => {
434
437
  d.current = a;
435
438
  }, [a]), h(() => {
436
- k && m(n, "set-context", {
439
+ k && g(n, "set-context", {
437
440
  context: {
438
441
  ...T,
439
442
  // TODO: Remove this once everything is migrated to params
@@ -442,56 +445,56 @@ const de = K.div`
442
445
  }
443
446
  });
444
447
  }, [T, p.colorMode, p.isFullScreen, n, k]), h(() => {
445
- k && (m(n, "set-params", { params: p }), m(n, "set-configuring", { isConfiguring: p.isConfiguring }));
448
+ k && (g(n, "set-params", { params: p }), g(n, "set-configuring", { isConfiguring: p.isConfiguring }));
446
449
  }, [p, n, k]), h(() => {
447
- r !== void 0 && e((r == null ? void 0 : r.indexOf("/")) === 0 ? r : `/${r}`);
448
- }, [r]), h(() => {
449
- F === void 0 || !k || m(n, "navigate", { path: F });
450
- }, [F, k, n, m]), h(() => {
451
- if (!o || !n || !z)
450
+ i !== void 0 && e((i == null ? void 0 : i.indexOf("/")) === 0 ? i : `/${i}`);
451
+ }, [i]), h(() => {
452
+ F === void 0 || !k || g(n, "navigate", { path: F });
453
+ }, [F, k, n, g]), h(() => {
454
+ if (!o || !n || !U)
452
455
  return;
453
- const $ = new URL(o);
454
- return l(n, $.origin, z), d.current(), E(G($.origin, n)), () => {
456
+ const P = new URL(o);
457
+ return l(n, P.origin, U), d.current(), E(G(P.origin, n)), () => {
455
458
  w(n);
456
459
  };
457
- }, [o, n, l, z]), h(() => {
458
- if (!(!I || y.current))
460
+ }, [o, n, l, U]), h(() => {
461
+ if (!(!S || z.current))
459
462
  return t.current = setTimeout(() => {
460
- P(!0), s.current && s.current();
463
+ A(!0), r.current && r.current();
461
464
  }, ce), () => clearTimeout(t.current);
462
- }, [I]);
465
+ }, [S]);
463
466
  const B = b(() => {
464
- y.current = !0, clearTimeout(t.current), P(!1);
467
+ z.current = !0, clearTimeout(t.current), A(!1);
465
468
  }, []);
466
- _(n, "init", B), _(n, "authorize", B), _(n, "login", B), _(n, "credential-request", () => {
467
- B(), S(!0);
469
+ v(n, "init", B), v(n, "authorize", B), v(n, "login", B), v(n, "credential-request", () => {
470
+ B(), O(!0);
468
471
  }), h(() => {
469
- !O || !u || m(n, "credential", { accessToken: u.accessToken });
470
- }, [O, u, n, m]), _(n, "ready", () => {
471
- B(), q(($) => $ + 1);
472
+ !C || !u || g(n, "credential", { accessToken: u.accessToken });
473
+ }, [C, u, n, g]), v(n, "ready", () => {
474
+ B(), $((P) => P + 1);
472
475
  });
473
- const v = R();
474
- return _(n, "heartbeat", () => {
475
- v.current && clearTimeout(v.current), v.current = setTimeout(() => {
476
- if (z.current) {
477
- const $ = new URL(o);
478
- z.current.src = G($.origin, n);
476
+ const W = R();
477
+ return v(n, "heartbeat", () => {
478
+ W.current && clearTimeout(W.current), W.current = setTimeout(() => {
479
+ if (U.current) {
480
+ const P = new URL(o);
481
+ U.current.src = G(P.origin, n);
479
482
  }
480
483
  }, 6e3);
481
484
  }), h(() => () => {
482
- v.current && clearTimeout(v.current);
483
- }, []), c ? M ? M(c) : /* @__PURE__ */ f.createElement(X, { entry: c }) : U && !k ? C ? C() : /* @__PURE__ */ f.createElement(fe, null) : /* @__PURE__ */ f.createElement(
485
+ W.current && clearTimeout(W.current);
486
+ }, []), c ? M ? M(c) : /* @__PURE__ */ f.createElement(X, { entry: c }) : y && !k ? L ? L() : /* @__PURE__ */ f.createElement(fe, null) : /* @__PURE__ */ f.createElement(
484
487
  le,
485
488
  {
486
- key: `auth-epoch-${i}`,
489
+ key: `auth-epoch-${s}`,
487
490
  $isReady: k,
488
- ref: z,
489
- src: I,
490
- ...j
491
+ ref: U,
492
+ src: S,
493
+ ...q
491
494
  }
492
495
  );
493
496
  };
494
- me.propTypes = {
497
+ ge.propTypes = {
495
498
  id: x.string,
496
499
  src: x.string.isRequired,
497
500
  path: x.string,
@@ -510,7 +513,7 @@ me.propTypes = {
510
513
  */
511
514
  renderTimeout: x.func
512
515
  };
513
- const ge = W.div`
516
+ const me = Z.div`
514
517
  position: fixed;
515
518
  inset: 0;
516
519
  background: rgba(0, 0, 0, 0.45);
@@ -518,7 +521,7 @@ const ge = W.div`
518
521
  align-items: center;
519
522
  justify-content: center;
520
523
  z-index: 10000;
521
- `, pe = W.div`
524
+ `, pe = Z.div`
522
525
  background: #fff;
523
526
  border-radius: 8px;
524
527
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
@@ -527,13 +530,13 @@ const ge = W.div`
527
530
  max-width: calc(100vw - 32px);
528
531
  font-family: sans-serif;
529
532
  color: #222;
530
- `, he = W.div`
533
+ `, he = Z.div`
531
534
  display: flex;
532
535
  align-items: center;
533
536
  justify-content: space-between;
534
537
  padding: 10px 0;
535
538
  gap: 16px;
536
- `, be = W.button`
539
+ `, be = Z.button`
537
540
  background: #16a500;
538
541
  color: #fff;
539
542
  border: none;
@@ -542,25 +545,25 @@ const ge = W.div`
542
545
  font-size: 14px;
543
546
  cursor: pointer;
544
547
  white-space: nowrap;
545
- `, Ee = W.p`
548
+ `, Ee = Z.p`
546
549
  color: #b3261e;
547
550
  font-size: 13px;
548
551
  margin: 4px 0 0;
549
552
  `, Te = () => {
550
553
  const { pending: n, authorize: o } = ne();
551
- return n.length === 0 ? null : /* @__PURE__ */ f.createElement(ge, null, /* @__PURE__ */ f.createElement(pe, null, /* @__PURE__ */ f.createElement("h3", { style: { margin: "0 0 8px" } }, "Authorization Required"), /* @__PURE__ */ f.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((r) => /* @__PURE__ */ f.createElement("div", { key: r.origin }, /* @__PURE__ */ f.createElement(he, null, /* @__PURE__ */ f.createElement("div", null, /* @__PURE__ */ f.createElement("div", { style: { fontWeight: 600 } }, r.name || r.origin), r.name && /* @__PURE__ */ f.createElement("div", { style: { fontSize: 12, color: "#888" } }, r.origin)), /* @__PURE__ */ f.createElement(be, { onClick: () => o(r) }, "Authorize")), r.popupBlocked && /* @__PURE__ */ f.createElement(Ee, null, "The sign-in window was blocked. Allow popups for this site, then try again.")))));
554
+ return n.length === 0 ? null : /* @__PURE__ */ f.createElement(me, null, /* @__PURE__ */ f.createElement(pe, null, /* @__PURE__ */ f.createElement("h3", { style: { margin: "0 0 8px" } }, "Authorization Required"), /* @__PURE__ */ f.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) => /* @__PURE__ */ f.createElement("div", { key: i.origin }, /* @__PURE__ */ f.createElement(he, null, /* @__PURE__ */ f.createElement("div", null, /* @__PURE__ */ f.createElement("div", { style: { fontWeight: 600 } }, i.origin)), /* @__PURE__ */ f.createElement(be, { onClick: () => o(i) }, "Authorize")), i.popupBlocked && /* @__PURE__ */ f.createElement(Ee, null, "The sign-in window was blocked. Allow popups for this site, then try again.")))));
552
555
  };
553
556
  export {
554
557
  D as EmbeddedAuthorizationContext,
555
558
  Te as EmbeddedAuthorizationPrompt,
556
- me as MMEmbeddableZone,
559
+ ge as MMEmbeddableZone,
557
560
  H as MMEmbeddableZoneContext,
558
561
  oe as MMEmbeddableZoneProvider,
559
562
  ee as isCrossSite,
560
563
  te as popupAuthorizationLauncher,
561
564
  ne as useEmbeddedAuthorizations,
562
- se as useEmbeddedCredential,
563
- _ as useEmbeddedEffect,
565
+ ie as useEmbeddedCredential,
566
+ v as useEmbeddedEffect,
564
567
  re as useEmbeddedOriginEpoch,
565
- ie as useEmbeddedUnsupported
568
+ se as useEmbeddedUnsupported
566
569
  };
@@ -1,4 +1,4 @@
1
- (function(g,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(g=typeof globalThis!="undefined"?globalThis:g||self,e(g["mm-react-embeddable"]={},g.React,g.PropTypes,g.styled))})(this,(function(g,e,k,j){"use strict";const _=e.createContext(null),W=e.createContext({pending:[],authorize:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ne=2e4,A=r=>{try{return new URL(r).origin}catch{return null}},H=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(".")},P=(r,u)=>{try{const s=new URL(r),f=new URL(u);return s.protocol!==f.protocol?!0:H(s.hostname)!==H(f.hostname)}catch{return!0}},K=(r,{onBlocked:u,onClosed:s})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return u(),()=>{};const b=setInterval(()=>{f.closed&&(clearInterval(b),s())},1e3);return()=>clearInterval(b)},V=({children:r,launcher:u})=>{const s=e.useContext(_),[f,b]=e.useState({}),[w,h]=e.useState({}),[C,U]=e.useState({}),[Z,M]=e.useState({}),F=e.useRef(C);F.current=C;const E=e.useRef(null),x=e.useRef({}),$=e.useRef({}),z=e.useCallback(n=>{const t=$.current[n];if(!t)return;const i=a=>a[n]?{...a,[n]:{...a[n],name:t}}:a;b(i),M(i)},[]),y=e.useCallback((n,t)=>{const i=A(n==null?void 0:n.authUrl);i&&(M(a=>a[i]?a:{...a,[i]:{origin:i,reason:t,name:null}}),z(i))},[z]),T=e.useCallback((n,{v2:t=!1}={})=>{const i=A(n==null?void 0:n.authUrl);i&&(t&&F.current[i]||(b(a=>a[i]?a:{...a,[i]:{origin:i,authUrl:n.authUrl,silentUrl:n.silentUrl||null,status:t&&n.silentUrl?"silent":"interactive",v2:t,name:null,popupBlocked:!1}}),z(i)))},[z]),L=e.useCallback(n=>{b(t=>{if(!t[n])return t;const i={...t};return delete i[n],i})},[]),o=e.useCallback(n=>{n&&(L(n),h(t=>({...t,[n]:(t[n]||0)+1})))},[L]),l=e.useCallback(n=>{b(t=>!t[n]||t[n].status==="interactive"?t:{...t,[n]:{...t[n],status:"interactive"}})},[]),c=e.useCallback(n=>{L(n.origin)},[L]),d=e.useCallback(n=>{const t=A(n==null?void 0:n.authUrl);if(t&&P(t,window.location.origin)){y(n,"cross-site-legacy");return}T(n)},[T,y]);e.useEffect(()=>{if(!s)return;const n=[s.addListener("*","init",t=>{t!=null&&t.origin&&t.name&&($.current[t.origin]=t.name,z(t.origin))}),s.addListener("*","authorize",d),s.addListener("*","login",d),s.addListener("*","credential-request",t=>T(t,{v2:!0})),s.addListener("*","authorized",t=>{t!=null&&t.accessToken||o(A(t==null?void 0:t.authUrl))})];return()=>n.forEach(t=>t())},[s,d,T,o,z]),e.useEffect(()=>{const n=t=>{const i=t==null?void 0:t.data;if(!i||i.type!=="authorized"&&i.type!=="authorize-failed")return;const a=A(i.authUrl);if(!(!a||t.origin!==a)){if(i.type==="authorized"&&i.accessToken){U(S=>({...S,[a]:{accessToken:i.accessToken}})),L(a);return}i.type==="authorize-failed"&&l(a)}};return window.addEventListener("message",n),()=>window.removeEventListener("message",n)},[L,l]),e.useEffect(()=>{const n=x.current;Object.values(f).forEach(t=>{t.status==="silent"&&!n[t.origin]&&(n[t.origin]=setTimeout(()=>{delete n[t.origin],l(t.origin)},ne))}),Object.keys(n).forEach(t=>{(!f[t]||f[t].status!=="silent")&&(clearTimeout(n[t]),delete n[t])})},[f,l]),e.useEffect(()=>()=>{E.current&&E.current(),Object.values(x.current).forEach(clearTimeout)},[]);const p=e.useCallback(n=>{E.current&&(E.current(),E.current=null);const t=u||K;E.current=t(n,{onBlocked:()=>{b(i=>i[n.origin]?{...i,[n.origin]:{...i[n.origin],popupBlocked:!0}}:i)},onClosed:()=>{if(n.v2){b(i=>!i[n.origin]||!i[n.origin].silentUrl?i:{...i,[n.origin]:{...i[n.origin],status:"silent"}});return}o(n.origin)}})},[u,o]),m={pending:Object.values(f).filter(n=>n.status==="interactive"),authorize:p,dismiss:c,originEpochs:w,credentials:C,unsupported:Z},O=Object.values(f).filter(n=>n.status==="silent"&&n.silentUrl);return e.createElement(W.Provider,{value:m},r,O.map(n=>e.createElement("iframe",{key:`mm-silent-auth-${n.origin}`,src:n.silentUrl,title:`MachineMetrics silent authorization (${n.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};V.propTypes={children:k.node,launcher:k.func};const G=()=>e.useContext(W),J=r=>{const{originEpochs:u}=e.useContext(W),s=A(r);return s&&u[s]||0},Q=r=>{const{credentials:u}=e.useContext(W),s=A(r);return s&&u[s]||null},X=r=>{const{unsupported:u}=e.useContext(W),s=A(r);return s&&u[s]||null},Y=({children:r,authorizationLauncher:u})=>{const[s,f]=e.useState({}),[b,w]=e.useState({}),h=e.useRef({}),C=e.useRef({}),U=e.useRef({}),Z=e.useCallback((o,l)=>{var c;return!!((c=U.current[o])!=null&&c.has(l))},[]),M=e.useCallback((o,l,c)=>{f(d=>({...d,[o]:{origin:l,frameRef:c}}))},[]),F=e.useCallback(o=>{f(l=>{const c={...l};return delete c[o],c}),w(l=>{const c={...l};return delete c[o],c}),delete U.current[o]},[]);e.useEffect(()=>{const o=(c=[],d)=>{c.forEach(p=>p(d))},l=c=>{var O,n,t,i,a;if(!((O=c==null?void 0:c.data)!=null&&O.instanceId))return;const{instanceId:d,type:p}=c.data,m=h.current;if(p==="response"){const{callbackId:S,response:v,error:B}=c.data,I=C.current[S];I&&(I(v,B),delete C.current[S]);return}if(p==="mark-registered"){const{requestType:S}=c.data;U.current[d]||(U.current[d]=new Set),U.current[d].add(S);return}p==="ready"&&w(S=>({...S,[d]:!0})),o((n=m[d])==null?void 0:n[p],c.data),o((t=m["*"])==null?void 0:t[p],c.data),o((i=m[d])==null?void 0:i["*"],c.data),o((a=m["*"])==null?void 0:a["*"],c.data)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[s]);const E=e.useCallback((o,l,c)=>{var p,m;const d=s[o];if(!d){console.warn(`Origin not set for id: ${o}. Unable to send message.`);return}(m=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&m.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...c,instanceId:o},d.origin)},[s]),x=e.useRef(0),$=e.useCallback(()=>`${Date.now()}-${++x.current}`,[]),z=e.useCallback(async(o,l,c={})=>new Promise((d,p)=>{const m=$(),O=(n,t)=>{delete C.current[m],t?p(t):d(n)};C.current[m]=O,E(o,"request",{callbackId:m,requestType:l,data:c})}),[E]),y=e.useCallback((o,l)=>{E(o,"load-options",l)},[E]),T=e.useCallback((o,l)=>{E(o,"validate",l)},[E]),L=e.useMemo(()=>({registerFrame:M,unregisterFrame:F,sendMessage:E,sendRequest:z,loadOptions:y,validate:T,hasHandler:Z,readyFrames:b,addListener:(o,l,c)=>(h.current[o]||(h.current[o]={}),h.current[o][l]||(h.current[o][l]=[]),h.current[o][l].push(c),()=>{h.current[o][l]=h.current[o][l].filter(d=>d!==c)})}),[M,F,E,z,y,T,Z,b]);return e.createElement(_.Provider,{value:L},e.createElement(V,{launcher:u},r))},q=(r,u,s)=>{const f=e.useContext(_);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=e.useRef(s);e.useEffect(()=>{b.current=s},[s]),e.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const w=u.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:w}),f.addListener(r,`key-pressed:${w}`,(...C)=>b.current(...C))}return f.addListener(r,u,(...w)=>b.current(...w))}},[f,r,u])};Y.propTypes={children:k.node.isRequired,authorizationLauncher:k.func};const D=j.default||j,te=1e4,re="credentials",N=(r,u)=>`${r}/mm-embedded-interop?id=${u}&origin=${window.location.origin}&caps=${re}`,se=D.iframe`
1
+ (function(g,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(g=typeof globalThis!="undefined"?globalThis:g||self,e(g["mm-react-embeddable"]={},g.React,g.PropTypes,g.styled))})(this,(function(g,e,C,q){"use strict";const _=e.createContext(null),W=e.createContext({pending:[],authorize:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ne=2e4,A=r=>{try{return new URL(r).origin}catch{return null}},H=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(".")},P=(r,u)=>{try{const i=new URL(r),f=new URL(u);return i.protocol!==f.protocol?!0:H(i.hostname)!==H(f.hostname)}catch{return!0}},K=(r,{onBlocked:u,onClosed:i})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return u(),()=>{};const b=setInterval(()=>{f.closed&&(clearInterval(b),i())},1e3);return()=>clearInterval(b)},V=({children:r,launcher:u})=>{const i=e.useContext(_),[f,b]=e.useState({}),[k,h]=e.useState({}),[w,S]=e.useState({}),[$,M]=e.useState({}),O=e.useRef(w);O.current=w;const E=e.useRef(null),x=e.useRef({}),y=e.useRef({}),z=e.useCallback(n=>{const t=y.current[n];if(!t)return;const s=a=>!a[n]||a[n].clientId?a:{...a,[n]:{...a[n],clientId:t}};b(s),M(s)},[]),j=e.useCallback((n,t)=>{const s=A(n==null?void 0:n.authUrl);s&&(M(a=>a[s]?a:{...a,[s]:{origin:s,reason:t,clientId:y.current[s]||null}}),z(s))},[z]),I=e.useCallback((n,{v2:t=!1}={})=>{const s=A(n==null?void 0:n.authUrl);s&&(t&&O.current[s]||(b(a=>a[s]?a:{...a,[s]:{origin:s,authUrl:n.authUrl,silentUrl:n.silentUrl||null,status:t&&n.silentUrl?"silent":"interactive",v2:t,clientId:n.clientId||y.current[s]||null,popupBlocked:!1}}),z(s)))},[z]),T=e.useCallback(n=>{b(t=>{if(!t[n])return t;const s={...t};return delete s[n],s})},[]),o=e.useCallback(n=>{n&&(T(n),h(t=>({...t,[n]:(t[n]||0)+1})))},[T]),l=e.useCallback(n=>{b(t=>!t[n]||t[n].status==="interactive"?t:{...t,[n]:{...t[n],status:"interactive"}})},[]),c=e.useCallback(n=>{T(n.origin)},[T]),d=e.useCallback(n=>{const t=A(n==null?void 0:n.authUrl);if(t&&P(t,window.location.origin)){j(n,"cross-site-legacy");return}I(n)},[I,j]);e.useEffect(()=>{if(!i)return;const n=[i.addListener("*","init",t=>{t!=null&&t.origin&&t.clientId&&(y.current[t.origin]=t.clientId,z(t.origin))}),i.addListener("*","authorize",d),i.addListener("*","login",d),i.addListener("*","credential-request",t=>I(t,{v2:!0})),i.addListener("*","authorized",t=>{t!=null&&t.accessToken||o(A(t==null?void 0:t.authUrl))})];return()=>n.forEach(t=>t())},[i,d,I,o,z]),e.useEffect(()=>{const n=t=>{const s=t==null?void 0:t.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const a=A(s.authUrl);if(!(!a||t.origin!==a)){if(s.type==="authorized"&&s.accessToken){S(U=>({...U,[a]:{accessToken:s.accessToken}})),T(a);return}s.type==="authorize-failed"&&l(a)}};return window.addEventListener("message",n),()=>window.removeEventListener("message",n)},[T,l]),e.useEffect(()=>{const n=x.current;Object.values(f).forEach(t=>{t.status==="silent"&&!n[t.origin]&&(n[t.origin]=setTimeout(()=>{delete n[t.origin],l(t.origin)},ne))}),Object.keys(n).forEach(t=>{(!f[t]||f[t].status!=="silent")&&(clearTimeout(n[t]),delete n[t])})},[f,l]),e.useEffect(()=>()=>{E.current&&E.current(),Object.values(x.current).forEach(clearTimeout)},[]);const p=e.useCallback(n=>{E.current&&(E.current(),E.current=null);const t=u||K;E.current=t(n,{onBlocked:()=>{b(s=>s[n.origin]?{...s,[n.origin]:{...s[n.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(n.v2){b(s=>!s[n.origin]||!s[n.origin].silentUrl?s:{...s,[n.origin]:{...s[n.origin],status:"silent"}});return}o(n.origin)}})},[u,o]),m={pending:Object.values(f).filter(n=>n.status==="interactive"),authorize:p,dismiss:c,originEpochs:k,credentials:w,unsupported:$},L=Object.values(f).filter(n=>n.status==="silent"&&n.silentUrl);return e.createElement(W.Provider,{value:m},r,L.map(n=>e.createElement("iframe",{key:`mm-silent-auth-${n.origin}`,src:n.silentUrl,title:`MachineMetrics silent authorization (${n.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};V.propTypes={children:C.node,launcher:C.func};const G=()=>e.useContext(W),J=r=>{const{originEpochs:u}=e.useContext(W),i=A(r);return i&&u[i]||0},Q=r=>{const{credentials:u}=e.useContext(W),i=A(r);return i&&u[i]||null},X=r=>{const{unsupported:u}=e.useContext(W),i=A(r);return i&&u[i]||null},Y=({children:r,authorizationLauncher:u})=>{const[i,f]=e.useState({}),[b,k]=e.useState({}),h=e.useRef({}),w=e.useRef({}),S=e.useRef({}),$=e.useCallback((o,l)=>{var c;return!!((c=S.current[o])!=null&&c.has(l))},[]),M=e.useCallback((o,l,c)=>{f(d=>({...d,[o]:{origin:l,frameRef:c}}))},[]),O=e.useCallback(o=>{f(l=>{const c={...l};return delete c[o],c}),k(l=>{const c={...l};return delete c[o],c}),delete S.current[o]},[]);e.useEffect(()=>{const o=(c=[],d)=>{c.forEach(p=>p(d))},l=c=>{var L,n,t,s,a;if(!((L=c==null?void 0:c.data)!=null&&L.instanceId))return;const{instanceId:d,type:p}=c.data,m=h.current;if(p==="response"){const{callbackId:U,response:B,error:v}=c.data,F=w.current[U];F&&(F(B,v),delete w.current[U]);return}if(p==="mark-registered"){const{requestType:U}=c.data;S.current[d]||(S.current[d]=new Set),S.current[d].add(U);return}p==="ready"&&k(U=>({...U,[d]:!0})),o((n=m[d])==null?void 0:n[p],c.data),o((t=m["*"])==null?void 0:t[p],c.data),o((s=m[d])==null?void 0:s["*"],c.data),o((a=m["*"])==null?void 0:a["*"],c.data)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[i]);const E=e.useCallback((o,l,c)=>{var p,m;const d=i[o];if(!d){console.warn(`Origin not set for id: ${o}. Unable to send message.`);return}(m=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&m.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...c,instanceId:o},d.origin)},[i]),x=e.useRef(0),y=e.useCallback(()=>`${Date.now()}-${++x.current}`,[]),z=e.useCallback(async(o,l,c={})=>new Promise((d,p)=>{const m=y(),L=(n,t)=>{delete w.current[m],t?p(t):d(n)};w.current[m]=L,E(o,"request",{callbackId:m,requestType:l,data:c})}),[E]),j=e.useCallback((o,l)=>{E(o,"load-options",l)},[E]),I=e.useCallback((o,l)=>{E(o,"validate",l)},[E]),T=e.useMemo(()=>({registerFrame:M,unregisterFrame:O,sendMessage:E,sendRequest:z,loadOptions:j,validate:I,hasHandler:$,readyFrames:b,addListener:(o,l,c)=>(h.current[o]||(h.current[o]={}),h.current[o][l]||(h.current[o][l]=[]),h.current[o][l].push(c),()=>{h.current[o][l]=h.current[o][l].filter(d=>d!==c)})}),[M,O,E,z,j,I,$,b]);return e.createElement(_.Provider,{value:T},e.createElement(V,{launcher:u},r))},Z=(r,u,i)=>{const f=e.useContext(_);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=e.useRef(i);e.useEffect(()=>{b.current=i},[i]),e.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const k=u.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:k}),f.addListener(r,`key-pressed:${k}`,(...w)=>b.current(...w))}return f.addListener(r,u,(...k)=>b.current(...k))}},[f,r,u])};Y.propTypes={children:C.node.isRequired,authorizationLauncher:C.func};const D=q.default||q,te=1e4,re="credentials",N=(r,u)=>`${r}/mm-embedded-interop?id=${u}&origin=${window.location.origin}&caps=${re}`,se=D.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
@@ -16,13 +16,13 @@
16
16
  text-align: center;
17
17
  color: #555;
18
18
  font-size: 14px;
19
- `,R=({entry:r})=>e.createElement(ie,null,e.createElement("div",{style:{fontWeight:600}},r&&(r.name||r.origin)||"This app"," can’t be embedded here"),e.createElement("div",{style:{fontSize:12,color:"#888"}},"It needs an update to support embedding from a different domain."));R.propTypes={entry:k.shape({origin:k.string,name:k.string,reason:k.string})};const oe=D.div`
19
+ `,R=({entry:r})=>e.createElement(ie,null,e.createElement("div",{style:{fontWeight:600}},r&&r.origin||"This app"," can’t be embedded here"),e.createElement("div",{style:{fontSize:12,color:"#888"}},"It needs an update to support embedding from a different domain."));R.propTypes={entry:C.shape({origin:C.string,name:C.string,reason:C.string})};const oe=D.div`
20
20
  height: 100%;
21
21
  display: flex;
22
22
  flex: 1;
23
23
  align-items: center;
24
24
  justify-content: center;
25
- `,ue=()=>e.createElement(oe,null,e.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),ee=({id:r,src:u,path:s,onInit:f=()=>{},onTimeout:b,context:w={},params:h={},renderUnsupported:C,renderTimeout:U,...Z})=>{if("isConfiguring"in Z)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in w)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in w)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const M=e.useRef(),[F,E]=e.useState(),[x,$]=e.useState(0),[z,y]=e.useState(!1),[T,L]=e.useState(!1),o=J(u),l=Q(u),c=X(u);e.useEffect(()=>{o>0&&$(0)},[o]);const{registerFrame:d,unregisterFrame:p,sendMessage:m}=e.useContext(_),[O,n]=e.useState(),t=e.useRef(),i=e.useRef(b),a=e.useRef(f),S=e.useRef(!1);e.useEffect(()=>{i.current=b},[b]),e.useEffect(()=>{a.current=f},[f]),e.useEffect(()=>{x&&m(r,"set-context",{context:{...w,colorMode:h.colorMode,isFullScreen:h.isFullScreen}})},[w,h.colorMode,h.isFullScreen,r,x]),e.useEffect(()=>{x&&(m(r,"set-params",{params:h}),m(r,"set-configuring",{isConfiguring:h.isConfiguring}))},[h,r,x]),e.useEffect(()=>{s!==void 0&&n((s==null?void 0:s.indexOf("/"))===0?s:`/${s}`)},[s]),e.useEffect(()=>{O===void 0||!x||m(r,"navigate",{path:O})},[O,x,r,m]),e.useEffect(()=>{if(!u||!r||!M)return;const I=new URL(u);return d(r,I.origin,M),a.current(),E(N(I.origin,r)),()=>{p(r)}},[u,r,d,M]),e.useEffect(()=>{if(!(!F||S.current))return t.current=setTimeout(()=>{y(!0),i.current&&i.current()},te),()=>clearTimeout(t.current)},[F]);const v=e.useCallback(()=>{S.current=!0,clearTimeout(t.current),y(!1)},[]);q(r,"init",v),q(r,"authorize",v),q(r,"login",v),q(r,"credential-request",()=>{v(),L(!0)}),e.useEffect(()=>{!T||!l||m(r,"credential",{accessToken:l.accessToken})},[T,l,r,m]),q(r,"ready",()=>{v(),$(I=>I+1)});const B=e.useRef();return q(r,"heartbeat",()=>{B.current&&clearTimeout(B.current),B.current=setTimeout(()=>{if(M.current){const I=new URL(u);M.current.src=N(I.origin,r)}},6e3)}),e.useEffect(()=>()=>{B.current&&clearTimeout(B.current)},[]),c?C?C(c):e.createElement(R,{entry:c}):z&&!x?U?U():e.createElement(ue,null):e.createElement(se,{key:`auth-epoch-${o}`,$isReady:x,ref:M,src:F,...Z})};ee.propTypes={id:k.string,src:k.string.isRequired,path:k.string,onInit:k.func,onTimeout:k.func,renderUnsupported:k.func,renderTimeout:k.func};const ce=j.div`
25
+ `,ue=()=>e.createElement(oe,null,e.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),ee=({id:r,src:u,path:i,onInit:f=()=>{},onTimeout:b,context:k={},params:h={},renderUnsupported:w,renderTimeout:S,...$})=>{if("isConfiguring"in $)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in k)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in k)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const M=e.useRef(),[O,E]=e.useState(),[x,y]=e.useState(0),[z,j]=e.useState(!1),[I,T]=e.useState(!1),o=J(u),l=Q(u),c=X(u);e.useEffect(()=>{o>0&&y(0)},[o]);const{registerFrame:d,unregisterFrame:p,sendMessage:m}=e.useContext(_),[L,n]=e.useState(),t=e.useRef(),s=e.useRef(b),a=e.useRef(f),U=e.useRef(!1);e.useEffect(()=>{s.current=b},[b]),e.useEffect(()=>{a.current=f},[f]),e.useEffect(()=>{x&&m(r,"set-context",{context:{...k,colorMode:h.colorMode,isFullScreen:h.isFullScreen}})},[k,h.colorMode,h.isFullScreen,r,x]),e.useEffect(()=>{x&&(m(r,"set-params",{params:h}),m(r,"set-configuring",{isConfiguring:h.isConfiguring}))},[h,r,x]),e.useEffect(()=>{i!==void 0&&n((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),e.useEffect(()=>{L===void 0||!x||m(r,"navigate",{path:L})},[L,x,r,m]),e.useEffect(()=>{if(!u||!r||!M)return;const F=new URL(u);return d(r,F.origin,M),a.current(),E(N(F.origin,r)),()=>{p(r)}},[u,r,d,M]),e.useEffect(()=>{if(!(!O||U.current))return t.current=setTimeout(()=>{j(!0),s.current&&s.current()},te),()=>clearTimeout(t.current)},[O]);const B=e.useCallback(()=>{U.current=!0,clearTimeout(t.current),j(!1)},[]);Z(r,"init",B),Z(r,"authorize",B),Z(r,"login",B),Z(r,"credential-request",()=>{B(),T(!0)}),e.useEffect(()=>{!I||!l||m(r,"credential",{accessToken:l.accessToken})},[I,l,r,m]),Z(r,"ready",()=>{B(),y(F=>F+1)});const v=e.useRef();return Z(r,"heartbeat",()=>{v.current&&clearTimeout(v.current),v.current=setTimeout(()=>{if(M.current){const F=new URL(u);M.current.src=N(F.origin,r)}},6e3)}),e.useEffect(()=>()=>{v.current&&clearTimeout(v.current)},[]),c?w?w(c):e.createElement(R,{entry:c}):z&&!x?S?S():e.createElement(ue,null):e.createElement(se,{key:`auth-epoch-${o}`,$isReady:x,ref:M,src:O,...$})};ee.propTypes={id:C.string,src:C.string.isRequired,path:C.string,onInit:C.func,onTimeout:C.func,renderUnsupported:C.func,renderTimeout:C.func};const ce=q.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
- `,le=j.div`
33
+ `,le=q.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
- `,de=j.div`
42
+ `,de=q.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
- `,fe=j.button`
48
+ `,fe=q.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
- `,ae=j.p`
57
+ `,ae=q.p`
58
58
  color: #b3261e;
59
59
  font-size: 13px;
60
60
  margin: 4px 0 0;
61
- `,me=()=>{const{pending:r,authorize:u}=G();return r.length===0?null:e.createElement(ce,null,e.createElement(le,null,e.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),e.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."),r.map(s=>e.createElement("div",{key:s.origin},e.createElement(de,null,e.createElement("div",null,e.createElement("div",{style:{fontWeight:600}},s.name||s.origin),s.name&&e.createElement("div",{style:{fontSize:12,color:"#888"}},s.origin)),e.createElement(fe,{onClick:()=>u(s)},"Authorize")),s.popupBlocked&&e.createElement(ae,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};g.EmbeddedAuthorizationContext=W,g.EmbeddedAuthorizationPrompt=me,g.MMEmbeddableZone=ee,g.MMEmbeddableZoneContext=_,g.MMEmbeddableZoneProvider=Y,g.isCrossSite=P,g.popupAuthorizationLauncher=K,g.useEmbeddedAuthorizations=G,g.useEmbeddedCredential=Q,g.useEmbeddedEffect=q,g.useEmbeddedOriginEpoch=J,g.useEmbeddedUnsupported=X,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
61
+ `,me=()=>{const{pending:r,authorize:u}=G();return r.length===0?null:e.createElement(ce,null,e.createElement(le,null,e.createElement("h3",{style:{margin:"0 0 8px"}},"Authorization Required"),e.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."),r.map(i=>e.createElement("div",{key:i.origin},e.createElement(de,null,e.createElement("div",null,e.createElement("div",{style:{fontWeight:600}},i.origin)),e.createElement(fe,{onClick:()=>u(i)},"Authorize")),i.popupBlocked&&e.createElement(ae,null,"The sign-in window was blocked. Allow popups for this site, then try again.")))))};g.EmbeddedAuthorizationContext=W,g.EmbeddedAuthorizationPrompt=me,g.MMEmbeddableZone=ee,g.MMEmbeddableZoneContext=_,g.MMEmbeddableZoneProvider=Y,g.isCrossSite=P,g.popupAuthorizationLauncher=K,g.useEmbeddedAuthorizations=G,g.useEmbeddedCredential=Q,g.useEmbeddedEffect=Z,g.useEmbeddedOriginEpoch=J,g.useEmbeddedUnsupported=X,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "1.4.0-1",
3
+ "version": "1.4.0-2",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",