1import{j as e,M as B,g as N,m as T,r as i,C as U,W as q,w as D,B as O,V as y}from"./index-c7pORbp_.js";function _({label:s,size:t}){return e.jsx("span",{"aria-label":s,className:"inline-flex shrink-0 motion-safe:animate-pulse",role:"status",style:{height:t,width:t},children:e.jsx(B,{size:t})})}function V(s){const t=s instanceof Error?s.message.toUpperCase():String(s).toUpperCase();return t.includes("REQUEST_ID_CONFLICT")||t.includes("INVALID_REQUEST")||/REQUEST_(?:EXPIRED|CLAIMED|REVOKED)/.test(t)?"request-expired":t.includes("NOT_AUTHENTICATED")?"auth-expired":t.includes("NOT_AUTHORIZED")||t.includes("SUBSCRIPTION_REQUIRED")?"workspace-unavailable":"retryable"}const $=T("v4/extension:listTenants"),Q=T("v4/extension:createSession"),W={listTenants:()=>N().query($,{}),createSession:s=>N().mutation(Q,s)};function M(s){return/^[A-Za-z0-9_-]{16,128}$/.test(s)}function P(s,t){var h;const x=t.filter(u=>u.name.trim().toLowerCase()===s.name.trim().toLowerCase());if(x.length<2)return null;const c=(h=s.subdomain)==null?void 0:h.trim();return c&&x.filter(u=>{var r;return((r=u.subdomain)==null?void 0:r.trim().toLowerCase())===c.toLowerCase()}).length===1?`Workspace ${c}`:`Workspace â¦${s.tenantId.slice(-6)}`}function z({actions:s=W}){const t=new URLSearchParams(window.location.search).get("requestId")??"",x=M(t),[c,h]=i.useState([]),[u,r]=i.useState("list-loading"),[g,A]=i.useState(t),[j,d]=i.useState(""),[F,C]=i.useState(0),[E,b]=i.useState(null),k=i.useRef(t),f=i.useRef(0),m=i.useRef(!1);k.current=t,i.useEffect(()=>{document.title="Connect extension · BuildVision AI"},[]),i.useEffect(()=>{if(!x)return;let n=!0;return f.current+=1,m.current=!1,A(t),h([]),r("list-loading"),d(""),b(null),s.listTenants().then(a=>{n&&(h(a),r(a.length===0?"empty":"selecting"))}).catch(()=>{n&&(d("We couldn't load your workspaces. This connection request is still valid."),r("list-error"))}),()=>{n=!1,f.current+=1,m.current=!1}},[s,F,t,x]);const w=n=>{if(u==="session-loading"||m.current)return;const a=++f.current,I=t;m.current=!0,r("session-loading"),d(""),b(null),s.createSession({requestId:t,tenantId:n.tenantId}).then(()=>{f.current===a&&k.current===I&&r("done")}).catch(L=>{if(f.current!==a||k.current!==I)return;const R=V(L);R==="request-expired"?(d("This connection request expired or was already used. Start a new connection from the extension."),r("request-expired")):R==="auth-expired"?(d("Your BuildVision session expired. Sign out, then start the connection again."),r("auth-expired")):R==="workspace-unavailable"?(d("That workspace is no longer available for extension access. Refresh to choose another workspace."),r("workspace-error")):(d("We couldn't connect the extension. This request is still valid, so you can retry."),b(n),r("session-error"))}).finally(()=>{f.current===a&&(m.current=!1)})};if(i.useEffect(()=>{g===t&&u==="selecting"&&c.length===1&&w(c[0])},[t,u,g,c]),!x)return e.jsx(v,{children:e.jsx(l,{error:!0,children:"Invalid link. Open connection from the BuildVision AI extension."})});const o=g===t?u:"list-loading";return e.jsxs(v,{children:[o==="list-loading"&&e.jsx(U,{label:"Loading your workspacesâ¦",className:"w-full"}),o==="session-loading"&&e.jsxs(e.Fragment,{children:[e.jsx(_,{label:"Connecting extension",size:24}),e.jsx(l,{children:"Connecting extensionâ¦"})]}),o==="empty"&&e.jsxs(e.Fragment,{children:[e.jsx(l,{children:"No eligible workspace is available for this account."}),e.jsx(p,{onboarding:!0})]}),o==="selecting"&&e.jsxs("div",{className:"w-full",children:[e.jsx(l,{children:"Select the workspace to connect:"}),e.jsx("div",{className:"mt-3 flex flex-col gap-2",children:c.map(n=>{const a=P(n,c);return e.jsxs("button",{type:"button","aria-label":a?`${n.name} â ${a}`:n.name,onClick:()=>w(n),className:"min-h-10 border border-edge bg-bg px-3 py-2 text-left text-sm text-ink hover:border-primary hover:bg-raised",children:[e.jsx("span",{className:"block",children:n.name}),a&&e.jsx("span",{className:"block font-mono text-[10px] text-ink-faint",children:a})]},n.tenantId)})}),e.jsx("div",{className:"mt-4 flex justify-center",children:e.jsx(q,{})})]}),o==="done"&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"flex h-10 w-10 items-center justify-center border border-success/40 bg-success/10 text-success",children:e.jsx(D,{size:20})}),e.jsx("h2",{className:"text-base font-semibold text-ink",children:"Connected"}),e.jsx(l,{children:"You can close this window and return to the extension."})]}),o==="list-error"&&e.jsxs(e.Fragment,{children:[e.jsx(l,{error:!0,children:j}),e.jsx(S,{label:"Retry workspaces",onClick:()=>C(n=>n+1)}),e.jsx(p,{})]}),o==="session-error"&&E&&e.jsxs(e.Fragment,{children:[e.jsx(l,{error:!0,children:j}),e.jsx(S,{label:"Retry connection",onClick:()=>w(E)}),e.jsx(p,{})]}),o==="workspace-error"&&e.jsxs(e.Fragment,{children:[e.jsx(l,{error:!0,children:j}),e.jsx(S,{label:"Refresh workspaces",onClick:()=>C(n=>n+1)}),e.jsx(p,{onboarding:!0})]}),(o==="request-expired"||o==="auth-expired")&&e.jsxs(e.Fragment,{children:[e.jsx(l,{error:!0,children:j}),e.jsx(p,{})]})]})}function S({label:s,onClick:t}){return e.jsx("button",{type:"button",onClick:t,className:"min-h-9 border border-primary px-3 text-xs text-primary hover:bg-raised",children:s})}function p({onboarding:s=!1}){return e.jsxs("div",{className:"flex flex-wrap items-center justify-center gap-4",children:[s&&e.jsx("a",{href:y("/"),className:"text-xs text-primary underline",children:"Create workspace"}),e.jsx("a",{href:y("/workspaces"),className:"text-xs text-ink-muted underline",children:"View workspaces"}),e.jsx(q,{})]})}function l({children:s,error:t=!1}){return e.jsx("p",{role:t?"alert":void 0,className:`text-center text-xs leading-5 ${t?"text-error":"text-ink-muted"}`,children:s})}function v({children:s}){return e.jsx("main",{id:"main-content",className:"auth-cad-grid flex min-h-full items-center justify-center px-5",children:e.jsxs("section",{className:"relative z-10 flex w-full max-w-sm flex-col items-center gap-3 border border-edge bg-surface p-6",children:[e.jsx(O,{}),e.jsx("h1",{className:"mt-2 text-lg font-semibold text-ink",children:"Connect extension"}),s]})})}export{z as ExtAuthPage};
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.