2019-12-27 03:40:59 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
2020-11-04 01:35:11 +01:00
|
|
|
import { ConversationListItemWithDetails } from '../ConversationListItem';
|
2019-12-30 05:27:52 +01:00
|
|
|
import { LeftPane, RowRendererParamsType } from '../LeftPane';
|
2019-12-30 03:33:38 +01:00
|
|
|
import {
|
|
|
|
SessionButton,
|
|
|
|
SessionButtonColor,
|
2020-01-03 01:49:42 +01:00
|
|
|
SessionButtonType,
|
2019-12-30 03:33:38 +01:00
|
|
|
} from './SessionButton';
|
2019-12-30 00:07:51 +01:00
|
|
|
import { AutoSizer, List } from 'react-virtualized';
|
2019-12-30 04:45:00 +01:00
|
|
|
import { validateNumber } from '../../types/PhoneNumber';
|
2019-12-30 07:41:13 +01:00
|
|
|
import { ConversationType } from '../../state/ducks/conversations';
|
2020-05-13 04:57:46 +02:00
|
|
|
import {
|
|
|
|
SessionClosableOverlay,
|
|
|
|
SessionClosableOverlayType,
|
|
|
|
} from './SessionClosableOverlay';
|
2020-08-14 02:56:22 +02:00
|
|
|
import { ToastUtils } from '../../session/utils';
|
2020-12-03 02:09:39 +01:00
|
|
|
import { DefaultTheme } from 'styled-components';
|
2020-12-14 12:20:09 +01:00
|
|
|
import { LeftPaneSectionHeader } from './LeftPaneSectionHeader';
|
2021-01-06 04:05:13 +01:00
|
|
|
import { ConversationController } from '../../session/conversations';
|
2019-12-30 04:45:00 +01:00
|
|
|
|
2019-12-27 03:40:59 +01:00
|
|
|
export interface Props {
|
2020-11-04 01:35:11 +01:00
|
|
|
directContacts: Array<ConversationType>;
|
2020-12-03 02:09:39 +01:00
|
|
|
theme: DefaultTheme;
|
2020-11-13 04:29:59 +01:00
|
|
|
openConversationExternal: (id: string, messageId?: string) => void;
|
2019-12-27 03:40:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 03:24:44 +01:00
|
|
|
interface State {
|
|
|
|
showAddContactView: boolean;
|
|
|
|
addContactRecipientID: string;
|
|
|
|
pubKeyPasted: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LeftPaneContactSection extends React.Component<Props, State> {
|
2019-12-27 03:40:59 +01:00
|
|
|
public constructor(props: Props) {
|
|
|
|
super(props);
|
2019-12-30 00:07:51 +01:00
|
|
|
this.state = {
|
|
|
|
showAddContactView: false,
|
2019-12-30 04:45:00 +01:00
|
|
|
addContactRecipientID: '',
|
2020-01-07 03:24:44 +01:00
|
|
|
pubKeyPasted: '',
|
2019-12-30 00:07:51 +01:00
|
|
|
};
|
2019-12-27 03:40:59 +01:00
|
|
|
|
2019-12-30 03:33:38 +01:00
|
|
|
this.handleToggleOverlay = this.handleToggleOverlay.bind(this);
|
2019-12-30 04:45:00 +01:00
|
|
|
this.handleOnAddContact = this.handleOnAddContact.bind(this);
|
2019-12-30 05:07:53 +01:00
|
|
|
this.handleRecipientSessionIDChanged = this.handleRecipientSessionIDChanged.bind(
|
|
|
|
this
|
|
|
|
);
|
2020-09-16 06:46:33 +02:00
|
|
|
this.closeOverlay = this.closeOverlay.bind(this);
|
2019-12-27 03:40:59 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 01:42:30 +02:00
|
|
|
public componentDidMount() {
|
|
|
|
window.Whisper.events.on('calculatingPoW', this.closeOverlay);
|
|
|
|
}
|
|
|
|
|
2019-12-27 03:40:59 +01:00
|
|
|
public componentWillUnmount() {
|
2019-12-30 05:07:53 +01:00
|
|
|
this.setState({ addContactRecipientID: '' });
|
2020-09-16 06:46:33 +02:00
|
|
|
window.Whisper.events.off('calculatingPoW', this.closeOverlay);
|
2019-12-30 00:07:51 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 03:33:38 +01:00
|
|
|
public renderHeader(): JSX.Element | undefined {
|
2020-12-14 12:20:09 +01:00
|
|
|
return (
|
|
|
|
<LeftPaneSectionHeader
|
|
|
|
label={window.i18n('contactsHeader')}
|
|
|
|
theme={this.props.theme}
|
|
|
|
/>
|
2019-12-30 03:33:38 +01:00
|
|
|
);
|
2019-12-27 03:40:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 06:50:46 +01:00
|
|
|
public render(): JSX.Element {
|
2019-12-30 00:07:51 +01:00
|
|
|
return (
|
|
|
|
<div className="left-pane-contact-section">
|
|
|
|
{this.renderHeader()}
|
2019-12-30 03:33:38 +01:00
|
|
|
{this.state.showAddContactView
|
2020-01-08 06:59:53 +01:00
|
|
|
? this.renderClosableOverlay()
|
2019-12-30 03:33:38 +01:00
|
|
|
: this.renderContacts()}
|
2019-12-30 00:07:51 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:49:42 +01:00
|
|
|
public renderRow = ({
|
|
|
|
index,
|
|
|
|
key,
|
|
|
|
style,
|
|
|
|
}: RowRendererParamsType): JSX.Element | undefined => {
|
2020-11-04 01:35:11 +01:00
|
|
|
const { directContacts } = this.props;
|
|
|
|
const item = directContacts[index];
|
2020-01-03 01:49:42 +01:00
|
|
|
|
|
|
|
return (
|
2020-09-14 08:06:24 +02:00
|
|
|
<ConversationListItemWithDetails
|
2020-11-04 01:35:11 +01:00
|
|
|
key={item.id}
|
2020-01-03 01:49:42 +01:00
|
|
|
style={style}
|
|
|
|
{...item}
|
|
|
|
i18n={window.i18n}
|
2020-11-13 04:29:59 +01:00
|
|
|
onClick={this.props.openConversationExternal}
|
2020-01-03 01:49:42 +01:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-01-08 06:59:53 +01:00
|
|
|
private renderClosableOverlay() {
|
|
|
|
return (
|
2020-01-08 07:06:47 +01:00
|
|
|
<SessionClosableOverlay
|
2020-05-11 02:37:40 +02:00
|
|
|
overlayMode={SessionClosableOverlayType.Contact}
|
2020-01-08 07:06:47 +01:00
|
|
|
onChangeSessionID={this.handleRecipientSessionIDChanged}
|
|
|
|
onCloseClick={this.handleToggleOverlay}
|
|
|
|
onButtonClick={this.handleOnAddContact}
|
2020-12-03 02:09:39 +01:00
|
|
|
theme={this.props.theme}
|
2020-01-08 07:06:47 +01:00
|
|
|
/>
|
2020-01-08 06:59:53 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-16 06:46:33 +02:00
|
|
|
private closeOverlay({ pubKey }: { pubKey: string }) {
|
|
|
|
if (this.state.addContactRecipientID === pubKey) {
|
|
|
|
this.setState({ showAddContactView: false, addContactRecipientID: '' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 03:33:38 +01:00
|
|
|
private handleToggleOverlay() {
|
2020-01-07 03:24:44 +01:00
|
|
|
this.setState((prevState: { showAddContactView: boolean }) => ({
|
2019-12-30 03:33:38 +01:00
|
|
|
showAddContactView: !prevState.showAddContactView,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:45:00 +01:00
|
|
|
private handleOnAddContact() {
|
2020-01-30 04:48:45 +01:00
|
|
|
const sessionID = this.state.addContactRecipientID.trim();
|
2019-12-30 04:45:00 +01:00
|
|
|
const error = validateNumber(sessionID, window.i18n);
|
|
|
|
|
|
|
|
if (error) {
|
2020-11-09 01:24:40 +01:00
|
|
|
ToastUtils.pushToastError('addContact', error);
|
2019-12-30 04:45:00 +01:00
|
|
|
} else {
|
2020-11-13 07:17:48 +01:00
|
|
|
// tslint:disable-next-line: no-floating-promises
|
2021-01-06 04:05:13 +01:00
|
|
|
ConversationController.getInstance()
|
|
|
|
.getOrCreateAndWait(sessionID, 'private')
|
|
|
|
.then(() => {
|
|
|
|
this.props.openConversationExternal(sessionID);
|
|
|
|
});
|
2019-12-30 04:45:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 04:41:12 +01:00
|
|
|
private handleRecipientSessionIDChanged(value: string) {
|
|
|
|
this.setState({ addContactRecipientID: value });
|
2019-12-30 04:45:00 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:07:51 +01:00
|
|
|
private renderContacts() {
|
|
|
|
return (
|
|
|
|
<div className="left-pane-contact-content">
|
|
|
|
{this.renderList()}
|
|
|
|
{this.renderBottomButtons()}
|
2019-12-30 03:33:38 +01:00
|
|
|
</div>
|
|
|
|
);
|
2019-12-30 00:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private renderBottomButtons(): JSX.Element {
|
|
|
|
const addContact = window.i18n('addContact');
|
2020-01-03 01:49:42 +01:00
|
|
|
|
2019-12-30 00:07:51 +01:00
|
|
|
return (
|
|
|
|
<div className="left-pane-contact-bottom-buttons">
|
2020-08-18 04:31:03 +02:00
|
|
|
<SessionButton
|
|
|
|
text={addContact}
|
|
|
|
buttonType={SessionButtonType.SquareOutline}
|
|
|
|
buttonColor={SessionButtonColor.Green}
|
|
|
|
onClick={this.handleToggleOverlay}
|
|
|
|
/>
|
2019-12-30 00:07:51 +01:00
|
|
|
</div>
|
2019-12-30 03:33:38 +01:00
|
|
|
);
|
2019-12-27 03:40:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:07:51 +01:00
|
|
|
private renderList() {
|
2020-11-04 01:35:11 +01:00
|
|
|
const { directContacts } = this.props;
|
|
|
|
const length = Number(directContacts.length);
|
2020-01-03 01:12:43 +01:00
|
|
|
|
2019-12-30 00:07:51 +01:00
|
|
|
const list = (
|
|
|
|
<div className="module-left-pane__list" key={0}>
|
2019-12-30 03:33:38 +01:00
|
|
|
<AutoSizer>
|
|
|
|
{({ height, width }) => (
|
|
|
|
<List
|
|
|
|
className="module-left-pane__virtual-list"
|
|
|
|
height={height}
|
2020-11-04 01:35:11 +01:00
|
|
|
directContacts={directContacts} // needed for change in props refresh
|
2019-12-30 07:41:13 +01:00
|
|
|
rowCount={length}
|
2019-12-30 03:33:38 +01:00
|
|
|
rowHeight={64}
|
|
|
|
rowRenderer={this.renderRow}
|
|
|
|
width={width}
|
2020-07-30 03:19:52 +02:00
|
|
|
autoHeight={false}
|
2019-12-30 03:33:38 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2019-12-30 00:07:51 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return [list];
|
|
|
|
}
|
2019-12-27 03:40:59 +01:00
|
|
|
}
|