2017-05-18 01:21:02 +03:00
|
|
|
/*
|
2017-06-28 14:26:05 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-07-11 20:07:32 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2017-05-18 01:21:02 +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.
|
|
|
|
*/
|
|
|
|
|
2017-06-20 12:54:41 +03:00
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-09-06 17:04:46 +03:00
|
|
|
import createReactClass from 'create-react-class';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../../MatrixClientPeg';
|
2017-06-20 12:54:41 +03:00
|
|
|
import AppTile from '../elements/AppTile';
|
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import dis from '../../../dispatcher';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
|
|
|
import * as ScalarMessaging from '../../../ScalarMessaging';
|
2017-06-28 15:55:18 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2018-06-26 13:59:16 +03:00
|
|
|
import WidgetUtils from '../../../utils/WidgetUtils';
|
2018-07-03 13:16:44 +03:00
|
|
|
import WidgetEchoStore from "../../../stores/WidgetEchoStore";
|
2018-10-02 14:55:24 +03:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2019-08-10 01:05:05 +03:00
|
|
|
import {IntegrationManagers} from "../../../integrations/IntegrationManagers";
|
2019-08-23 18:12:40 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2017-06-28 15:55:18 +03:00
|
|
|
|
2017-08-11 01:53:43 +03:00
|
|
|
// The maximum number of widgets that can be added in a room
|
|
|
|
const MAX_WIDGETS = 2;
|
2017-05-30 15:47:17 +03:00
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
export default createReactClass({
|
2017-05-18 01:21:02 +03:00
|
|
|
displayName: 'AppsDrawer',
|
|
|
|
|
|
|
|
propTypes: {
|
2018-02-23 18:37:33 +03:00
|
|
|
userId: PropTypes.string.isRequired,
|
2017-12-26 04:03:18 +03:00
|
|
|
room: PropTypes.object.isRequired,
|
2018-02-23 18:37:33 +03:00
|
|
|
showApps: PropTypes.bool, // Should apps be rendered
|
|
|
|
hide: PropTypes.bool, // If rendered, should apps drawer be visible
|
2018-02-09 16:23:34 +03:00
|
|
|
},
|
|
|
|
|
2019-08-05 12:08:07 +03:00
|
|
|
getDefaultProps: () => ({
|
2018-02-09 16:23:34 +03:00
|
|
|
showApps: true,
|
|
|
|
hide: false,
|
2019-08-05 12:08:07 +03:00
|
|
|
}),
|
2017-06-13 16:28:37 +03:00
|
|
|
|
2017-06-28 14:54:47 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
apps: this._getApps(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-03-31 23:14:17 +03:00
|
|
|
componentDidMount: function() {
|
2017-06-20 19:56:45 +03:00
|
|
|
ScalarMessaging.startListening();
|
2018-02-09 16:23:34 +03:00
|
|
|
MatrixClientPeg.get().on('RoomState.events', this.onRoomStateEvents);
|
2018-07-05 22:00:42 +03:00
|
|
|
WidgetEchoStore.on('update', this._updateApps);
|
2017-08-21 16:34:13 +03:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
componentWillUnmount: function() {
|
2017-06-27 13:28:38 +03:00
|
|
|
ScalarMessaging.stopListening();
|
2017-06-13 16:28:37 +03:00
|
|
|
if (MatrixClientPeg.get()) {
|
2018-02-09 16:23:34 +03:00
|
|
|
MatrixClientPeg.get().removeListener('RoomState.events', this.onRoomStateEvents);
|
2017-06-13 16:28:37 +03:00
|
|
|
}
|
2018-07-05 22:00:42 +03:00
|
|
|
WidgetEchoStore.removeListener('update', this._updateApps);
|
2020-03-31 23:05:56 +03:00
|
|
|
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
|
2017-08-18 16:48:58 +03:00
|
|
|
},
|
|
|
|
|
2020-04-01 23:35:39 +03:00
|
|
|
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
|
|
|
|
UNSAFE_componentWillReceiveProps(newProps) {
|
2017-08-18 16:48:58 +03:00
|
|
|
// Room has changed probably, update apps
|
|
|
|
this._updateApps();
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(action) {
|
2018-02-09 16:23:34 +03:00
|
|
|
const hideWidgetKey = this.props.room.roomId + '_hide_widget_drawer';
|
2017-08-18 16:48:58 +03:00
|
|
|
switch (action.action) {
|
|
|
|
case 'appsDrawer':
|
2020-04-09 23:47:20 +03:00
|
|
|
// Note: these booleans are awkward because localstorage is fundamentally
|
|
|
|
// string-based. We also do exact equality on the strings later on.
|
2017-08-18 16:48:58 +03:00
|
|
|
if (action.show) {
|
2020-04-09 23:47:20 +03:00
|
|
|
localStorage.setItem(hideWidgetKey, "false");
|
2017-10-25 02:21:46 +03:00
|
|
|
} else {
|
|
|
|
// Store hidden state of widget
|
|
|
|
// Don't show if previously hidden
|
2020-04-09 23:47:20 +03:00
|
|
|
localStorage.setItem(hideWidgetKey, "true");
|
2017-08-18 16:48:58 +03:00
|
|
|
}
|
2017-10-25 02:21:46 +03:00
|
|
|
|
2017-08-18 16:48:58 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-06-05 20:21:31 +03:00
|
|
|
},
|
|
|
|
|
2017-06-13 16:28:37 +03: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 13:16:44 +03:00
|
|
|
const widgets = WidgetEchoStore.getEchoedRoomWidgets(
|
2018-07-05 20:43:20 +03:00
|
|
|
this.props.room.roomId, WidgetUtils.getRoomWidgets(this.props.room),
|
2018-07-03 13:16:44 +03:00
|
|
|
);
|
|
|
|
return widgets.map((ev) => {
|
2019-11-19 04:02:47 +03:00
|
|
|
return WidgetUtils.makeAppConfig(
|
|
|
|
ev.getStateKey(), ev.getContent(), ev.getSender(), ev.getRoomId(), ev.getId(),
|
|
|
|
);
|
2017-06-13 16:28:37 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateApps: function() {
|
|
|
|
const apps = this._getApps();
|
|
|
|
this.setState({
|
2017-06-28 17:53:18 +03:00
|
|
|
apps: apps,
|
2017-06-13 16:28:37 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-28 01:38:02 +03:00
|
|
|
_canUserModify: function() {
|
|
|
|
try {
|
|
|
|
return WidgetUtils.canUserModifyWidgets(this.props.room.roomId);
|
2017-11-16 16:19:36 +03:00
|
|
|
} catch (err) {
|
2017-07-28 01:38:02 +03:00
|
|
|
console.error(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-18 16:48:58 +03:00
|
|
|
_launchManageIntegrations: function() {
|
2019-08-23 18:12:40 +03:00
|
|
|
if (SettingsStore.isFeatureEnabled("feature_many_integration_managers")) {
|
|
|
|
IntegrationManagers.sharedInstance().openAll();
|
|
|
|
} else {
|
|
|
|
IntegrationManagers.sharedInstance().getPrimaryManager().open(this.props.room, 'add_integ');
|
|
|
|
}
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
2017-06-13 16:31:37 +03:00
|
|
|
|
2017-08-18 16:48:58 +03:00
|
|
|
onClickAddWidget: function(e) {
|
|
|
|
e.preventDefault();
|
2017-08-11 01:53:43 +03:00
|
|
|
// Display a warning dialog if the max number of widgets have already been added to the room
|
2017-08-18 17:35:01 +03:00
|
|
|
const apps = this._getApps();
|
|
|
|
if (apps && apps.length >= MAX_WIDGETS) {
|
2018-02-09 16:23:34 +03:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
2017-08-11 01:53:43 +03: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 16:23:34 +03: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-11 01:53:43 +03:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-08-18 16:48:58 +03:00
|
|
|
this._launchManageIntegrations();
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2018-07-11 20:07:32 +03:00
|
|
|
const apps = this.state.apps.map((app, index, arr) => {
|
2018-07-12 20:43:49 +03:00
|
|
|
const capWhitelist = WidgetUtils.getCapWhitelistForAppTypeInRoomId(app.type, this.props.room.roomId);
|
2018-07-11 20:07:32 +03:00
|
|
|
|
|
|
|
return (<AppTile
|
|
|
|
key={app.id}
|
2020-04-01 12:00:33 +03:00
|
|
|
app={app}
|
2018-07-11 20:07:32 +03:00
|
|
|
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 14:34:27 +03:00
|
|
|
|
2018-05-25 05:17:29 +03:00
|
|
|
if (apps.length == 0) {
|
|
|
|
return <div></div>;
|
|
|
|
}
|
|
|
|
|
2017-08-16 20:19:12 +03:00
|
|
|
let addWidget;
|
|
|
|
if (this.props.showApps &&
|
|
|
|
this._canUserModify()
|
|
|
|
) {
|
2018-10-02 14:55:24 +03:00
|
|
|
addWidget = <AccessibleButton
|
2017-08-16 20:19:12 +03:00
|
|
|
onClick={this.onClickAddWidget}
|
2017-08-21 13:29:42 +03:00
|
|
|
className={this.state.apps.length<2 ?
|
2018-02-09 16:23:34 +03:00
|
|
|
'mx_AddWidget_button mx_AddWidget_button_full_width' :
|
|
|
|
'mx_AddWidget_button'
|
2017-08-21 13:29:42 +03:00
|
|
|
}
|
2017-08-11 01:53:43 +03:00
|
|
|
title={_t('Add a widget')}>
|
2017-09-28 13:21:06 +03:00
|
|
|
[+] { _t('Add a widget') }
|
2018-10-02 14:55:24 +03:00
|
|
|
</AccessibleButton>;
|
2017-08-16 20:19:12 +03:00
|
|
|
}
|
2017-06-06 16:50:43 +03:00
|
|
|
|
2018-07-03 13:16:44 +03:00
|
|
|
let spinner;
|
|
|
|
if (
|
|
|
|
apps.length === 0 && WidgetEchoStore.roomHasPendingWidgets(
|
2018-07-05 20:43:20 +03:00
|
|
|
this.props.room.roomId,
|
2018-07-03 13:16:44 +03:00
|
|
|
WidgetUtils.getRoomWidgets(this.props.room),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
spinner = <Loader />;
|
|
|
|
}
|
|
|
|
|
2017-05-18 01:21:02 +03:00
|
|
|
return (
|
2018-02-09 16:23:34 +03:00
|
|
|
<div className={'mx_AppsDrawer' + (this.props.hide ? ' mx_AppsDrawer_hidden' : '')}>
|
|
|
|
<div id='apps' className='mx_AppsContainer'>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ apps }
|
2018-07-03 13:16:44 +03:00
|
|
|
{ spinner }
|
2017-05-22 14:34:27 +03:00
|
|
|
</div>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ this._canUserModify() && addWidget }
|
2017-05-18 01:21:02 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|