/* 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, { useRef, useState } from "react"; import { Room } from "matrix-js-sdk/src/models/room"; import { JoinRule } from "matrix-js-sdk/src/@types/partials"; import { logger } from "matrix-js-sdk/src/logger"; import { _t } from "../../../languageHandler"; import BaseDialog from "./BaseDialog"; import AccessibleButton, { ButtonEvent } from "../elements/AccessibleButton"; import MatrixClientContext from "../../../contexts/MatrixClientContext"; import { BetaPill } from "../beta/BetaCard"; import Field from "../elements/Field"; import RoomAliasField from "../elements/RoomAliasField"; import { createSpace, SpaceCreateForm } from "../spaces/SpaceCreateMenu"; import { SubspaceSelector } from "./AddExistingToSpaceDialog"; import JoinRuleDropdown from "../elements/JoinRuleDropdown"; interface IProps { space: Room; onAddExistingSpaceClick(): void; onFinished(added?: boolean): void; } const CreateSubspaceDialog: React.FC = ({ space, onAddExistingSpaceClick, onFinished }) => { const [parentSpace, setParentSpace] = useState(space); const [busy, setBusy] = useState(false); const [name, setName] = useState(""); const spaceNameField = useRef(); const [alias, setAlias] = useState(""); const spaceAliasField = useRef(); const [avatar, setAvatar] = useState(null); const [topic, setTopic] = useState(""); const spaceJoinRule = space.getJoinRule(); let defaultJoinRule = JoinRule.Restricted; if (spaceJoinRule === JoinRule.Public) { defaultJoinRule = JoinRule.Public; } const [joinRule, setJoinRule] = useState(defaultJoinRule); const onCreateSubspaceClick = async (e: ButtonEvent): Promise => { e.preventDefault(); if (busy) return; setBusy(true); // require & validate the space name field if (!(await spaceNameField.current.validate({ allowEmpty: false }))) { spaceNameField.current.focus(); spaceNameField.current.validate({ allowEmpty: false, focused: true }); setBusy(false); return; } // validate the space name alias field but do not require it if (joinRule === JoinRule.Public && !(await spaceAliasField.current.validate({ allowEmpty: true }))) { spaceAliasField.current.focus(); spaceAliasField.current.validate({ allowEmpty: true, focused: true }); setBusy(false); return; } try { await createSpace(name, joinRule === JoinRule.Public, alias, topic, avatar, {}, { parentSpace, joinRule }); onFinished(true); } catch (e) { logger.error(e); } }; let joinRuleMicrocopy: JSX.Element; if (joinRule === JoinRule.Restricted) { joinRuleMicrocopy = (

{_t( "Anyone in will be able to find and join.", {}, { SpaceName: () => {parentSpace.name}, }, )}

); } else if (joinRule === JoinRule.Public) { joinRuleMicrocopy = (

{_t( "Anyone will be able to find and join this space, not just members of .", {}, { SpaceName: () => {parentSpace.name}, }, )}

); } else if (joinRule === JoinRule.Invite) { joinRuleMicrocopy =

{_t("Only people invited will be able to find and join this space.")}

; } return ( } className="mx_CreateSubspaceDialog" contentId="mx_CreateSubspaceDialog" onFinished={onFinished} fixedWidth={false} >
{_t("Add a space to a space you manage.")}
{joinRuleMicrocopy}
{_t("Want to add an existing space instead?")}
{ onAddExistingSpaceClick(); onFinished(); }} > {_t("Add existing space")}
onFinished(false)}> {_t("Cancel")} {busy ? _t("Adding...") : _t("Add")}
); }; export default CreateSubspaceDialog;