2018-07-12 18:43:49 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2018 New Vector Ltd
|
2020-04-01 10:00:33 +01:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2018-07-12 18:43:49 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-10-22 17:23:32 -05:00
|
|
|
import { EventSubscription } from 'fbemitter';
|
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
|
2018-07-12 18:43:49 +01:00
|
|
|
import RoomViewStore from '../../../stores/RoomViewStore';
|
2021-09-26 19:57:02 +02:00
|
|
|
import ActiveWidgetStore, { ActiveWidgetStoreEvent } from '../../../stores/ActiveWidgetStore';
|
2018-07-12 18:43:49 +01:00
|
|
|
import WidgetUtils from '../../../utils/WidgetUtils';
|
2021-06-29 13:11:58 +01:00
|
|
|
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
|
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-09-16 18:46:18 +02:00
|
|
|
import AppTile from "./AppTile";
|
2022-01-11 11:25:21 +01:00
|
|
|
import { Container, WidgetLayoutStore } from '../../../stores/widgets/WidgetLayoutStore';
|
|
|
|
|
import { RightPanelPhases } from '../../../stores/right-panel/RightPanelStorePhases';
|
|
|
|
|
import RightPanelStore from '../../../stores/right-panel/RightPanelStore';
|
|
|
|
|
import { UPDATE_EVENT } from '../../../stores/AsyncStore';
|
2021-09-16 18:46:18 +02:00
|
|
|
|
2021-10-18 13:47:42 -06:00
|
|
|
interface IProps {
|
|
|
|
|
// none
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 18:46:18 +02:00
|
|
|
interface IState {
|
|
|
|
|
roomId: string;
|
|
|
|
|
persistentWidgetId: string;
|
2022-01-11 11:25:21 +01:00
|
|
|
rightPanelPhase?: RightPanelPhases;
|
2021-09-16 18:46:18 +02:00
|
|
|
}
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2021-03-08 19:59:41 -07:00
|
|
|
@replaceableComponent("views.elements.PersistentApp")
|
2021-10-18 13:47:42 -06:00
|
|
|
export default class PersistentApp extends React.Component<IProps, IState> {
|
2021-09-16 18:46:18 +02:00
|
|
|
private roomStoreToken: EventSubscription;
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2021-10-18 13:47:42 -06:00
|
|
|
constructor(props: IProps) {
|
|
|
|
|
super(props);
|
2021-09-16 18:46:18 +02:00
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
roomId: RoomViewStore.getRoomId(),
|
2021-09-26 19:57:02 +02:00
|
|
|
persistentWidgetId: ActiveWidgetStore.instance.getPersistentWidgetId(),
|
2022-01-11 11:25:21 +01:00
|
|
|
rightPanelPhase: RightPanelStore.instance.currentCard.phase,
|
2021-09-16 18:46:18 +02:00
|
|
|
};
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2021-09-16 18:46:18 +02:00
|
|
|
public componentDidMount(): void {
|
|
|
|
|
this.roomStoreToken = RoomViewStore.addListener(this.onRoomViewStoreUpdate);
|
2021-09-26 19:57:02 +02:00
|
|
|
ActiveWidgetStore.instance.on(ActiveWidgetStoreEvent.Update, this.onActiveWidgetStoreUpdate);
|
2022-01-11 11:25:21 +01:00
|
|
|
RightPanelStore.instance.on(UPDATE_EVENT, this.onRightPanelStoreUpdate);
|
2021-09-16 18:46:18 +02:00
|
|
|
MatrixClientPeg.get().on("Room.myMembership", this.onMyMembership);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public componentWillUnmount(): void {
|
|
|
|
|
if (this.roomStoreToken) {
|
|
|
|
|
this.roomStoreToken.remove();
|
2018-07-12 18:43:49 +01:00
|
|
|
}
|
2021-09-26 19:57:02 +02:00
|
|
|
ActiveWidgetStore.instance.removeListener(ActiveWidgetStoreEvent.Update, this.onActiveWidgetStoreUpdate);
|
2022-01-11 11:25:21 +01:00
|
|
|
RightPanelStore.instance.off(UPDATE_EVENT, this.onRightPanelStoreUpdate);
|
2021-02-24 18:10:35 -07:00
|
|
|
if (MatrixClientPeg.get()) {
|
2021-09-16 18:46:18 +02:00
|
|
|
MatrixClientPeg.get().removeListener("Room.myMembership", this.onMyMembership);
|
2021-02-24 18:10:35 -07:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2021-09-16 18:46:18 +02:00
|
|
|
private onRoomViewStoreUpdate = (): void => {
|
2018-07-12 18:43:49 +01:00
|
|
|
if (RoomViewStore.getRoomId() === this.state.roomId) return;
|
|
|
|
|
this.setState({
|
|
|
|
|
roomId: RoomViewStore.getRoomId(),
|
|
|
|
|
});
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2022-01-11 11:25:21 +01:00
|
|
|
private onRightPanelStoreUpdate = () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
rightPanelPhase: RightPanelStore.instance.currentCard.phase,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-16 18:46:18 +02:00
|
|
|
private onActiveWidgetStoreUpdate = (): void => {
|
2018-07-24 16:21:43 +01:00
|
|
|
this.setState({
|
2021-09-26 19:57:02 +02:00
|
|
|
persistentWidgetId: ActiveWidgetStore.instance.getPersistentWidgetId(),
|
2018-07-24 16:21:43 +01:00
|
|
|
});
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2018-07-24 16:21:43 +01:00
|
|
|
|
2021-09-16 18:46:18 +02:00
|
|
|
private onMyMembership = async (room: Room, membership: string): Promise<void> => {
|
2021-09-26 19:57:02 +02:00
|
|
|
const persistentWidgetInRoomId = ActiveWidgetStore.instance.getRoomId(this.state.persistentWidgetId);
|
2021-02-24 18:27:59 -07:00
|
|
|
if (membership !== "join") {
|
2021-02-24 18:10:35 -07:00
|
|
|
// we're not in the room anymore - delete
|
2021-09-16 18:46:18 +02:00
|
|
|
if (room .roomId === persistentWidgetInRoomId) {
|
2021-09-26 19:57:02 +02:00
|
|
|
ActiveWidgetStore.instance.destroyPersistentWidget(this.state.persistentWidgetId);
|
2021-02-24 18:10:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-16 18:46:18 +02:00
|
|
|
public render(): JSX.Element {
|
2022-01-11 11:25:21 +01:00
|
|
|
const wId = this.state.persistentWidgetId;
|
|
|
|
|
if (wId) {
|
|
|
|
|
const persistentWidgetInRoomId = ActiveWidgetStore.instance.getRoomId(wId);
|
2020-10-13 14:55:44 -06:00
|
|
|
|
2021-02-24 18:10:35 -07:00
|
|
|
const persistentWidgetInRoom = MatrixClientPeg.get().getRoom(persistentWidgetInRoomId);
|
2020-10-13 14:55:44 -06:00
|
|
|
|
2021-02-24 18:10:35 -07:00
|
|
|
// Sanity check the room - the widget may have been destroyed between render cycles, and
|
|
|
|
|
// thus no room is associated anymore.
|
|
|
|
|
if (!persistentWidgetInRoom) return null;
|
|
|
|
|
|
2022-01-11 11:25:21 +01:00
|
|
|
const wls = WidgetLayoutStore.instance;
|
|
|
|
|
|
|
|
|
|
const userIsPartOfTheRoom = persistentWidgetInRoom.getMyMembership() == "join";
|
|
|
|
|
const fromAnotherRoom = this.state.roomId !== persistentWidgetInRoomId;
|
|
|
|
|
|
|
|
|
|
const notInRightPanel =
|
|
|
|
|
!(this.state.rightPanelPhase == RightPanelPhases.Widget &&
|
|
|
|
|
wId == RightPanelStore.instance.currentCard.state?.widgetId);
|
|
|
|
|
const notInCenterContainer =
|
|
|
|
|
!wls.getContainerWidgets(persistentWidgetInRoom, Container.Center).some((app) => app.id == wId);
|
|
|
|
|
const notInTopContainer =
|
|
|
|
|
!wls.getContainerWidgets(persistentWidgetInRoom, Container.Top).some(app => app.id == wId);
|
|
|
|
|
if (
|
|
|
|
|
// the widget should only be shown as a persistent app (in a floating pip container) if it is not visible on screen
|
|
|
|
|
// either, because we are viewing a different room OR because it is in none of the possible containers of the room view.
|
|
|
|
|
(fromAnotherRoom && userIsPartOfTheRoom) ||
|
|
|
|
|
(notInRightPanel && notInCenterContainer && notInTopContainer && userIsPartOfTheRoom)
|
|
|
|
|
) {
|
2018-07-12 18:43:49 +01:00
|
|
|
// get the widget data
|
|
|
|
|
const appEvent = WidgetUtils.getRoomWidgets(persistentWidgetInRoom).find((ev) => {
|
2021-09-26 19:57:02 +02:00
|
|
|
return ev.getStateKey() === ActiveWidgetStore.instance.getPersistentWidgetId();
|
2018-07-12 18:43:49 +01:00
|
|
|
});
|
|
|
|
|
const app = WidgetUtils.makeAppConfig(
|
2019-11-18 18:02:47 -07:00
|
|
|
appEvent.getStateKey(), appEvent.getContent(), appEvent.getSender(),
|
|
|
|
|
persistentWidgetInRoomId, appEvent.getId(),
|
2018-07-12 18:43:49 +01:00
|
|
|
);
|
|
|
|
|
return <AppTile
|
|
|
|
|
key={app.id}
|
2020-04-01 10:00:33 +01:00
|
|
|
app={app}
|
2018-07-12 18:43:49 +01:00
|
|
|
fullWidth={true}
|
|
|
|
|
room={persistentWidgetInRoom}
|
|
|
|
|
userId={MatrixClientPeg.get().credentials.userId}
|
|
|
|
|
creatorUserId={app.creatorUserId}
|
2020-08-28 10:50:27 +01:00
|
|
|
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
|
2018-07-12 18:43:49 +01:00
|
|
|
waitForIframeLoad={app.waitForIframeLoad}
|
|
|
|
|
miniMode={true}
|
2020-09-16 22:38:12 -06:00
|
|
|
showMenubar={false}
|
2018-07-12 18:43:49 +01:00
|
|
|
/>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 18:43:49 +01:00
|
|
|
|