/* Copyright 2021 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, { useMemo, useState } from "react"; import { Room, JoinRule } from "matrix-js-sdk/src/matrix"; import { _t } from "../../../languageHandler"; import DialogButtons from "../elements/DialogButtons"; import BaseDialog from "../dialogs/BaseDialog"; import SpaceStore from "../../../stores/spaces/SpaceStore"; import SpaceChildrenPicker from "../spaces/SpaceChildrenPicker"; import { filterBoolean } from "../../../utils/arrays"; interface IProps { space: Room; onFinished(leave: boolean, rooms?: Room[]): void; } const isOnlyAdmin = (room: Room): boolean => { const userId = room.client.getSafeUserId(); if (room.getMember(userId)?.powerLevelNorm !== 100) { return false; // user is not an admin } return room.getJoinedMembers().every((member) => { // return true if every other member has a lower power level (we are highest) return member.userId === userId || member.powerLevelNorm < 100; }); }; const LeaveSpaceDialog: React.FC = ({ space, onFinished }) => { const spaceChildren = useMemo(() => { const roomSet = new Set(SpaceStore.instance.getSpaceFilteredRoomIds(space.roomId)); SpaceStore.instance.traverseSpace( space.roomId, (spaceId) => { if (space.roomId === spaceId) return; // skip the root node roomSet.add(spaceId); }, false, ); return filterBoolean(Array.from(roomSet).map((roomId) => space.client.getRoom(roomId))); }, [space]); const [roomsToLeave, setRoomsToLeave] = useState([]); const selectedRooms = useMemo(() => new Set(roomsToLeave), [roomsToLeave]); let rejoinWarning; if (space.getJoinRule() !== JoinRule.Public) { rejoinWarning = _t("space|leave_dialog_public_rejoin_warning"); } let onlyAdminWarning; if (isOnlyAdmin(space)) { onlyAdminWarning = _t("space|leave_dialog_only_admin_warning"); } else { const numChildrenOnlyAdminIn = roomsToLeave.filter(isOnlyAdmin).length; if (numChildrenOnlyAdminIn > 0) { onlyAdminWarning = _t("space|leave_dialog_only_admin_room_warning"); } } return ( onFinished(false)} fixedWidth={false} >

{_t( "space|leave_dialog_description", {}, { spaceName: () => {space.name}, }, )}   {rejoinWarning} {rejoinWarning && <> } {spaceChildren.length > 0 && _t("space|leave_dialog_option_intro")}

{spaceChildren.length > 0 && ( )} {onlyAdminWarning &&
{onlyAdminWarning}
}
onFinished(true, roomsToLeave)} hasCancel={true} onCancel={() => onFinished(false)} />
); }; export default LeaveSpaceDialog;