Files
ThreadNet-Web/src/components/views/right_panel/WidgetCard.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-09-08 08:48:03 +01:00
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
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, { useContext, useEffect } from "react";
import { Room } from "matrix-js-sdk/src/models/room";
2020-09-08 08:48:03 +01:00
import MatrixClientContext from "../../../contexts/MatrixClientContext";
import BaseCard from "./BaseCard";
import WidgetUtils from "../../../utils/WidgetUtils";
import AppTile from "../elements/AppTile";
import { _t } from "../../../languageHandler";
import { useWidgets } from "./RoomSummaryCard";
import { ChevronFace, ContextMenuButton, useContextMenu } from "../../structures/ContextMenu";
import { WidgetContextMenu } from "../context_menus/WidgetContextMenu";
import { Container, WidgetLayoutStore } from "../../../stores/widgets/WidgetLayoutStore";
import UIStore from "../../../stores/UIStore";
2022-01-05 16:14:44 +01:00
import RightPanelStore from "../../../stores/right-panel/RightPanelStore";
import Heading from "../typography/Heading";
2020-09-08 08:48:03 +01:00
interface IProps {
room: Room;
widgetId: string;
onClose(): void;
}
const WidgetCard: React.FC<IProps> = ({ room, widgetId, onClose }) => {
const cli = useContext(MatrixClientContext);
const apps = useWidgets(room);
const app = apps.find((a) => a.id === widgetId);
2022-02-16 11:19:28 +00:00
const isRight = app && WidgetLayoutStore.instance.isInContainer(room, app, Container.Right);
2020-09-08 08:48:03 +01:00
2020-09-08 10:19:51 +01:00
const [menuDisplayed, handle, openMenu, closeMenu] = useContextMenu();
2020-09-08 08:48:03 +01:00
useEffect(() => {
2022-02-16 11:19:28 +00:00
if (!app || !isRight) {
2020-09-08 16:27:09 +01:00
// stop showing this card
RightPanelStore.instance.popCard();
2020-09-08 08:48:03 +01:00
}
2022-02-16 11:19:28 +00:00
}, [app, isRight]);
2020-09-08 08:48:03 +01:00
// Don't render anything as we are about to transition
2022-02-16 11:19:28 +00:00
if (!app || !isRight) return null;
2020-09-08 08:48:03 +01:00
2020-09-08 10:19:51 +01:00
let contextMenu;
if (menuDisplayed) {
const rect = handle.current.getBoundingClientRect();
contextMenu = (
2020-10-12 09:51:49 +01:00
<WidgetContextMenu
2020-09-08 10:19:51 +01:00
chevronFace={ChevronFace.None}
right={UIStore.instance.windowWidth - rect.right - 12}
2020-10-09 08:42:21 +01:00
top={rect.bottom + 12}
2020-09-08 10:19:51 +01:00
onFinished={closeMenu}
2020-09-30 15:38:35 +01:00
app={app}
/>
2020-09-08 10:19:51 +01:00
);
}
const header = (
<div className="mx_BaseCard_header_title">
<Heading size="h4" className="mx_BaseCard_header_title_heading">
{WidgetUtils.getWidgetName(app)}
</Heading>
2020-09-08 10:19:51 +01:00
<ContextMenuButton
className="mx_BaseCard_header_title_button--option"
2020-09-08 10:19:51 +01:00
inputRef={handle}
onClick={openMenu}
isExpanded={menuDisplayed}
label={_t("Options")}
2020-09-09 12:50:48 +01:00
/>
2020-09-08 10:19:51 +01:00
{contextMenu}
</div>
);
2020-09-08 08:48:03 +01:00
return (
2020-10-09 08:42:21 +01:00
<BaseCard header={header} className="mx_WidgetCard" onClose={onClose} withoutScrollContainer>
2020-09-08 08:48:03 +01:00
<AppTile
app={app}
fullWidth
showMenubar={false}
room={room}
userId={cli.getUserId()}
creatorUserId={app.creatorUserId}
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
waitForIframeLoad={app.waitForIframeLoad}
/>
</BaseCard>
);
};
export default WidgetCard;