2017-11-21 14:50:41 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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 PropTypes from 'prop-types';
|
2019-09-06 20:35:52 +03:00
|
|
|
import createReactClass from 'create-react-class';
|
2018-02-14 20:53:54 +03:00
|
|
|
import { Draggable, Droppable } from 'react-beautiful-dnd';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from '../../../dispatcher/dispatcher';
|
2017-11-21 14:50:41 +03:00
|
|
|
import FlairStore from '../../../stores/FlairStore';
|
2019-12-17 20:26:12 +03:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2017-11-21 14:50:41 +03:00
|
|
|
|
2018-06-15 20:55:21 +03:00
|
|
|
function nop() {}
|
2018-02-14 19:46:06 +03:00
|
|
|
|
2019-09-06 20:35:52 +03:00
|
|
|
const GroupTile = createReactClass({
|
2017-11-21 14:50:41 +03:00
|
|
|
displayName: 'GroupTile',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
groupId: PropTypes.string.isRequired,
|
|
|
|
// Whether to show the short description of the group on the tile
|
|
|
|
showDescription: PropTypes.bool,
|
|
|
|
// Height of the group avatar in pixels
|
|
|
|
avatarHeight: PropTypes.number,
|
2019-02-22 21:31:17 +03:00
|
|
|
draggable: PropTypes.bool,
|
2017-11-21 14:50:41 +03:00
|
|
|
},
|
|
|
|
|
2019-12-17 20:26:12 +03:00
|
|
|
statics: {
|
|
|
|
contextType: MatrixClientContext,
|
2017-11-21 14:50:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
profile: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
showDescription: true,
|
|
|
|
avatarHeight: 50,
|
2019-02-22 21:31:17 +03:00
|
|
|
draggable: true,
|
2017-11-21 14:50:41 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-03-31 23:14:17 +03:00
|
|
|
componentDidMount: function() {
|
2019-12-17 20:26:12 +03:00
|
|
|
FlairStore.getGroupProfileCached(this.context, this.props.groupId).then((profile) => {
|
2017-11-21 14:50:41 +03:00
|
|
|
this.setState({profile});
|
2017-11-28 13:11:25 +03:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Error whilst getting cached profile for GroupTile', err);
|
2017-11-21 14:50:41 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-02-28 21:31:45 +03:00
|
|
|
onMouseDown: function(e) {
|
2017-11-21 14:50:41 +03:00
|
|
|
e.preventDefault();
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_group',
|
|
|
|
group_id: this.props.groupId,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
const BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
|
|
|
|
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
|
|
|
|
const profile = this.state.profile || {};
|
|
|
|
const name = profile.name || this.props.groupId;
|
|
|
|
const avatarHeight = this.props.avatarHeight;
|
|
|
|
const descElement = this.props.showDescription ?
|
|
|
|
<div className="mx_GroupTile_desc">{ profile.shortDescription }</div> :
|
|
|
|
<div />;
|
2019-12-17 20:26:12 +03:00
|
|
|
const httpUrl = profile.avatarUrl ? this.context.mxcUrlToHttp(
|
2019-02-22 21:31:17 +03:00
|
|
|
profile.avatarUrl, avatarHeight, avatarHeight, "crop") : null;
|
|
|
|
|
|
|
|
let avatarElement = (
|
|
|
|
<div className="mx_GroupTile_avatar">
|
|
|
|
<BaseAvatar
|
|
|
|
name={name}
|
|
|
|
idName={this.props.groupId}
|
|
|
|
url={httpUrl}
|
|
|
|
width={avatarHeight}
|
|
|
|
height={avatarHeight} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
if (this.props.draggable) {
|
|
|
|
const avatarClone = avatarElement;
|
|
|
|
avatarElement = (
|
|
|
|
<Droppable droppableId="my-groups-droppable" type="draggable-TagTile">
|
|
|
|
{ (droppableProvided, droppableSnapshot) => (
|
|
|
|
<div ref={droppableProvided.innerRef}>
|
|
|
|
<Draggable
|
|
|
|
key={"GroupTile " + this.props.groupId}
|
|
|
|
draggableId={"GroupTile " + this.props.groupId}
|
|
|
|
index={this.props.groupId}
|
|
|
|
type="draggable-TagTile"
|
|
|
|
>
|
|
|
|
{ (provided, snapshot) => (
|
|
|
|
<div>
|
|
|
|
<div
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
>
|
|
|
|
{avatarClone}
|
|
|
|
</div>
|
|
|
|
{ /* Instead of a blank placeholder, use a copy of the avatar itself. */ }
|
|
|
|
{ provided.placeholder ? avatarClone : <div /> }
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</Draggable>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</Droppable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:31:45 +03:00
|
|
|
// XXX: Use onMouseDown as a workaround for https://github.com/atlassian/react-beautiful-dnd/issues/273
|
|
|
|
// instead of onClick. Otherwise we experience https://github.com/vector-im/riot-web/issues/6156
|
2018-06-15 20:55:21 +03:00
|
|
|
return <AccessibleButton className="mx_GroupTile" onMouseDown={this.onMouseDown} onClick={nop}>
|
2019-02-22 21:31:17 +03:00
|
|
|
{ avatarElement }
|
2017-11-21 14:50:41 +03:00
|
|
|
<div className="mx_GroupTile_profile">
|
|
|
|
<div className="mx_GroupTile_name">{ name }</div>
|
|
|
|
{ descElement }
|
|
|
|
<div className="mx_GroupTile_groupId">{ this.props.groupId }</div>
|
|
|
|
</div>
|
|
|
|
</AccessibleButton>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default GroupTile;
|