@@ -26,6 +26,7 @@ import {
2626import { useCallback , useEffect , useMemo } from 'react' ;
2727import type { InstalledPackage } from '@objectstack/spec/kernel' ;
2828import { useProjectDetail } from '@/hooks/useProjects' ;
29+ import { useSession } from '@/hooks/useSession' ;
2930import { useRegisterActiveProject } from '@/components/production-guard' ;
3031import { toast } from '@/hooks/use-toast' ;
3132import { AppSidebar } from '@/components/app-sidebar' ;
@@ -37,6 +38,7 @@ function ProjectLayoutComponent() {
3738 } ) ;
3839 const { detail, error } = useProjectDetail ( projectId ) ;
3940 const registerActiveProject = useRegisterActiveProject ( ) ;
41+ const { session, setActiveOrganization } = useSession ( ) ;
4042 const navigate = useNavigate ( ) ;
4143 const location = useLocation ( ) ;
4244
@@ -72,6 +74,16 @@ function ProjectLayoutComponent() {
7274 if ( pkg && pkg !== selectedPackage ) setSelectedPackage ( pkg ) ;
7375 } , [ activePackageId , packages , selectedPackage , setSelectedPackage ] ) ;
7476
77+ // Sync active organization: if the project belongs to a different org than
78+ // the current session's active org, switch automatically so that all
79+ // org-scoped API calls (project list, member list, etc.) use the right org.
80+ useEffect ( ( ) => {
81+ const projectOrgId = detail ?. project ?. organization_id ;
82+ if ( ! projectOrgId ) return ;
83+ if ( session ?. activeOrganizationId === projectOrgId ) return ;
84+ setActiveOrganization ( projectOrgId ) ;
85+ } , [ detail ?. project ?. organization_id , session ?. activeOrganizationId , setActiveOrganization ] ) ;
86+
7587 const handleSelectPackage = useCallback (
7688 ( pkg : InstalledPackage ) => {
7789 const nextId = pkg . manifest ?. id ;
0 commit comments