2017-09-26 16:49:13 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 New Vector Ltd.
|
|
|
|
|
|
|
|
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';
|
|
|
|
import { _t } from '../../../languageHandler';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2018-05-01 13:18:45 +03:00
|
|
|
import GroupStore from '../../../stores/GroupStore';
|
2017-09-26 16:49:13 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-10 19:21:55 +03:00
|
|
|
import { showGroupAddRoomDialog } from '../../../GroupAddressPicker';
|
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2020-03-14 02:22:13 +03:00
|
|
|
import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
|
2017-09-26 16:49:13 +03:00
|
|
|
|
|
|
|
const INITIAL_LOAD_NUM_ROOMS = 30;
|
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class GroupRoomList extends React.Component {
|
|
|
|
static propTypes = {
|
2017-09-26 16:49:13 +03:00
|
|
|
groupId: PropTypes.string.isRequired,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
state = {
|
|
|
|
rooms: null,
|
|
|
|
truncateAt: INITIAL_LOAD_NUM_ROOMS,
|
|
|
|
searchQuery: "",
|
|
|
|
};
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidMount() {
|
2017-09-26 16:49:13 +03:00
|
|
|
this._unmounted = false;
|
2017-10-05 16:30:04 +03:00
|
|
|
this._initGroupStore(this.props.groupId);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2018-05-01 13:18:45 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
this._unmounted = true;
|
|
|
|
this._unregisterGroupStore();
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-05-01 13:18:45 +03:00
|
|
|
|
|
|
|
_unregisterGroupStore() {
|
|
|
|
GroupStore.unregisterListener(this.onGroupStoreUpdated);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-05-01 13:18:45 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_initGroupStore(groupId) {
|
2018-05-01 13:18:45 +03:00
|
|
|
GroupStore.registerListener(groupId, this.onGroupStoreUpdated);
|
|
|
|
// XXX: This should be more fluxy - let's get the error from GroupStore .getError or something
|
|
|
|
// XXX: This is also leaked - we should remove it when unmounting
|
|
|
|
GroupStore.on('error', (err, errorGroupId) => {
|
|
|
|
if (errorGroupId !== groupId) return;
|
2017-09-26 16:49:13 +03:00
|
|
|
this.setState({
|
2017-10-05 16:30:04 +03:00
|
|
|
rooms: null,
|
2017-09-26 16:49:13 +03:00
|
|
|
});
|
2017-10-05 16:30:04 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-10-05 16:30:04 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onGroupStoreUpdated = () => {
|
2017-10-05 16:30:04 +03:00
|
|
|
if (this._unmounted) return;
|
|
|
|
this.setState({
|
2018-05-01 13:18:45 +03:00
|
|
|
rooms: GroupStore.getGroupRooms(this.props.groupId),
|
2017-09-26 16:49:13 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_createOverflowTile = (overflowCount, totalCount) => {
|
2017-09-26 16:49:13 +03:00
|
|
|
// For now we'll pretend this is any entity. It should probably be a separate tile.
|
|
|
|
const EntityTile = sdk.getComponent("rooms.EntityTile");
|
|
|
|
const BaseAvatar = sdk.getComponent("avatars.BaseAvatar");
|
|
|
|
const text = _t("and %(count)s others...", { count: overflowCount });
|
|
|
|
return (
|
|
|
|
<EntityTile className="mx_EntityTile_ellipsis" avatarJsx={
|
2019-01-11 04:37:28 +03:00
|
|
|
<BaseAvatar url={require("../../../../res/img/ellipsis.svg")} name="..." width={36} height={36} />
|
2017-09-26 16:49:13 +03:00
|
|
|
} name={text} presenceState="online" suppressOnHover={true}
|
|
|
|
onClick={this._showFullRoomList} />
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_showFullRoomList = () => {
|
2017-09-26 16:49:13 +03:00
|
|
|
this.setState({
|
|
|
|
truncateAt: -1,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onSearchQueryChanged = ev => {
|
2017-09-26 16:49:13 +03:00
|
|
|
this.setState({ searchQuery: ev.target.value });
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onAddRoomToGroupButtonClick = () => {
|
2018-12-10 19:21:55 +03:00
|
|
|
showGroupAddRoomDialog(this.props.groupId).then(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-12-10 19:21:55 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
makeGroupRoomTiles(query) {
|
2017-09-26 16:49:13 +03:00
|
|
|
const GroupRoomTile = sdk.getComponent("groups.GroupRoomTile");
|
|
|
|
query = (query || "").toLowerCase();
|
|
|
|
|
|
|
|
let roomList = this.state.rooms;
|
|
|
|
if (query) {
|
|
|
|
roomList = roomList.filter((room) => {
|
2017-11-08 13:55:51 +03:00
|
|
|
const matchesName = (room.name || "").toLowerCase().includes(query);
|
2017-09-26 16:49:13 +03:00
|
|
|
const matchesAlias = (room.canonicalAlias || "").toLowerCase().includes(query);
|
|
|
|
return matchesName || matchesAlias;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
roomList = roomList.map((groupRoom, index) => {
|
|
|
|
return (
|
|
|
|
<GroupRoomTile
|
|
|
|
key={index}
|
|
|
|
groupId={this.props.groupId}
|
|
|
|
groupRoom={groupRoom} />
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return roomList;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-09-26 16:49:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2017-10-05 16:30:04 +03:00
|
|
|
if (this.state.rooms === null) {
|
2017-09-26 16:49:13 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:21:55 +03:00
|
|
|
let inviteButton;
|
|
|
|
if (GroupStore.isUserPrivileged(this.props.groupId)) {
|
2018-12-11 13:09:11 +03:00
|
|
|
inviteButton = (
|
|
|
|
<AccessibleButton
|
2020-07-20 16:47:06 +03:00
|
|
|
className="mx_MemberList_invite mx_MemberList_addRoomToCommunity"
|
2018-12-11 13:09:11 +03:00
|
|
|
onClick={this.onAddRoomToGroupButtonClick}
|
|
|
|
>
|
2020-07-20 16:47:06 +03:00
|
|
|
<span>{ _t('Add rooms to this community') }</span>
|
2018-12-11 13:09:11 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
);
|
2018-12-10 19:21:55 +03:00
|
|
|
}
|
2017-09-26 16:49:13 +03:00
|
|
|
const inputBox = (
|
2018-12-10 19:45:30 +03:00
|
|
|
<input className="mx_GroupRoomList_query mx_textinput" id="mx_GroupRoomList_query" type="text"
|
|
|
|
onChange={this.onSearchQueryChanged} value={this.state.searchQuery}
|
2018-12-11 12:54:36 +03:00
|
|
|
placeholder={_t('Filter community rooms')} autoComplete="off" />
|
2017-09-26 16:49:13 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const TruncatedList = sdk.getComponent("elements.TruncatedList");
|
|
|
|
return (
|
2019-10-03 11:35:39 +03:00
|
|
|
<div className="mx_GroupRoomList" role="tabpanel">
|
2018-12-10 19:21:55 +03:00
|
|
|
{ inviteButton }
|
2020-03-14 02:22:13 +03:00
|
|
|
<AutoHideScrollbar className="mx_GroupRoomList_joined mx_GroupRoomList_outerWrapper">
|
2017-09-26 16:49:13 +03:00
|
|
|
<TruncatedList className="mx_GroupRoomList_wrapper" truncateAt={this.state.truncateAt}
|
|
|
|
createOverflowElement={this._createOverflowTile}>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ this.makeGroupRoomTiles(this.state.searchQuery) }
|
2017-09-26 16:49:13 +03:00
|
|
|
</TruncatedList>
|
2020-03-14 02:22:13 +03:00
|
|
|
</AutoHideScrollbar>
|
2018-12-10 19:21:55 +03:00
|
|
|
{ inputBox }
|
2017-09-26 16:49:13 +03:00
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|