// Dependencies import { useState, useContext } from 'react' import { useTranslation } from 'next-i18next' // Hooks import { useAccount } from 'shared/hooks/use-account.mjs' import { useBackend } from 'shared/hooks/use-backend.mjs' import { useToast } from 'shared/hooks/use-toast.mjs' // Context import { LoadingContext } from 'shared/context/loading-context.mjs' // Components import { Spinner } from 'shared/components/spinner.mjs' import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs' import { OkIcon, NoIcon } from 'shared/components/icons.mjs' import { ContinueButton } from 'shared/components/buttons/continue-button.mjs' export const ns = ['account', 'toast'] export const UsernameSettings = ({ title = false, welcome = false }) => { // Context const { loading, startLoading, stopLoading } = useContext(LoadingContext) // Hooks const { account, setAccount, token } = useAccount() const backend = useBackend(token) const toast = useToast() const { t } = useTranslation(ns) const [username, setUsername] = useState(account.username) const [available, setAvailable] = useState(true) const update = async (evt) => { evt.preventDefault() if (evt.target.value !== username) { setUsername(evt.target.value) const free = await backend.isUsernameAvailable(evt.target.value) setAvailable(free) } } const save = async () => { startLoading() const result = await backend.updateAccount({ username }) if (result.success) { setAccount(result.data.account) toast.for.settingsSaved() } else toast.for.backendError() stopLoading() } const nextHref = welcomeSteps[account.control].length > 4 ? '/welcome/' + welcomeSteps[account.control][5] : '/docs/guide' let btnClasses = 'btn mt-4 capitalize ' if (welcome) { btnClasses += 'w-64 ' if (loading) btnClasses += 'btn-accent ' else btnClasses += 'btn-secondary ' } else { btnClasses += 'w-full ' if (loading) btnClasses += 'btn-accent ' else btnClasses += 'btn-primary ' } return (