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