Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-ads-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/shared": patch
---

Moved helper to enable Organizations feature to React-specific shared path
27 changes: 0 additions & 27 deletions packages/shared/src/organization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { useEffect, useRef } from 'react';

import { useClerk } from './react';
import type { OrganizationMembershipResource } from './types';

/**
Expand All @@ -18,27 +15,3 @@ export function getCurrentOrganizationMembership(
organizationMembership => organizationMembership.organization.id === organizationId,
);
}

/**
* Attempts to enable the organizations environment setting for a given caller
*
* @internal
*/
export function useAttemptToEnableOrganizations(caller: 'useOrganization' | 'useOrganizationList') {
const clerk = useClerk();
const hasAttempted = useRef(false);

useEffect(() => {
// Guard to not run this effect twice on Clerk resource update
if (hasAttempted.current) {
return;
}

hasAttempted.current = true;
// Optional chaining is important for `@clerk/clerk-react` usage with older clerk-js versions that don't have the method
clerk.__internal_attemptToEnableEnvironmentSetting?.({
for: 'organizations',
caller,
});
}, [clerk, caller]);
}
1 change: 1 addition & 0 deletions packages/shared/src/react/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { assertContextExists, createContextAndHook } from './createContextAndHoo
export { useAPIKeys as __experimental_useAPIKeys } from './useAPIKeys';
export { useOrganization } from './useOrganization';
export { useOrganizationList } from './useOrganizationList';
export { useAttemptToEnableOrganizations } from './useAttemptToEnableOrganizations';
export { useSafeLayoutEffect } from './useSafeLayoutEffect';
export { useSession } from './useSession';
export { useSessionList } from './useSessionList';
Expand Down
27 changes: 27 additions & 0 deletions packages/shared/src/react/hooks/useAttemptToEnableOrganizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect, useRef } from 'react';

import { useClerk } from './useClerk';

/**
* Attempts to enable the organizations environment setting for a given caller
*
* @internal
*/
export function useAttemptToEnableOrganizations(caller: 'useOrganization' | 'useOrganizationList') {
const clerk = useClerk();
const hasAttempted = useRef(false);

useEffect(() => {
// Guard to not run this effect twice on Clerk resource update
if (hasAttempted.current) {
return;
}

hasAttempted.current = true;
// Optional chaining is important for `@clerk/clerk-react` usage with older clerk-js versions that don't have the method
clerk.__internal_attemptToEnableEnvironmentSetting?.({
for: 'organizations',
caller,
});
}, [clerk, caller]);
}
3 changes: 2 additions & 1 deletion packages/shared/src/react/hooks/useOrganization.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentOrganizationMembership, useAttemptToEnableOrganizations } from '../../organization';
import { getCurrentOrganizationMembership } from '../../organization';
import { eventMethodCalled } from '../../telemetry/events/method-called';
import type {
GetDomainsParams,
Expand All @@ -20,6 +20,7 @@ import {
import { STABLE_KEYS } from '../stable-keys';
import type { PaginatedHookConfig, PaginatedResources, PaginatedResourcesWithDefault } from '../types';
import { createCacheKeys } from './createCacheKeys';
import { useAttemptToEnableOrganizations } from './useAttemptToEnableOrganizations';
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/react/hooks/useOrganizationList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useAttemptToEnableOrganizations } from '../../organization';
import { eventMethodCalled } from '../../telemetry/events/method-called';
import type {
CreateOrganizationParams,
Expand All @@ -15,6 +14,7 @@ import { useAssertWrappedByClerkProvider, useClerkInstanceContext, useUserContex
import { STABLE_KEYS } from '../stable-keys';
import type { PaginatedHookConfig, PaginatedResources, PaginatedResourcesWithDefault } from '../types';
import { createCacheKeys } from './createCacheKeys';
import { useAttemptToEnableOrganizations } from './useAttemptToEnableOrganizations';
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';

/**
Expand Down