import React, { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Link from 'next/link' import { useRouter, useSearchParams } from 'next/navigation' import { RiDoorLockLine } from '@remixicon/react' import Loading from '../components/base/loading' import MailAndCodeAuth from './components/mail-and-code-auth' import MailAndPasswordAuth from './components/mail-and-password-auth' import SocialAuth from './components/social-auth' import SSOAuth from './components/sso-auth' import cn from '@/utils/classnames' import { getSystemFeatures, invitationCheck } from '@/service/common' import { defaultSystemFeatures } from '@/types/feature' import Toast from '@/app/components/base/toast' import { IS_CE_EDITION } from '@/config' const NormalForm = () => { const { t } = useTranslation() const router = useRouter() const searchParams = useSearchParams() const consoleToken = decodeURIComponent(searchParams.get('access_token') || '') const refreshToken = decodeURIComponent(searchParams.get('refresh_token') || '') const message = decodeURIComponent(searchParams.get('message') || '') const invite_token = decodeURIComponent(searchParams.get('invite_token') || '') const [isLoading, setIsLoading] = useState(true) const [systemFeatures, setSystemFeatures] = useState(defaultSystemFeatures) const [authType, updateAuthType] = useState<'code' | 'password'>('password') const [showORLine, setShowORLine] = useState(false) const [allMethodsAreDisabled, setAllMethodsAreDisabled] = useState(false) const [workspaceName, setWorkSpaceName] = useState('') const isInviteLink = Boolean(invite_token && invite_token !== 'null') const init = useCallback(async () => { try { if (consoleToken && refreshToken) { localStorage.setItem('console_token', consoleToken) localStorage.setItem('refresh_token', refreshToken) router.replace('/apps') return } if (message) { Toast.notify({ type: 'error', message, }) } const features = await getSystemFeatures() const allFeatures = { ...defaultSystemFeatures, ...features } setSystemFeatures(allFeatures) setAllMethodsAreDisabled(!allFeatures.enable_social_oauth_login && !allFeatures.enable_email_code_login && !allFeatures.enable_email_password_login && !allFeatures.sso_enforced_for_signin) setShowORLine((allFeatures.enable_social_oauth_login || allFeatures.sso_enforced_for_signin) && (allFeatures.enable_email_code_login || allFeatures.enable_email_password_login)) updateAuthType(allFeatures.enable_email_password_login ? 'password' : 'code') if (isInviteLink) { const checkRes = await invitationCheck({ url: '/activate/check', params: { token: invite_token, }, }) setWorkSpaceName(checkRes?.data?.workspace_name || '') } } catch (error) { console.error(error) setAllMethodsAreDisabled(true) setSystemFeatures(defaultSystemFeatures) } finally { setIsLoading(false) } }, [consoleToken, refreshToken, message, router, invite_token, isInviteLink]) useEffect(() => { init() }, [init]) if (isLoading || consoleToken) { return
} return ( <>
{isInviteLink ?

{t('login.join')}{workspaceName}

{t('login.joinTipStart')}{workspaceName}{t('login.joinTipEnd')}

:

{t('login.pageTitle')}

{t('login.welcome')}

}
{systemFeatures.enable_social_oauth_login && } {systemFeatures.sso_enforced_for_signin &&
}
{showORLine &&
{t('login.or')}
} { (systemFeatures.enable_email_code_login || systemFeatures.enable_email_password_login) && <> {systemFeatures.enable_email_code_login && authType === 'code' && <> {systemFeatures.enable_email_password_login &&
{ updateAuthType('password') }}> {t('login.usePassword')}
} } {systemFeatures.enable_email_password_login && authType === 'password' && <> {systemFeatures.enable_email_code_login &&
{ updateAuthType('code') }}> {t('login.useVerificationCode')}
} } } {allMethodsAreDisabled && <>

{t('login.noLoginMethod')}

{t('login.noLoginMethodTip')}

}
{t('login.tosDesc')}   {t('login.tos')}  &  {t('login.pp')}
{IS_CE_EDITION &&
{t('login.goToInit')}   {t('login.setAdminAccount')}
}
) } export default NormalForm