activateForm.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. 'use client'
  2. import { useCallback, 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 Link from 'next/link'
  8. import { CheckCircleIcon } from '@heroicons/react/24/solid'
  9. import style from './style.module.css'
  10. import cn from '@/utils/classnames'
  11. import Button from '@/app/components/base/button'
  12. import { SimpleSelect } from '@/app/components/base/select'
  13. import { timezones } from '@/utils/timezone'
  14. import { LanguagesSupported, languages } from '@/i18n/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, setLocaleOnClient } = 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. ...workspaceID && { workspace_id: workspaceID },
  31. ...email && { 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(Intl.DateTimeFormat().resolvedOptions().timeZone)
  41. const [language, setLanguage] = useState(locale)
  42. const [showSuccess, setShowSuccess] = useState(false)
  43. const showErrorMessage = useCallback((message: string) => {
  44. Toast.notify({
  45. type: 'error',
  46. message,
  47. })
  48. }, [])
  49. const valid = useCallback(() => {
  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 false
  61. }
  62. return true
  63. }, [name, password, showErrorMessage, t])
  64. const handleActivate = useCallback(async () => {
  65. if (!valid())
  66. return
  67. try {
  68. await activateMember({
  69. url: '/activate',
  70. body: {
  71. workspace_id: workspaceID,
  72. email,
  73. token,
  74. name,
  75. password,
  76. interface_language: language,
  77. timezone,
  78. },
  79. })
  80. setLocaleOnClient(language, false)
  81. setShowSuccess(true)
  82. }
  83. catch {
  84. recheck()
  85. }
  86. }, [email, language, name, password, recheck, setLocaleOnClient, timezone, token, valid, workspaceID])
  87. return (
  88. <div className={
  89. cn(
  90. 'flex flex-col items-center w-full grow justify-center',
  91. 'px-6',
  92. 'md:px-[108px]',
  93. )
  94. }>
  95. {!checkRes && <Loading />}
  96. {checkRes && !checkRes.is_valid && (
  97. <div className="flex flex-col md:w-[400px]">
  98. <div className="w-full mx-auto">
  99. <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>
  100. <h2 className="text-[32px] font-bold text-gray-900">{t('login.invalid')}</h2>
  101. </div>
  102. <div className="w-full mx-auto mt-6">
  103. <Button variant='primary' className='w-full !text-sm'>
  104. <a href="https://dify.ai">{t('login.explore')}</a>
  105. </Button>
  106. </div>
  107. </div>
  108. )}
  109. {checkRes && checkRes.is_valid && !showSuccess && (
  110. <div className='flex flex-col md:w-[400px]'>
  111. <div className="w-full mx-auto">
  112. <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}`}>
  113. </div>
  114. <h2 className="text-[32px] font-bold text-gray-900">
  115. {`${t('login.join')} ${checkRes.workspace_name}`}
  116. </h2>
  117. <p className='mt-1 text-sm text-gray-600 '>
  118. {`${t('login.joinTipStart')} ${checkRes.workspace_name} ${t('login.joinTipEnd')}`}
  119. </p>
  120. </div>
  121. <div className="w-full mx-auto mt-6">
  122. <div className="bg-white">
  123. {/* username */}
  124. <div className='mb-5'>
  125. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  126. {t('login.name')}
  127. </label>
  128. <div className="mt-1 relative rounded-md shadow-sm">
  129. <input
  130. id="name"
  131. type="text"
  132. value={name}
  133. onChange={e => setName(e.target.value)}
  134. placeholder={t('login.namePlaceholder') || ''}
  135. 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'}
  136. />
  137. </div>
  138. </div>
  139. {/* password */}
  140. <div className='mb-5'>
  141. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  142. {t('login.password')}
  143. </label>
  144. <div className="mt-1 relative rounded-md shadow-sm">
  145. <input
  146. id="password"
  147. type='password'
  148. value={password}
  149. onChange={e => setPassword(e.target.value)}
  150. placeholder={t('login.passwordPlaceholder') || ''}
  151. 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'}
  152. />
  153. </div>
  154. <div className='mt-1 text-xs text-gray-500'>{t('login.error.passwordInvalid')}</div>
  155. </div>
  156. {/* language */}
  157. <div className='mb-5'>
  158. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  159. {t('login.interfaceLanguage')}
  160. </label>
  161. <div className="relative mt-1 rounded-md shadow-sm">
  162. <SimpleSelect
  163. defaultValue={LanguagesSupported[0]}
  164. items={languages.filter(item => item.supported)}
  165. onSelect={(item) => {
  166. setLanguage(item.value as string)
  167. }}
  168. />
  169. </div>
  170. </div>
  171. {/* timezone */}
  172. <div className='mb-4'>
  173. <label htmlFor="timezone" className="block text-sm font-medium text-gray-700">
  174. {t('login.timezone')}
  175. </label>
  176. <div className="relative mt-1 rounded-md shadow-sm">
  177. <SimpleSelect
  178. defaultValue={timezone}
  179. items={timezones}
  180. onSelect={(item) => {
  181. setTimezone(item.value as string)
  182. }}
  183. />
  184. </div>
  185. </div>
  186. <div>
  187. <Button
  188. variant='primary'
  189. className='w-full !text-sm'
  190. onClick={handleActivate}
  191. >
  192. {`${t('login.join')} ${checkRes.workspace_name}`}
  193. </Button>
  194. </div>
  195. <div className="block w-hull mt-2 text-xs text-gray-600">
  196. {t('login.license.tip')}
  197. &nbsp;
  198. <Link
  199. className='text-primary-600'
  200. target='_blank' rel='noopener noreferrer'
  201. href={`https://docs.dify.ai/${language !== LanguagesSupported[1] ? 'user-agreement' : `v/${locale.toLowerCase()}/policies`}/open-source`}
  202. >{t('login.license.link')}</Link>
  203. </div>
  204. </div>
  205. </div>
  206. </div>
  207. )}
  208. {checkRes && checkRes.is_valid && showSuccess && (
  209. <div className="flex flex-col md:w-[400px]">
  210. <div className="w-full mx-auto">
  211. <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">
  212. <CheckCircleIcon className='w-10 h-10 text-[#039855]' />
  213. </div>
  214. <h2 className="text-[32px] font-bold text-gray-900">
  215. {`${t('login.activatedTipStart')} ${checkRes.workspace_name} ${t('login.activatedTipEnd')}`}
  216. </h2>
  217. </div>
  218. <div className="w-full mx-auto mt-6">
  219. <Button variant='primary' className='w-full !text-sm'>
  220. <a href="/signin">{t('login.activated')}</a>
  221. </Button>
  222. </div>
  223. </div>
  224. )}
  225. </div>
  226. )
  227. }
  228. export default ActivateForm