Skip to content

Commit adc1aa4

Browse files
committed
feat(auth): sync active organization based on project details in ProjectLayoutComponent
1 parent bc5bd71 commit adc1aa4

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

apps/studio/src/routes/projects.$projectId.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { useCallback, useEffect, useMemo } from 'react';
2727
import type { InstalledPackage } from '@objectstack/spec/kernel';
2828
import { useProjectDetail } from '@/hooks/useProjects';
29+
import { useSession } from '@/hooks/useSession';
2930
import { useRegisterActiveProject } from '@/components/production-guard';
3031
import { toast } from '@/hooks/use-toast';
3132
import { 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

Comments
 (0)