Files
element-call/src/room/AppSelectionModal.tsx
T

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-09-17 17:48:03 -04:00
/*
Copyright 2023, 2024 New Vector Ltd.
2023-09-17 17:48:03 -04:00
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
2023-09-17 17:48:03 -04:00
*/
import { FC, MouseEvent, useCallback, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Button, Text } from "@vector-im/compound-web";
import { PopOutIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
import { logger } from "matrix-js-sdk/src/logger";
2023-09-17 17:48:03 -04:00
2023-09-18 11:50:09 -04:00
import { Modal } from "../Modal";
2024-04-23 15:15:13 +02:00
import { useRoomEncryptionSystem } from "../e2ee/sharedKeyManagement";
import { getAbsoluteRoomUrl } from "../utils/matrix";
2023-09-17 17:48:03 -04:00
import styles from "./AppSelectionModal.module.css";
import { editFragmentQuery } from "../UrlParams";
2024-04-23 15:15:13 +02:00
import { E2eeType } from "../e2ee/e2eeType";
2023-09-17 17:48:03 -04:00
interface Props {
2024-04-23 15:15:13 +02:00
roomId: string;
2023-09-17 17:48:03 -04:00
}
export const AppSelectionModal: FC<Props> = ({ roomId }) => {
const { t } = useTranslation();
const [open, setOpen] = useState(true);
const onBrowserClick = useCallback(
(e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
setOpen(false);
},
2023-10-11 10:42:04 -04:00
[setOpen],
2023-09-17 17:48:03 -04:00
);
2024-04-23 15:15:13 +02:00
const e2eeSystem = useRoomEncryptionSystem(roomId);
2023-09-17 17:48:03 -04:00
2024-04-23 15:15:13 +02:00
if (e2eeSystem.kind === E2eeType.NONE) {
logger.error(
2023-10-11 10:42:04 -04:00
"Generating app redirect URL for encrypted room but don't have key available!",
);
}
2023-09-17 17:48:03 -04:00
const appUrl = useMemo(() => {
// If the room ID is not known, fall back to the URL of the current page
2023-09-19 18:23:44 +01:00
// Also, we don't really know the room name at this stage as we haven't
// started a client and synced to get the room details. We could take the one
// we got in our own URL and use that, but it's not a string that a human
// ever sees so it's somewhat redundant. We just don't pass a name.
2023-09-17 17:48:03 -04:00
const url = new URL(
2023-09-22 18:20:29 +01:00
roomId === null
2023-09-17 17:48:03 -04:00
? window.location.href
2024-04-23 15:15:13 +02:00
: getAbsoluteRoomUrl(roomId, e2eeSystem),
2023-09-17 17:48:03 -04:00
);
2023-09-18 20:47:47 -04:00
// Edit the URL to prevent the app selection prompt from appearing a second
// time within the app, and to keep the user confined to the current room
2023-09-17 17:48:03 -04:00
url.hash = editFragmentQuery(url.hash, (params) => {
2023-09-18 20:47:47 -04:00
params.set("appPrompt", "false");
2023-09-19 07:12:27 -04:00
params.set("confineToRoom", "true");
2023-09-17 17:48:03 -04:00
return params;
});
2023-09-20 19:55:17 +02:00
const result = new URL("io.element.call:/");
2023-09-17 17:48:03 -04:00
result.searchParams.set("url", url.toString());
return result.toString();
2024-04-23 15:15:13 +02:00
}, [e2eeSystem, roomId]);
2023-09-17 17:48:03 -04:00
return (
2023-11-20 13:38:01 +00:00
<Modal
className={styles.modal}
title={t("app_selection_modal.title")}
open={open}
>
2023-09-17 17:48:03 -04:00
<Text size="md" weight="semibold">
2023-11-20 13:00:43 +00:00
{t("app_selection_modal.text")}
2023-09-17 17:48:03 -04:00
</Text>
<Button kind="secondary" onClick={onBrowserClick}>
2023-11-20 13:00:43 +00:00
{t("app_selection_modal.continue_in_browser")}
2023-09-17 17:48:03 -04:00
</Button>
<Button as="a" href={appUrl} Icon={PopOutIcon}>
2023-11-20 13:00:43 +00:00
{t("app_selection_modal.open_in_app")}
2023-09-17 17:48:03 -04:00
</Button>
</Modal>
);
};