Join.tsx (5410B)
1 import React, { useState, useContext } from "react"; 2 import { Client } from "../context"; 3 import { Header } from "../components/header"; 4 import { useNavigate } from "react-router-dom"; 5 import { useTranslation } from "react-i18next"; 6 7 const ACTIVE_TAB_CSS = "inline-block p-4 w-full text-gray-900 bg-gray-100 hover:ring-blue-300 hover:ring-1 focus:ring-transparent focus:outline-none dark:bg-gray-700 dark:text-white"; 8 const TAB_CSS = "inline-block p-4 w-full bg-white hover:text-gray-700 hover:bg-gray-50 hover:ring-blue-300 hover:ring-1 focus:ring-transparent focus:outline-none dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700"; 9 10 export default function Join() { 11 const navigate = useNavigate(); 12 const client = useContext(Client); 13 const [homeserver, setHomeserver] = useState(import.meta.env.VITE_MATRIX_SERVER_URL); 14 const [username, setUsername] = useState(""); 15 const [password, setPassword] = useState(""); 16 const [createProfile, setCreateProfile] = useState(true); 17 const [currentTab, setCurrentTab] = useState("login"); 18 const { t } = useTranslation(); 19 20 const onSubmit = async (_e: React.FormEvent) => { 21 if (!client) { 22 return; 23 } 24 if (homeserver === "" || username === "" || password === "") { 25 return; 26 } 27 if (currentTab === "login" && !client?.isLoggedIn()) { 28 await client.login(homeserver, username, password, createProfile); 29 } else if (currentTab === "register" && !client.isLoggedIn()) { 30 await client.register(homeserver, username, password, createProfile); 31 } 32 navigate('/', { replace: true }); 33 } 34 35 const onInput = (e: React.FormEvent) => { 36 const target = (e.target as HTMLInputElement); 37 const value = target.type === 'checkbox' ? target.checked : target.value; 38 const name = target.name; 39 switch (name) { 40 case "homeserver": { 41 setHomeserver(value as string); 42 break; 43 } 44 case "username": { 45 setUsername(value as string); 46 break; 47 } 48 case "password": { 49 setPassword(value as string); 50 break; 51 } 52 case "createProfile": { 53 setCreateProfile(value as boolean); 54 break; 55 } 56 } 57 } 58 59 return ( 60 <div className="flex flex-col"> 61 <header> 62 <Header /> 63 </header> 64 65 <main className="m-12 mt-6"> 66 <h1 className="text-3xl font-bold mb-4 text-white">{t('Join')}</h1> 67 <div className="flex items-center flex-col"> 68 <ul className="w-1/4 mb-8 hidden text-sm font-medium text-center text-gray-500 rounded-lg divide-x divide-gray-200 shadow sm:flex dark:divide-gray-700 dark:text-gray-400"> 69 <li className="w-full"> 70 <a href="#" className={currentTab === "login" ? `${ACTIVE_TAB_CSS} rounded-l-lg` : `${TAB_CSS} rounded-l-lg`} onClick={(_e) => { setCurrentTab("login"); }}>{t('Login')}</a> 71 </li> 72 <li className="w-full"> 73 <a href="#" className={currentTab === "register" ? `${ACTIVE_TAB_CSS} rounded-r-lg` : `${TAB_CSS} rounded-r-lg`} onClick={(_e) => { setCurrentTab("register"); }}>{t('Register')}</a> 74 </li> 75 </ul> 76 <form onSubmit={onSubmit} className="flex flex-col items-start w-1/4"> 77 <input className="search-bg shadow rounded-lg border-0 py-3 px-4 placeholder:text-data text-white mb-4 w-full" type="url" placeholder={t("Homeserver") as string} name="homeserver" value={homeserver} onInput={onInput} /> 78 <input className="search-bg shadow rounded-lg border-0 py-3 px-4 placeholder:text-data text-white mb-4 w-full" autoComplete="username" type="text" placeholder={t("Username") as string} name="username" value={username} onInput={onInput} /> 79 <input className="search-bg shadow rounded-lg border-0 py-3 px-4 placeholder:text-data text-white mb-4 w-full" autoComplete="current-password" type="password" placeholder={t("Password") as string} name="password" value={password} onInput={onInput} /> 80 <label className="mb-4 flex items-center"> 81 <input className="checked:bg-blue-600 checked:border-transparent checked:bg-no-repeat checked:bg-[url('/tick.svg')] outline-none focus:outline-offset-2 focus:ring-blue-600 focus:ring-offset-2 focus:ring-offset-white p-0 inline-block align-middle box-border select-none shrink-0 shadow h-5 w-5 appearance-none rounded bg-white border-none" type="checkbox" name="createProfile" checked={createProfile} onChange={onInput} /> 82 <span className="ml-4 text-data text-base font-medium">{t('Create account')}</span> 83 </label> 84 85 <button type="submit" className="text-white font-bold text-lg logo-bg rounded-xl py-2 px-10 shadow transition-transform ease-in-out duration-300 hover:scale-105 w-1/2 self-center justify-center flex">{currentTab === "login" ? t("Login") : t("Register")}</button> 86 </form> 87 </div> 88 </main > 89 </div > 90 ); 91 }