activateForm.tsx 9.4 KB

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