Files
coder/site/src/modules/groups.ts
T
George K f5d4926bc1 fix(site): use total_member_count for group subtitles when sharing (#21744)
Justification:

- Populating `members` is authorized with `group_member.read` which is
not required to be able to share a workspace

- Populating `total_member_count` is authorized with `group.read` which
is required to be able to share

- The updated helper is only used in template/workspace sharing UIs, so
other pages that might need counts of readable members are unaffected

Related to: https://github.com/coder/internal/issues/1302
2026-01-29 08:33:02 -08:00

52 lines
1.3 KiB
TypeScript

import type {
Group,
OrganizationMemberWithUserData,
ReducedUser,
User,
WorkspaceUser,
} from "api/typesGenerated";
/**
* Union of all user-like types that can be distinguished from Group.
*/
type UserLike =
| User
| ReducedUser
| WorkspaceUser
| OrganizationMemberWithUserData;
/**
* Type guard to check if the value is a Group.
* Groups have a "members" property that users don't have.
*/
export const isGroup = (value: UserLike | Group): value is Group => {
return "members" in value;
};
/**
* Returns true if the provided group is the 'Everyone' group.
* The everyone group represents all the users in an organization
* for which every organization member is implicitly a member of.
*
* @param {Group} group - The group to evaluate.
* @returns {boolean} - Returns true if the group's ID matches its
* organization ID.
*/
export const isEveryoneGroup = (group: Group): boolean =>
group.id === group.organization_id;
export const getGroupSubtitle = (group: Group): string => {
// It is the everyone group when a group id is the same of the org id
if (group.id === group.organization_id) {
return "All users";
}
const total = group.total_member_count ?? group.members?.length ?? 0;
if (total === 1) {
return "1 member";
}
return `${total} members`;
};