2017-08-30 12:55:25 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-10-19 12:28:59 +03:00
|
|
|
import FlairStore from '../../../stores/FlairStore';
|
2017-09-21 15:25:36 +03:00
|
|
|
import dis from '../../../dispatcher';
|
2019-12-17 20:26:12 +03:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2017-08-30 12:55:25 +03:00
|
|
|
|
|
|
|
|
2017-09-21 15:25:36 +03:00
|
|
|
class FlairAvatar extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.onClick = this.onClick.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
// Don't trigger onClick of parent element
|
|
|
|
ev.stopPropagation();
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_group',
|
2017-09-21 16:22:43 +03:00
|
|
|
group_id: this.props.groupProfile.groupId,
|
2017-09-21 15:25:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-12-17 20:26:12 +03:00
|
|
|
const httpUrl = this.context.mxcUrlToHttp(
|
2017-10-23 16:56:24 +03:00
|
|
|
this.props.groupProfile.avatarUrl, 16, 16, 'scale', false);
|
2017-11-09 15:38:43 +03:00
|
|
|
const tooltip = this.props.groupProfile.name ?
|
|
|
|
`${this.props.groupProfile.name} (${this.props.groupProfile.groupId})`:
|
|
|
|
this.props.groupProfile.groupId;
|
2017-09-27 16:35:59 +03:00
|
|
|
return <img
|
|
|
|
src={httpUrl}
|
2017-10-23 16:56:24 +03:00
|
|
|
width="16"
|
|
|
|
height="16"
|
2017-09-27 16:35:59 +03:00
|
|
|
onClick={this.onClick}
|
2019-05-28 14:19:35 +03:00
|
|
|
title={tooltip} />;
|
2017-09-21 15:25:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FlairAvatar.propTypes = {
|
2017-09-21 16:22:43 +03:00
|
|
|
groupProfile: PropTypes.shape({
|
|
|
|
groupId: PropTypes.string.isRequired,
|
2017-11-10 14:43:05 +03:00
|
|
|
name: PropTypes.string,
|
2017-09-21 16:22:43 +03:00
|
|
|
avatarUrl: PropTypes.string.isRequired,
|
|
|
|
}),
|
2017-09-21 15:25:36 +03:00
|
|
|
};
|
|
|
|
|
2019-12-17 20:26:12 +03:00
|
|
|
FlairAvatar.contextType = MatrixClientContext;
|
2017-09-21 15:25:36 +03:00
|
|
|
|
2017-08-30 12:55:25 +03:00
|
|
|
export default class Flair extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
2017-09-21 15:25:36 +03:00
|
|
|
profiles: [],
|
2017-08-30 12:55:25 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-08 14:10:37 +03:00
|
|
|
componentDidMount() {
|
2017-08-30 12:55:25 +03:00
|
|
|
this._unmounted = false;
|
2017-11-28 13:04:34 +03:00
|
|
|
this._generateAvatars(this.props.groups);
|
2017-10-04 16:06:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:10:37 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
this._unmounted = true;
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:04:34 +03:00
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
this._generateAvatars(newProps.groups);
|
2017-08-30 12:55:25 +03:00
|
|
|
}
|
|
|
|
|
2017-09-21 15:25:36 +03:00
|
|
|
async _getGroupProfiles(groups) {
|
2017-08-30 12:55:25 +03:00
|
|
|
const profiles = [];
|
|
|
|
for (const groupId of groups) {
|
|
|
|
let groupProfile = null;
|
|
|
|
try {
|
2019-12-17 20:26:12 +03:00
|
|
|
groupProfile = await FlairStore.getGroupProfileCached(this.context, groupId);
|
2017-08-30 12:55:25 +03:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Could not get profile for group', groupId, err);
|
|
|
|
}
|
|
|
|
profiles.push(groupProfile);
|
|
|
|
}
|
2017-09-21 15:25:36 +03:00
|
|
|
return profiles.filter((p) => p !== null);
|
2017-08-30 12:55:25 +03:00
|
|
|
}
|
|
|
|
|
2017-11-28 13:04:34 +03:00
|
|
|
async _generateAvatars(groups) {
|
2017-08-31 18:46:39 +03:00
|
|
|
if (!groups || groups.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-21 15:25:36 +03:00
|
|
|
const profiles = await this._getGroupProfiles(groups);
|
2017-08-30 12:55:25 +03:00
|
|
|
if (!this.unmounted) {
|
2018-01-19 13:39:38 +03:00
|
|
|
this.setState({
|
|
|
|
profiles: profiles.filter((profile) => {
|
|
|
|
return profile ? profile.avatarUrl : false;
|
2018-01-19 17:07:13 +03:00
|
|
|
}),
|
2018-01-19 13:39:38 +03:00
|
|
|
});
|
2017-08-30 12:55:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-09-21 15:25:36 +03:00
|
|
|
if (this.state.profiles.length === 0) {
|
2019-03-05 16:50:39 +03:00
|
|
|
return <span className="mx_Flair" />;
|
2017-08-30 12:55:25 +03:00
|
|
|
}
|
2017-09-21 15:25:36 +03:00
|
|
|
const avatars = this.state.profiles.map((profile, index) => {
|
2017-09-28 13:21:06 +03:00
|
|
|
return <FlairAvatar key={index} groupProfile={profile} />;
|
2017-08-30 12:55:25 +03:00
|
|
|
});
|
|
|
|
return (
|
2017-10-11 18:02:01 +03:00
|
|
|
<span className="mx_Flair">
|
2017-09-28 13:21:06 +03:00
|
|
|
{ avatars }
|
2017-08-30 12:55:25 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Flair.propTypes = {
|
2017-11-28 13:04:34 +03:00
|
|
|
groups: PropTypes.arrayOf(PropTypes.string),
|
2017-08-30 12:55:25 +03:00
|
|
|
};
|
|
|
|
|
2019-12-17 20:26:12 +03:00
|
|
|
Flair.contextType = MatrixClientContext;
|