Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

installForm.tsx 7.5KB

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