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.
|
|
|
|
*/
|
|
|
|
|
2020-08-21 18:38:28 +03:00
|
|
|
import React, {useState} from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-09-08 12:19:51 +03:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import {Resizable} from "re-resizable";
|
|
|
|
|
2017-06-20 12:54:41 +03:00
|
|
|
import AppTile from '../elements/AppTile';
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from '../../../dispatcher/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";
|
2020-08-27 12:27:27 +03:00
|
|
|
import {useLocalStorageState} from "../../../hooks/useLocalStorageState";
|
2020-09-02 13:13:00 +03:00
|
|
|
import ResizeNotifier from "../../../utils/ResizeNotifier";
|
2020-09-08 12:19:51 +03:00
|
|
|
import WidgetStore from "../../../stores/WidgetStore";
|
2017-05-30 15:47:17 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class AppsDrawer extends React.Component {
|
|
|
|
static 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,
|
2020-09-02 13:13:00 +03:00
|
|
|
resizeNotifier: PropTypes.instanceOf(ResizeNotifier).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
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-02-09 16:23:34 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
static defaultProps = {
|
2018-02-09 16:23:34 +03:00
|
|
|
showApps: true,
|
|
|
|
hide: false,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-06-13 16:28:37 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
2017-06-28 14:54:47 +03:00
|
|
|
apps: this._getApps(),
|
|
|
|
};
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-06-28 14:54:47 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidMount() {
|
2017-06-20 19:56:45 +03:00
|
|
|
ScalarMessaging.startListening();
|
2020-09-08 12:19:51 +03:00
|
|
|
WidgetStore.instance.on(this.props.room.roomId, this._updateApps);
|
2017-08-21 16:34:13 +03:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-05-18 01:21:02 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentWillUnmount() {
|
2017-06-27 13:28:38 +03:00
|
|
|
ScalarMessaging.stopListening();
|
2020-09-08 12:19:51 +03:00
|
|
|
WidgetStore.instance.off(this.props.room.roomId, this._updateApps);
|
2020-03-31 23:05:56 +03:00
|
|
|
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-08-18 16:48:58 +03:00
|
|
|
|
2020-04-01 23:35:39 +03:00
|
|
|
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
|
2020-08-29 14:57:11 +03:00
|
|
|
// eslint-disable-next-line camelcase
|
2020-04-01 23:35:39 +03:00
|
|
|
UNSAFE_componentWillReceiveProps(newProps) {
|
2017-08-18 16:48:58 +03:00
|
|
|
// Room has changed probably, update apps
|
|
|
|
this._updateApps();
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-08-18 16:48:58 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onAction = (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;
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-06-05 20:21:31 +03:00
|
|
|
|
2020-09-08 12:19:51 +03:00
|
|
|
_getApps = () => WidgetStore.instance.getApps(this.props.room, true);
|
2017-06-13 16:28:37 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_updateApps = () => {
|
2017-06-13 16:28:37 +03:00
|
|
|
this.setState({
|
2020-09-08 12:19:51 +03:00
|
|
|
apps: this._getApps(),
|
2017-06-13 16:28:37 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-06-13 16:28:37 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_canUserModify() {
|
2017-07-28 01:38:02 +03:00
|
|
|
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;
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-07-28 01:38:02 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_launchManageIntegrations() {
|
2020-08-17 22:12:18 +03:00
|
|
|
if (SettingsStore.getValue("feature_many_integration_managers")) {
|
2019-08-23 18:12:40 +03:00
|
|
|
IntegrationManagers.sharedInstance().openAll();
|
|
|
|
} else {
|
|
|
|
IntegrationManagers.sharedInstance().getPrimaryManager().open(this.props.room, 'add_integ');
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-06-13 16:31:37 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onClickAddWidget = (e) => {
|
2017-08-18 16:48:58 +03:00
|
|
|
e.preventDefault();
|
|
|
|
this._launchManageIntegrations();
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-05-18 01:21:02 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
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}
|
2020-08-21 18:29:07 +03:00
|
|
|
fullWidth={arr.length < 2}
|
2018-07-11 20:07:32 +03:00
|
|
|
room={this.props.room}
|
|
|
|
userId={this.props.userId}
|
|
|
|
show={this.props.showApps}
|
|
|
|
creatorUserId={app.creatorUserId}
|
2020-08-28 12:50:27 +03:00
|
|
|
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
|
2018-07-11 20:07:32 +03:00
|
|
|
waitForIframeLoad={app.waitForIframeLoad}
|
|
|
|
whitelistCapabilities={capWhitelist}
|
|
|
|
/>);
|
|
|
|
});
|
2017-05-22 14:34:27 +03:00
|
|
|
|
2020-08-21 18:29:07 +03:00
|
|
|
if (apps.length === 0) {
|
|
|
|
return <div />;
|
2018-05-25 05:17:29 +03:00
|
|
|
}
|
|
|
|
|
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 />;
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:52:28 +03:00
|
|
|
const classes = classNames({
|
|
|
|
"mx_AppsDrawer": true,
|
|
|
|
"mx_AppsDrawer_hidden": this.props.hide,
|
|
|
|
"mx_AppsDrawer_fullWidth": apps.length < 2,
|
|
|
|
"mx_AppsDrawer_minimised": !this.props.showApps,
|
|
|
|
});
|
|
|
|
|
2017-05-18 01:21:02 +03:00
|
|
|
return (
|
2020-08-21 18:29:07 +03:00
|
|
|
<div className={classes}>
|
|
|
|
<PersistentVResizer
|
|
|
|
id={"apps-drawer_" + this.props.room.roomId}
|
|
|
|
minHeight={100}
|
2020-09-02 13:13:00 +03:00
|
|
|
maxHeight={this.props.maxHeight ? this.props.maxHeight - 50 : undefined}
|
2020-08-21 18:38:28 +03:00
|
|
|
handleClass="mx_AppsContainer_resizerHandle"
|
2020-08-21 18:29:07 +03:00
|
|
|
className="mx_AppsContainer"
|
2020-09-02 13:13:00 +03:00
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
2020-08-21 18:29:07 +03:00
|
|
|
>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ apps }
|
2018-07-03 13:16:44 +03:00
|
|
|
{ spinner }
|
2020-08-21 18:29:07 +03:00
|
|
|
</PersistentVResizer>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ this._canUserModify() && addWidget }
|
2017-05-18 01:21:02 +03:00
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 18:29:07 +03:00
|
|
|
|
2020-09-02 13:13:00 +03:00
|
|
|
const PersistentVResizer = ({
|
|
|
|
id,
|
|
|
|
minHeight,
|
|
|
|
maxHeight,
|
|
|
|
className,
|
|
|
|
handleWrapperClass,
|
|
|
|
handleClass,
|
|
|
|
resizeNotifier,
|
|
|
|
children,
|
|
|
|
}) => {
|
2020-08-21 18:29:07 +03:00
|
|
|
const [height, setHeight] = useLocalStorageState("pvr_" + id, 100);
|
2020-08-21 18:38:28 +03:00
|
|
|
const [resizing, setResizing] = useState(false);
|
2020-08-21 18:29:07 +03:00
|
|
|
|
|
|
|
return <Resizable
|
|
|
|
size={{height: Math.min(height, maxHeight)}}
|
|
|
|
minHeight={minHeight}
|
|
|
|
maxHeight={maxHeight}
|
2020-08-21 18:38:28 +03:00
|
|
|
onResizeStart={() => {
|
2020-08-21 18:40:05 +03:00
|
|
|
if (!resizing) setResizing(true);
|
2020-09-02 14:00:35 +03:00
|
|
|
resizeNotifier.startResizing();
|
2020-08-21 18:38:28 +03:00
|
|
|
}}
|
2020-09-02 13:13:00 +03:00
|
|
|
onResize={() => {
|
|
|
|
resizeNotifier.notifyTimelineHeightChanged();
|
|
|
|
}}
|
2020-08-21 18:29:07 +03:00
|
|
|
onResizeStop={(e, dir, ref, d) => {
|
|
|
|
setHeight(height + d.height);
|
2020-08-21 18:40:05 +03:00
|
|
|
if (resizing) setResizing(false);
|
2020-09-02 14:00:35 +03:00
|
|
|
resizeNotifier.stopResizing();
|
2020-08-21 18:29:07 +03:00
|
|
|
}}
|
|
|
|
handleWrapperClass={handleWrapperClass}
|
|
|
|
handleClasses={{bottom: handleClass}}
|
2020-08-21 18:38:28 +03:00
|
|
|
className={classNames(className, {
|
|
|
|
mx_AppsDrawer_resizing: resizing,
|
|
|
|
})}
|
2020-08-21 18:29:07 +03:00
|
|
|
enable={{bottom: true}}
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</Resizable>;
|
|
|
|
};
|