You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

installForm.tsx 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use client'
  2. import React, { useCallback, useEffect } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useDebounceFn } from 'ahooks'
  5. import Link from 'next/link'
  6. import { useRouter } from 'next/navigation'
  7. import type { SubmitHandler } from 'react-hook-form'
  8. import { useForm } from 'react-hook-form'
  9. import { z } from 'zod'
  10. import { zodResolver } from '@hookform/resolvers/zod'
  11. import Loading from '../components/base/loading'
  12. import classNames from '@/utils/classnames'
  13. import Button from '@/app/components/base/button'
  14. import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common'
  15. import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common'
  16. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  17. const accountFormSchema = z.object({
  18. email: z
  19. .string()
  20. .min(1, { message: 'login.error.emailInValid' })
  21. .email('login.error.emailInValid'),
  22. name: z.string().min(1, { message: 'login.error.nameEmpty' }),
  23. password: z.string().min(8, {
  24. message: 'login.error.passwordLengthInValid',
  25. }).regex(validPassword, 'login.error.passwordInvalid'),
  26. })
  27. type AccountFormValues = z.infer<typeof accountFormSchema>
  28. const InstallForm = () => {
  29. const { t } = useTranslation()
  30. const router = useRouter()
  31. const [showPassword, setShowPassword] = React.useState(false)
  32. const [loading, setLoading] = React.useState(true)
  33. const {
  34. register,
  35. handleSubmit,
  36. formState: { errors, isSubmitting },
  37. } = useForm<AccountFormValues>({
  38. resolver: zodResolver(accountFormSchema),
  39. defaultValues: {
  40. name: '',
  41. password: '',
  42. email: '',
  43. },
  44. })
  45. const onSubmit: SubmitHandler<AccountFormValues> = async (data) => {
  46. await setup({
  47. body: {
  48. ...data,
  49. },
  50. })
  51. router.push('/signin')
  52. }
  53. const handleSetting = async () => {
  54. if (isSubmitting) return
  55. handleSubmit(onSubmit)()
  56. }
  57. const { run: debouncedHandleKeyDown } = useDebounceFn(
  58. (e: React.KeyboardEvent) => {
  59. if (e.key === 'Enter') {
  60. e.preventDefault()
  61. handleSetting()
  62. }
  63. },
  64. { wait: 200 },
  65. )
  66. const handleKeyDown = useCallback(debouncedHandleKeyDown, [debouncedHandleKeyDown])
  67. useEffect(() => {
  68. fetchSetupStatus().then((res: SetupStatusResponse) => {
  69. if (res.step === 'finished') {
  70. localStorage.setItem('setup_status', 'finished')
  71. router.push('/signin')
  72. }
  73. else {
  74. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  75. if (res.status === 'not_started')
  76. router.push('/init')
  77. })
  78. }
  79. setLoading(false)
  80. })
  81. }, [])
  82. return (
  83. loading
  84. ? <Loading />
  85. : <>
  86. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  87. <h2 className="text-[32px] font-bold text-text-primary">{t('login.setAdminAccount')}</h2>
  88. <p className='mt-1 text-sm text-text-secondary'>{t('login.setAdminAccountDesc')}</p>
  89. </div>
  90. <div className="mt-8 grow sm:mx-auto sm:w-full sm:max-w-md">
  91. <div className="relative">
  92. <form onSubmit={handleSubmit(onSubmit)} onKeyDown={handleKeyDown}>
  93. <div className='mb-5'>
  94. <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-text-primary">
  95. {t('login.email')}
  96. </label>
  97. <div className="mt-1 rounded-md shadow-sm">
  98. <input
  99. {...register('email')}
  100. placeholder={t('login.emailPlaceholder') || ''}
  101. className={'w-full appearance-none rounded-md border border-transparent bg-components-input-bg-normal py-[7px] pl-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs'}
  102. />
  103. {errors.email && <span className='text-sm text-red-400'>{t(`${errors.email?.message}`)}</span>}
  104. </div>
  105. </div>
  106. <div className='mb-5'>
  107. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-text-primary">
  108. {t('login.name')}
  109. </label>
  110. <div className="relative mt-1 rounded-md shadow-sm">
  111. <input
  112. {...register('name')}
  113. placeholder={t('login.namePlaceholder') || ''}
  114. className={'w-full appearance-none rounded-md border border-transparent bg-components-input-bg-normal py-[7px] pl-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs'}
  115. />
  116. </div>
  117. {errors.name && <span className='text-sm text-red-400'>{t(`${errors.name.message}`)}</span>}
  118. </div>
  119. <div className='mb-5'>
  120. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-text-primary">
  121. {t('login.password')}
  122. </label>
  123. <div className="relative mt-1 rounded-md shadow-sm">
  124. <input
  125. {...register('password')}
  126. type={showPassword ? 'text' : 'password'}
  127. placeholder={t('login.passwordPlaceholder') || ''}
  128. className={'w-full appearance-none rounded-md border border-transparent bg-components-input-bg-normal py-[7px] pl-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs'}
  129. />
  130. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  131. <button
  132. type="button"
  133. onClick={() => setShowPassword(!showPassword)}
  134. className="text-text-quaternary hover:text-text-tertiary focus:text-text-tertiary focus:outline-none"
  135. >
  136. {showPassword ? '👀' : '😝'}
  137. </button>
  138. </div>
  139. </div>
  140. <div className={classNames('mt-1 text-xs text-text-tertiary', {
  141. 'text-red-400 !text-sm': errors.password,
  142. })}>{t('login.error.passwordInvalid')}</div>
  143. </div>
  144. <div>
  145. <Button variant='primary' className='w-full' onClick={handleSetting}>
  146. {t('login.installBtn')}
  147. </Button>
  148. </div>
  149. </form>
  150. <div className="mt-2 block w-full text-xs text-text-tertiary">
  151. {t('login.license.tip')}
  152. &nbsp;
  153. <Link
  154. className='text-text-accent'
  155. target='_blank' rel='noopener noreferrer'
  156. href={'https://docs.dify.ai/user-agreement/open-source'}
  157. >{t('login.license.link')}</Link>
  158. </div>
  159. </div>
  160. </div>
  161. </>
  162. )
  163. }
  164. export default InstallForm