installForm.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use client'
  2. import React, { useEffect } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Link from 'next/link'
  5. import { useRouter } from 'next/navigation'
  6. // import { useContext } from 'use-context-selector'
  7. import Toast from '../components/base/toast'
  8. import Loading from '../components/base/loading'
  9. import Button from '@/app/components/base/button'
  10. // import I18n from '@/context/i18n'
  11. import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common'
  12. import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common'
  13. const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/
  14. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  15. const InstallForm = () => {
  16. const { t } = useTranslation()
  17. // const { locale } = useContext(I18n)
  18. // const language = getModelRuntimeSupported(locale)
  19. const router = useRouter()
  20. const [email, setEmail] = React.useState('')
  21. const [name, setName] = React.useState('')
  22. const [password, setPassword] = React.useState('')
  23. const [showPassword, setShowPassword] = React.useState(false)
  24. const [loading, setLoading] = React.useState(true)
  25. const showErrorMessage = (message: string) => {
  26. Toast.notify({
  27. type: 'error',
  28. message,
  29. })
  30. }
  31. const valid = () => {
  32. if (!email) {
  33. showErrorMessage(t('login.error.emailEmpty'))
  34. return false
  35. }
  36. if (!validEmailReg.test(email)) {
  37. showErrorMessage(t('login.error.emailInValid'))
  38. return false
  39. }
  40. if (!name.trim()) {
  41. showErrorMessage(t('login.error.nameEmpty'))
  42. return false
  43. }
  44. if (!password.trim()) {
  45. showErrorMessage(t('login.error.passwordEmpty'))
  46. return false
  47. }
  48. if (!validPassword.test(password))
  49. showErrorMessage(t('login.error.passwordInvalid'))
  50. return true
  51. }
  52. const handleSetting = async () => {
  53. if (!valid())
  54. return
  55. await setup({
  56. body: {
  57. email,
  58. name,
  59. password,
  60. },
  61. })
  62. router.push('/signin')
  63. }
  64. useEffect(() => {
  65. fetchSetupStatus().then((res: SetupStatusResponse) => {
  66. if (res.step === 'finished') {
  67. window.location.href = '/signin'
  68. }
  69. else {
  70. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  71. if (res.status === 'not_started')
  72. window.location.href = '/init'
  73. })
  74. }
  75. setLoading(false)
  76. })
  77. }, [])
  78. return (
  79. loading
  80. ? <Loading />
  81. : <>
  82. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  83. <h2 className="text-[32px] font-bold text-gray-900">{t('login.setAdminAccount')}</h2>
  84. <p className='
  85. mt-1 text-sm text-gray-600
  86. '>{t('login.setAdminAccountDesc')}</p>
  87. </div>
  88. <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md">
  89. <div className="bg-white ">
  90. <form onSubmit={() => { }}>
  91. <div className='mb-5'>
  92. <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  93. {t('login.email')}
  94. </label>
  95. <div className="mt-1">
  96. <input
  97. id="email"
  98. type="email"
  99. value={email}
  100. onChange={e => setEmail(e.target.value)}
  101. placeholder={t('login.emailPlaceholder') || ''}
  102. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm'}
  103. />
  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-gray-900">
  108. {t('login.name')}
  109. </label>
  110. <div className="mt-1 relative rounded-md shadow-sm">
  111. <input
  112. id="name"
  113. type="text"
  114. value={name}
  115. onChange={e => setName(e.target.value)}
  116. placeholder={t('login.namePlaceholder') || ''}
  117. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  118. />
  119. </div>
  120. </div>
  121. <div className='mb-5'>
  122. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  123. {t('login.password')}
  124. </label>
  125. <div className="mt-1 relative rounded-md shadow-sm">
  126. <input
  127. id="password"
  128. type={showPassword ? 'text' : 'password'}
  129. value={password}
  130. onChange={e => setPassword(e.target.value)}
  131. placeholder={t('login.passwordPlaceholder') || ''}
  132. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  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-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  139. >
  140. {showPassword ? '👀' : '😝'}
  141. </button>
  142. </div>
  143. </div>
  144. <div className='mt-1 text-xs text-gray-500'>{t('login.error.passwordInvalid')}</div>
  145. </div>
  146. {/* <div className="flex items-center justify-between">
  147. <div className="text-sm">
  148. <div className="flex items-center mb-4">
  149. <input id="default-checkbox" type="checkbox" className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 rounded" />
  150. <label htmlFor="default-checkbox" className="ml-2 text-sm font-medium cursor-pointer text-primary-600 hover:text-gray-500">{t('login.acceptPP')}</label>
  151. </div>
  152. </div>
  153. </div> */}
  154. <div>
  155. <Button type='primary' className='w-full !fone-medium !text-sm' onClick={handleSetting}>
  156. {t('login.installBtn')}
  157. </Button>
  158. </div>
  159. </form>
  160. <div className="block w-hull mt-2 text-xs text-gray-600">
  161. {t('login.license.tip')}
  162. &nbsp;
  163. <Link
  164. className='text-primary-600'
  165. target='_blank' rel='noopener noreferrer'
  166. href={'https://docs.dify.ai/user-agreement/open-source'}
  167. >{t('login.license.link')}</Link>
  168. </div>
  169. </div>
  170. </div>
  171. </>
  172. )
  173. }
  174. export default InstallForm