|
| 1 | +/* |
| 2 | +Copyright 2021 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 | + |
| 17 | +import React, { useContext } from "react"; |
| 18 | +import { Room } from "matrix-js-sdk/src/models/room"; |
| 19 | +import { EventType } from "matrix-js-sdk/src/@types/event"; |
| 20 | + |
| 21 | +import { |
| 22 | + IProps as IContextMenuProps, |
| 23 | +} from "../../structures/ContextMenu"; |
| 24 | +import IconizedContextMenu, { IconizedContextMenuOption, IconizedContextMenuOptionList } from "./IconizedContextMenu"; |
| 25 | +import { _t } from "../../../languageHandler"; |
| 26 | +import { |
| 27 | + leaveSpace, |
| 28 | + shouldShowSpaceSettings, |
| 29 | + showAddExistingRooms, |
| 30 | + showCreateNewRoom, |
| 31 | + showCreateNewSubspace, |
| 32 | + showSpaceInvite, |
| 33 | + showSpaceSettings, |
| 34 | +} from "../../../utils/space"; |
| 35 | +import MatrixClientContext from "../../../contexts/MatrixClientContext"; |
| 36 | +import { ButtonEvent } from "../elements/AccessibleButton"; |
| 37 | +import defaultDispatcher from "../../../dispatcher/dispatcher"; |
| 38 | +import RoomViewStore from "../../../stores/RoomViewStore"; |
| 39 | +import { SetRightPanelPhasePayload } from "../../../dispatcher/payloads/SetRightPanelPhasePayload"; |
| 40 | +import { Action } from "../../../dispatcher/actions"; |
| 41 | +import { RightPanelPhases } from "../../../stores/RightPanelStorePhases"; |
| 42 | +import { BetaPill } from "../beta/BetaCard"; |
| 43 | + |
| 44 | +interface IProps extends IContextMenuProps { |
| 45 | + space: Room; |
| 46 | +} |
| 47 | + |
| 48 | +const SpaceContextMenu = ({ space, onFinished, ...props }: IProps) => { |
| 49 | + const cli = useContext(MatrixClientContext); |
| 50 | + const userId = cli.getUserId(); |
| 51 | + |
| 52 | + let inviteOption; |
| 53 | + if (space.getJoinRule() === "public" || space.canInvite(userId)) { |
| 54 | + const onInviteClick = (ev: ButtonEvent) => { |
| 55 | + ev.preventDefault(); |
| 56 | + ev.stopPropagation(); |
| 57 | + |
| 58 | + showSpaceInvite(space); |
| 59 | + onFinished(); |
| 60 | + }; |
| 61 | + |
| 62 | + inviteOption = ( |
| 63 | + <IconizedContextMenuOption |
| 64 | + className="mx_SpacePanel_contextMenu_inviteButton" |
| 65 | + iconClassName="mx_SpacePanel_iconInvite" |
| 66 | + label={_t("Invite people")} |
| 67 | + onClick={onInviteClick} |
| 68 | + /> |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + let settingsOption; |
| 73 | + let leaveSection; |
| 74 | + if (shouldShowSpaceSettings(space)) { |
| 75 | + const onSettingsClick = (ev: ButtonEvent) => { |
| 76 | + ev.preventDefault(); |
| 77 | + ev.stopPropagation(); |
| 78 | + |
| 79 | + showSpaceSettings(space); |
| 80 | + onFinished(); |
| 81 | + }; |
| 82 | + |
| 83 | + settingsOption = ( |
| 84 | + <IconizedContextMenuOption |
| 85 | + iconClassName="mx_SpacePanel_iconSettings" |
| 86 | + label={_t("Settings")} |
| 87 | + onClick={onSettingsClick} |
| 88 | + /> |
| 89 | + ); |
| 90 | + } else { |
| 91 | + const onLeaveClick = (ev: ButtonEvent) => { |
| 92 | + ev.preventDefault(); |
| 93 | + ev.stopPropagation(); |
| 94 | + |
| 95 | + leaveSpace(space); |
| 96 | + onFinished(); |
| 97 | + }; |
| 98 | + |
| 99 | + leaveSection = <IconizedContextMenuOptionList red first> |
| 100 | + <IconizedContextMenuOption |
| 101 | + iconClassName="mx_SpacePanel_iconLeave" |
| 102 | + label={_t("Leave space")} |
| 103 | + onClick={onLeaveClick} |
| 104 | + /> |
| 105 | + </IconizedContextMenuOptionList>; |
| 106 | + } |
| 107 | + |
| 108 | + const canAddRooms = space.currentState.maySendStateEvent(EventType.SpaceChild, userId); |
| 109 | + |
| 110 | + let newRoomSection; |
| 111 | + if (space.currentState.maySendStateEvent(EventType.SpaceChild, userId)) { |
| 112 | + const onNewRoomClick = (ev: ButtonEvent) => { |
| 113 | + ev.preventDefault(); |
| 114 | + ev.stopPropagation(); |
| 115 | + |
| 116 | + showCreateNewRoom(space); |
| 117 | + onFinished(); |
| 118 | + }; |
| 119 | + |
| 120 | + const onAddExistingRoomClick = (ev: ButtonEvent) => { |
| 121 | + ev.preventDefault(); |
| 122 | + ev.stopPropagation(); |
| 123 | + |
| 124 | + showAddExistingRooms(space); |
| 125 | + onFinished(); |
| 126 | + }; |
| 127 | + |
| 128 | + const onNewSubspaceClick = (ev: ButtonEvent) => { |
| 129 | + ev.preventDefault(); |
| 130 | + ev.stopPropagation(); |
| 131 | + |
| 132 | + showCreateNewSubspace(space); |
| 133 | + onFinished(); |
| 134 | + }; |
| 135 | + |
| 136 | + newRoomSection = <IconizedContextMenuOptionList first> |
| 137 | + <IconizedContextMenuOption |
| 138 | + iconClassName="mx_SpacePanel_iconPlus" |
| 139 | + label={_t("Create new room")} |
| 140 | + onClick={onNewRoomClick} |
| 141 | + /> |
| 142 | + <IconizedContextMenuOption |
| 143 | + iconClassName="mx_SpacePanel_iconHash" |
| 144 | + label={_t("Add existing room")} |
| 145 | + onClick={onAddExistingRoomClick} |
| 146 | + /> |
| 147 | + <IconizedContextMenuOption |
| 148 | + iconClassName="mx_SpacePanel_iconPlus" |
| 149 | + label={_t("Add space")} |
| 150 | + onClick={onNewSubspaceClick} |
| 151 | + > |
| 152 | + <BetaPill /> |
| 153 | + </IconizedContextMenuOption> |
| 154 | + </IconizedContextMenuOptionList>; |
| 155 | + } |
| 156 | + |
| 157 | + const onMembersClick = (ev: ButtonEvent) => { |
| 158 | + ev.preventDefault(); |
| 159 | + ev.stopPropagation(); |
| 160 | + |
| 161 | + if (!RoomViewStore.getRoomId()) { |
| 162 | + defaultDispatcher.dispatch({ |
| 163 | + action: "view_room", |
| 164 | + room_id: space.roomId, |
| 165 | + }, true); |
| 166 | + } |
| 167 | + |
| 168 | + defaultDispatcher.dispatch<SetRightPanelPhasePayload>({ |
| 169 | + action: Action.SetRightPanelPhase, |
| 170 | + phase: RightPanelPhases.SpaceMemberList, |
| 171 | + refireParams: { space: space }, |
| 172 | + }); |
| 173 | + onFinished(); |
| 174 | + }; |
| 175 | + |
| 176 | + const onExploreRoomsClick = (ev: ButtonEvent) => { |
| 177 | + ev.preventDefault(); |
| 178 | + ev.stopPropagation(); |
| 179 | + |
| 180 | + defaultDispatcher.dispatch({ |
| 181 | + action: "view_room", |
| 182 | + room_id: space.roomId, |
| 183 | + }); |
| 184 | + onFinished(); |
| 185 | + }; |
| 186 | + |
| 187 | + return <IconizedContextMenu |
| 188 | + {...props} |
| 189 | + onFinished={onFinished} |
| 190 | + className="mx_SpacePanel_contextMenu" |
| 191 | + compact |
| 192 | + > |
| 193 | + <div className="mx_SpacePanel_contextMenu_header"> |
| 194 | + { space.name } |
| 195 | + </div> |
| 196 | + <IconizedContextMenuOptionList first> |
| 197 | + { inviteOption } |
| 198 | + <IconizedContextMenuOption |
| 199 | + iconClassName="mx_SpacePanel_iconMembers" |
| 200 | + label={_t("Members")} |
| 201 | + onClick={onMembersClick} |
| 202 | + /> |
| 203 | + { settingsOption } |
| 204 | + <IconizedContextMenuOption |
| 205 | + iconClassName="mx_SpacePanel_iconExplore" |
| 206 | + label={canAddRooms ? _t("Manage & explore rooms") : _t("Explore rooms")} |
| 207 | + onClick={onExploreRoomsClick} |
| 208 | + /> |
| 209 | + </IconizedContextMenuOptionList> |
| 210 | + { newRoomSection } |
| 211 | + { leaveSection } |
| 212 | + </IconizedContextMenu>; |
| 213 | +}; |
| 214 | + |
| 215 | +export default SpaceContextMenu; |
| 216 | + |
0 commit comments