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.

98 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-09-08 08:48:03 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-09-08 08:48:03 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2020-09-08 08:48:03 +01:00
*/
import React, { useContext, useEffect } from "react";
import { Room } from "matrix-js-sdk/src/matrix";
2020-09-08 08:48:03 +01:00
import MatrixClientContext from "../../../contexts/MatrixClientContext";
import BaseCard from "./BaseCard";
import WidgetUtils, { useWidgets } from "../../../utils/WidgetUtils";
2020-09-08 08:48:03 +01:00
import AppTile from "../elements/AppTile";
import { _t } from "../../../languageHandler";
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
let contextMenu: JSX.Element | undefined;
2020-09-08 10:19:51 +01:00
if (menuDisplayed) {
const rect = handle.current?.getBoundingClientRect();
const rightMargin = rect ? rect.right : 0;
const bottomMargin = rect ? rect.bottom : 0;
2020-09-08 10:19:51 +01:00
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 - rightMargin - 12}
top={bottomMargin + 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">
2023-06-29 11:30:25 +01:00
<Heading size="4" 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"
ref={handle}
2020-09-08 10:19:51 +01:00
onClick={openMenu}
isExpanded={menuDisplayed}
label={_t("common|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.getSafeUserId()}
2020-09-08 08:48:03 +01:00
creatorUserId={app.creatorUserId}
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
waitForIframeLoad={app.waitForIframeLoad}
/>
</BaseCard>
);
};
export default WidgetCard;