installForm.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 { fetchSetupStatus, setup } from '@/service/common'
  12. import type { 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. else
  69. setLoading(false)
  70. })
  71. }, [])
  72. return (
  73. loading
  74. ? <Loading />
  75. : <>
  76. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  77. <h2 className="text-[32px] font-bold text-gray-900">{t('login.setAdminAccount')}</h2>
  78. <p className='
  79. mt-1 text-sm text-gray-600
  80. '>{t('login.setAdminAccountDesc')}</p>
  81. </div>
  82. <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md">
  83. <div className="bg-white ">
  84. <form onSubmit={() => { }}>
  85. <div className='mb-5'>
  86. <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  87. {t('login.email')}
  88. </label>
  89. <div className="mt-1">
  90. <input
  91. id="email"
  92. type="email"
  93. value={email}
  94. onChange={e => setEmail(e.target.value)}
  95. placeholder={t('login.emailPlaceholder') || ''}
  96. 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'}
  97. />
  98. </div>
  99. </div>
  100. <div className='mb-5'>
  101. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  102. {t('login.name')}
  103. </label>
  104. <div className="mt-1 relative rounded-md shadow-sm">
  105. <input
  106. id="name"
  107. type="text"
  108. value={name}
  109. onChange={e => setName(e.target.value)}
  110. placeholder={t('login.namePlaceholder') || ''}
  111. 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'}
  112. />
  113. </div>
  114. </div>
  115. <div className='mb-5'>
  116. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  117. {t('login.password')}
  118. </label>
  119. <div className="mt-1 relative rounded-md shadow-sm">
  120. <input
  121. id="password"
  122. type={showPassword ? 'text' : 'password'}
  123. value={password}
  124. onChange={e => setPassword(e.target.value)}
  125. placeholder={t('login.passwordPlaceholder') || ''}
  126. 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'}
  127. />
  128. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  129. <button
  130. type="button"
  131. onClick={() => setShowPassword(!showPassword)}
  132. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  133. >
  134. {showPassword ? '👀' : '😝'}
  135. </button>
  136. </div>
  137. </div>
  138. <div className='mt-1 text-xs text-gray-500'>{t('login.error.passwordInvalid')}</div>
  139. </div>
  140. {/* <div className="flex items-center justify-between">
  141. <div className="text-sm">
  142. <div className="flex items-center mb-4">
  143. <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" />
  144. <label htmlFor="default-checkbox" className="ml-2 text-sm font-medium cursor-pointer text-primary-600 hover:text-gray-500">{t('login.acceptPP')}</label>
  145. </div>
  146. </div>
  147. </div> */}
  148. <div>
  149. <Button type='primary' className='w-full !fone-medium !text-sm' onClick={handleSetting}>
  150. {t('login.installBtn')}
  151. </Button>
  152. </div>
  153. </form>
  154. <div className="block w-hull mt-2 text-xs text-gray-600">
  155. {t('login.license.tip')}
  156. &nbsp;
  157. <Link
  158. className='text-primary-600'
  159. target={'_blank'}
  160. href={'https://docs.dify.ai/user-agreement/open-source'}
  161. >{t('login.license.link')}</Link>
  162. </div>
  163. </div>
  164. </div>
  165. </>
  166. )
  167. }
  168. export default InstallForm