session-desktop/ts/components/session/SessionClosableOverlay.tsx

291 lines
8.1 KiB
TypeScript
Raw Normal View History

import React from 'react';
2020-01-08 07:06:47 +01:00
import { SessionIconButton, SessionIconSize, SessionIconType } from './icon';
import { SessionIdEditable } from './SessionIdEditable';
import { UserSearchDropdown } from './UserSearchDropdown';
2020-02-04 08:07:31 +01:00
import { ContactType, SessionMemberListItem } from './SessionMemberListItem';
2020-02-04 01:55:01 +01:00
import { ConversationType } from '../../state/ducks/conversations';
import {
2020-01-08 07:06:47 +01:00
SessionButton,
SessionButtonColor,
SessionButtonType,
} from './SessionButton';
import { SessionSpinner } from './SessionSpinner';
2020-02-04 01:55:01 +01:00
import { SessionGroupType } from './LeftPaneChannelSection';
interface Props {
2020-02-04 01:55:01 +01:00
overlayMode: 'message' | 'contact' | SessionGroupType;
2020-01-08 07:06:47 +01:00
onChangeSessionID: any;
onCloseClick: any;
onButtonClick: any;
2020-02-04 08:07:31 +01:00
contacts?: Array<ConversationType>;
2020-01-08 07:06:47 +01:00
searchTerm?: string;
searchResults?: any;
updateSearch?: any;
showSpinner?: boolean;
}
2020-02-04 08:07:31 +01:00
interface State {
groupName: string;
selectedMembers: Array<ContactType>;
}
export class SessionClosableOverlay extends React.Component<Props, State> {
2020-01-28 06:39:12 +01:00
private readonly inputRef: React.RefObject<SessionIdEditable>;
2020-01-08 07:06:47 +01:00
public constructor(props: Props) {
super(props);
2020-01-28 06:39:12 +01:00
2020-02-04 08:07:31 +01:00
this.state = {
groupName: '',
selectedMembers: [],
};
2020-01-28 06:39:12 +01:00
this.inputRef = React.createRef();
2020-02-04 08:14:18 +01:00
this.onGroupNameChanged = this.onGroupNameChanged.bind(this);
2020-01-28 06:39:12 +01:00
}
public componentDidMount() {
if (this.inputRef.current) {
this.inputRef.current.focus();
}
2020-01-08 07:06:47 +01:00
}
2020-02-04 08:07:31 +01:00
public getContacts() {
const conversations = window.getConversations() || [];
2020-02-04 08:07:31 +01:00
const conversationList = conversations.filter((conversation: any) => {
return (
2020-02-11 01:02:06 +01:00
!conversation.isMe() &&
conversation.isPrivate() &&
!conversation.isSecondaryDevice() &&
conversation.isFriend()
);
});
2020-02-04 08:07:31 +01:00
return conversationList.map((d: any) => {
2020-02-04 08:07:31 +01:00
const lokiProfile = d.getLokiProfile();
const name = lokiProfile ? lokiProfile.displayName : 'Anonymous';
// TODO: should take existing members into account
const existingMember = false;
return {
id: d.id,
authorPhoneNumber: d.id,
authorProfileName: name,
selected: false,
authorName: name,
authorColor: d.getColor(),
checkmarked: false,
existingMember,
};
});
}
2020-02-05 04:19:01 +01:00
// tslint:disable-next-line max-func-body-length */
2020-01-08 07:06:47 +01:00
public render(): JSX.Element {
const {
overlayMode,
onCloseClick,
onChangeSessionID,
showSpinner,
searchTerm,
updateSearch,
searchResults,
onButtonClick,
} = this.props;
2020-01-08 07:06:47 +01:00
const isAddContactView = overlayMode === 'contact';
const isMessageView = overlayMode === 'message';
2020-02-04 01:55:01 +01:00
2020-02-04 08:07:31 +01:00
const isOpenGroupView = overlayMode === SessionGroupType.Open;
2020-02-04 01:55:01 +01:00
const isClosedGroupView = overlayMode === SessionGroupType.Closed;
2020-01-08 07:06:47 +01:00
let title;
let buttonText;
let descriptionLong;
let subtitle;
let placeholder;
switch (overlayMode) {
case 'message':
2020-02-04 01:55:01 +01:00
title = window.i18n('newSession');
2020-01-08 07:06:47 +01:00
buttonText = window.i18n('next');
descriptionLong = window.i18n('usersCanShareTheir...');
subtitle = window.i18n('enterSessionID');
placeholder = window.i18n('pasteSessionIDRecipient');
break;
case 'contact':
title = window.i18n('addContact');
2020-01-21 01:23:12 +01:00
buttonText = window.i18n('next');
2020-01-08 07:06:47 +01:00
descriptionLong = window.i18n('usersCanShareTheir...');
subtitle = window.i18n('enterSessionID');
placeholder = window.i18n('pasteSessionIDRecipient');
break;
2020-02-04 01:55:01 +01:00
case 'open-group':
2020-01-08 07:06:47 +01:00
title = window.i18n('addChannel');
buttonText = window.i18n('joinChannel');
descriptionLong = window.i18n('addChannelDescription');
subtitle = window.i18n('enterChannelURL');
placeholder = window.i18n('channelUrlPlaceholder');
2020-02-04 01:55:01 +01:00
break;
case 'closed-group':
2020-02-05 04:19:01 +01:00
title = window.i18n('newClosedGroup');
2020-02-04 01:55:01 +01:00
buttonText = window.i18n('createClosedGroup');
descriptionLong = window.i18n('createClosedGroupDescription');
subtitle = window.i18n('createClosedGroupNamePrompt');
placeholder = window.i18n('createClosedGroupPlaceholder');
break;
default:
2020-01-08 07:06:47 +01:00
}
2020-02-04 08:07:31 +01:00
const { groupName, selectedMembers } = this.state;
2020-01-08 07:06:47 +01:00
const ourSessionID = window.textsecure.storage.user.getNumber();
2020-02-05 04:19:01 +01:00
const contacts = this.getContacts();
const noContactsForClosedGroup =
overlayMode === SessionGroupType.Closed && contacts.length === 0;
2020-01-08 07:06:47 +01:00
return (
<div className="module-left-pane-overlay">
<div className="exit">
<SessionIconButton
iconSize={SessionIconSize.Small}
iconType={SessionIconType.Exit}
onClick={onCloseClick}
/>
</div>
2020-02-04 08:07:31 +01:00
<div className="spacer-md" />
2020-01-08 07:06:47 +01:00
<h2>{title}</h2>
2020-02-05 04:19:01 +01:00
<h3>
{subtitle}
<hr className="green-border" />
</h3>
<hr className="white-border" />
{isOpenGroupView || isClosedGroupView ? (
<div className="create-group-name-input">
2020-02-04 08:07:31 +01:00
<SessionIdEditable
ref={this.inputRef}
2020-02-05 04:19:01 +01:00
editable={!noContactsForClosedGroup}
2020-02-04 08:07:31 +01:00
placeholder={placeholder}
value={this.state.groupName}
maxLength={window.CONSTANTS.MAX_GROUPNAME_LENGTH}
onChange={this.onGroupNameChanged}
2020-02-04 08:07:31 +01:00
/>
2020-02-05 04:19:01 +01:00
{/* */}
</div>
) : (
<SessionIdEditable
ref={this.inputRef}
editable={true}
placeholder={placeholder}
onChange={onChangeSessionID}
/>
)}
2020-02-05 00:08:55 +01:00
2020-01-08 07:06:47 +01:00
{showSpinner && <SessionSpinner />}
2020-02-04 01:55:01 +01:00
{isClosedGroupView && (
2020-02-04 08:07:31 +01:00
<>
<div className="spacer-lg" />
2020-02-04 08:07:31 +01:00
<div className="group-member-list__container">
2020-02-05 04:19:01 +01:00
{noContactsForClosedGroup ? (
<div className="group-member-list__no-contacts">
{window.i18n('noContactsForGroup')}
</div>
) : (
<div className="group-member-list__selection">
{this.renderMemberList()}
</div>
)}
2020-02-04 08:07:31 +01:00
</div>
<div className="spacer-lg" />
2020-02-04 08:07:31 +01:00
</>
2020-02-04 01:55:01 +01:00
)}
2020-01-08 07:06:47 +01:00
<div className="session-description-long">{descriptionLong}</div>
{isMessageView && <h4>{window.i18n('or')}</h4>}
2020-01-08 07:06:47 +01:00
{isMessageView && (
<UserSearchDropdown
searchTerm={searchTerm || ''}
updateSearch={updateSearch}
placeholder={window.i18n('searchByIDOrDisplayName')}
searchResults={searchResults}
/>
)}
2020-01-08 07:06:47 +01:00
{isAddContactView && (
<div className="panel-text-divider">
<span>{window.i18n('yourPublicKey')}</span>
</div>
)}
2020-01-08 07:06:47 +01:00
{isAddContactView && (
<SessionIdEditable
editable={false}
placeholder=""
text={ourSessionID}
/>
)}
2020-02-04 01:55:01 +01:00
2020-01-08 07:06:47 +01:00
<SessionButton
buttonColor={SessionButtonColor.Green}
buttonType={SessionButtonType.BrandOutline}
text={buttonText}
2020-02-05 04:19:01 +01:00
disabled={noContactsForClosedGroup}
2020-02-04 08:07:31 +01:00
onClick={() => onButtonClick(groupName, selectedMembers)}
2020-01-08 07:06:47 +01:00
/>
</div>
);
}
2020-02-04 08:07:31 +01:00
private renderMemberList() {
const members = this.getContacts();
return members.map((member: ContactType) => (
<SessionMemberListItem
member={member}
isSelected={false}
onSelect={(selectedMember: ContactType) => {
this.handleSelectMember(selectedMember);
}}
onUnselect={(selectedMember: ContactType) => {
this.handleUnselectMember(selectedMember);
}}
/>
));
2020-02-04 08:07:31 +01:00
}
private handleSelectMember(member: ContactType) {
if (this.state.selectedMembers.includes(member)) {
2020-02-04 08:07:31 +01:00
return;
}
2020-02-04 08:07:31 +01:00
this.setState({
selectedMembers: [...this.state.selectedMembers, member],
2020-02-04 08:07:31 +01:00
});
}
private handleUnselectMember(member: ContactType) {
2020-02-04 08:07:31 +01:00
this.setState({
selectedMembers: this.state.selectedMembers.filter(selectedMember => {
return selectedMember !== member;
}),
2020-02-04 08:07:31 +01:00
});
}
private onGroupNameChanged(event: any) {
this.setState({
2020-02-04 23:59:55 +01:00
groupName: event,
2020-02-04 08:07:31 +01:00
});
}
}