|
| 1 | +/* |
| 2 | +Copyright 2024 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +import { Icon as ExternalLinkIcon } from "@vector-im/compound-design-tokens/icons/link.svg"; |
| 17 | +import { Button, IconButton, Tooltip } from "@vector-im/compound-web"; |
| 18 | +import React, { useCallback } from "react"; |
| 19 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 20 | +import { EventType, IJoinRuleEventContent, JoinRule, Room } from "matrix-js-sdk/src/matrix"; |
| 21 | + |
| 22 | +import Modal from "../../../../Modal"; |
| 23 | +import ShareDialog from "../../dialogs/ShareDialog"; |
| 24 | +import { _t } from "../../../../languageHandler"; |
| 25 | +import SettingsStore from "../../../../settings/SettingsStore"; |
| 26 | +import { calculateRoomVia } from "../../../../utils/permalinks/Permalinks"; |
| 27 | +import BaseDialog from "../../dialogs/BaseDialog"; |
| 28 | +import { useGuestAccessInformation } from "../../../../hooks/room/useGuestAccessInformation"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Display a button to open a dialog to share a link to the call using a element call guest spa url (`element_call:guest_spa_url` in the EW config). |
| 32 | + * @param room |
| 33 | + * @returns Nothing if there is not the option to share a link (No guest_spa_url is set) or a button to open a dialog to share the link. |
| 34 | + */ |
| 35 | +export const CallGuestLinkButton: React.FC<{ room: Room }> = ({ room }) => { |
| 36 | + const { canInviteGuests, guestSpaUrl, isRoomJoinable, canInvite } = useGuestAccessInformation(room); |
| 37 | + |
| 38 | + const generateCallLink = useCallback(() => { |
| 39 | + if (!isRoomJoinable()) throw new Error("Cannot create link for room that users can not join without invite."); |
| 40 | + if (!guestSpaUrl) throw new Error("No guest SPA url for external links provided."); |
| 41 | + const url = new URL(guestSpaUrl); |
| 42 | + url.pathname = "/room/"; |
| 43 | + // Set params for the sharable url |
| 44 | + url.searchParams.set("roomId", room.roomId); |
| 45 | + if (room.hasEncryptionStateEvent()) url.searchParams.set("perParticipantE2EE", "true"); |
| 46 | + for (const server of calculateRoomVia(room)) { |
| 47 | + url.searchParams.set("viaServers", server); |
| 48 | + } |
| 49 | + |
| 50 | + // Move params into hash |
| 51 | + url.hash = "/" + room.name + url.search; |
| 52 | + url.search = ""; |
| 53 | + |
| 54 | + logger.info("Generated element call external url:", url); |
| 55 | + return url; |
| 56 | + }, [guestSpaUrl, isRoomJoinable, room]); |
| 57 | + |
| 58 | + const showLinkModal = useCallback(() => { |
| 59 | + try { |
| 60 | + // generateCallLink throws if the invite rules are not met |
| 61 | + const target = generateCallLink(); |
| 62 | + Modal.createDialog(ShareDialog, { |
| 63 | + target, |
| 64 | + customTitle: _t("share|share_call"), |
| 65 | + subtitle: _t("share|share_call_subtitle"), |
| 66 | + }); |
| 67 | + } catch (e) { |
| 68 | + logger.error("Could not generate call link.", e); |
| 69 | + } |
| 70 | + }, [generateCallLink]); |
| 71 | + |
| 72 | + const shareClick = useCallback(() => { |
| 73 | + if (isRoomJoinable()) { |
| 74 | + showLinkModal(); |
| 75 | + } else { |
| 76 | + // the room needs to be set to public or knock to generate a link |
| 77 | + Modal.createDialog(JoinRuleDialog, { |
| 78 | + room, |
| 79 | + // If the user cannot invite the Knocking is not given as an option. |
| 80 | + canInvite, |
| 81 | + }).finished.then(() => { |
| 82 | + // we need to use the function here because the callback got called before the state was updated. |
| 83 | + if (isRoomJoinable()) showLinkModal(); |
| 84 | + }); |
| 85 | + } |
| 86 | + }, [isRoomJoinable, showLinkModal, room, canInvite]); |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + {canInviteGuests && ( |
| 91 | + <Tooltip label={_t("voip|get_call_link")}> |
| 92 | + <IconButton onClick={shareClick} aria-label={_t("voip|get_call_link")}> |
| 93 | + <ExternalLinkIcon /> |
| 94 | + </IconButton> |
| 95 | + </Tooltip> |
| 96 | + )} |
| 97 | + </> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * A dialog to change the join rule of a room to public or knock. |
| 103 | + * @param room The room to change the join rule of. |
| 104 | + * @param onFinished Callback that is getting called if the dialog wants to close. |
| 105 | + */ |
| 106 | +export const JoinRuleDialog: React.FC<{ |
| 107 | + onFinished(): void; |
| 108 | + room: Room; |
| 109 | + canInvite: boolean; |
| 110 | +}> = ({ onFinished, room, canInvite }) => { |
| 111 | + const askToJoinEnabled = SettingsStore.getValue("feature_ask_to_join"); |
| 112 | + const [isUpdating, setIsUpdating] = React.useState<undefined | JoinRule>(undefined); |
| 113 | + const changeJoinRule = useCallback( |
| 114 | + async (newRule: JoinRule) => { |
| 115 | + if (isUpdating !== undefined) return; |
| 116 | + setIsUpdating(newRule); |
| 117 | + await room.client.sendStateEvent( |
| 118 | + room.roomId, |
| 119 | + EventType.RoomJoinRules, |
| 120 | + { |
| 121 | + join_rule: newRule, |
| 122 | + } as IJoinRuleEventContent, |
| 123 | + "", |
| 124 | + ); |
| 125 | + // Show the dialog for a bit to give the user feedback |
| 126 | + setTimeout(() => onFinished(), 500); |
| 127 | + }, |
| 128 | + [isUpdating, onFinished, room.client, room.roomId], |
| 129 | + ); |
| 130 | + return ( |
| 131 | + <BaseDialog title={_t("update_room_access_modal|title")} onFinished={onFinished} className="mx_JoinRuleDialog"> |
| 132 | + <p>{_t("update_room_access_modal|description")}</p> |
| 133 | + <div className="mx_JoinRuleDialogButtons"> |
| 134 | + {askToJoinEnabled && canInvite && ( |
| 135 | + <Button |
| 136 | + kind="secondary" |
| 137 | + className="mx_Dialog_nonDialogButton" |
| 138 | + disabled={isUpdating === JoinRule.Knock} |
| 139 | + onClick={() => changeJoinRule(JoinRule.Knock)} |
| 140 | + > |
| 141 | + {_t("action|ask_to_join")} |
| 142 | + </Button> |
| 143 | + )} |
| 144 | + <Button |
| 145 | + className="mx_Dialog_nonDialogButton" |
| 146 | + kind="destructive" |
| 147 | + disabled={isUpdating === JoinRule.Public} |
| 148 | + onClick={() => changeJoinRule(JoinRule.Public)} |
| 149 | + > |
| 150 | + {_t("common|public")} |
| 151 | + </Button> |
| 152 | + </div> |
| 153 | + <p>{_t("update_room_access_modal|dont_change_description")}</p> |
| 154 | + <div className="mx_JoinRuleDialogButtons"> |
| 155 | + <Button |
| 156 | + kind="tertiary" |
| 157 | + className="mx_Dialog_nonDialogButton" |
| 158 | + onClick={() => { |
| 159 | + if (isUpdating === undefined) onFinished(); |
| 160 | + }} |
| 161 | + > |
| 162 | + {_t("update_room_access_modal|no_change")} |
| 163 | + </Button> |
| 164 | + </div> |
| 165 | + </BaseDialog> |
| 166 | + ); |
| 167 | +}; |
0 commit comments