2017-05-17 22:21:02 +00:00
|
|
|
/*
|
2017-06-28 11:26:05 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-07-11 17:07:32 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2017-05-17 22:21:02 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-20 09:54:41 +00:00
|
|
|
import React from 'react';
|
2017-12-26 01:03:18 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-20 09:54:41 +00:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import AppTile from '../elements/AppTile';
|
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import dis from '../../../dispatcher';
|
|
|
|
import sdk from '../../../index';
|
2017-06-20 16:56:45 +00:00
|
|
|
import ScalarMessaging from '../../../ScalarMessaging';
|
2017-06-28 12:55:18 +00:00
|
|
|
import { _t } from '../../../languageHandler';
|
2018-06-26 10:59:16 +00:00
|
|
|
import WidgetUtils from '../../../utils/WidgetUtils';
|
2018-07-03 10:16:44 +00:00
|
|
|
import WidgetEchoStore from "../../../stores/WidgetEchoStore";
|
2018-10-02 11:55:24 +00:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2019-08-09 22:05:05 +00:00
|
|
|
import {IntegrationManagers} from "../../../integrations/IntegrationManagers";
|
2017-06-28 12:55:18 +00:00
|
|
|
|
2017-08-10 22:53:43 +00:00
|
|
|
// The maximum number of widgets that can be added in a room
|
|
|
|
const MAX_WIDGETS = 2;
|
2017-05-30 12:47:17 +00:00
|
|
|
|
2017-05-17 22:21:02 +00:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'AppsDrawer',
|
|
|
|
|
|
|
|
propTypes: {
|
2018-02-23 15:37:33 +00:00
|
|
|
userId: PropTypes.string.isRequired,
|
2017-12-26 01:03:18 +00:00
|
|
|
room: PropTypes.object.isRequired,
|
2018-02-23 15:37:33 +00:00
|
|
|
showApps: PropTypes.bool, // Should apps be rendered
|
|
|
|
hide: PropTypes.bool, // If rendered, should apps drawer be visible
|
2018-02-09 13:23:34 +00:00
|
|
|
},
|
|
|
|
|
2019-08-05 09:08:07 +00:00
|
|
|
getDefaultProps: () => ({
|
2018-02-09 13:23:34 +00:00
|
|
|
showApps: true,
|
|
|
|
hide: false,
|
2019-08-05 09:08:07 +00:00
|
|
|
}),
|
2017-06-13 13:28:37 +00:00
|
|
|
|
2017-06-28 11:54:47 +00:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
apps: this._getApps(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-06-13 13:28:37 +00:00
|
|
|
componentWillMount: function() {
|
2017-06-20 16:56:45 +00:00
|
|
|
ScalarMessaging.startListening();
|
2018-02-09 13:23:34 +00:00
|
|
|
MatrixClientPeg.get().on('RoomState.events', this.onRoomStateEvents);
|
2018-07-05 19:00:42 +00:00
|
|
|
WidgetEchoStore.on('update', this._updateApps);
|
2017-05-17 22:21:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
2017-08-21 13:34:13 +00:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2017-05-17 22:21:02 +00:00
|
|
|
},
|
|
|
|
|
2017-06-13 13:28:37 +00:00
|
|
|
componentWillUnmount: function() {
|
2017-06-27 10:28:38 +00:00
|
|
|
ScalarMessaging.stopListening();
|
2017-06-13 13:28:37 +00:00
|
|
|
if (MatrixClientPeg.get()) {
|
2018-02-09 13:23:34 +00:00
|
|
|
MatrixClientPeg.get().removeListener('RoomState.events', this.onRoomStateEvents);
|
2017-06-13 13:28:37 +00:00
|
|
|
}
|
2018-07-05 19:00:42 +00:00
|
|
|
WidgetEchoStore.removeListener('update', this._updateApps);
|
2017-08-18 13:48:58 +00:00
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
// Room has changed probably, update apps
|
|
|
|
this._updateApps();
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(action) {
|
2018-02-09 13:23:34 +00:00
|
|
|
const hideWidgetKey = this.props.room.roomId + '_hide_widget_drawer';
|
2017-08-18 13:48:58 +00:00
|
|
|
switch (action.action) {
|
|
|
|
case 'appsDrawer':
|
|
|
|
if (action.show) {
|
2017-10-26 18:17:13 +00:00
|
|
|
localStorage.removeItem(hideWidgetKey);
|
2017-10-24 23:21:46 +00:00
|
|
|
} else {
|
|
|
|
// Store hidden state of widget
|
|
|
|
// Don't show if previously hidden
|
2017-10-26 18:17:13 +00:00
|
|
|
localStorage.setItem(hideWidgetKey, true);
|
2017-08-18 13:48:58 +00:00
|
|
|
}
|
2017-10-24 23:21:46 +00:00
|
|
|
|
2017-08-18 13:48:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-06-05 17:21:31 +00:00
|
|
|
},
|
|
|
|
|
2017-06-13 13:28:37 +00:00
|
|
|
onRoomStateEvents: function(ev, state) {
|
|
|
|
if (ev.getRoomId() !== this.props.room.roomId || ev.getType() !== 'im.vector.modular.widgets') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._updateApps();
|
|
|
|
},
|
|
|
|
|
|
|
|
_getApps: function() {
|
2018-07-03 10:16:44 +00:00
|
|
|
const widgets = WidgetEchoStore.getEchoedRoomWidgets(
|
2018-07-05 17:43:20 +00:00
|
|
|
this.props.room.roomId, WidgetUtils.getRoomWidgets(this.props.room),
|
2018-07-03 10:16:44 +00:00
|
|
|
);
|
|
|
|
return widgets.map((ev) => {
|
2018-07-18 08:35:57 +00:00
|
|
|
return WidgetUtils.makeAppConfig(ev.getStateKey(), ev.getContent(), ev.sender);
|
2017-06-13 13:28:37 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateApps: function() {
|
|
|
|
const apps = this._getApps();
|
|
|
|
this.setState({
|
2017-06-28 14:53:18 +00:00
|
|
|
apps: apps,
|
2017-06-13 13:28:37 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-27 22:38:02 +00:00
|
|
|
_canUserModify: function() {
|
|
|
|
try {
|
|
|
|
return WidgetUtils.canUserModifyWidgets(this.props.room.roomId);
|
2017-11-16 13:19:36 +00:00
|
|
|
} catch (err) {
|
2017-07-27 22:38:02 +00:00
|
|
|
console.error(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-18 13:48:58 +00:00
|
|
|
_launchManageIntegrations: function() {
|
2019-08-09 22:05:05 +00:00
|
|
|
IntegrationManagers.sharedInstance().getPrimaryManager().open(this.props.room, 'add_integ');
|
2017-05-17 22:21:02 +00:00
|
|
|
},
|
2017-06-13 13:31:37 +00:00
|
|
|
|
2017-08-18 13:48:58 +00:00
|
|
|
onClickAddWidget: function(e) {
|
|
|
|
e.preventDefault();
|
2017-08-10 22:53:43 +00:00
|
|
|
// Display a warning dialog if the max number of widgets have already been added to the room
|
2017-08-18 14:35:01 +00:00
|
|
|
const apps = this._getApps();
|
|
|
|
if (apps && apps.length >= MAX_WIDGETS) {
|
2018-02-09 13:23:34 +00:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
2017-08-10 22:53:43 +00:00
|
|
|
const errorMsg = `The maximum number of ${MAX_WIDGETS} widgets have already been added to this room.`;
|
|
|
|
console.error(errorMsg);
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2018-02-09 13:23:34 +00:00
|
|
|
title: _t('Cannot add any more widgets'),
|
|
|
|
description: _t('The maximum permitted number of widgets have already been added to this room.'),
|
2017-08-10 22:53:43 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-08-18 13:48:58 +00:00
|
|
|
this._launchManageIntegrations();
|
2017-05-17 22:21:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2018-07-11 17:07:32 +00:00
|
|
|
const apps = this.state.apps.map((app, index, arr) => {
|
2018-07-12 17:43:49 +00:00
|
|
|
const capWhitelist = WidgetUtils.getCapWhitelistForAppTypeInRoomId(app.type, this.props.room.roomId);
|
2018-07-11 17:07:32 +00:00
|
|
|
|
|
|
|
return (<AppTile
|
|
|
|
key={app.id}
|
|
|
|
id={app.id}
|
|
|
|
url={app.url}
|
|
|
|
name={app.name}
|
|
|
|
type={app.type}
|
|
|
|
fullWidth={arr.length<2 ? true : false}
|
|
|
|
room={this.props.room}
|
|
|
|
userId={this.props.userId}
|
|
|
|
show={this.props.showApps}
|
|
|
|
creatorUserId={app.creatorUserId}
|
|
|
|
widgetPageTitle={(app.data && app.data.title) ? app.data.title : ''}
|
|
|
|
waitForIframeLoad={app.waitForIframeLoad}
|
|
|
|
whitelistCapabilities={capWhitelist}
|
|
|
|
/>);
|
|
|
|
});
|
2017-05-22 11:34:27 +00:00
|
|
|
|
2018-05-25 02:17:29 +00:00
|
|
|
if (apps.length == 0) {
|
|
|
|
return <div></div>;
|
|
|
|
}
|
|
|
|
|
2017-08-16 17:19:12 +00:00
|
|
|
let addWidget;
|
|
|
|
if (this.props.showApps &&
|
|
|
|
this._canUserModify()
|
|
|
|
) {
|
2018-10-02 11:55:24 +00:00
|
|
|
addWidget = <AccessibleButton
|
2017-08-16 17:19:12 +00:00
|
|
|
onClick={this.onClickAddWidget}
|
2017-08-21 10:29:42 +00:00
|
|
|
className={this.state.apps.length<2 ?
|
2018-02-09 13:23:34 +00:00
|
|
|
'mx_AddWidget_button mx_AddWidget_button_full_width' :
|
|
|
|
'mx_AddWidget_button'
|
2017-08-21 10:29:42 +00:00
|
|
|
}
|
2017-08-10 22:53:43 +00:00
|
|
|
title={_t('Add a widget')}>
|
2017-09-28 10:21:06 +00:00
|
|
|
[+] { _t('Add a widget') }
|
2018-10-02 11:55:24 +00:00
|
|
|
</AccessibleButton>;
|
2017-08-16 17:19:12 +00:00
|
|
|
}
|
2017-06-06 13:50:43 +00:00
|
|
|
|
2018-07-03 10:16:44 +00:00
|
|
|
let spinner;
|
|
|
|
if (
|
|
|
|
apps.length === 0 && WidgetEchoStore.roomHasPendingWidgets(
|
2018-07-05 17:43:20 +00:00
|
|
|
this.props.room.roomId,
|
2018-07-03 10:16:44 +00:00
|
|
|
WidgetUtils.getRoomWidgets(this.props.room),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
spinner = <Loader />;
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:21:02 +00:00
|
|
|
return (
|
2018-02-09 13:23:34 +00:00
|
|
|
<div className={'mx_AppsDrawer' + (this.props.hide ? ' mx_AppsDrawer_hidden' : '')}>
|
|
|
|
<div id='apps' className='mx_AppsContainer'>
|
2017-09-28 10:21:06 +00:00
|
|
|
{ apps }
|
2018-07-03 10:16:44 +00:00
|
|
|
{ spinner }
|
2017-05-22 11:34:27 +00:00
|
|
|
</div>
|
2017-09-28 10:21:06 +00:00
|
|
|
{ this._canUserModify() && addWidget }
|
2017-05-17 22:21:02 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|