@@ -23,7 +23,7 @@ import {
2323 useNavigate ,
2424 useParams ,
2525} from '@tanstack/react-router' ;
26- import { useCallback , useEffect , useMemo } from 'react' ;
26+ import { useCallback , useEffect , useMemo , useRef } from 'react' ;
2727import type { InstalledPackage } from '@objectstack/spec/kernel' ;
2828import { useProjectDetail } from '@/hooks/useProjects' ;
2929import { useSession } from '@/hooks/useSession' ;
@@ -39,6 +39,9 @@ function ProjectLayoutComponent() {
3939 const { detail, error } = useProjectDetail ( projectId ) ;
4040 const registerActiveProject = useRegisterActiveProject ( ) ;
4141 const { session, setActiveOrganization } = useSession ( ) ;
42+ // Track whether we already initiated an org switch for this project mount,
43+ // so we don't re-trigger during the session refresh that follows the switch.
44+ const orgSwitchedRef = useRef ( false ) ;
4245 const navigate = useNavigate ( ) ;
4346 const location = useLocation ( ) ;
4447
@@ -77,10 +80,14 @@ function ProjectLayoutComponent() {
7780 // Sync active organization: if the project belongs to a different org than
7881 // the current session's active org, switch automatically so that all
7982 // org-scoped API calls (project list, member list, etc.) use the right org.
83+ // orgSwitchedRef prevents the effect from re-firing during the session
84+ // refresh that setActiveOrganization triggers internally.
8085 useEffect ( ( ) => {
8186 const projectOrgId = detail ?. project ?. organization_id ;
8287 if ( ! projectOrgId ) return ;
88+ if ( orgSwitchedRef . current ) return ;
8389 if ( session ?. activeOrganizationId === projectOrgId ) return ;
90+ orgSwitchedRef . current = true ;
8491 setActiveOrganization ( projectOrgId ) ;
8592 } , [ detail ?. project ?. organization_id , session ?. activeOrganizationId , setActiveOrganization ] ) ;
8693
0 commit comments