2026-04-05 02:50:51 +02:00
|
|
|
import { create } from 'zustand';
|
|
|
|
|
|
|
|
|
|
export interface OrgInfo {
|
|
|
|
|
id: string; // Logto org ID (for token scoping)
|
|
|
|
|
name: string;
|
|
|
|
|
slug?: string;
|
|
|
|
|
tenantId?: string; // DB tenant UUID (for API calls)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface OrgState {
|
2026-04-05 14:04:06 +02:00
|
|
|
currentOrgId: string | null; // Logto org ID — used for getAccessToken(resource, orgId)
|
|
|
|
|
currentTenantId: string | null; // DB UUID — used for API calls like /api/tenants/{id}
|
2026-04-05 02:50:51 +02:00
|
|
|
organizations: OrgInfo[];
|
2026-04-05 14:04:06 +02:00
|
|
|
scopes: Set<string>;
|
2026-04-05 02:50:51 +02:00
|
|
|
setCurrentOrg: (orgId: string | null) => void;
|
|
|
|
|
setOrganizations: (orgs: OrgInfo[]) => void;
|
2026-04-05 14:04:06 +02:00
|
|
|
setScopes: (scopes: Set<string>) => void;
|
2026-04-05 02:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useOrgStore = create<OrgState>((set, get) => ({
|
|
|
|
|
currentOrgId: null,
|
|
|
|
|
currentTenantId: null,
|
|
|
|
|
organizations: [],
|
2026-04-05 14:04:06 +02:00
|
|
|
scopes: new Set(),
|
2026-04-05 02:50:51 +02:00
|
|
|
setCurrentOrg: (orgId) => {
|
|
|
|
|
const org = get().organizations.find((o) => o.id === orgId);
|
|
|
|
|
set({ currentOrgId: orgId, currentTenantId: org?.tenantId ?? null });
|
|
|
|
|
},
|
2026-04-05 10:29:03 +02:00
|
|
|
setOrganizations: (orgs) => {
|
|
|
|
|
const { currentOrgId } = get();
|
|
|
|
|
const match = currentOrgId ? orgs.find((o) => o.id === currentOrgId) : null;
|
|
|
|
|
set({
|
|
|
|
|
organizations: orgs,
|
|
|
|
|
// Re-resolve tenantId when org list is enriched (e.g., OrgResolver adds tenantId)
|
|
|
|
|
currentTenantId: match?.tenantId ?? get().currentTenantId,
|
|
|
|
|
});
|
|
|
|
|
},
|
2026-04-05 14:04:06 +02:00
|
|
|
setScopes: (scopes) => set({ scopes }),
|
2026-04-05 02:50:51 +02:00
|
|
|
}));
|