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.

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