You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

page.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { useDocLink } from '@/context/i18n'
  4. import { useCallback, useState } from 'react'
  5. import Link from 'next/link'
  6. import { useContext } from 'use-context-selector'
  7. import { useRouter, useSearchParams } from 'next/navigation'
  8. import useSWR from 'swr'
  9. import { RiAccountCircleLine } from '@remixicon/react'
  10. import Input from '@/app/components/base/input'
  11. import { SimpleSelect } from '@/app/components/base/select'
  12. import Button from '@/app/components/base/button'
  13. import { timezones } from '@/utils/timezone'
  14. import { LanguagesSupported, languages } from '@/i18n-config/language'
  15. import I18n from '@/context/i18n'
  16. import { activateMember, invitationCheck } from '@/service/common'
  17. import Loading from '@/app/components/base/loading'
  18. import Toast from '@/app/components/base/toast'
  19. import { noop } from 'lodash-es'
  20. import { useGlobalPublicStore } from '@/context/global-public-context'
  21. export default function InviteSettingsPage() {
  22. const { t } = useTranslation()
  23. const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
  24. const docLink = useDocLink()
  25. const router = useRouter()
  26. const searchParams = useSearchParams()
  27. const token = decodeURIComponent(searchParams.get('invite_token') as string)
  28. const { setLocaleOnClient } = useContext(I18n)
  29. const [name, setName] = useState('')
  30. const [language, setLanguage] = useState(LanguagesSupported[0])
  31. const [timezone, setTimezone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone || 'America/Los_Angeles')
  32. const checkParams = {
  33. url: '/activate/check',
  34. params: {
  35. token,
  36. },
  37. }
  38. const { data: checkRes, mutate: recheck } = useSWR(checkParams, invitationCheck, {
  39. revalidateOnFocus: false,
  40. })
  41. const handleActivate = useCallback(async () => {
  42. try {
  43. if (!name) {
  44. Toast.notify({ type: 'error', message: t('login.enterYourName') })
  45. return
  46. }
  47. const res = await activateMember({
  48. url: '/activate',
  49. body: {
  50. token,
  51. name,
  52. interface_language: language,
  53. timezone,
  54. },
  55. })
  56. if (res.result === 'success') {
  57. localStorage.setItem('console_token', res.data.access_token)
  58. localStorage.setItem('refresh_token', res.data.refresh_token)
  59. await setLocaleOnClient(language, false)
  60. router.replace('/apps')
  61. }
  62. }
  63. catch {
  64. recheck()
  65. }
  66. }, [language, name, recheck, setLocaleOnClient, timezone, token, router, t])
  67. if (!checkRes)
  68. return <Loading />
  69. if (!checkRes.is_valid) {
  70. return <div className="flex flex-col md:w-[400px]">
  71. <div className="mx-auto w-full">
  72. <div className="mb-3 flex h-14 w-14 items-center justify-center rounded-2xl border border-components-panel-border-subtle text-2xl font-bold shadow-lg">🤷‍♂️</div>
  73. <h2 className="title-4xl-semi-bold">{t('login.invalid')}</h2>
  74. </div>
  75. <div className="mx-auto mt-6 w-full">
  76. <Button variant='primary' className='w-full !text-sm'>
  77. <a href="https://dify.ai">{t('login.explore')}</a>
  78. </Button>
  79. </div>
  80. </div>
  81. }
  82. return <div className='flex flex-col gap-3'>
  83. <div className='inline-flex h-14 w-14 items-center justify-center rounded-2xl border border-components-panel-border-subtle bg-background-default-dodge shadow-lg'>
  84. <RiAccountCircleLine className='h-6 w-6 text-2xl text-text-accent-light-mode-only' />
  85. </div>
  86. <div className='pb-4 pt-2'>
  87. <h2 className='title-4xl-semi-bold'>{t('login.setYourAccount')}</h2>
  88. </div>
  89. <form onSubmit={noop}>
  90. <div className='mb-5'>
  91. <label htmlFor="name" className="system-md-semibold my-2">
  92. {t('login.name')}
  93. </label>
  94. <div className="mt-1">
  95. <Input
  96. id="name"
  97. type="text"
  98. value={name}
  99. onChange={e => setName(e.target.value)}
  100. placeholder={t('login.namePlaceholder') || ''}
  101. onKeyDown={(e) => {
  102. if (e.key === 'Enter') {
  103. e.preventDefault()
  104. e.stopPropagation()
  105. handleActivate()
  106. }
  107. }}
  108. />
  109. </div>
  110. </div>
  111. <div className='mb-5'>
  112. <label htmlFor="name" className="system-md-semibold my-2">
  113. {t('login.interfaceLanguage')}
  114. </label>
  115. <div className="mt-1">
  116. <SimpleSelect
  117. defaultValue={LanguagesSupported[0]}
  118. items={languages.filter(item => item.supported)}
  119. onSelect={(item) => {
  120. setLanguage(item.value as string)
  121. }}
  122. />
  123. </div>
  124. </div>
  125. {/* timezone */}
  126. <div className='mb-5'>
  127. <label htmlFor="timezone" className="system-md-semibold">
  128. {t('login.timezone')}
  129. </label>
  130. <div className="mt-1">
  131. <SimpleSelect
  132. defaultValue={timezone}
  133. items={timezones}
  134. onSelect={(item) => {
  135. setTimezone(item.value as string)
  136. }}
  137. />
  138. </div>
  139. </div>
  140. <div>
  141. <Button
  142. variant='primary'
  143. className='w-full'
  144. onClick={handleActivate}
  145. >
  146. {`${t('login.join')} ${checkRes?.data?.workspace_name}`}
  147. </Button>
  148. </div>
  149. </form>
  150. {!systemFeatures.branding.enabled && <div className="system-xs-regular mt-2 block w-full">
  151. {t('login.license.tip')}
  152. &nbsp;
  153. <Link
  154. className='system-xs-medium text-text-accent-secondary'
  155. target='_blank' rel='noopener noreferrer'
  156. href={docLink('/policies/open-source')}
  157. >{t('login.license.link')}</Link>
  158. </div>}
  159. </div>
  160. }