PageSourceSearch

https://clawli.ai/assets/RoleContext-CI5Mw_D4.js

js clawli.ai collected 2026-10-05 12:59:47 UTC 4,010 bytes, 11 lines download raw bytes

1import{n as e,s as t,t as n}from"./jsx-runtime-Bzw2jH9X.js";import{n as r}from"./client-DyLjmcRC.js";import{t as i,x as a}from"./useQuery-C-rKJMfR.js";import{n as o}from"./AuthContext-DhgTOrcU.js";import{n as s}from"./dist-DBCDZwJ7.js";var c=t(e(),1),l=n(),u=`techtide-ai`,d=(0,c.createContext)(null);function f({children:e}){let{user:t}=o(),n=a(),[f,p]=(0,c.useState)(null),m=(0,c.useRef)(null);(0,c.useEffect)(()=>()=>{m.current&&clearTimeout(m.current)},[]);let{data:h,isLoading:g,refetch:_}=i({queryKey:[`user_role`,t?.id],queryFn:async()=>{if(!t)return null;let{data:e,error:n}=await r.from(`user_roles`).select(`role`).eq(`user_id`,t.id);if(n)throw n;if(!e||e.length===0)return null;let i={super_admin:3,admin:2,user:1};return e.reduce((e,t)=>(i[t.role]??0)>(i[e.role]??0)?t:e).role},enabled:!!t,staleTime:9e5,placeholderData:e=>e}),{data:v,isLoading:y}=i({queryKey:[`profile`,t?.id],queryFn:async()=>{if(!t)return null;let{data:e,error:n}=await r.from(`profiles`).select(`id, email, full_name, avatar_url, unique_handle`).eq(`id`,t.id).maybeSingle();if(n)throw n;return e?{id:e.id,email:e.email,fullName:e.full_name,avatarUrl:e.avatar_url,uniqueHandle:e.unique_handle}:null},enabled:!!t,staleTime:6e5}),{data:b,isLoading:x}=i({queryKey:[`org_membership`,t?.id],queryFn:async()=>{if(!t)return null;let{data:e,error:n}=await r.from(`organization_members`).select(`
2          role_in_org,
3          organizations (
4            id,
5            name,
6            slug,
7            branding,
8            subscription_status,
9            is_personal
10          )
11        `).eq(`user_id`,t.id).order(`joined_at`,{ascending:!0}).limit(1).maybeSingle();if(n)throw n;return e},enabled:!!t,staleTime:9e5,placeholderData:e=>e}),S=h===`super_admin`,{data:C=[]}=i({queryKey:[`all_organizations`],queryFn:async()=>{let{data:e,error:t}=await r.from(`organizations`).select(`id, name, slug, branding, subscription_status`).order(`name`);if(t)throw t;return(e??[]).map(e=>({id:e.id,name:e.name,slug:e.slug,branding:e.branding||{},subscriptionStatus:e.subscription_status||`active`}))},enabled:S}),w=b?.organizations,T=w?{id:w.id,name:w.name,slug:w.slug,branding:w.branding||{},subscriptionStatus:w.subscription_status||`active`,isPersonal:w.is_personal===!0}
11:null,E=b?.role_in_org,D=T?.slug===u,O=g||y||x,k=h??null,A=!O&&!!t&&!!v&&!T,j=(0,c.useRef)(0);(0,c.useEffect)(()=>{if(!A||O||!t||j.current>=3)return;let e=!1,i=async()=>{j.current+=1;try{let{error:a}=await r.rpc(`create_personal_workspace`);if(e)return;if(a){j.current<3?(s.error(`Workspace setup hit a snag, retrying`),setTimeout(()=>{e||i()},1e3*j.current)):s.error(`Couldn't set up your workspace, try refreshing the page`);return}n.invalidateQueries({queryKey:[`org_membership`,t.id]})}catch{if(e)return;j.current<3?(s.error(`Workspace setup hit a snag, retrying`),setTimeout(()=>{e||i()},1e3*j.current)):s.error(`Couldn't set up your workspace, try refreshing the page`)}};return i(),()=>{e=!0}},[A,O,t,n]);let M=S&&f?f:T,N=S&&f!==null&&f.id!==T?.id;return(0,l.jsx)(d.Provider,{value:{role:k,orgRole:E,organization:T,profile:v??null,isLoading:O,isSuperAdmin:S,isAdmin:k===`admin`||k===`super_admin`,isUser:k===`user`,isTechTideOrg:D,needsOnboarding:A,refetch:()=>{_()},viewingOrganization:M,setViewingOrganization:e=>{S&&t&&(!e||e.id===T?.id?(f&&r.rpc(`log_audit_event`,{_action:`admin.org_switch_end`,_user_id:t.id,_target_id:f.id,_target_type:`organization`,_details:{org_name:f.name}}).then(()=>{},()=>{}),p(null),m.current&&clearTimeout(m.current)):(r.rpc(`log_audit_event`,{_action:`admin.org_switch_start`,_user_id:t.id,_target_id:e.id,_target_type:`organization`,_details:{org_name:e.name}}).then(()=>{},()=>{}),p(e),m.current&&clearTimeout(m.current),m.current=setTimeout(()=>{p(null),s.info(`Org view session expired, switched back to your workspace`)},18e5)))},isViewingAsOtherOrg:N,allOrganizations:C},children:e})}function p(){let e=(0,c.useContext)(d);if(!e)throw Error(`useRole must be used within a RoleProvider`);return e}export{p as n,f as t};

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.