activateForm.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 'use client'
  2. import { useState } from 'react'
  3. import { useContext } from 'use-context-selector'
  4. import { useTranslation } from 'react-i18next'
  5. import useSWR from 'swr'
  6. import { useSearchParams } from 'next/navigation'
  7. import cn from 'classnames'
  8. import Link from 'next/link'
  9. import { CheckCircleIcon } from '@heroicons/react/24/solid'
  10. import style from './style.module.css'
  11. import Button from '@/app/components/base/button'
  12. import { SimpleSelect } from '@/app/components/base/select'
  13. import { timezones } from '@/utils/timezone'
  14. import { languageMaps, languages } from '@/utils/language'
  15. import { activateMember, invitationCheck } from '@/service/common'
  16. import Toast from '@/app/components/base/toast'
  17. import Loading from '@/app/components/base/loading'
  18. import I18n from '@/context/i18n'
  19. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  20. const ActivateForm = () => {
  21. const { t } = useTranslation()
  22. const { locale } = useContext(I18n)
  23. const searchParams = useSearchParams()
  24. const workspaceID = searchParams.get('workspace_id')
  25. const email = searchParams.get('email')
  26. const token = searchParams.get('token')
  27. const checkParams = {
  28. url: '/activate/check',
  29. params: {
  30. workspace_id: workspaceID,
  31. email,
  32. token,
  33. },
  34. }
  35. const { data: checkRes, mutate: recheck } = useSWR(checkParams, invitationCheck, {
  36. revalidateOnFocus: false,
  37. })
  38. const [name, setName] = useState('')
  39. const [password, setPassword] = useState('')
  40. const [timezone, setTimezone] = useState('Asia/Shanghai')
  41. const [language, setLanguage] = useState('en-US')
  42. const [showSuccess, setShowSuccess] = useState(false)
  43. const showErrorMessage = (message: string) => {
  44. Toast.notify({
  45. type: 'error',
  46. message,
  47. })
  48. }
  49. const valid = () => {
  50. if (!name.trim()) {
  51. showErrorMessage(t('login.error.nameEmpty'))
  52. return false
  53. }
  54. if (!password.trim()) {
  55. showErrorMessage(t('login.error.passwordEmpty'))
  56. return false
  57. }
  58. if (!validPassword.test(password))
  59. showErrorMessage(t('login.error.passwordInvalid'))
  60. return true
  61. }
  62. const handleActivate = async () => {
  63. if (!valid())
  64. return
  65. try {
  66. await activateMember({
  67. url: '/activate',
  68. body: {
  69. workspace_id: workspaceID,
  70. email,
  71. token,
  72. name,
  73. password,
  74. interface_language: language,
  75. timezone,
  76. },
  77. })
  78. setShowSuccess(true)
  79. }
  80. catch {
  81. recheck()
  82. }
  83. }
  84. return (
  85. <div className={
  86. cn(
  87. 'flex flex-col items-center w-full grow items-center justify-center',
  88. 'px-6',
  89. 'md:px-[108px]',
  90. )
  91. }>
  92. {!checkRes && <Loading/>}
  93. {checkRes && !checkRes.is_valid && (
  94. <div className="flex flex-col md:w-[400px]">
  95. <div className="w-full mx-auto">
  96. <div className="mb-3 flex justify-center items-center w-20 h-20 p-5 rounded-[20px] border border-gray-100 shadow-lg text-[40px] font-bold">🤷‍♂️</div>
  97. <h2 className="text-[32px] font-bold text-gray-900">{t('login.invalid')}</h2>
  98. </div>
  99. <div className="w-full mx-auto mt-6">
  100. <Button type='primary' className='w-full !fone-medium !text-sm'>
  101. <a href="https://dify.ai">{t('login.explore')}</a>
  102. </Button>
  103. </div>
  104. </div>
  105. )}
  106. {checkRes && checkRes.is_valid && !showSuccess && (
  107. <div className='flex flex-col md:w-[400px]'>
  108. <div className="w-full mx-auto">
  109. <div className={`mb-3 flex justify-center items-center w-20 h-20 p-5 rounded-[20px] border border-gray-100 shadow-lg text-[40px] font-bold ${style.logo}`}>
  110. </div>
  111. <h2 className="text-[32px] font-bold text-gray-900">
  112. {`${t('login.join')} ${checkRes.workspace_name}`}
  113. </h2>
  114. <p className='mt-1 text-sm text-gray-600 '>
  115. {`${t('login.joinTipStart')} ${checkRes.workspace_name} ${t('login.joinTipEnd')}`}
  116. </p>
  117. </div>
  118. <div className="w-full mx-auto mt-6">
  119. <div className="bg-white">
  120. {/* username */}
  121. <div className='mb-5'>
  122. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  123. {t('login.name')}
  124. </label>
  125. <div className="mt-1 relative rounded-md shadow-sm">
  126. <input
  127. id="name"
  128. type="text"
  129. value={name}
  130. onChange={e => setName(e.target.value)}
  131. placeholder={t('login.namePlaceholder') || ''}
  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>
  135. </div>
  136. {/* password */}
  137. <div className='mb-5'>
  138. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  139. {t('login.password')}
  140. </label>
  141. <div className="mt-1 relative rounded-md shadow-sm">
  142. <input
  143. id="password"
  144. type='password'
  145. value={password}
  146. onChange={e => setPassword(e.target.value)}
  147. placeholder={t('login.passwordPlaceholder') || ''}
  148. 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'}
  149. />
  150. </div>
  151. <div className='mt-1 text-xs text-gray-500'>{t('login.error.passwordInvalid')}</div>
  152. </div>
  153. {/* language */}
  154. <div className='mb-5'>
  155. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  156. {t('login.interfaceLanguage')}
  157. </label>
  158. <div className="relative mt-1 rounded-md shadow-sm">
  159. <SimpleSelect
  160. defaultValue={languageMaps.en}
  161. items={languages}
  162. onSelect={(item) => {
  163. setLanguage(item.value as string)
  164. }}
  165. />
  166. </div>
  167. </div>
  168. {/* timezone */}
  169. <div className='mb-4'>
  170. <label htmlFor="timezone" className="block text-sm font-medium text-gray-700">
  171. {t('login.timezone')}
  172. </label>
  173. <div className="relative mt-1 rounded-md shadow-sm">
  174. <SimpleSelect
  175. defaultValue={timezone}
  176. items={timezones}
  177. onSelect={(item) => {
  178. setTimezone(item.value as string)
  179. }}
  180. />
  181. </div>
  182. </div>
  183. <div>
  184. <Button
  185. type='primary'
  186. className='w-full !fone-medium !text-sm'
  187. onClick={handleActivate}
  188. >
  189. {`${t('login.join')} ${checkRes.workspace_name}`}
  190. </Button>
  191. </div>
  192. <div className="block w-hull mt-2 text-xs text-gray-600">
  193. {t('login.license.tip')}
  194. &nbsp;
  195. <Link
  196. className='text-primary-600'
  197. target={'_blank'}
  198. href={`https://docs.dify.ai/${locale === 'en' ? '' : `v/${locale.toLowerCase()}`}/community/open-source`}
  199. >{t('login.license.link')}</Link>
  200. </div>
  201. </div>
  202. </div>
  203. </div>
  204. )}
  205. {checkRes && checkRes.is_valid && showSuccess && (
  206. <div className="flex flex-col md:w-[400px]">
  207. <div className="w-full mx-auto">
  208. <div className="mb-3 flex justify-center items-center w-20 h-20 p-5 rounded-[20px] border border-gray-100 shadow-lg text-[40px] font-bold">
  209. <CheckCircleIcon className='w-10 h-10 text-[#039855]' />
  210. </div>
  211. <h2 className="text-[32px] font-bold text-gray-900">
  212. {`${t('login.activatedTipStart')} ${checkRes.workspace_name} ${t('login.activatedTipEnd')}`}
  213. </h2>
  214. </div>
  215. <div className="w-full mx-auto mt-6">
  216. <Button type='primary' className='w-full !fone-medium !text-sm'>
  217. <a href="/signin">{t('login.activated')}</a>
  218. </Button>
  219. </div>
  220. </div>
  221. )}
  222. </div>
  223. )
  224. }
  225. export default ActivateForm