diff --git a/lib/permissions/add-users-permissions.js b/lib/permissions/add-users-permissions.js new file mode 100644 --- /dev/null +++ b/lib/permissions/add-users-permissions.js @@ -0,0 +1,41 @@ +// @flow + +import { primaryInviteLinksSelector } from '../selectors/invite-links-selectors.js'; +import { useThreadHasPermission } from '../shared/thread-utils.js'; +import type { InviteLink } from '../types/link-types.js'; +import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; +import { threadPermissions } from '../types/thread-permission-types.js'; +import { threadTypeIsCommunityRoot } from '../types/thread-types-enum.js'; +import { useSelector } from '../utils/redux-utils.js'; + +type AddUsersPermissions = { + +inviteLink: InviteLink, + +canManageLinks: boolean, + +canAddMembers: boolean, + +isCommunityRoot: boolean, +}; + +function useAddUsersPermissions(threadInfo: ThreadInfo): AddUsersPermissions { + const inviteLink = useSelector(primaryInviteLinksSelector)[threadInfo.id]; + const canManageLinks = useThreadHasPermission( + threadInfo, + threadPermissions.MANAGE_INVITE_LINKS, + ); + + const isCommunityRoot = threadTypeIsCommunityRoot(threadInfo.type); + + const canAddMembersViaInviteLink = + isCommunityRoot && (!!inviteLink || canManageLinks); + + const hasAddMembersPermission = useThreadHasPermission( + threadInfo, + threadPermissions.ADD_MEMBERS, + ); + const canAddMembersManually = hasAddMembersPermission && !isCommunityRoot; + + const canAddMembers = canAddMembersManually || canAddMembersViaInviteLink; + + return { inviteLink, canManageLinks, canAddMembers, isCommunityRoot }; +} + +export { useAddUsersPermissions }; diff --git a/web/modals/threads/members/members-modal.react.js b/web/modals/threads/members/members-modal.react.js --- a/web/modals/threads/members/members-modal.react.js +++ b/web/modals/threads/members/members-modal.react.js @@ -3,16 +3,11 @@ import * as React from 'react'; import { useModalContext } from 'lib/components/modal-provider.react.js'; -import { primaryInviteLinksSelector } from 'lib/selectors/invite-links-selectors.js'; +import { useAddUsersPermissions } from 'lib/permissions/add-users-permissions.js'; import { useUserSearchIndex } from 'lib/selectors/nav-selectors.js'; import { threadInfoSelector } from 'lib/selectors/thread-selectors.js'; -import { - roleIsAdminRole, - useThreadHasPermission, -} from 'lib/shared/thread-utils.js'; +import { roleIsAdminRole } from 'lib/shared/thread-utils.js'; import type { RelativeMemberInfo } from 'lib/types/minimally-encoded-thread-permissions-types.js'; -import { threadPermissions } from 'lib/types/thread-permission-types.js'; -import { threadTypeIsCommunityRoot } from 'lib/types/thread-types-enum.js'; import { useRolesFromCommunityThreadInfo } from 'lib/utils/role-utils.js'; import { AddMembersModal } from './add-members-modal.react.js'; @@ -121,13 +116,8 @@ const threadInfo = useSelector(state => threadInfoSelector(state)[threadID]); - const inviteLink = useSelector(primaryInviteLinksSelector)[threadID]; - const canManageLinks = useThreadHasPermission( - threadInfo, - threadPermissions.MANAGE_INVITE_LINKS, - ); - - const isCommunityRoot = threadTypeIsCommunityRoot(threadInfo.type); + const { inviteLink, canManageLinks, canAddMembers, isCommunityRoot } = + useAddUsersPermissions(threadInfo); const onClickAddMembers = React.useCallback(() => { if (!isCommunityRoot) { @@ -146,17 +136,6 @@ threadID, ]); - const canAddMembersViaInviteLink = - isCommunityRoot && (inviteLink || canManageLinks); - - const hasAddMembersPermission = useThreadHasPermission( - threadInfo, - threadPermissions.ADD_MEMBERS, - ); - const canAddMembersManually = hasAddMembersPermission && !isCommunityRoot; - - const canAddMembers = canAddMembersManually || canAddMembersViaInviteLink; - const addMembersButton = React.useMemo(() => { if (!canAddMembers) { return null;