2018-07-12 20:43:49 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
2020-04-01 12:00:33 +03:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2018-07-12 20:43:49 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import RoomViewStore from '../../../stores/RoomViewStore';
|
|
|
|
import ActiveWidgetStore from '../../../stores/ActiveWidgetStore';
|
|
|
|
import WidgetUtils from '../../../utils/WidgetUtils';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../../MatrixClientPeg';
|
2021-03-09 05:59:41 +03:00
|
|
|
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
2018-07-12 20:43:49 +03:00
|
|
|
|
2021-03-09 05:59:41 +03:00
|
|
|
@replaceableComponent("views.elements.PersistentApp")
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class PersistentApp extends React.Component {
|
|
|
|
state = {
|
|
|
|
roomId: RoomViewStore.getRoomId(),
|
|
|
|
persistentWidgetId: ActiveWidgetStore.getPersistentWidgetId(),
|
|
|
|
};
|
2018-07-12 20:43:49 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidMount() {
|
2018-07-12 20:43:49 +03:00
|
|
|
this._roomStoreToken = RoomViewStore.addListener(this._onRoomViewStoreUpdate);
|
2018-07-24 18:21:43 +03:00
|
|
|
ActiveWidgetStore.on('update', this._onActiveWidgetStoreUpdate);
|
2021-02-25 04:10:35 +03:00
|
|
|
MatrixClientPeg.get().on("Room.myMembership", this._onMyMembership);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-07-12 20:43:49 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentWillUnmount() {
|
2018-07-12 20:43:49 +03:00
|
|
|
if (this._roomStoreToken) {
|
|
|
|
this._roomStoreToken.remove();
|
|
|
|
}
|
2018-07-24 18:21:43 +03:00
|
|
|
ActiveWidgetStore.removeListener('update', this._onActiveWidgetStoreUpdate);
|
2021-02-25 04:10:35 +03:00
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener("Room.myMembership", this._onMyMembership);
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-07-12 20:43:49 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_onRoomViewStoreUpdate = payload => {
|
2018-07-12 20:43:49 +03:00
|
|
|
if (RoomViewStore.getRoomId() === this.state.roomId) return;
|
|
|
|
this.setState({
|
|
|
|
roomId: RoomViewStore.getRoomId(),
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-07-12 20:43:49 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_onActiveWidgetStoreUpdate = () => {
|
2018-07-24 18:21:43 +03:00
|
|
|
this.setState({
|
|
|
|
persistentWidgetId: ActiveWidgetStore.getPersistentWidgetId(),
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-07-24 18:21:43 +03:00
|
|
|
|
2021-02-25 04:27:59 +03:00
|
|
|
_onMyMembership = async (room, membership) => {
|
2021-02-25 04:10:35 +03:00
|
|
|
const persistentWidgetInRoomId = ActiveWidgetStore.getRoomId(this.state.persistentWidgetId);
|
2021-02-25 04:27:59 +03:00
|
|
|
if (membership !== "join") {
|
2021-02-25 04:10:35 +03:00
|
|
|
// we're not in the room anymore - delete
|
|
|
|
if (room.roomId === persistentWidgetInRoomId) {
|
|
|
|
ActiveWidgetStore.destroyPersistentWidget(this.state.persistentWidgetId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2018-07-24 18:21:43 +03:00
|
|
|
if (this.state.persistentWidgetId) {
|
|
|
|
const persistentWidgetInRoomId = ActiveWidgetStore.getRoomId(this.state.persistentWidgetId);
|
2020-10-13 23:55:44 +03:00
|
|
|
|
2021-02-25 04:10:35 +03:00
|
|
|
const persistentWidgetInRoom = MatrixClientPeg.get().getRoom(persistentWidgetInRoomId);
|
|
|
|
|
|
|
|
// Sanity check the room - the widget may have been destroyed between render cycles, and
|
|
|
|
// thus no room is associated anymore.
|
|
|
|
if (!persistentWidgetInRoom) return null;
|
2020-10-13 23:55:44 +03:00
|
|
|
|
2021-02-25 04:10:35 +03:00
|
|
|
const myMembership = persistentWidgetInRoom.getMyMembership();
|
|
|
|
if (this.state.roomId !== persistentWidgetInRoomId && myMembership === "join") {
|
2018-07-12 20:43:49 +03:00
|
|
|
// get the widget data
|
|
|
|
const appEvent = WidgetUtils.getRoomWidgets(persistentWidgetInRoom).find((ev) => {
|
|
|
|
return ev.getStateKey() === ActiveWidgetStore.getPersistentWidgetId();
|
|
|
|
});
|
|
|
|
const app = WidgetUtils.makeAppConfig(
|
2019-11-19 04:02:47 +03:00
|
|
|
appEvent.getStateKey(), appEvent.getContent(), appEvent.getSender(),
|
|
|
|
persistentWidgetInRoomId, appEvent.getId(),
|
2018-07-12 20:43:49 +03:00
|
|
|
);
|
|
|
|
const AppTile = sdk.getComponent('elements.AppTile');
|
|
|
|
return <AppTile
|
|
|
|
key={app.id}
|
2020-04-01 12:00:33 +03:00
|
|
|
app={app}
|
2018-07-12 20:43:49 +03:00
|
|
|
fullWidth={true}
|
|
|
|
room={persistentWidgetInRoom}
|
|
|
|
userId={MatrixClientPeg.get().credentials.userId}
|
|
|
|
creatorUserId={app.creatorUserId}
|
2020-08-28 12:50:27 +03:00
|
|
|
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
|
2018-07-12 20:43:49 +03:00
|
|
|
waitForIframeLoad={app.waitForIframeLoad}
|
|
|
|
miniMode={true}
|
2020-09-17 07:38:12 +03:00
|
|
|
showMenubar={false}
|
2018-07-12 20:43:49 +03:00
|
|
|
/>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-12 20:43:49 +03:00
|
|
|
|