1import{o as e}from"./rolldown-runtime-C0FnF6B9.js";import{u as t}from"./axiosClient-DnVO32-R.js";import{_ as n,b as r}from"./vendor-react-Ccu8BlQU.js";import{n as i,s as a}from"./AuthContext-BwFq1txD.js";import{t as o}from"./WorkspaceAPI-NXczXWIe.js";import{t as s}from"./useWorkspaceUnlockState-CC8XdN-l.js";import"./native-mobile-app-DhQk5gto.js";import{r as c}from"./useHideNativeBottomTabs-C_i-vpDp.js";var l=e(r());function u(e){let[t,n]=(0,l.useState)(null);(0,l.useEffect)(()=>n(null),[e]);let r=(0,l.useCallback)(t=>{e&&n({userId:e,designations:Object.fromEntries(t.map(({id:e,designation:t})=>[e,t]))})},[e]);return{featureVisibilityDesignations:t&&t.userId===e?t.designations:void 0,rememberDesignations:r}}var d=n(),f=(0,l.createContext)();function p({children:e}){let{isNativeMobile:n,platform:r}=c(),p=(0,l.useRef)(r);p.current=r;let[m,h]=(0,l.useState)(()=>localStorage.getItem(`activeWorkspaceId`)||null),[g,_]=(0,l.useState)([]),[v,y]=(0,l.useState)(!1),[b,x]=(0,l.useState)(!0),[S,C]=(0,l.useState)(!0),[w,T]=(0,l.useState)(!1),[E,D]=(0,l.useState)(null),{reloadUser:O,isAuthenticated:k,user:A}=i(),{featureVisibilityDesignations:j,rememberDesignations:M}=u(k?A?.id:void 0),N=(0,l.useRef)(0),P=(0,l.useRef)(!1),F=(0,l.useRef)(null),I=(0,l.useMemo)(()=>!m||!g.length?null:g.find(e=>e.id===m)||null,[m,g]);(0,l.useEffect)(()=>{let e=!1;return n&&!m&&p.current.getLatestActiveWorkSpace?.().then(t=>{!e&&t&&h(t)}),()=>{e=!0}},[n,m]),(0,l.useEffect)(()=>{if(t(()=>m),k){T(!1);let e=++N.current;O().catch(()=>{}).finally(()=>{N.current===e&&T(!0)})}return()=>{t(()=>null)}},[m,k,O]),(0,l.useEffect)(()=>{n&&m&&p.current.setActiveWorkSpace?.({workspaceId:m})},[m]),(0,l.useEffect)(()=>{if(!A?.id){x(!1);return}let e=localStorage.getItem(`activeWorkspaceId`);e&&t(()=>e),x(!1)},[A?.id]),(0,l.useEffect)(()=>{if(!A?.id||F.current===A.id)return;let e=F.current!==null;if(F.current=A.id,e&&(P.current=!1,_([]),y(!1)),P.current)return;let t=localStorage.getItem(`activeWorkspaceId:${A.id}`);t&&t!==m?z(t):!t&&e&&z(null)},[A?.id]),(0,l.useEffect)(()=>{A?.id&&(m?localStorage.setItem(`activeWorkspaceId:${A.id}`,m):localStorage.removeItem(`activeWorkspaceId:${A.id}`))},[m,A?.id]);let L=(0,l.useRef)(0),R=(0,l.useCallback)(async()=>{let e=++L.current;try{C(!0),D(null);let t=await o.getWorkspaces();if(L.current!==e)return;_(t.workspaces||t||[]),M(t.workspaces||t||[]),y(t.workspace_creation_restricted===!0)}catch(t){if(L.current!==e)return;D(t),_([]),y(!1)}finally{L.current===e&&C(!1)}},[M]);(0,l.useEffect)(()=>{k&&R()},[R,k,A?.id]);let z=(0,l.useCallback)(e=>{let n=typeof e==`string`?e:e?.id;h(e=>(e!==n&&T(!1),n)),t(()=>n),n?localStorage.setItem(`activeWorkspaceId`,n):localStorage.removeItem(`activeWorkspaceId`)},[]),B=(0,l.useCallback)((e,t)=>{_(n=>n.map(n=>n.id===e?{...n,...t}:n))},[]);(0,l.useEffect)(()=>{let e=new URLSearchParams(window.location.search),t=e.get(`active_workspace`);if(!t)return;P.current=!0,z(t),e.delete(`active_workspace`);let n=e.toString(),r=`${window.location.pathname}${n?`?${n}`:``}${window.location.hash}`;window.history.replaceState({},``,r)},[z]),(0,l.useEffect)(()=>{if(S||!I||s(I).isUnlocked)return;let e=g.find(e=>e.id!==I.id&&s(e).isUnlocked);if(!e)return;a();let t=window.location.pathname;if(t===`/workspace/${I.id}`||t.startsWith(`/workspace/${I.id}/`)){window.location.assign(`/workspace/${e.id}`);return}z(e)},[S,I,g,z]),(0,l.useEffect)(()=>{if(S||g.length===0||I)return;let e=g.find(e=>s(e).isUnlocked);e&&z(e)},[g,S,I,z]);let V={featureVisibilityDesignations:j,activeWorkspace:I,activeWorkspaceId:m,isPersonal:S||!I&&g.length>0?null:!!I?.is_personal,hasNoWorkspaces:!S&&!E&&g.length===0,isSettlingWorkspace:!S&&(!I||!s(I).isUnlocked)&&g.some(e=>e.id!==I?.id&&s(e).isUnlocked),workspaceCreationRestricted:v,isWorkspaceReady:w,workspaces:g,isLoading:b,isLoadingWorkspaces:S,error:E,switchActiveWorkspace:z,patchWorkspace:B,refetchWorkspaces:R};return(0,d.jsx)(f.Provider,{value:V,children:e})}function m(){let e=(0,l.useContext)(f);if(e===void 0)throw Error(`useWorkspace must be used within a WorkspaceProvider`);return e}export{p as n,m as r,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.