make RoomDetailRow reusable for the Room Directory
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
292b912038
commit
f0752572a7
2 changed files with 133 additions and 95 deletions
|
@ -18,121 +18,40 @@ import sdk from '../../../index';
|
||||||
import dis from '../../../dispatcher';
|
import dis from '../../../dispatcher';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { _t } from '../../../languageHandler';
|
import { _t } from '../../../languageHandler';
|
||||||
import linkifyString from 'linkifyjs/string';
|
|
||||||
import sanitizeHtml from 'sanitize-html';
|
|
||||||
import { ContentRepo } from 'matrix-js-sdk';
|
|
||||||
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
|
||||||
function getDisplayAliasForRoom(room) {
|
import {roomShape} from './RoomDetailRow';
|
||||||
return room.canonicalAlias || (room.aliases ? room.aliases[0] : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
const RoomDetailRow = React.createClass({
|
|
||||||
propTypes: {
|
|
||||||
room: PropTypes.shape({
|
|
||||||
name: PropTypes.string,
|
|
||||||
topic: PropTypes.string,
|
|
||||||
roomId: PropTypes.string,
|
|
||||||
avatarUrl: PropTypes.string,
|
|
||||||
numJoinedMembers: PropTypes.number,
|
|
||||||
canonicalAlias: PropTypes.string,
|
|
||||||
aliases: PropTypes.arrayOf(PropTypes.string),
|
|
||||||
|
|
||||||
worldReadable: PropTypes.bool,
|
|
||||||
guestCanJoin: PropTypes.bool,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
|
|
||||||
onClick: function(ev) {
|
|
||||||
ev.preventDefault();
|
|
||||||
dis.dispatch({
|
|
||||||
action: 'view_room',
|
|
||||||
room_id: this.props.room.roomId,
|
|
||||||
room_alias: this.props.room.canonicalAlias || (this.props.room.aliases || [])[0],
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
onTopicClick: function(ev) {
|
|
||||||
// When clicking a link in the topic, prevent the event being propagated
|
|
||||||
// to `onClick`.
|
|
||||||
ev.stopPropagation();
|
|
||||||
},
|
|
||||||
|
|
||||||
render: function() {
|
|
||||||
const BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
|
|
||||||
|
|
||||||
const room = this.props.room;
|
|
||||||
const name = room.name || getDisplayAliasForRoom(room) || _t('Unnamed room');
|
|
||||||
const topic = linkifyString(sanitizeHtml(room.topic || ''));
|
|
||||||
|
|
||||||
const guestRead = room.worldReadable ? (
|
|
||||||
<div className="mx_RoomDirectory_perm">{ _t('World readable') }</div>
|
|
||||||
) : <div />;
|
|
||||||
const guestJoin = room.guestCanJoin ? (
|
|
||||||
<div className="mx_RoomDirectory_perm">{ _t('Guests can join') }</div>
|
|
||||||
) : <div />;
|
|
||||||
|
|
||||||
const perms = (guestRead || guestJoin) ? (<div className="mx_RoomDirectory_perms">
|
|
||||||
{ guestRead }
|
|
||||||
{ guestJoin }
|
|
||||||
</div>) : <div />;
|
|
||||||
|
|
||||||
return <tr key={room.roomId} onClick={this.onClick}>
|
|
||||||
<td className="mx_RoomDirectory_roomAvatar">
|
|
||||||
<BaseAvatar width={24} height={24} resizeMethod='crop'
|
|
||||||
name={name} idName={name}
|
|
||||||
url={ContentRepo.getHttpUriForMxc(
|
|
||||||
MatrixClientPeg.get().getHomeserverUrl(),
|
|
||||||
room.avatarUrl, 24, 24, "crop")} />
|
|
||||||
</td>
|
|
||||||
<td className="mx_RoomDirectory_roomDescription">
|
|
||||||
<div className="mx_RoomDirectory_name">{ name }</div>
|
|
||||||
{ perms }
|
|
||||||
<div className="mx_RoomDirectory_topic"
|
|
||||||
onClick={this.onTopicClick}
|
|
||||||
dangerouslySetInnerHTML={{ __html: topic }} />
|
|
||||||
<div className="mx_RoomDirectory_alias">{ getDisplayAliasForRoom(room) }</div>
|
|
||||||
</td>
|
|
||||||
<td className="mx_RoomDirectory_roomMemberCount">
|
|
||||||
{ room.numJoinedMembers }
|
|
||||||
</td>
|
|
||||||
</tr>;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default React.createClass({
|
export default React.createClass({
|
||||||
displayName: 'RoomDetailList',
|
displayName: 'RoomDetailList',
|
||||||
|
|
||||||
propTypes: {
|
propTypes: {
|
||||||
rooms: PropTypes.arrayOf(PropTypes.shape({
|
rooms: PropTypes.arrayOf(roomShape),
|
||||||
name: PropTypes.string,
|
|
||||||
topic: PropTypes.string,
|
|
||||||
roomId: PropTypes.string,
|
|
||||||
avatarUrl: PropTypes.string,
|
|
||||||
numJoinedMembers: PropTypes.number,
|
|
||||||
canonicalAlias: PropTypes.string,
|
|
||||||
aliases: PropTypes.arrayOf(PropTypes.string),
|
|
||||||
|
|
||||||
worldReadable: PropTypes.bool,
|
|
||||||
guestCanJoin: PropTypes.bool,
|
|
||||||
})),
|
|
||||||
|
|
||||||
className: PropTypes.string,
|
className: PropTypes.string,
|
||||||
},
|
},
|
||||||
|
|
||||||
getRows: function() {
|
getRows: function() {
|
||||||
if (!this.props.rooms) return [];
|
if (!this.props.rooms) return [];
|
||||||
|
|
||||||
|
const RoomDetailRow = sdk.getComponent('rooms.RoomDetailRow');
|
||||||
return this.props.rooms.map((room, index) => {
|
return this.props.rooms.map((room, index) => {
|
||||||
return <RoomDetailRow key={index} room={room} />;
|
return <RoomDetailRow key={index} room={room} onClick={this.onDetailsClick} />;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onDetailsClick: function(ev, room) {
|
||||||
|
dis.dispatch({
|
||||||
|
action: 'view_room',
|
||||||
|
room_id: room.roomId,
|
||||||
|
room_alias: room.canonicalAlias || (room.aliases || [])[0],
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const rows = this.getRows();
|
const rows = this.getRows();
|
||||||
let rooms;
|
let rooms;
|
||||||
if (rows.length == 0) {
|
if (rows.length === 0) {
|
||||||
rooms = <i>{ _t('No rooms to show') }</i>;
|
rooms = <i>{ _t('No rooms to show') }</i>;
|
||||||
} else {
|
} else {
|
||||||
rooms = <table ref="directory_table" className="mx_RoomDirectory_table">
|
rooms = <table ref="directory_table" className="mx_RoomDirectory_table">
|
||||||
|
|
119
src/components/views/rooms/RoomDetailRow.js
Normal file
119
src/components/views/rooms/RoomDetailRow.js
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
/*
|
||||||
|
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 sdk from '../../../index';
|
||||||
|
import dis from '../../../dispatcher';
|
||||||
|
import React from 'react';
|
||||||
|
import { _t } from '../../../languageHandler';
|
||||||
|
import linkifyElement from 'linkifyjs/element';
|
||||||
|
import linkifyMatrix from '../../../linkify-matrix';
|
||||||
|
import { ContentRepo } from 'matrix-js-sdk';
|
||||||
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import classNames from 'classnames';
|
||||||
|
|
||||||
|
function getDisplayAliasForRoom(room) {
|
||||||
|
return room.canonicalAlias || (room.aliases ? room.aliases[0] : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
export const roomShape = PropTypes.shape({
|
||||||
|
name: PropTypes.string,
|
||||||
|
topic: PropTypes.string,
|
||||||
|
roomId: PropTypes.string,
|
||||||
|
avatarUrl: PropTypes.string,
|
||||||
|
numJoinedMembers: PropTypes.number,
|
||||||
|
canonicalAlias: PropTypes.string,
|
||||||
|
aliases: PropTypes.arrayOf(PropTypes.string),
|
||||||
|
|
||||||
|
worldReadable: PropTypes.bool,
|
||||||
|
guestCanJoin: PropTypes.bool,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default React.createClass({
|
||||||
|
propTypes: {
|
||||||
|
room: roomShape,
|
||||||
|
// passes ev, room as args
|
||||||
|
onClick: PropTypes.func,
|
||||||
|
onMouseDown: PropTypes.func,
|
||||||
|
},
|
||||||
|
|
||||||
|
_linkifyTopic: function() {
|
||||||
|
if (this.refs.topic) {
|
||||||
|
linkifyElement(this.refs.topic, linkifyMatrix.options);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
componentDidMount: function() {
|
||||||
|
this._linkifyTopic();
|
||||||
|
},
|
||||||
|
|
||||||
|
componentDidUpdate: function() {
|
||||||
|
this._linkifyTopic();
|
||||||
|
},
|
||||||
|
|
||||||
|
onClick: function(ev) {
|
||||||
|
ev.preventDefault();
|
||||||
|
if (this.props.onClick) {
|
||||||
|
this.props.onClick(ev, this.props.room);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onTopicClick: function(ev) {
|
||||||
|
// When clicking a link in the topic, prevent the event being propagated
|
||||||
|
// to `onClick`.
|
||||||
|
ev.stopPropagation();
|
||||||
|
},
|
||||||
|
|
||||||
|
render: function() {
|
||||||
|
const BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
|
||||||
|
|
||||||
|
const room = this.props.room;
|
||||||
|
const name = room.name || getDisplayAliasForRoom(room) || _t('Unnamed room');
|
||||||
|
|
||||||
|
const guestRead = room.worldReadable ? (
|
||||||
|
<div className="mx_RoomDirectory_perm">{ _t('World readable') }</div>
|
||||||
|
) : <div />;
|
||||||
|
const guestJoin = room.guestCanJoin ? (
|
||||||
|
<div className="mx_RoomDirectory_perm">{ _t('Guests can join') }</div>
|
||||||
|
) : <div />;
|
||||||
|
|
||||||
|
const perms = (guestRead || guestJoin) ? (<div className="mx_RoomDirectory_perms">
|
||||||
|
{ guestRead }
|
||||||
|
{ guestJoin }
|
||||||
|
</div>) : <div />;
|
||||||
|
|
||||||
|
return <tr key={room.roomId} onClick={this.onClick} onMouseDown={this.props.onMouseDown}>
|
||||||
|
<td className="mx_RoomDirectory_roomAvatar">
|
||||||
|
<BaseAvatar width={24} height={24} resizeMethod='crop'
|
||||||
|
name={name} idName={name}
|
||||||
|
url={ContentRepo.getHttpUriForMxc(
|
||||||
|
MatrixClientPeg.get().getHomeserverUrl(),
|
||||||
|
room.avatarUrl, 24, 24, "crop")} />
|
||||||
|
</td>
|
||||||
|
<td className="mx_RoomDirectory_roomDescription">
|
||||||
|
<div className="mx_RoomDirectory_name">{ name }</div>
|
||||||
|
{ perms }
|
||||||
|
<div className="mx_RoomDirectory_topic" ref="topic" onClick={this.onTopicClick}>
|
||||||
|
{ room.topic }
|
||||||
|
</div>
|
||||||
|
<div className="mx_RoomDirectory_alias">{ getDisplayAliasForRoom(room) }</div>
|
||||||
|
</td>
|
||||||
|
<td className="mx_RoomDirectory_roomMemberCount">
|
||||||
|
{ room.numJoinedMembers }
|
||||||
|
</td>
|
||||||
|
</tr>;
|
||||||
|
},
|
||||||
|
});
|
Loading…
Reference in a new issue