2023-03-24 16:33:14 +01:00
|
|
|
// Dependencies
|
2023-04-28 21:23:06 +02:00
|
|
|
import { useState, useContext } from 'react'
|
2023-01-26 19:31:23 +01:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-03-24 16:33:14 +01:00
|
|
|
// 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'
|
2023-04-28 21:23:06 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingContext } from 'shared/context/loading-context.mjs'
|
2023-01-29 16:44:02 +01:00
|
|
|
// Components
|
2023-01-26 19:31:23 +01:00
|
|
|
import Markdown from 'react-markdown'
|
2023-02-12 18:35:54 +01:00
|
|
|
import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { Popout } from 'shared/components/popout.mjs'
|
2023-02-12 18:35:54 +01:00
|
|
|
import { SaveSettingsButton } from 'site/components/buttons/save-settings-button.mjs'
|
|
|
|
import { ContinueButton } from 'site/components/buttons/continue-button.mjs'
|
2023-01-26 19:31:23 +01:00
|
|
|
|
2023-02-12 18:35:54 +01:00
|
|
|
export const ns = ['account', 'toast']
|
2023-01-26 19:31:23 +01:00
|
|
|
|
2023-04-22 16:41:13 +02:00
|
|
|
export const Tab = ({ id, activeTab, setActiveTab, t }) => (
|
2023-01-26 19:31:23 +01:00
|
|
|
<button
|
|
|
|
className={`text-xl font-bold capitalize tab tab-bordered grow
|
|
|
|
${activeTab === id ? 'tab-active' : ''}`}
|
|
|
|
onClick={() => setActiveTab(id)}
|
|
|
|
>
|
|
|
|
{t(id)}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
export const BioSettings = ({ title = false, welcome = false }) => {
|
|
|
|
// Context
|
|
|
|
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
|
|
|
|
|
|
|
|
// Hooks
|
2023-03-24 16:33:14 +01:00
|
|
|
const { account, setAccount, token } = useAccount()
|
|
|
|
const backend = useBackend(token)
|
2023-01-28 18:10:29 +01:00
|
|
|
const { t } = useTranslation(ns)
|
2023-02-12 11:20:59 +01:00
|
|
|
const toast = useToast()
|
2023-04-28 21:23:06 +02:00
|
|
|
|
|
|
|
// State
|
2023-03-24 16:33:14 +01:00
|
|
|
const [bio, setBio] = useState(account.bio)
|
2023-01-26 19:31:23 +01:00
|
|
|
const [activeTab, setActiveTab] = useState('edit')
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to save bio
|
2023-01-26 19:31:23 +01:00
|
|
|
const save = async () => {
|
2023-04-28 21:23:06 +02:00
|
|
|
startLoading()
|
2023-02-12 11:20:59 +01:00
|
|
|
const result = await backend.updateAccount({ bio })
|
2023-03-24 16:33:14 +01:00
|
|
|
if (result.success) {
|
|
|
|
setAccount(result.data.account)
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
} else toast.for.backendError()
|
2023-04-28 21:23:06 +02:00
|
|
|
stopLoading()
|
2023-01-26 19:31:23 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Next step in the onboarding
|
2023-01-26 19:31:23 +01:00
|
|
|
const nextHref =
|
2023-03-24 16:33:14 +01:00
|
|
|
welcomeSteps[account.control].length > 5
|
|
|
|
? '/welcome/' + welcomeSteps[account.control][6]
|
2023-01-26 19:31:23 +01:00
|
|
|
: '/docs/guide'
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Shared props for tabs
|
2023-01-26 19:31:23 +01:00
|
|
|
const tabProps = { activeTab, setActiveTab, t }
|
|
|
|
|
|
|
|
return (
|
2023-03-27 19:07:48 +02:00
|
|
|
<div className="max-w-xl xl:pl-4">
|
2023-02-12 18:35:54 +01:00
|
|
|
{title ? <h1 className="text-4xl">{t('bioTitle')}</h1> : null}
|
2023-01-26 19:31:23 +01:00
|
|
|
<div className="tabs w-full">
|
|
|
|
<Tab id="edit" {...tabProps} />
|
|
|
|
<Tab id="preview" {...tabProps} />
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-row items-center mt-4">
|
|
|
|
{activeTab === 'edit' ? (
|
|
|
|
<textarea
|
|
|
|
rows="5"
|
|
|
|
className="textarea textarea-bordered textarea-lg w-full"
|
|
|
|
placeholder={t('placeholder')}
|
|
|
|
onChange={(evt) => setBio(evt.target.value)}
|
2023-01-27 21:09:44 +01:00
|
|
|
value={bio}
|
|
|
|
/>
|
2023-01-26 19:31:23 +01:00
|
|
|
) : (
|
|
|
|
<div className="text-left px-4 border w-full">
|
|
|
|
<Markdown>{bio}</Markdown>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-04-28 21:23:06 +02:00
|
|
|
<SaveSettingsButton btnProps={{ onClick: save }} welcome={welcome} />
|
|
|
|
{!welcome && <BackToAccountButton loading={loading} />}
|
2023-01-26 19:31:23 +01:00
|
|
|
<Popout tip compact>
|
|
|
|
{t('mdSupport')}
|
|
|
|
</Popout>
|
|
|
|
|
|
|
|
{welcome ? (
|
|
|
|
<>
|
2023-04-28 21:23:06 +02:00
|
|
|
<ContinueButton btnProps={{ href: nextHref }} link />
|
2023-03-24 16:33:14 +01:00
|
|
|
{welcomeSteps[account.control].length > 0 ? (
|
2023-01-26 19:31:23 +01:00
|
|
|
<>
|
|
|
|
<progress
|
|
|
|
className="progress progress-primary w-full mt-12"
|
2023-03-24 16:33:14 +01:00
|
|
|
value={600 / welcomeSteps[account.control].length}
|
2023-01-26 19:31:23 +01:00
|
|
|
max="100"
|
|
|
|
></progress>
|
|
|
|
<span className="pt-4 text-sm font-bold opacity-50">
|
2023-03-24 16:33:14 +01:00
|
|
|
6 / {welcomeSteps[account.control].length}
|
2023-01-26 19:31:23 +01:00
|
|
|
</span>
|
|
|
|
<Icons
|
2023-03-24 16:33:14 +01:00
|
|
|
done={welcomeSteps[account.control].slice(0, 5)}
|
|
|
|
todo={welcomeSteps[account.control].slice(6)}
|
2023-01-26 19:31:23 +01:00
|
|
|
current="bio"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
) : null}
|
2023-03-27 19:07:48 +02:00
|
|
|
</div>
|
2023-01-26 19:31:23 +01:00
|
|
|
)
|
|
|
|
}
|