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

173 lines
5.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { ConversationListItemWithDetails } from '../ConversationListItem';
2021-01-27 23:46:15 +01:00
import { RowRendererParamsType } from '../LeftPane';
2021-04-22 10:03:58 +02:00
import { SessionButton, SessionButtonColor, SessionButtonType } from './SessionButton';
import { AutoSizer, List } from 'react-virtualized';
2021-04-20 07:52:19 +02:00
import { ConversationType as ReduxConversationType } from '../../state/ducks/conversations';
2021-04-22 10:03:58 +02:00
import { SessionClosableOverlay, SessionClosableOverlayType } from './SessionClosableOverlay';
import { ToastUtils } from '../../session/utils';
import { DefaultTheme } from 'styled-components';
import { LeftPaneSectionHeader } from './LeftPaneSectionHeader';
import { ConversationController } from '../../session/conversations';
2021-01-27 23:46:15 +01:00
import { PubKey } from '../../session/types';
2021-04-20 07:52:19 +02:00
import { ConversationType } from '../../models/conversation';
export interface Props {
2021-04-20 07:52:19 +02:00
directContacts: Array<ReduxConversationType>;
theme: DefaultTheme;
2020-11-13 04:29:59 +01:00
openConversationExternal: (id: string, messageId?: string) => void;
}
2020-01-07 03:24:44 +01:00
interface State {
showAddContactView: boolean;
addContactRecipientID: string;
pubKeyPasted: string;
}
export class LeftPaneContactSection extends React.Component<Props, State> {
public constructor(props: Props) {
super(props);
this.state = {
showAddContactView: false,
addContactRecipientID: '',
2020-01-07 03:24:44 +01:00
pubKeyPasted: '',
};
this.handleToggleOverlay = this.handleToggleOverlay.bind(this);
this.handleOnAddContact = this.handleOnAddContact.bind(this);
2021-04-22 10:03:58 +02:00
this.handleRecipientSessionIDChanged = this.handleRecipientSessionIDChanged.bind(this);
this.closeOverlay = this.closeOverlay.bind(this);
}
2020-09-18 01:42:30 +02:00
public componentDidMount() {
window.Whisper.events.on('calculatingPoW', this.closeOverlay);
}
public componentWillUnmount() {
this.setState({ addContactRecipientID: '' });
window.Whisper.events.off('calculatingPoW', this.closeOverlay);
}
public renderHeader(): JSX.Element | undefined {
2021-04-22 10:03:58 +02:00
return <LeftPaneSectionHeader label={window.i18n('contactsHeader')} theme={this.props.theme} />;
}
public render(): JSX.Element {
return (
<div className="left-pane-contact-section">
{this.renderHeader()}
2021-04-22 10:03:58 +02:00
{this.state.showAddContactView ? this.renderClosableOverlay() : this.renderContacts()}
</div>
);
}
2021-04-22 10:03:58 +02:00
public renderRow = ({ index, key, style }: RowRendererParamsType): JSX.Element | undefined => {
const { directContacts } = this.props;
const item = directContacts[index];
2020-01-03 01:49:42 +01:00
return (
<ConversationListItemWithDetails
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
/>
);
};
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}
theme={this.props.theme}
2020-01-08 07:06:47 +01:00
/>
);
}
private closeOverlay({ pubKey }: { pubKey: string }) {
if (this.state.addContactRecipientID === pubKey) {
this.setState({ showAddContactView: false, addContactRecipientID: '' });
}
}
private handleToggleOverlay() {
2020-01-07 03:24:44 +01:00
this.setState((prevState: { showAddContactView: boolean }) => ({
showAddContactView: !prevState.showAddContactView,
}));
}
private handleOnAddContact() {
const sessionID = this.state.addContactRecipientID.trim();
2021-01-27 23:46:15 +01:00
const error = PubKey.validateWithError(sessionID, window.i18n);
if (error) {
ToastUtils.pushToastError('addContact', error);
} else {
2021-03-23 06:47:39 +01:00
void ConversationController.getInstance()
2021-04-20 07:52:19 +02:00
.getOrCreateAndWait(sessionID, ConversationType.PRIVATE)
.then(() => {
this.props.openConversationExternal(sessionID);
});
}
}
2020-01-29 04:41:12 +01:00
private handleRecipientSessionIDChanged(value: string) {
this.setState({ addContactRecipientID: value });
}
private renderContacts() {
return (
<div className="left-pane-contact-content">
{this.renderList()}
{this.renderBottomButtons()}
</div>
);
}
private renderBottomButtons(): JSX.Element {
const addContact = window.i18n('addContact');
2020-01-03 01:49:42 +01:00
return (
<div className="left-pane-contact-bottom-buttons">
<SessionButton
text={addContact}
buttonType={SessionButtonType.SquareOutline}
buttonColor={SessionButtonColor.Green}
onClick={this.handleToggleOverlay}
/>
</div>
);
}
private renderList() {
const { directContacts } = this.props;
const length = Number(directContacts.length);
const list = (
<div className="module-left-pane__list" key={0}>
<AutoSizer>
{({ height, width }) => (
<List
className="module-left-pane__virtual-list"
height={height}
directContacts={directContacts} // needed for change in props refresh
rowCount={length}
rowHeight={64}
rowRenderer={this.renderRow}
width={width}
autoHeight={false}
/>
)}
</AutoSizer>
</div>
);
return [list];
}
}