// 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 { useLoadingStatus } from 'shared/hooks/use-loading-status.mjs' 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 }) => { // Hooks const { account, setAccount, token } = useAccount() const backend = useBackend(token) const { setLoadingStatus, LoadingStatus } = useLoadingStatus() 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 result = await backend.isUsernameAvailable(evt.target.value) if (result?.response?.response?.status === 404) setAvailable(true) else setAvailable(false) } } const save = async () => { setLoadingStatus([true, 'processingUpdate']) const result = await backend.updateAccount({ username }) if (result.success) { setAccount(result.data.account) setLoadingStatus([true, 'settingsSaved', true, true]) } else setLoadingStatus([true, 'backendError', true, true]) } 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 btn-secondary' else btnClasses += 'w-full btn-primary' return (