/* 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, {useContext, useRef, useState} from "react"; import classNames from "classnames"; import {EventType, RoomType, RoomCreateTypeField} from "matrix-js-sdk/src/@types/event"; import FocusLock from "react-focus-lock"; import {_t} from "../../../languageHandler"; import AccessibleTooltipButton from "../elements/AccessibleTooltipButton"; import {ChevronFace, ContextMenu} from "../../structures/ContextMenu"; import createRoom, {IStateEvent, Preset} from "../../../createRoom"; import MatrixClientContext from "../../../contexts/MatrixClientContext"; import {SpaceAvatar} from "./SpaceBasicSettings"; import AccessibleButton from "../elements/AccessibleButton"; import {BetaPill} from "../beta/BetaCard"; import defaultDispatcher from "../../../dispatcher/dispatcher"; import {Action} from "../../../dispatcher/actions"; import {USER_LABS_TAB} from "../dialogs/UserSettingsDialog"; import Field from "../elements/Field"; import withValidation from "../elements/Validation"; import {SpaceFeedbackPrompt} from "../../structures/SpaceRoomView"; const SpaceCreateMenuType = ({ title, description, className, onClick }) => { return (

{ title }

{ description }
); }; enum Visibility { Public, Private, } const spaceNameValidator = withValidation({ rules: [ { key: "required", test: async ({ value }) => !!value, invalid: () => _t("Please enter a name for the space"), }, ], }); const SpaceCreateMenu = ({ onFinished }) => { const cli = useContext(MatrixClientContext); const [visibility, setVisibility] = useState(null); const [busy, setBusy] = useState(false); const [name, setName] = useState(""); const spaceNameField = useRef(); const [avatar, setAvatar] = useState(null); const [topic, setTopic] = useState(""); const onSpaceCreateClick = async (e) => { 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; } const initialState: IStateEvent[] = [ { type: EventType.RoomHistoryVisibility, content: { "history_visibility": visibility === Visibility.Public ? "world_readable" : "invited", }, }, ]; if (avatar) { const url = await cli.uploadContent(avatar); initialState.push({ type: EventType.RoomAvatar, content: { url }, }); } if (topic) { initialState.push({ type: EventType.RoomTopic, content: { topic }, }); } try { await createRoom({ createOpts: { preset: visibility === Visibility.Public ? Preset.PublicChat : Preset.PrivateChat, name, creation_content: { // Based on MSC1840 [RoomCreateTypeField]: RoomType.Space, }, initial_state: initialState, power_level_content_override: { // Only allow Admins to write to the timeline to prevent hidden sync spam events_default: 100, ...Visibility.Public ? { invite: 0 } : {}, }, }, spinner: false, encryption: false, andView: true, inlineErrors: true, }); onFinished(); } catch (e) { console.error(e); } }; let body; if (visibility === null) { body =

{ _t("Create a space") }

{ _t("Spaces are a new way to group rooms and people. " + "To join an existing space you'll need an invite.") }

setVisibility(Visibility.Public)} /> setVisibility(Visibility.Private)} />

{ _t("You can change this later") }

; } else { body = setVisibility(null)} title={_t("Go back")} />

{ visibility === Visibility.Public ? _t("Your public space") : _t("Your private space") }

{ _t("Add some details to help people recognise it.") } { _t("You can change these anytime.") }

setName(ev.target.value)} ref={spaceNameField} onValidate={spaceNameValidator} disabled={busy} /> setTopic(ev.target.value)} rows={3} disabled={busy} /> { busy ? _t("Creating...") : _t("Create") }
; } return { onFinished(); defaultDispatcher.dispatch({ action: Action.ViewUserSettings, initialTabId: USER_LABS_TAB, }); }} /> { body } ; } export default SpaceCreateMenu;