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-10-13 14:29:12 +03:00
|
|
|
import React 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';
|
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";
|
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-09-02 13:13:00 +03:00
|
|
|
import ResizeNotifier from "../../../utils/ResizeNotifier";
|
2020-10-13 14:29:12 +03:00
|
|
|
import ResizeHandle from "../elements/ResizeHandle";
|
|
|
|
import Resizer from "../../../resizer/resizer";
|
|
|
|
import PercentageDistributor from "../../../resizer/distributors/percentage";
|
2021-01-19 03:41:54 +03:00
|
|
|
import {Container, WidgetLayoutStore} from "../../../stores/widgets/WidgetLayoutStore";
|
2021-01-19 05:27:11 +03:00
|
|
|
import {clamp, percentageOf, percentageWithin} from "../../../utils/numbers";
|
|
|
|
import {useStateCallback} from "../../../hooks/useStateCallback";
|
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
|
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,
|
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-10-13 14:29:12 +03:00
|
|
|
|
|
|
|
this._resizeContainer = null;
|
|
|
|
this.resizer = this._createResizer();
|
2020-10-19 15:08:11 +03:00
|
|
|
|
|
|
|
this.props.resizeNotifier.on("isResizing", this.onIsResizing);
|
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();
|
2021-01-19 03:41:54 +03:00
|
|
|
WidgetLayoutStore.instance.on(WidgetLayoutStore.emissionForRoom(this.props.room), 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();
|
2021-01-19 03:41:54 +03:00
|
|
|
WidgetLayoutStore.instance.off(WidgetLayoutStore.emissionForRoom(this.props.room), this._updateApps);
|
2020-03-31 23:05:56 +03:00
|
|
|
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
|
2020-10-13 14:29:12 +03:00
|
|
|
if (this._resizeContainer) {
|
|
|
|
this.resizer.detach();
|
|
|
|
}
|
2020-10-19 15:08:11 +03:00
|
|
|
this.props.resizeNotifier.off("isResizing", this.onIsResizing);
|
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-10-19 15:08:11 +03:00
|
|
|
onIsResizing = (resizing) => {
|
|
|
|
this.setState({ resizing });
|
|
|
|
if (!resizing) {
|
|
|
|
this._relaxResizer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-13 14:29:12 +03:00
|
|
|
_createResizer() {
|
|
|
|
const classNames = {
|
|
|
|
handle: "mx_ResizeHandle",
|
|
|
|
vertical: "mx_ResizeHandle_vertical",
|
|
|
|
reverse: "mx_ResizeHandle_reverse",
|
|
|
|
};
|
|
|
|
const collapseConfig = {
|
|
|
|
onResizeStart: () => {
|
|
|
|
this._resizeContainer.classList.add("mx_AppsDrawer_resizing");
|
|
|
|
},
|
|
|
|
onResizeStop: () => {
|
|
|
|
this._resizeContainer.classList.remove("mx_AppsDrawer_resizing");
|
2021-01-19 04:48:07 +03:00
|
|
|
WidgetLayoutStore.instance.setResizerDistributions(
|
|
|
|
this.props.room, Container.Top,
|
|
|
|
this.state.apps.slice(1).map((_, i) => this.resizer.forHandleAt(i).size),
|
|
|
|
);
|
2020-10-13 14:29:12 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
// pass a truthy container for now, we won't call attach until we update it
|
|
|
|
const resizer = new Resizer({}, PercentageDistributor, collapseConfig);
|
|
|
|
resizer.setClassNames(classNames);
|
|
|
|
return resizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
_collectResizer = (ref) => {
|
|
|
|
if (this._resizeContainer) {
|
|
|
|
this.resizer.detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref) {
|
|
|
|
this.resizer.container = ref;
|
|
|
|
this.resizer.attach();
|
|
|
|
}
|
|
|
|
this._resizeContainer = ref;
|
|
|
|
this._loadResizerPreferences();
|
|
|
|
};
|
|
|
|
|
2020-10-15 13:14:48 +03:00
|
|
|
_getAppsHash = (apps) => apps.map(app => app.id).join("~");
|
2020-10-13 14:29:12 +03:00
|
|
|
|
2020-10-15 13:14:48 +03:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (this._getAppsHash(this.state.apps) !== this._getAppsHash(prevState.apps)) {
|
|
|
|
this._loadResizerPreferences();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:08:11 +03:00
|
|
|
_relaxResizer = () => {
|
|
|
|
const distributors = this.resizer.getDistributors();
|
|
|
|
|
|
|
|
// relax all items if they had any overconstrained flexboxes
|
|
|
|
distributors.forEach(d => d.start());
|
|
|
|
distributors.forEach(d => d.finish());
|
|
|
|
};
|
|
|
|
|
2020-10-15 13:14:48 +03:00
|
|
|
_loadResizerPreferences = () => {
|
2021-01-19 04:48:07 +03:00
|
|
|
const distributions = WidgetLayoutStore.instance.getResizerDistributions(this.props.room, Container.Top);
|
|
|
|
if (this.state.apps && (this.state.apps.length - 1) === distributions.length) {
|
|
|
|
distributions.forEach((size, i) => {
|
|
|
|
const distributor = this.resizer.forHandleAt(i);
|
|
|
|
if (distributor) {
|
|
|
|
distributor.size = size;
|
|
|
|
distributor.finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (this.state.apps) {
|
2020-10-15 13:14:48 +03:00
|
|
|
const distributors = this.resizer.getDistributors();
|
|
|
|
distributors.forEach(d => d.item.clearSize());
|
2020-10-20 18:07:47 +03:00
|
|
|
distributors.forEach(d => d.start());
|
2020-10-15 13:14:48 +03:00
|
|
|
distributors.forEach(d => d.finish());
|
2020-10-13 14:29:12 +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
|
|
|
|
2021-01-19 03:41:54 +03:00
|
|
|
_getApps = () => WidgetLayoutStore.instance.getContainerWidgets(this.props.room, Container.Top);
|
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(),
|
2020-10-15 13:14:48 +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
|
|
|
_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
|
|
|
render() {
|
2020-10-09 10:42:21 +03:00
|
|
|
if (!this.props.showApps) return <div />;
|
|
|
|
|
2018-07-11 20:07:32 +03:00
|
|
|
const apps = this.state.apps.map((app, index, arr) => {
|
|
|
|
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}
|
|
|
|
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}
|
|
|
|
/>);
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
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({
|
2020-10-12 15:36:09 +03:00
|
|
|
mx_AppsDrawer: true,
|
|
|
|
mx_AppsDrawer_fullWidth: apps.length < 2,
|
|
|
|
mx_AppsDrawer_resizing: this.state.resizing,
|
2020-10-21 11:48:02 +03:00
|
|
|
mx_AppsDrawer_2apps: apps.length === 2,
|
|
|
|
mx_AppsDrawer_3apps: apps.length === 3,
|
2020-04-23 22:52:28 +03:00
|
|
|
});
|
|
|
|
|
2017-05-18 01:21:02 +03:00
|
|
|
return (
|
2020-08-21 18:29:07 +03:00
|
|
|
<div className={classes}>
|
|
|
|
<PersistentVResizer
|
2021-01-19 05:27:11 +03:00
|
|
|
room={this.props.room}
|
2020-08-21 18:29:07 +03:00
|
|
|
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"
|
2021-01-21 01:05:18 +03:00
|
|
|
handleWrapperClass="mx_AppsContainer_resizerHandleContainer"
|
2020-10-13 14:29:12 +03:00
|
|
|
className="mx_AppsContainer_resizer"
|
2020-09-02 13:13:00 +03:00
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
2020-08-21 18:29:07 +03:00
|
|
|
>
|
2020-10-13 14:29:12 +03:00
|
|
|
<div className="mx_AppsContainer" ref={this._collectResizer}>
|
|
|
|
{ apps.map((app, i) => {
|
|
|
|
if (i < 1) return app;
|
|
|
|
return <React.Fragment key={app.key}>
|
|
|
|
<ResizeHandle reverse={i > apps.length / 2} />
|
|
|
|
{ app }
|
|
|
|
</React.Fragment>;
|
|
|
|
}) }
|
|
|
|
</div>
|
2020-08-21 18:29:07 +03:00
|
|
|
</PersistentVResizer>
|
2020-10-13 14:29:12 +03:00
|
|
|
{ spinner }
|
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 = ({
|
2021-01-19 05:27:11 +03:00
|
|
|
room,
|
2020-09-02 13:13:00 +03:00
|
|
|
minHeight,
|
|
|
|
maxHeight,
|
|
|
|
className,
|
|
|
|
handleWrapperClass,
|
|
|
|
handleClass,
|
|
|
|
resizeNotifier,
|
|
|
|
children,
|
|
|
|
}) => {
|
2021-01-19 05:27:11 +03:00
|
|
|
let defaultHeight = WidgetLayoutStore.instance.getContainerHeight(room, Container.Top);
|
|
|
|
|
|
|
|
// Arbitrary defaults to avoid NaN problems. 100 px or 3/4 of the visible window.
|
|
|
|
if (!minHeight) minHeight = 100;
|
|
|
|
if (!maxHeight) maxHeight = (window.innerHeight / 4) * 3;
|
|
|
|
|
|
|
|
// Convert from percentage to height. Note that the default height is 280px.
|
|
|
|
if (defaultHeight) {
|
|
|
|
defaultHeight = clamp(defaultHeight, 0, 100);
|
|
|
|
defaultHeight = percentageWithin(defaultHeight / 100, minHeight, maxHeight);
|
|
|
|
} else {
|
|
|
|
defaultHeight = 280;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [height, setHeight] = useStateCallback(defaultHeight, newHeight => {
|
|
|
|
newHeight = percentageOf(newHeight, minHeight, maxHeight) * 100;
|
2021-01-19 06:53:15 +03:00
|
|
|
WidgetLayoutStore.instance.setContainerHeight(room, Container.Top, newHeight);
|
2021-01-19 05:27:11 +03:00
|
|
|
});
|
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-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-09-02 14:00:35 +03:00
|
|
|
resizeNotifier.stopResizing();
|
2020-08-21 18:29:07 +03:00
|
|
|
}}
|
|
|
|
handleWrapperClass={handleWrapperClass}
|
|
|
|
handleClasses={{bottom: handleClass}}
|
2020-10-12 15:36:09 +03:00
|
|
|
className={className}
|
2020-08-21 18:29:07 +03:00
|
|
|
enable={{bottom: true}}
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</Resizable>;
|
|
|
|
};
|