1import{d as V,r as a,j as e,N as M}from"./vendor-react-C5PewRsl.js";import{H as G,Y as f,Z as p,bg as Y,bh as Z,i as L,ag as J,bi as X,bj as ee,bk as te,f as se,bl as k,bm as v,bn as ne,bo as ae,a1 as re,a2 as ie,C as oe,ae as ce}from"./main-DoxpiNgY.js";import{T as ue}from"./ThreadRadio-uN-gwsux.js";import{a as D,u as de}from"./vendor-misc-DVk-0spb.js";import{s as le,a as ge}from"./index-FFROJYLR.js";import{M as S,a as q,b as z,c as K}from"./McpPage-DMPeWiR9.js";import"./vendor-runtime-DG-GlVKR.js";import"./vendor-ui-BKWhI8D_.js";import"./vendor-icons-Nh3lIdcn.js";import"./vendor-analytics-aNB2-Vwh.js";import"./vendor-sentry-SKD9L_Jy.js";try{let s=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{},r=new s.Error().stack;r&&(s._sentryDebugIds=s._sentryDebugIds||{},s._sentryDebugIds[r]="8ca4ecd9-0878-4f60-881c-b8b8beeeec22",s._sentryDebugIdIdentifier="sentry-dbid-8ca4ecd9-0878-4f60-881c-b8b8beeeec22")}catch{}const fe=["claude.ai","claude.com"],me=s=>fe.includes(s)?k.CLAUDE:k.CHATGPT,pe=[v.ACTIVE,v.TRIAL,v.DELINQUENT,v.PAUSED],he=({memberships:s,selectedOrgId:r,onSelect:i,onContinue:u})=>e.jsx(z,{children:e.jsx(K,{title:"Choose an organization",description:"The connecting app will only access data from the organization you pick.",actions:e.jsx(L,{variant:"primary",size:"md",label:"Continue",className:"w-full flex-1",onClick:u,disabled:!r}),children:e.jsx("div",{role:"radiogroup","aria-label":"Organization",className:"space-y-2",children:s.map(h=>{const x=h.data(),l=x.organisationId,b=`mcp-org-${l}`,_=r===l;return e.jsxs("label",{htmlFor:b,className:"group/radio flex cursor-pointer items-center gap-3 rounded-lg border border-rb-gray-neutral bg-rb-white p-3 shadow-rb-shadow-soft-xs transition-colors hover:border-rb-gray-hovered hover:bg-rb-parchment-light has-focus-visible:ring-2 has-focus-visible:ring-rb-gray-hovered has-focus-visible:ring-offset-1",children:[e.jsx("input",{id:b,type:"radio",name:"mcp-organization",value:l,checked:_,onChange:()=>i(l),className:"sr-only"}),e.jsx(ue,{checked:_}),e.jsx("span",{className:"min-w-0 truncate text-sm leading-[1.4] font-medium text-rb-darkbtn",children:x.organisationName||l})]},l)})})})}),be=({stateId:s,resetStoredOrgSelection:r})=>{var F,H;const i=J(),{authUser:u,memberships:h}=G(),[x,l]=a.useState(!1),[b,_]=a.useState(null),A=a.useRef(!1),o=a.useMemo(()=>h.value??[],[h.value]),C=a.useMemo(()=>o.map(n=>n.data().organisationId),[o]),m=(F=u.value)==null?void 0:F.uid,g=D({queryKey:["mcp-org-picker-subscriptions",m,[...C].sort().join(",")],queryFn:async()=>{const n=await Promise.all(ne([...new Set(C)],10).map(t=>ae(re(oe.Subscription,ie("organisationId","in",t))))),c=new Set;return n.flatMap(t=>t.docs).forEach(ce(t=>{pe.includes(t.status)&&c.add(t.organisationId)})),c},enabled:o.length>1}),j=a.useMemo(()=>{const n=g.data??new Set,c=X(o,t=>t.data().createdAt,"asc");return[...c.filter(t=>n.has(t.data().organisationId)),...c.filter(t=>!n.has(t.data().organisationId))]},[o,g.data]),[d,O]=a.useState(()=>r?null:f.session.string(p.MCP_OAUTH_ORG_ID).get()??null),[y,I]=a.useState(()=>r?null:f.session.string(p.MCP_OAUTH_ORG_ID).get()??null);a.useEffect(()=>{r&&(f.session.string(p.MCP_OAUTH_ORG_ID).remove(),O(null),I(null))},[r]),a.useEffect(()=>{if(h.isLoading)return;const n=f.session.string(p.MCP_OAUTH_ORG_ID).get()??null,c=t=>!!t&&C.includes(t);if(o.length===1){const t=o[0].data().organisationId;f.session.string(p.MCP_OAUTH_ORG_ID).set(t),O(t),I(t);return}if(c(n)){O(n),I(t=>c(t)?t:n);return}n&&f.session.string(p.MCP_OAUTH_ORG_ID).remove(),O(t=>c(t)?t:null),I(t=>c(t)?t:null)},[h.isLoading,o,C]),a.useEffect(()=>{var c;if(y||o.length<=1||g.isPending)return;const n=(c=j[0])==null?void 0:c.data().organisationId;n&&I(n)},[y,o.length,g.isPending,j]);const T=n=>{I(n)},Q=()=>{y&&(f.session.string(p.MCP_OAUTH_ORG_ID).set(y),O(y))},{data:E,isLoading:B,isFetching:W}=D({queryKey:["flags",m,d],queryFn:()=>i.flags.get({organisationId:d??void 0}),enabled:!!m&&!!d}),w=B||W,P=E==null?void 0:E.flag.flags.onboardingComplete,N=de({mutationFn:i.mcpOauth.updateState,onSuccess:()=>l(!0),onError:n=>_(n instanceof Error?n.message:"Failed to update state")}),R=D({queryKey:["mcp-consent-details",s],queryFn:()=>i.mcpOauth.getConsentDetails({stateId:s}
1),enabled:!!s&&x}),U=(H=R.data)==null?void 0:H.redirectHost,$=!!U;return a.useEffect(()=>{U&&f.local.enum(ee.SIGNUP_SOURCE).set(me(U))},[U]),a.useEffect(()=>{if(!A.current&&!w&&s&&m&&d&&!b){A.current=!0;const n=P?"pending_consent":"pending_onboarding";N.mutate({stateId:s,userId:m,organisationId:d,status:n})}},[s,m,d,P,b,w]),h.isLoading?e.jsx(S,{}):o.length===0?e.jsx(M,{to:"/org"}):o.length>1&&!d?g.isPending?e.jsx(S,{}):e.jsx(he,{memberships:j,selectedOrgId:y,onSelect:T,onContinue:Q}):w||N.isPending?e.jsx(S,{}):b?e.jsx(q,{title:"Authorization Error",message:b}):x?!$&&!R.isError?e.jsx(S,{}):!P&&d?e.jsx(M,{to:te(d,s)}):e.jsx(M,{to:se({path:"/auth/mcp/consent",search:{state_id:s}})}):e.jsx(S,{})},ye=({email:s,onUseDifferentEmail:r,onContinueAsNew:i})=>e.jsx(z,{children:e.jsx(K,{title:"We don't recognize this email",description:e.jsxs(e.Fragment,{children:[e.jsx("strong",{children:s})," isn't linked to a Fyxer account. If you already use Fyxer, sign in with the email your Fyxer inbox is connected to â otherwise the connection will point at an empty new account."]}),actionsClassName:"sm:flex-col",actions:e.jsxs(e.Fragment,{children:[e.jsx(L,{variant:"primary",size:"md",label:"Use a different email",onClick:r,className:"w-full flex-1"}),e.jsx(L,{variant:"ghost",size:"md",label:"I'm new â create a Fyxer account",onClick:i,className:"w-full flex-1"})]})})}),Ee=()=>{const[s,r]=V(),i=s.get("state_id"),{authUser:u,appCheckToken:h,user:x,emailSignUp:l,freeTrial:b,memberships:_}=G(),[A,o]=a.useState(null),C=f.session.string(p.MCP_OAUTH_STATE_ID).get()??null,m=!!i&&C!==i,g=i||C;if(a.useEffect(()=>{i&&(m&&f.session.string(p.MCP_OAUTH_ORG_ID).remove(),f.session.string(p.MCP_OAUTH_STATE_ID).set(i))},[m,i]),a.useEffect(()=>{if(u.isLoading||u.value||!g)return;const y=`/auth/mcp?state_id=${g}`;s.get("return_path")!==y&&r(I=>{const T=new URLSearchParams(I);return T.set("return_path",y),T},{replace:!0})},[u.isLoading,u.value,g,s,r]),u.isLoading)return e.jsx(S,{});if(!g)return e.jsx(q,{title:"Invalid request",message:"Invalid OAuth request. Please start from your AI assistant."});if(!u.value)return e.jsx(Y,{authType:Z.LOG_IN,subtitleOverride:"Connect your existing Fyxer account â sign in with the email your Fyxer inbox is connected to"});if(h.isLoading||x.isLoading||l.isLoading||b.isLoading||_.isLoading)return e.jsx(S,{});if(!x.value||!l.value)return e.jsx(S,{});const d=u.value.uid;return(_.value??[]).length===0&&A!==d?e.jsx(ye,{email:u.value.email??"",onUseDifferentEmail:()=>void le(ge).catch(console.error),onContinueAsNew:()=>o(d)}):e.jsx(be,{stateId:g,resetStoredOrgSelection:m})};export{Ee as McpAuthPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.