Manage Group Members: Allow removing via context menu (#2048)

This commit is contained in:
Alexander Zinchuk 2022-09-20 14:59:17 +02:00
parent 04cffadd06
commit 15c5119a79

View File

@ -1,6 +1,6 @@
import type { FC } from '../../../lib/teact/teact'; import type { FC } from '../../../lib/teact/teact';
import React, { import React, {
memo, useCallback, useMemo, useRef, memo, useCallback, useMemo, useRef, useState,
} from '../../../lib/teact/teact'; } from '../../../lib/teact/teact';
import { getActions, getGlobal, withGlobal } from '../../../global'; import { getActions, getGlobal, withGlobal } from '../../../global';
@ -10,7 +10,7 @@ import { ManagementScreens } from '../../../types';
import { unique } from '../../../util/iteratees'; import { unique } from '../../../util/iteratees';
import { selectChat } from '../../../global/selectors'; import { selectChat } from '../../../global/selectors';
import { import {
sortUserIds, isChatChannel, filterUsersByName, sortChatIds, isUserBot, sortUserIds, isChatChannel, filterUsersByName, sortChatIds, isUserBot, getHasAdminRight,
} from '../../../global/helpers'; } from '../../../global/helpers';
import useLang from '../../../hooks/useLang'; import useLang from '../../../hooks/useLang';
import useHistoryBack from '../../../hooks/useHistoryBack'; import useHistoryBack from '../../../hooks/useHistoryBack';
@ -23,6 +23,7 @@ import ListItem from '../../ui/ListItem';
import InputText from '../../ui/InputText'; import InputText from '../../ui/InputText';
import InfiniteScroll from '../../ui/InfiniteScroll'; import InfiniteScroll from '../../ui/InfiniteScroll';
import Loading from '../../ui/Loading'; import Loading from '../../ui/Loading';
import DeleteMemberModal from '../DeleteMemberModal';
type OwnProps = { type OwnProps = {
chatId: string; chatId: string;
@ -44,6 +45,8 @@ type StateProps = {
localUserIds?: string[]; localUserIds?: string[];
globalUserIds?: string[]; globalUserIds?: string[];
serverTimeOffset: number; serverTimeOffset: number;
currentUserId?: string;
canDeleteMembers?: boolean;
}; };
const ManageGroupMembers: FC<OwnProps & StateProps> = ({ const ManageGroupMembers: FC<OwnProps & StateProps> = ({
@ -59,6 +62,8 @@ const ManageGroupMembers: FC<OwnProps & StateProps> = ({
isSearching, isSearching,
searchQuery, searchQuery,
serverTimeOffset, serverTimeOffset,
currentUserId,
canDeleteMembers,
onClose, onClose,
onScreenSelect, onScreenSelect,
onChatMemberSelect, onChatMemberSelect,
@ -70,6 +75,8 @@ const ManageGroupMembers: FC<OwnProps & StateProps> = ({
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const [deletingUserId, setDeletingUserId] = useState<string | undefined>();
const adminIds = useMemo(() => { const adminIds = useMemo(() => {
return noAdmins ? adminMembers?.map(({ userId }) => userId) || [] : []; return noAdmins ? adminMembers?.map(({ userId }) => userId) || [] : [];
}, [adminMembers, noAdmins]); }, [adminMembers, noAdmins]);
@ -141,11 +148,25 @@ const ManageGroupMembers: FC<OwnProps & StateProps> = ({
} }
}, '.ListItem-button', true); }, '.ListItem-button', true);
const handleDeleteMembersModalClose = useCallback(() => {
setDeletingUserId(undefined);
}, []);
useHistoryBack({ useHistoryBack({
isActive, isActive,
onBack: onClose, onBack: onClose,
}); });
function getMemberContextAction(memberId: string) {
return memberId === currentUserId || !canDeleteMembers ? undefined : [{
title: lang('lng_context_remove_from_group'),
icon: 'stop',
handler: () => {
setDeletingUserId(memberId);
},
}];
}
function renderSearchField() { function renderSearchField() {
return ( return (
<div className="Management__filter" dir={lang.isRtl ? 'rtl' : undefined}> <div className="Management__filter" dir={lang.isRtl ? 'rtl' : undefined}>
@ -179,6 +200,7 @@ const ManageGroupMembers: FC<OwnProps & StateProps> = ({
className="chat-item-clickable scroll-item" className="chat-item-clickable scroll-item"
// eslint-disable-next-line react/jsx-no-bind // eslint-disable-next-line react/jsx-no-bind
onClick={() => handleMemberClick(id)} onClick={() => handleMemberClick(id)}
contextActions={getMemberContextAction(id)}
> >
<PrivateChatInfo userId={id} forceShowSelf /> <PrivateChatInfo userId={id} forceShowSelf />
</ListItem> </ListItem>
@ -195,6 +217,13 @@ const ManageGroupMembers: FC<OwnProps & StateProps> = ({
)} )}
</div> </div>
</div> </div>
{canDeleteMembers && (
<DeleteMemberModal
isOpen={Boolean(deletingUserId)}
userId={deletingUserId}
onClose={handleDeleteMembersModalClose}
/>
)}
</div> </div>
); );
}; };
@ -215,6 +244,8 @@ export default memo(withGlobal<OwnProps>(
localUserIds, localUserIds,
} = global.userSearch; } = global.userSearch;
const canDeleteMembers = chat && (chat.isCreator || getHasAdminRight(chat, 'banUsers'));
return { return {
members, members,
adminMembers, adminMembers,
@ -225,7 +256,9 @@ export default memo(withGlobal<OwnProps>(
isSearching: fetchingStatus, isSearching: fetchingStatus,
globalUserIds, globalUserIds,
localUserIds, localUserIds,
canDeleteMembers,
serverTimeOffset: global.serverTimeOffset, serverTimeOffset: global.serverTimeOffset,
currentUserId: global.currentUserId,
}; };
}, },
)(ManageGroupMembers)); )(ManageGroupMembers));