2017-03-03 16:48:37 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-21 13:37:06 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2017-03-03 16:48:37 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 16:48:37 +03:00
|
|
|
import sdk from '../../../index';
|
2017-06-01 16:16:25 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-03-03 16:48:37 +03:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import DMRoomMap from '../../../utils/DMRoomMap';
|
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
|
|
|
import Unread from '../../../Unread';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2017-03-13 02:50:12 +03:00
|
|
|
export default class ChatCreateOrReuseDialog extends React.Component {
|
2017-03-03 16:48:37 +03:00
|
|
|
|
2017-03-06 17:22:12 +03:00
|
|
|
constructor(props) {
|
2017-03-03 16:48:37 +03:00
|
|
|
super(props);
|
2018-06-21 13:37:06 +03:00
|
|
|
this.onFinished = this.onFinished.bind(this);
|
2017-03-06 20:44:29 +03:00
|
|
|
this.onRoomTileClick = this.onRoomTileClick.bind(this);
|
2017-03-03 16:48:37 +03:00
|
|
|
|
2017-06-01 16:16:25 +03:00
|
|
|
this.state = {
|
|
|
|
tiles: [],
|
|
|
|
profile: {
|
|
|
|
displayName: null,
|
|
|
|
avatarUrl: null,
|
|
|
|
},
|
2017-06-02 13:50:58 +03:00
|
|
|
profileError: null,
|
2017-06-01 16:16:25 +03:00
|
|
|
};
|
2017-03-03 16:48:37 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:25 +03:00
|
|
|
componentWillMount() {
|
2017-03-03 16:48:37 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
|
|
|
const dmRoomMap = new DMRoomMap(client);
|
|
|
|
const dmRooms = dmRoomMap.getDMRoomsForUserId(this.props.userId);
|
|
|
|
|
|
|
|
const RoomTile = sdk.getComponent("rooms.RoomTile");
|
|
|
|
|
|
|
|
const tiles = [];
|
|
|
|
for (const roomId of dmRooms) {
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (room) {
|
2018-08-02 12:42:05 +03:00
|
|
|
const isInvite = room.getMyMembership() === "invite";
|
|
|
|
const highlight = room.getUnreadNotificationCount('highlight') > 0 || isInvite;
|
2017-03-03 16:48:37 +03:00
|
|
|
tiles.push(
|
|
|
|
<RoomTile key={room.roomId} room={room}
|
2018-03-05 15:36:02 +03:00
|
|
|
transparent={true}
|
2017-03-03 16:48:37 +03:00
|
|
|
collapsed={false}
|
|
|
|
selected={false}
|
|
|
|
unread={Unread.doesRoomHaveUnreadMessages(room)}
|
|
|
|
highlight={highlight}
|
2018-08-02 12:42:05 +03:00
|
|
|
isInvite={isInvite}
|
2017-03-06 20:44:29 +03:00
|
|
|
onClick={this.onRoomTileClick}
|
2017-06-01 16:16:25 +03:00
|
|
|
/>,
|
2017-03-03 16:48:37 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:25 +03:00
|
|
|
this.setState({
|
|
|
|
tiles: tiles,
|
2017-03-03 16:48:37 +03:00
|
|
|
});
|
2017-06-01 16:16:25 +03:00
|
|
|
|
|
|
|
if (tiles.length === 0) {
|
|
|
|
this.setState({
|
|
|
|
busyProfile: true,
|
|
|
|
});
|
2017-06-02 13:50:58 +03:00
|
|
|
MatrixClientPeg.get().getProfileInfo(this.props.userId).done((resp) => {
|
2017-06-01 16:16:25 +03:00
|
|
|
const profile = {
|
|
|
|
displayName: resp.displayname,
|
|
|
|
avatarUrl: null,
|
|
|
|
};
|
|
|
|
if (resp.avatar_url) {
|
|
|
|
profile.avatarUrl = MatrixClientPeg.get().mxcUrlToHttp(
|
|
|
|
resp.avatar_url, 48, 48, "crop",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.setState({
|
2017-06-02 13:50:58 +03:00
|
|
|
busyProfile: false,
|
2017-06-01 16:16:25 +03:00
|
|
|
profile: profile,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-06-02 13:50:58 +03:00
|
|
|
console.error(
|
|
|
|
'Unable to get profile for user ' + this.props.userId + ':',
|
|
|
|
err,
|
|
|
|
);
|
2017-06-01 16:16:25 +03:00
|
|
|
this.setState({
|
|
|
|
busyProfile: false,
|
2017-06-02 13:50:58 +03:00
|
|
|
profileError: err,
|
2017-06-01 16:16:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onRoomTileClick(roomId) {
|
|
|
|
this.props.onExistingRoomSelected(roomId);
|
|
|
|
}
|
|
|
|
|
2018-06-21 13:37:06 +03:00
|
|
|
onFinished() {
|
|
|
|
this.props.onFinished(false);
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:25 +03:00
|
|
|
render() {
|
|
|
|
let title = '';
|
|
|
|
let content = null;
|
|
|
|
if (this.state.tiles.length > 0) {
|
|
|
|
// Show the existing rooms with a "+" to add a new dm
|
|
|
|
title = _t('Create a new chat or reuse an existing one');
|
|
|
|
const labelClasses = classNames({
|
|
|
|
mx_MemberInfo_createRoom_label: true,
|
|
|
|
mx_RoomTile_name: true,
|
|
|
|
});
|
|
|
|
const startNewChat = <AccessibleButton
|
|
|
|
className="mx_MemberInfo_createRoom"
|
|
|
|
onClick={this.props.onNewDMClick}
|
|
|
|
>
|
|
|
|
<div className="mx_RoomTile_avatar">
|
|
|
|
<img src="img/create-big.svg" width="26" height="26" />
|
|
|
|
</div>
|
|
|
|
<div className={labelClasses}><i>{ _t("Start new chat") }</i></div>
|
|
|
|
</AccessibleButton>;
|
2017-12-05 15:52:20 +03:00
|
|
|
content = <div className="mx_Dialog_content" id='mx_Dialog_content'>
|
2017-06-01 16:16:25 +03:00
|
|
|
{ _t('You already have existing direct chats with this user:') }
|
|
|
|
<div className="mx_ChatCreateOrReuseDialog_tiles">
|
|
|
|
{ this.state.tiles }
|
|
|
|
{ startNewChat }
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
} else {
|
|
|
|
// Show the avatar, name and a button to confirm that a new chat is requested
|
|
|
|
const BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
|
2017-12-23 06:15:28 +03:00
|
|
|
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
|
2017-06-01 16:16:25 +03:00
|
|
|
const Spinner = sdk.getComponent('elements.Spinner');
|
|
|
|
title = _t('Start chatting');
|
|
|
|
|
|
|
|
let profile = null;
|
|
|
|
if (this.state.busyProfile) {
|
|
|
|
profile = <Spinner />;
|
2017-06-02 13:50:58 +03:00
|
|
|
} else if (this.state.profileError) {
|
2017-12-05 15:52:20 +03:00
|
|
|
profile = <div className="error" role="alert">
|
2017-06-02 13:50:58 +03:00
|
|
|
Unable to load profile information for { this.props.userId }
|
|
|
|
</div>;
|
2017-06-01 16:16:25 +03:00
|
|
|
} else {
|
|
|
|
profile = <div className="mx_ChatCreateOrReuseDialog_profile">
|
|
|
|
<BaseAvatar
|
|
|
|
name={this.state.profile.displayName || this.props.userId}
|
|
|
|
url={this.state.profile.avatarUrl}
|
|
|
|
width={48} height={48}
|
|
|
|
/>
|
|
|
|
<div className="mx_ChatCreateOrReuseDialog_profile_name">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ this.state.profile.displayName || this.props.userId }
|
2017-06-01 16:16:25 +03:00
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
content = <div>
|
2017-12-05 15:52:20 +03:00
|
|
|
<div className="mx_Dialog_content" id='mx_Dialog_content'>
|
2017-06-01 16:16:25 +03:00
|
|
|
<p>
|
|
|
|
{ _t('Click on the button below to start chatting!') }
|
|
|
|
</p>
|
|
|
|
{ profile }
|
|
|
|
</div>
|
2017-12-23 06:15:28 +03:00
|
|
|
<DialogButtons primaryButton={_t('Start Chatting')}
|
2018-06-15 20:39:12 +03:00
|
|
|
onPrimaryButtonClick={this.props.onNewDMClick} focus={true} />
|
2017-06-01 16:16:25 +03:00
|
|
|
</div>;
|
|
|
|
}
|
2017-03-03 16:48:37 +03:00
|
|
|
|
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
|
|
|
return (
|
2017-03-13 02:50:12 +03:00
|
|
|
<BaseDialog className='mx_ChatCreateOrReuseDialog'
|
2018-06-21 13:37:06 +03:00
|
|
|
onFinished={this.onFinished}
|
2017-06-01 16:16:25 +03:00
|
|
|
title={title}
|
2017-12-05 15:52:20 +03:00
|
|
|
contentId='mx_Dialog_content'
|
2017-03-03 16:48:37 +03:00
|
|
|
>
|
2017-06-01 16:16:25 +03:00
|
|
|
{ content }
|
2017-03-03 16:48:37 +03:00
|
|
|
</BaseDialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 13:15:30 +03:00
|
|
|
ChatCreateOrReuseDialog.propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
userId: PropTypes.string.isRequired,
|
2017-06-01 16:16:25 +03:00
|
|
|
// Called when clicking outside of the dialog
|
2017-12-26 04:03:18 +03:00
|
|
|
onFinished: PropTypes.func.isRequired,
|
|
|
|
onNewDMClick: PropTypes.func.isRequired,
|
|
|
|
onExistingRoomSelected: PropTypes.func.isRequired,
|
2017-03-03 16:48:37 +03:00
|
|
|
};
|