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 { currentOrgId: string | null; // Logto org ID — used for getAccessToken(resource, orgId) currentTenantId: string | null; // DB UUID — used for API calls like /api/tenants/{id} currentOrgRoles: string[] | null; // Logto org roles for the current org (e.g. 'admin', 'member') organizations: OrgInfo[]; isPlatformAdmin: boolean; setCurrentOrg: (orgId: string | null) => void; setOrganizations: (orgs: OrgInfo[]) => void; setIsPlatformAdmin: (value: boolean) => void; setCurrentOrgRoles: (roles: string[] | null) => void; } export const useOrgStore = create((set, get) => ({ currentOrgId: null, currentTenantId: null, currentOrgRoles: null, organizations: [], isPlatformAdmin: false, setCurrentOrg: (orgId) => { const org = get().organizations.find((o) => o.id === orgId); set({ currentOrgId: orgId, currentTenantId: org?.tenantId ?? null }); }, 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, }); }, setIsPlatformAdmin: (value) => set({ isPlatformAdmin: value }), setCurrentOrgRoles: (roles) => set({ currentOrgRoles: roles }), }));