session-desktop/ts/components/conversation/InviteContactsDialog.tsx

236 lines
7.2 KiB
TypeScript
Raw Normal View History

2021-06-16 01:33:40 +02:00
import React, { useState } from 'react';
2019-11-22 06:16:43 +01:00
2020-01-20 05:40:24 +01:00
import { SessionModal } from '../session/SessionModal';
import { SessionButton, SessionButtonColor } from '../session/SessionButton';
2021-04-22 10:03:58 +02:00
import { ContactType, SessionMemberListItem } from '../session/SessionMemberListItem';
import { DefaultTheme, useTheme } from 'styled-components';
import { ConversationController } from '../../session/conversations';
import { ToastUtils, UserUtils } from '../../session/utils';
import { initiateGroupUpdate } from '../../session/group';
import { ConversationModel, ConversationTypeEnum } from '../../models/conversation';
import { getCompleteUrlForV2ConvoId } from '../../interactions/conversationInteractions';
import _ from 'lodash';
import { VALIDATION } from '../../session/constants';
2021-06-16 01:33:40 +02:00
import { SessionWrapperModal } from '../session/SessionWrapperModal';
2021-06-17 08:51:59 +02:00
import { SpacerLG } from '../basic/Text';
import { useDispatch } from 'react-redux';
import { updateInviteContactModal } from '../../state/ducks/modalDialog';
2021-06-16 01:33:40 +02:00
type Props = {
conversationId: string;
};
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const InviteContactsDialogInner = (props: Props) => {
const { conversationId } = props;
const theme = useTheme();
const dispatch = useDispatch();
const convo = ConversationController.getInstance().get(conversationId);
// tslint:disable-next-line: max-func-body-length
2019-11-22 06:16:43 +01:00
let contacts = ConversationController.getInstance()
.getConversations()
.filter(d => !!d && !d.isBlocked() && d.isPrivate() && !d.isMe() && !!d.get('active_at'));
2021-06-16 01:33:40 +02:00
if (!convo.isPublic()) {
// filter our zombies and current members from the list of contact we can add
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const members = convo.get('members') || [];
const zombies = convo.get('zombies') || [];
contacts = contacts.filter(d => !members.includes(d.id) && !zombies.includes(d.id));
2021-06-16 01:33:40 +02:00
}
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const chatName = convo.get('name');
// const chatServer = convo.get('server');
// const channelId = convo.get('channelId');
const isPublicConvo = convo.isPublic();
2020-02-04 08:07:31 +01:00
2021-06-16 01:33:40 +02:00
const [contactList, setContactList] = useState(
contacts.map((d: ConversationModel) => {
2019-11-22 06:16:43 +01:00
const lokiProfile = d.getLokiProfile();
const nickname = d.getNickname();
const name = nickname
? nickname
: lokiProfile
? lokiProfile.displayName
: window.i18n('anonymous');
2019-11-22 06:16:43 +01:00
// TODO: should take existing members into account
const existingMember = false;
return {
id: d.id,
authorPhoneNumber: d.id,
authorProfileName: name,
2021-06-16 01:33:40 +02:00
authorAvatarPath: d?.getAvatarPath() || '',
2019-11-22 06:16:43 +01:00
selected: false,
authorName: name,
checkmarked: false,
existingMember,
};
2021-06-16 01:33:40 +02:00
})
);
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const closeDialog = () => {
window.removeEventListener('keyup', onKeyUp);
dispatch(updateInviteContactModal(null));
};
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const onClickOK = () => {
const selectedContacts = contactList
.filter((d: ContactType) => d.checkmarked)
.map((d: ContactType) => d.id);
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
if (selectedContacts.length > 0) {
if (isPublicConvo) {
void submitForOpenGroup(selectedContacts);
} else {
void submitForClosedGroup(selectedContacts);
}
}
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
closeDialog();
};
2021-06-16 01:33:40 +02:00
const onKeyUp = (event: any) => {
switch (event.key) {
case 'Enter':
onClickOK();
break;
case 'Esc':
case 'Escape':
closeDialog();
break;
default:
}
};
2021-06-16 01:33:40 +02:00
window.addEventListener('keyup', onKeyUp);
const titleText = `${window.i18n('addingContacts')} ${chatName}`;
const cancelText = window.i18n('cancel');
const okText = window.i18n('ok');
const hasContacts = contactList.length !== 0;
2021-06-16 01:33:40 +02:00
const submitForOpenGroup = async (pubkeys: Array<string>) => {
if (convo.isOpenGroupV2()) {
const completeUrl = await getCompleteUrlForV2ConvoId(convo.id);
const groupInvitation = {
serverAddress: completeUrl,
serverName: convo.getName(),
};
pubkeys.forEach(async pubkeyStr => {
const privateConvo = await ConversationController.getInstance().getOrCreateAndWait(
pubkeyStr,
ConversationTypeEnum.PRIVATE
);
if (privateConvo) {
void privateConvo.sendMessage('', null, null, null, groupInvitation);
}
});
}
};
2021-06-16 01:33:40 +02:00
const submitForClosedGroup = async (pubkeys: Array<string>) => {
// closed group chats
const ourPK = UserUtils.getOurPubKeyStrFromCache();
// we only care about real members. If a member is currently a zombie we have to be able to add him back
let existingMembers = convo.get('members') || [];
// at least make sure it's an array
if (!Array.isArray(existingMembers)) {
existingMembers = [];
}
existingMembers = _.compact(existingMembers);
2021-05-05 10:14:59 +02:00
const existingZombies = convo.get('zombies') || [];
const newMembers = pubkeys.filter(d => !existingMembers.includes(d));
if (newMembers.length > 0) {
// Do not trigger an update if there is too many members
2021-05-05 10:14:59 +02:00
// be sure to include current zombies in this count
if (
newMembers.length + existingMembers.length + existingZombies.length >
VALIDATION.CLOSED_GROUP_SIZE_LIMIT
2021-05-05 10:14:59 +02:00
) {
ToastUtils.pushTooManyMembers();
return;
}
const allMembers = _.concat(existingMembers, newMembers, [ourPK]);
const uniqMembers = _.uniq(allMembers);
const groupId = convo.get('id');
const groupName = convo.get('name');
await initiateGroupUpdate(
groupId,
groupName || window.i18n('unknown'),
uniqMembers,
undefined
);
}
};
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const renderMemberList = () => {
const members = contactList;
const selectedContacts = contactList
.filter((d: ContactType) => d.checkmarked)
.map((d: ContactType) => d.id);
return members.map((member: ContactType, index: number) => (
<SessionMemberListItem
member={member}
2020-05-21 07:10:54 +02:00
key={index}
index={index}
isSelected={selectedContacts.some(m => m === member.id)}
onSelect={(selectedMember: ContactType) => {
2021-06-16 01:33:40 +02:00
onMemberClicked(selectedMember);
}}
onUnselect={(selectedMember: ContactType) => {
2021-06-16 01:33:40 +02:00
onMemberClicked(selectedMember);
}}
/>
));
};
2019-11-22 06:16:43 +01:00
2021-06-16 01:33:40 +02:00
const onMemberClicked = (clickedMember: ContactType) => {
const updatedContacts = contactList.map((member: ContactType) => {
if (member.id === clickedMember.id) {
2019-11-22 06:16:43 +01:00
return { ...member, checkmarked: !member.checkmarked };
} else {
return member;
}
});
2021-06-16 01:33:40 +02:00
setContactList(updatedContacts);
};
2021-06-16 01:33:40 +02:00
return (
<SessionWrapperModal title={titleText} onClose={closeDialog}>
2021-06-17 08:51:59 +02:00
<SpacerLG />
2021-06-16 01:33:40 +02:00
<div className="contact-selection-list">{renderMemberList()}</div>
{hasContacts ? null : (
<>
2021-06-17 08:51:59 +02:00
<SpacerLG />
2021-06-16 01:33:40 +02:00
<p className="no-contacts">{window.i18n('noContactsToAdd')}</p>
2021-06-17 08:51:59 +02:00
<SpacerLG />
2021-06-16 01:33:40 +02:00
</>
)}
2021-06-17 08:51:59 +02:00
<SpacerLG />
2021-06-16 01:33:40 +02:00
<div className="session-modal__button-group">
<SessionButton text={cancelText} onClick={closeDialog} />
<SessionButton
text={okText}
disabled={!hasContacts}
onClick={onClickOK}
buttonColor={SessionButtonColor.Green}
/>
</div>
</SessionWrapperModal>
);
};
export const InviteContactsDialog = InviteContactsDialogInner;