/* 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, JoinRule } from "matrix-js-sdk/src/matrix"; 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(null); const [alias, setAlias] = useState(""); const spaceAliasField = useRef(null); const [avatar, setAvatar] = useState(); 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 (spaceNameField.current && !(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 ( spaceAliasField.current && 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( space.client, name, joinRule === JoinRule.Public, alias, topic, avatar, {}, { parentSpace, joinRule }, ); onFinished(true); } catch (e) { logger.error(e); } }; let joinRuleMicrocopy: JSX.Element | undefined; if (joinRule === JoinRule.Restricted) { joinRuleMicrocopy = (

{_t( "create_space|subspace_join_rule_restricted_description", {}, { SpaceName: () => {parentSpace.name}, }, )}

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

{_t( "create_space|subspace_join_rule_public_description", {}, { SpaceName: () => {parentSpace.name}, }, )}

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

{_t("create_space|subspace_join_rule_invite_description")}

; } return ( } className="mx_CreateSubspaceDialog" contentId="mx_CreateSubspaceDialog" onFinished={onFinished} fixedWidth={false} >
{_t("create_space|subspace_beta_notice")}
{joinRuleMicrocopy}
{_t("create_space|subspace_existing_space_prompt")}
{ onAddExistingSpaceClick(); onFinished(); }} > {_t("space|add_existing_subspace|space_dropdown_title")}
onFinished(false)}> {_t("action|cancel")} {busy ? _t("create_space|subspace_adding") : _t("action|add")}
); }; export default CreateSubspaceDialog;