Skip to content

Commit

Permalink
chore(vue,shared): Move getCurrentOrganizationMembership helper to …
Browse files Browse the repository at this point in the history
…shared package (#5168)
  • Loading branch information
wobsoriano authored Feb 14, 2025
1 parent 74868fa commit 92d17d7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/rotten-jokes-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@clerk/shared": patch
"@clerk/vue": patch
---

Previously, the `getCurrentOrganizationMembership()` function was duplicated in both `@clerk/vue` and `@clerk/shared/react`. This change moves the function to `@clerk/shared/organization`.
3 changes: 2 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
"oauth",
"web3",
"getEnvVariable",
"pathMatcher"
"pathMatcher",
"organization"
],
"scripts": {
"build": "tsup",
Expand Down
31 changes: 31 additions & 0 deletions packages/shared/src/__tests__/organization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { OrganizationMembershipResource } from '@clerk/types';

import { getCurrentOrganizationMembership } from '../organization';

describe('getCurrentOrganizationMembership', () => {
const mockMemberships: OrganizationMembershipResource[] = [
{
id: 'mem_1',
organization: { id: 'org_1' },
} as OrganizationMembershipResource,
{
id: 'mem_2',
organization: { id: 'org_2' },
} as OrganizationMembershipResource,
];

it('returns the correct membership for a given organization ID', () => {
const result = getCurrentOrganizationMembership(mockMemberships, 'org_1');
expect(result).toBe(mockMemberships[0]);
});

it('returns undefined when organization ID is not found', () => {
const result = getCurrentOrganizationMembership(mockMemberships, 'org_nonexistent');
expect(result).toBeUndefined();
});

it('returns undefined when memberships array is empty', () => {
const result = getCurrentOrganizationMembership([], 'org_1');
expect(result).toBeUndefined();
});
});
16 changes: 16 additions & 0 deletions packages/shared/src/organization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { OrganizationMembershipResource } from '@clerk/types';

/**
* Finds the organization membership for a given organization ID from a list of memberships
* @param organizationMemberships - Array of organization memberships to search through
* @param organizationId - ID of the organization to find the membership for
* @returns The matching organization membership or undefined if not found
*/
export function getCurrentOrganizationMembership(
organizationMemberships: OrganizationMembershipResource[],
organizationId: string,
) {
return organizationMemberships.find(
organizationMembership => organizationMembership.organization.id === organizationId,
);
}
10 changes: 1 addition & 9 deletions packages/shared/src/react/hooks/useOrganization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
OrganizationResource,
} from '@clerk/types';

import { getCurrentOrganizationMembership } from '../../organization';
import { eventMethodCalled } from '../../telemetry/events/method-called';
import {
useAssertWrappedByClerkProvider,
Expand Down Expand Up @@ -366,12 +367,3 @@ export const useOrganization: UseOrganization = params => {
invitations,
};
};

function getCurrentOrganizationMembership(
organizationMemberships: OrganizationMembershipResource[],
activeOrganizationId: string,
) {
return organizationMemberships.find(
organizationMembership => organizationMembership.organization.id === activeOrganizationId,
);
}
10 changes: 1 addition & 9 deletions packages/vue/src/composables/useOrganization.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCurrentOrganizationMembership } from '@clerk/shared/organization';
import type { OrganizationMembershipResource, OrganizationResource } from '@clerk/types';
import { computed } from 'vue';

Expand Down Expand Up @@ -85,12 +86,3 @@ export const useOrganization: UseOrganization = () => {

return toComputedRefs(result);
};

function getCurrentOrganizationMembership(
organizationMemberships: OrganizationMembershipResource[],
activeOrganizationId: string,
) {
return organizationMemberships.find(
organizationMembership => organizationMembership.organization.id === activeOrganizationId,
);
}

0 comments on commit 92d17d7

Please sign in to comment.