|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { FC, useState, type JSX, FormEvent } from "react"; |
| 9 | +import { type Api, type AccountAuthInfo, type DialogProps } from "@element-hq/element-web-module-api"; |
| 10 | +import { Form } from "@vector-im/compound-web"; |
| 11 | + |
| 12 | +import { ModuleConfig } from "./config.ts"; |
| 13 | + |
| 14 | +interface RegisterDialogProps extends DialogProps<AccountAuthInfo> { |
| 15 | + api: Api; |
| 16 | + config: ModuleConfig; |
| 17 | +} |
| 18 | + |
| 19 | +const enum State { |
| 20 | + Idle, |
| 21 | + Busy, |
| 22 | + Error, |
| 23 | +} |
| 24 | + |
| 25 | +const RegisterDialog: FC<RegisterDialogProps> = ({ api, config, onCancel, onSubmit }) => { |
| 26 | + const [username, setUsername] = useState(""); |
| 27 | + const [state, setState] = useState<State>(State.Idle); |
| 28 | + |
| 29 | + async function trySubmit(ev: FormEvent): Promise<void> { |
| 30 | + ev.preventDefault(); |
| 31 | + setState(State.Busy); |
| 32 | + |
| 33 | + try { |
| 34 | + const homeserverUrl = config.guest_user_homeserver_url; |
| 35 | + |
| 36 | + const url = new URL("/_synapse/client/register_guest", homeserverUrl); |
| 37 | + |
| 38 | + const response = await fetch(url, { |
| 39 | + method: "POST", |
| 40 | + headers: { "Content-Type": "application/json" }, |
| 41 | + body: JSON.stringify({ displayname: username }), |
| 42 | + }); |
| 43 | + |
| 44 | + if (response.ok) { |
| 45 | + const accountAuthInfo = await response.json(); |
| 46 | + onSubmit(accountAuthInfo); |
| 47 | + } |
| 48 | + } catch (e) { |
| 49 | + console.error("Failed to create guest account", e); |
| 50 | + setState(State.Error); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + let message: JSX.Element | undefined; |
| 55 | + if (state === State.Error) { |
| 56 | + message = <Form.ErrorMessage>{api.i18n.translate("register_dialog_error")}</Form.ErrorMessage>; |
| 57 | + } else if (state === State.Busy) { |
| 58 | + message = <Form.LoadingMessage>{api.i18n.translate("register_dialog_busy")}</Form.LoadingMessage>; |
| 59 | + } |
| 60 | + |
| 61 | + const disabled = state !== State.Idle; |
| 62 | + |
| 63 | + return ( |
| 64 | + <Form.Root onSubmit={trySubmit}> |
| 65 | + <Form.Field name="mxid"> |
| 66 | + <Form.Label>{api.i18n.translate("register_dialog_register_username_label")}</Form.Label> |
| 67 | + <Form.TextControl |
| 68 | + disabled={disabled} |
| 69 | + value={username} |
| 70 | + onChange={(event) => { |
| 71 | + setUsername(event.currentTarget.value); |
| 72 | + }} |
| 73 | + placeholder={api.i18n.translate("register_dialog_field_label")} |
| 74 | + /> |
| 75 | + {message} |
| 76 | + </Form.Field> |
| 77 | + |
| 78 | + <a href={config.skip_single_sign_on ? "/#/login" : "/#/start_sso"} onClick={onCancel}> |
| 79 | + {api.i18n.translate("register_dialog_existing_account")} |
| 80 | + </a> |
| 81 | + |
| 82 | + <Form.Submit disabled={disabled || !username}> |
| 83 | + {api.i18n.translate("register_dialog_continue_label")} |
| 84 | + </Form.Submit> |
| 85 | + </Form.Root> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default RegisterDialog; |
0 commit comments