element-web/src/components/views/rooms/AppsDrawer.js

208 lines
6.8 KiB
JavaScript
Raw Normal View History

2017-05-18 01:21:02 +03:00
/*
2017-06-28 14:26:05 +03:00
Copyright 2017 Vector Creations Ltd
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';
import PropTypes from 'prop-types';
2020-09-08 12:19:51 +03:00
import classNames from 'classnames';
import {Resizable} from "re-resizable";
import AppTile from '../elements/AppTile';
import dis from '../../../dispatcher/dispatcher';
import * as sdk from '../../../index';
import * as ScalarMessaging from '../../../ScalarMessaging';
2018-06-26 13:59:16 +03:00
import WidgetUtils from '../../../utils/WidgetUtils';
import WidgetEchoStore from "../../../stores/WidgetEchoStore";
import {IntegrationManagers} from "../../../integrations/IntegrationManagers";
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 = {
userId: PropTypes.string.isRequired,
room: PropTypes.object.isRequired,
2020-09-02 13:13:00 +03:00
resizeNotifier: PropTypes.instanceOf(ResizeNotifier).isRequired,
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
};
2020-08-29 14:14:16 +03:00
static defaultProps = {
showApps: true,
hide: false,
2020-08-29 14:14:16 +03:00
};
constructor(props) {
super(props);
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() {
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() {
ScalarMessaging.stopListening();
2020-09-08 12:19:51 +03:00
WidgetStore.instance.off(this.props.room.roomId, this._updateApps);
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
2020-08-29 14:14:16 +03:00
}
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
2020-08-29 14:57:11 +03:00
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(newProps) {
// Room has changed probably, update apps
this._updateApps();
2020-08-29 14:14:16 +03:00
}
2020-08-29 14:14:16 +03:00
onAction = (action) => {
const hideWidgetKey = this.props.room.roomId + '_hide_widget_drawer';
switch (action.action) {
case 'appsDrawer':
// Note: these booleans are awkward because localstorage is fundamentally
// string-based. We also do exact equality on the strings later on.
if (action.show) {
localStorage.setItem(hideWidgetKey, "false");
} else {
// Store hidden state of widget
// Don't show if previously hidden
localStorage.setItem(hideWidgetKey, "true");
}
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);
2020-08-29 14:14:16 +03:00
_updateApps = () => {
this.setState({
2020-09-08 12:19:51 +03:00
apps: this._getApps(),
});
2020-08-29 14:14:16 +03:00
};
2020-08-29 14:14:16 +03:00
_launchManageIntegrations() {
if (SettingsStore.getValue("feature_many_integration_managers")) {
IntegrationManagers.sharedInstance().openAll();
} else {
IntegrationManagers.sharedInstance().getPrimaryManager().open(this.props.room, 'add_integ');
}
2020-08-29 14:14:16 +03:00
}
2020-08-29 14:14:16 +03:00
render() {
if (!this.props.showApps) return <div />;
const apps = this.state.apps.map((app, index, arr) => {
const capWhitelist = WidgetUtils.getCapWhitelistForAppTypeInRoomId(app.type, this.props.room.roomId);
return (<AppTile
key={app.id}
app={app}
fullWidth={arr.length < 2}
room={this.props.room}
userId={this.props.userId}
creatorUserId={app.creatorUserId}
2020-08-28 12:50:27 +03:00
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
waitForIframeLoad={app.waitForIframeLoad}
whitelistCapabilities={capWhitelist}
/>);
});
2017-05-22 14:34:27 +03:00
if (apps.length === 0) {
return <div />;
2018-05-25 05:17:29 +03:00
}
let spinner;
if (
apps.length === 0 && WidgetEchoStore.roomHasPendingWidgets(
this.props.room.roomId,
WidgetUtils.getRoomWidgets(this.props.room),
)
) {
const Loader = sdk.getComponent("elements.Spinner");
spinner = <Loader />;
}
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 (
<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"
className="mx_AppsContainer"
2020-09-02 13:13:00 +03:00
resizeNotifier={this.props.resizeNotifier}
>
{ apps }
{ spinner }
</PersistentVResizer>
2017-05-18 01:21:02 +03:00
</div>
);
2020-08-29 14:14:16 +03:00
}
}
2020-09-02 13:13:00 +03:00
const PersistentVResizer = ({
id,
minHeight,
maxHeight,
className,
handleWrapperClass,
handleClass,
resizeNotifier,
children,
}) => {
2020-09-09 15:15:07 +03:00
const [height, setHeight] = useLocalStorageState("pvr_" + id, 280); // old fixed height was 273px
2020-08-21 18:38:28 +03:00
const [resizing, setResizing] = useState(false);
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);
resizeNotifier.startResizing();
2020-08-21 18:38:28 +03:00
}}
2020-09-02 13:13:00 +03:00
onResize={() => {
resizeNotifier.notifyTimelineHeightChanged();
}}
onResizeStop={(e, dir, ref, d) => {
setHeight(height + d.height);
2020-08-21 18:40:05 +03:00
if (resizing) setResizing(false);
resizeNotifier.stopResizing();
}}
handleWrapperClass={handleWrapperClass}
handleClasses={{bottom: handleClass}}
2020-08-21 18:38:28 +03:00
className={classNames(className, {
mx_AppsDrawer_resizing: resizing,
})}
enable={{bottom: true}}
>
{ children }
</Resizable>;
};