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