2017-10-04 15:19:57 +03:00
|
|
|
/*
|
2019-02-22 02:40:12 +03:00
|
|
|
Copyright 2017, 2019 New Vector Ltd.
|
2017-10-04 15:19:57 +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-10-04 15:19:57 +03:00
|
|
|
import {MatrixEvent, MatrixClient} from 'matrix-js-sdk';
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import Modal from '../../../Modal';
|
2019-02-22 02:40:12 +03:00
|
|
|
import ErrorDialog from "../dialogs/ErrorDialog";
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2018-10-27 07:00:51 +03:00
|
|
|
const GROUP_ID_REGEX = /\+\S+:\S+/;
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
export default class RelatedGroupSettings extends React.Component {
|
|
|
|
static propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
roomId: PropTypes.string.isRequired,
|
|
|
|
canSetRelatedGroups: PropTypes.bool.isRequired,
|
|
|
|
relatedGroupsEvent: PropTypes.instanceOf(MatrixEvent),
|
2019-02-22 02:45:57 +03:00
|
|
|
};
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
static contextTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
matrixClient: PropTypes.instanceOf(MatrixClient),
|
2019-02-22 02:45:57 +03:00
|
|
|
};
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
static defaultProps = {
|
|
|
|
canSetRelatedGroups: false,
|
|
|
|
};
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2019-02-22 02:40:12 +03:00
|
|
|
newGroupId: "",
|
2019-02-22 02:45:57 +03:00
|
|
|
newGroupsList: props.relatedGroupsEvent ? (props.relatedGroupsEvent.getContent().groups || []) : [],
|
2017-10-04 15:19:57 +03:00
|
|
|
};
|
2019-02-22 02:45:57 +03:00
|
|
|
}
|
2018-01-05 13:53:55 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
updateGroups(newGroupsList) {
|
2019-02-22 02:40:12 +03:00
|
|
|
this.context.matrixClient.sendStateEvent(this.props.roomId, 'm.room.related_groups', {
|
2019-02-22 02:45:57 +03:00
|
|
|
groups: newGroupsList,
|
2019-02-22 02:40:12 +03:00
|
|
|
}, '').catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
Modal.createTrackedDialog('Error updating flair', '', ErrorDialog, {
|
|
|
|
title: _t("Error updating flair"),
|
|
|
|
description: _t(
|
|
|
|
"There was an error updating the flair for this room. The server may not allow it or " +
|
2019-02-22 03:53:29 +03:00
|
|
|
"a temporary error occurred.",
|
2019-02-22 02:40:12 +03:00
|
|
|
),
|
|
|
|
});
|
2019-02-22 03:53:29 +03:00
|
|
|
});
|
2019-02-22 02:45:57 +03:00
|
|
|
}
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
validateGroupId(groupId) {
|
2017-10-04 16:08:31 +03:00
|
|
|
if (!GROUP_ID_REGEX.test(groupId)) {
|
2017-10-04 15:19:57 +03:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2017-10-16 15:18:39 +03:00
|
|
|
Modal.createTrackedDialog('Invalid related community ID', '', ErrorDialog, {
|
|
|
|
title: _t('Invalid community ID'),
|
|
|
|
description: _t('\'%(groupId)s\' is not a valid community ID', { groupId }),
|
2017-10-04 15:19:57 +03:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-02-22 02:45:57 +03:00
|
|
|
}
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
onNewGroupChanged = (newGroupId) => {
|
2017-10-04 15:19:57 +03:00
|
|
|
this.setState({ newGroupId });
|
2019-02-22 02:45:57 +03:00
|
|
|
};
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
onGroupAdded = (groupId) => {
|
2017-10-04 15:19:57 +03:00
|
|
|
if (groupId.length === 0 || !this.validateGroupId(groupId)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-22 02:45:57 +03:00
|
|
|
const newGroupsList = [...this.state.newGroupsList, groupId];
|
2017-10-04 15:19:57 +03:00
|
|
|
this.setState({
|
2019-02-22 02:45:57 +03:00
|
|
|
newGroupsList: newGroupsList,
|
2017-10-04 15:19:57 +03:00
|
|
|
newGroupId: '',
|
|
|
|
});
|
2019-02-22 02:45:57 +03:00
|
|
|
this.updateGroups(newGroupsList);
|
|
|
|
};
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
onGroupDeleted = (index) => {
|
|
|
|
const group = this.state.newGroupsList[index];
|
|
|
|
const newGroupsList = this.state.newGroupsList.filter((g) => g !== group);
|
2018-01-05 13:53:55 +03:00
|
|
|
this.setState({ newGroupsList });
|
2019-02-22 02:45:57 +03:00
|
|
|
this.updateGroups(newGroupsList);
|
|
|
|
};
|
2017-10-04 15:19:57 +03:00
|
|
|
|
2019-02-22 02:45:57 +03:00
|
|
|
render() {
|
2017-10-04 15:19:57 +03:00
|
|
|
const localDomain = this.context.matrixClient.getDomain();
|
|
|
|
const EditableItemList = sdk.getComponent('elements.EditableItemList');
|
2017-11-21 13:52:07 +03:00
|
|
|
return <div>
|
2017-10-04 15:19:57 +03:00
|
|
|
<EditableItemList
|
2019-07-06 13:16:01 +03:00
|
|
|
id="relatedGroups"
|
2017-10-04 15:19:57 +03:00
|
|
|
items={this.state.newGroupsList}
|
|
|
|
className={"mx_RelatedGroupSettings"}
|
|
|
|
newItem={this.state.newGroupId}
|
2019-02-22 03:15:25 +03:00
|
|
|
canRemove={this.props.canSetRelatedGroups}
|
2017-10-24 19:57:26 +03:00
|
|
|
canEdit={this.props.canSetRelatedGroups}
|
2017-10-04 15:19:57 +03:00
|
|
|
onNewItemChanged={this.onNewGroupChanged}
|
|
|
|
onItemAdded={this.onGroupAdded}
|
|
|
|
onItemRemoved={this.onGroupDeleted}
|
2017-11-21 13:52:07 +03:00
|
|
|
itemsLabel={_t('Showing flair for these communities:')}
|
|
|
|
noItemsLabel={_t('This room is not showing flair for any communities')}
|
2017-10-04 15:19:57 +03:00
|
|
|
placeholder={_t(
|
2017-10-16 15:18:39 +03:00
|
|
|
'New community ID (e.g. +foo:%(localDomain)s)', {localDomain},
|
2017-10-04 15:19:57 +03:00
|
|
|
)}
|
|
|
|
/>
|
2017-11-21 13:52:07 +03:00
|
|
|
</div>;
|
2019-02-22 02:45:57 +03:00
|
|
|
}
|
2019-02-22 03:53:29 +03:00
|
|
|
}
|