diff --git a/lib/permissions/add-users-permissions.js b/lib/permissions/add-users-permissions.js new file mode 100644 index 000000000..ae957d16a --- /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 index b9aca3603..1874f1902 100644 --- a/web/modals/threads/members/members-modal.react.js +++ b/web/modals/threads/members/members-modal.react.js @@ -1,186 +1,165 @@ // @flow 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'; import ThreadMembersList from './members-list.react.js'; import css from './members-modal.css'; import Button from '../../../components/button.react.js'; import Tabs, { type TabData } from '../../../components/tabs.react.js'; import ManageInviteLinksModal from '../../../invite-links/manage-invite-links-modal.react.js'; import ViewInviteLinkModal from '../../../invite-links/view-invite-link-modal.react.js'; import { useSelector } from '../../../redux/redux-utils.js'; import SearchModal from '../../search-modal.react.js'; type TabType = 'All Members' | 'Admins'; const tabsData: $ReadOnlyArray> = [ { id: 'All Members', header: 'All Members', }, { id: 'Admins', header: 'Admins', }, ]; type ContentProps = { +searchText: string, +threadID: string, }; function ThreadMembersModalContent(props: ContentProps): React.Node { const { threadID, searchText } = props; const [tab, setTab] = React.useState('All Members'); const threadInfo = useSelector(state => threadInfoSelector(state)[threadID]); const { members: threadMembersNotFiltered } = threadInfo; const userSearchIndex = useUserSearchIndex(threadMembersNotFiltered); const userIDs = React.useMemo( () => userSearchIndex.getSearchResults(searchText), [searchText, userSearchIndex], ); const allMembers = React.useMemo( () => threadMembersNotFiltered.filter( (member: RelativeMemberInfo) => searchText.length === 0 || userIDs.includes(member.id), ), [searchText.length, threadMembersNotFiltered, userIDs], ); const roles = useRolesFromCommunityThreadInfo(threadInfo, allMembers); const adminMembers = React.useMemo( () => allMembers.filter((member: RelativeMemberInfo) => roleIsAdminRole(roles.get(member.id)), ), [allMembers, roles], ); const tabs = React.useMemo( () => , [tab], ); const tabContent = React.useMemo(() => { if (tab === 'All Members') { return ( ); } return ( ); }, [adminMembers, allMembers, tab, threadInfo]); const threadMembersModalContent = React.useMemo( () => (
{tabs}
{tabContent}
), [tabContent, tabs], ); return threadMembersModalContent; } type Props = { +threadID: string, +onClose: () => void, }; function ThreadMembersModal(props: Props): React.Node { const { onClose, threadID } = props; const renderModalContent = React.useCallback( (searchText: string) => ( ), [threadID], ); const { pushModal, popModal } = useModalContext(); 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) { pushModal(); } else if (inviteLink) { pushModal(); } else if (canManageLinks) { pushModal(); } }, [ canManageLinks, inviteLink, isCommunityRoot, popModal, pushModal, 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; } return (
); }, [canAddMembers, onClickAddMembers]); return ( {renderModalContent} ); } export default ThreadMembersModal;