Skip to content

Commit

Permalink
Add comment about non working includeMembersCount
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarah committed Sep 13, 2023
1 parent c37f859 commit 3e5323e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface MembershipCountProps {
locale: string;
}

// TODO: This is a workaround for the fact that `includeMembersCount` does not work.
// See comment in `community.ts` for more information.
async function MembershipCount({communityId, locale}: MembershipCountProps) {
const t = await getTranslator(locale, 'Communities');
const memberships = await getMemberships(communityId);
Expand Down
38 changes: 33 additions & 5 deletions apps/researcher/src/lib/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

export type Community = Pick<
FullCommunity,
'id' | 'name' | 'slug' | 'imageUrl'
'id' | 'name' | 'slug' | 'imageUrl' | 'createdAt'
>;

export type Membership = Pick<OrganizationMembership, 'id' | 'role'> & {
Expand All @@ -31,13 +31,41 @@ export async function getMemberships(
});
}

export async function getAllCommunities({query = ''} = {}): Promise<
Community[]
> {
return clerkClient.organizations.getOrganizationList({
interface GetAllCommunitiesProps {
query?: string;
orderBy?: 'nameAsc' | 'nameDsc' | 'createdAt';
limit: number;
offset: number;
}

export async function getAllCommunities({
query = '',
orderBy = 'nameAsc',
limit,
offset,
}: GetAllCommunitiesProps): Promise<Community[]> {
const communities = await clerkClient.organizations.getOrganizationList({
limit,
offset,
query,
// TODO: `includeMembersCount` is not working, I have reported this bug to Clerk.
// They have confirmed it and are working on a fix.
// When the membership count is present, we can use it to sort the communities.
// https://discord.com/channels/856971667393609759/1151078450627624970
includeMembersCount: true,
});

return communities.sort((a, b) => {
if (orderBy === 'nameAsc') {
return a.name.localeCompare(b.name);
} else if (orderBy === 'nameDsc') {
return b.name.localeCompare(a.name);
} else if (orderBy === 'createdAt') {
return b.createdAt - a.createdAt;
} else {
return 0;
}
});
}

export function isAdmin(memberships: ReadonlyArray<Membership>): boolean {
Expand Down

0 comments on commit 3e5323e

Please sign in to comment.