1import{R as H,w as K,x as B,r as n,j as e,bC as Q,bL as V,cP as X,s as i}from"./index-bYGaXa-6.js";import{O as Z}from"./OrgHeaderLogo-DXW_BmkW.js";async function ee(){const{data:{session:l}}=await i.auth.getSession();return l||await new Promise(w=>{const f=i.auth.onAuthStateChange((c,x)=>{x&&(clearTimeout(u),f.data.subscription.unsubscribe(),w(x))}),u=setTimeout(()=>{f.data.subscription.unsubscribe(),w(null)},1500)})}function j(){try{localStorage.removeItem("post_auth_redirect")}catch{}}function oe(){const l=H(),[w]=K(),{recheck:f}=B(),[u,c]=n.useState("loading"),[x,p]=n.useState(""),[y,P]=n.useState(null),[g,A]=n.useState(""),[D,F]=n.useState(null),[q,U]=n.useState(null),[z,J]=n.useState(null),[S,R]=n.useState(""),[$,k]=n.useState(""),[h,C]=n.useState(""),[b,I]=n.useState(""),[N,M]=n.useState(""),[E,v]=n.useState(!1),m=w.get("id");n.useEffect(()=>{let t=!1;return(async()=>{if(!m){c("error"),p("Invalid invitation link.");return}const o=await ee();if(t)return;if(!o){try{localStorage.setItem("post_auth_redirect",`/accept-invitation?id=${m}`)}catch{}c("needs_auth");return}j();const{data:a}=await i.from("org_invitations").select("id, email, org_id, accepted_at").eq("id",m).maybeSingle();if(t)return;if(!a){k(o.user.email||""),c("wrong_email");return}R(a.email),k(o.user.email||""),P(a.org_id);const{data:r}=await i.from("organizations").select("name, logo_url, logo_dark_url, website").eq("id",a.org_id).maybeSingle();if(t)return;r!=null&&r.name&&A(r.name),r!=null&&r.logo_url&&F(r.logo_url),r!=null&&r.logo_dark_url&&U(r.logo_dark_url),r!=null&&r.website&&J(r.website);const s=(o.user.email||"").trim().toLowerCase(),_=(a.email||"").trim().toLowerCase();if(s&&_&&s!==_){c("wrong_email");return}const{data:G}=await i.from("org_members").select("org_id").eq("org_id",a.org_id).eq("user_id",o.user.id).maybeSingle();if(!t){if(G){await L(a.org_id);return}c("review")}})(),()=>{t=!0}},[m]);const L=async t=>{j();try{const s=JSON.parse(localStorage.getItem("recently_joined_org_ids")||"[]"),_=Array.from(new Set([...s,t]));localStorage.setItem("recently_joined_org_ids",JSON.stringify(_))}catch{localStorage.setItem("recently_joined_org_ids",JSON.stringify([t]))}const{data:{session:d}}=await i.auth.getSession();if(!d){l("/auth",{replace:!0});return}const{data:o}=await i.from("profiles").select("id, profile_type, name, surname, onboarding_completed, is_exempt_from_onboarding").eq("user_id",d.user.id),a=o==null?void 0:o.find(s=>s.profile_type==="personal"&&(s.onboarding_completed||s.is_exempt_from_onboarding));if(a){localStorage.setItem("active_profile_id",a.id),localStorage.setItem("active_org_id",t),localStorage.setItem("active_account_kind","org"),c("success"),await f(),l("/",{replace:!0});return}const r=o==null?void 0:o.find(s=>s.profile_type==="personal");r&&(C(r.name||""),I(r.surname||"")),c("complete_profile")},T=async(t,d)=>{c("accepting");const{data:o,error:a}=await i.rpc("accept_org_invitation",{_invitation_id:t}),r=!a&&o&&o.success,s=o==null?void 0:o.error;if(!r&&!(s==="already_accepted")){if(s==="email_mismatch"){c("wrong_email");return}c("error"),p(s||(a==null?void 0:a.message)||"Could not accept the invitation.");return}await L(d)},W=async()=>{!m||!y||await T(m,y)},Y=()=>{j(),l("/",{replace:!0})},O=async()=>{if(!y)return;if(!h.trim()||!b.trim()){p("Please enter your name and surname.");return}v(!0),p("");const{data:{session:t}}=await i.auth.getSession();if(!t){v(!1),l("/auth",{replace:!0});return}const{data:d}=await i.from("profiles").select("id").eq("user_id",t.user.id).eq("profile_type","personal").maybeSingle();let o=(d==null?void 0:d.id)??null;if(o){const{error:a}=await i.from("profiles").update({name:h.trim(),surname:b.trim(),headline:N.trim()}).eq("id",o);if(a){v(!1),p(a.message);return}}else{const{data:a,error:r}=await i.from("profiles").insert({user_id:t.user.id,profile_type:"personal",name:h.trim(),surname:b.trim(),headline:N.trim(),is_active:!0}).select("id").single();if(r||!a){v(!1),p((r==null?void 0:r.message)||"Could not create profile.");return}o=a.id}j(),localStorage.setItem("active_org_id",y),localStorage.removeItem("active_profile_id"),localStorage.setItem("active_account_kind","org"),c("success"),await f(),l("/",{replace:!0})};return u==="loading"||u==="accepting"||u==="success"?e.jsx(Q,{}):e.jsxs("div",{className:"min-h-screen flex flex-col items-center justify-center bg-background px-6",children:[e.jsx("div",{className:"mb-5 flex items-start justify-center gap-0 pb-[20px]",children:e.jsx(Z,{logoUrl:D,logoDarkUrl:q,website:z,heightPx:44,alt:g||"Organization logo"})}),u==="complete_profile"?e.jsxs("div",{className:"w-full max-w-[460px] rounded-[12px] p-7",style:{boxShadow:"0 0 0 1px hsl(var(--border))"},children:[e.jsxs("h1",{className:"text-xl font-bold text-foreground mb-2 text-center",children:["Welcome to ",g||"the team"," ð"]}),e.jsx("p",{className:"text-sm text-muted-foreground mb-6 text-center",children:"Tell us who you are so your teammates know who joined."}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium text-foreground mb-1.5 block",children:"Name"}),e.jsx("input",{type:"text",value:h,onChange:t=>C(t.target.value),placeholder:"John",className:"w-full h-10 px-3.5 rounded-[6px] text-sm input-ring bg-transparent text-foreground placeholder:text-muted-foreground focus:outline-none",autoFocus:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium text-foreground mb-1.5 block",children:"Surname"}),e.jsx("input",{type:"text",value:b,onChange:t=>I(t.target.value),placeholder:"Doe",className:"w-full h-10 px-3.5 rounded-[6px] text-sm input-ring bg-transparent text-foreground placeholder:text-muted-foreground focus:outline-none"})]})]}),e.jsxs("div",{className:"mt-3",children:[e.jsx("label",{className:"text-sm font-medium text-foreground mb-1.5 block",children:"Role"}),e.jsx("input",{type:"text",value:N,onChange:t=>M(t.target.value),placeholder:"e.g. Founder, CEO, Designerâ¦",className:"w-full h-10 px-3.5 rounded-[6px] text-sm input-ring bg-transparent text-foreground placeholder:text-muted-foreground focus:outline-none",onKeyDown:t=>{t.key==="Enter"&&O()}})]}),x&&e.jsx("p",{className:"text-xs text-destructive mt-3 text-center",children:x}),e.jsx("button",{onClick:O,disabled:E||!h.trim()||!b.trim(),className:"w-full mt-6 h-8 rounded-[6px] bg-primary text-primary-foreground px-4 py-1.5 text-sm font-semibold disabled:opacity-50 cursor-pointer",children:E?"Enteringâ¦":`Enter ${g||"the workspace"}`})]}):e.jsxs("div",{className:"w-full max-w-[420px] rounded-[12px] p-7 text-center",style:{boxShadow:"0 0 0 1px hsl(var(--border))"},children:[u==="needs_auth"&&e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"text-xl font-bold text-foreground mb-2",children:"Sign in to accept"}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-6",children:["You've been invited to join",g?e.jsxs(e.Fragment,{children:[" ",e.jsx("strong",{className:"text-foreground",children:g})]}):" an organization"," on Linkers. Sign in with the email the invitation was sent to."]}),e.jsx("button",{onClick:()=>l("/auth",{replace:!0}),className:"w-full h-8 rounded-[6px] bg-primary text-primary-foreground px-4 py-1.5 text-sm font-semibold cursor-pointer",children:"Sign in"})]}),u==="review"&&e.jsxs(e.Fragment,{children:[e.jsxs("h1",{className:"text-xl font-bold text-foreground mb-2",children:["Join ",g||"this organization","?"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-6",children:["You've been invited to join",g?e.jsxs(e.Fragment,{children:[" ",e.jsx("strong",{className:"text-foreground",children:g})]}):" this organization"," on Linkers as ",e.jsx("strong",{className:"text-foreground",children:S}),"."]}),e.jsx("button",{onClick:W,className:"w-full h-8 rounded-[6px] bg-primary text-primary-foreground px-4 py-1.5 text-sm font-semibold cursor-pointer mb-2",children:"Accept invitation"}),e.jsx("button",{onClick:Y,className:"w-full mt-2 text-sm font-medium text-muted-foreground hover:text-foreground cursor-pointer transition-colors",children:"Decline"})]}),u==="wrong_email"&&(()=>{const t=V().find(d=>(d.email||"").trim().toLowerCase()===(S||"").trim().toLowerCase());return e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"text-xl font-bold text-foreground mb-2",children:"Wrong account"}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-6",children:["You're signed in as ",e.jsx("strong",{className:"text-foreground",children:$}),", but this invitation was sent to ",e.jsx("strong",{className:"text-foreground",children:S}),"."]}),t&&e.jsxs("button",{onClick:async()=>
1{if(!await X(t.user_id,`/accept-invitation?id=${m}`)){try{localStorage.setItem("post_auth_redirect",`/accept-invitation?id=${m}`)}catch{}l("/auth",{replace:!0})}},className:"w-full h-11 rounded-[6px] bg-primary text-primary-foreground px-4 text-sm font-semibold cursor-pointer mb-2",children:["Switch to ",S]}),e.jsx("button",{onClick:async()=>{await i.auth.signOut(),localStorage.clear();try{localStorage.setItem("post_auth_redirect",`/accept-invitation?id=${m}`)}catch{}l("/auth",{replace:!0})},className:"w-full h-11 rounded-[6px] px-4 text-sm font-semibold cursor-pointer "+(t?"bg-card text-foreground shadow-[0_0_0_1px_rgba(0,0,0,0.12)] hover:shadow-[0_0_0_1px_rgba(0,0,0,0.32)] transition-shadow":"bg-primary text-primary-foreground"),children:"Sign out and switch account"})]})})(),u==="error"&&e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"text-xl font-bold text-foreground mb-2",children:"Something went wrong"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-6",children:x}),e.jsx("button",{onClick:()=>l("/",{replace:!0}),className:"w-full h-8 rounded-[6px] bg-primary text-primary-foreground px-4 py-1.5 text-sm font-semibold cursor-pointer",children:"Go home"})]})]})]})}export{oe as default};
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.