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.

index.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { Fragment, useState } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { useContextSelector } from 'use-context-selector'
  6. import {
  7. RiAccountCircleLine,
  8. RiArrowRightUpLine,
  9. RiBookOpenLine,
  10. RiGithubLine,
  11. RiGraduationCapFill,
  12. RiInformation2Line,
  13. RiLogoutBoxRLine,
  14. RiMap2Line,
  15. RiSettings3Line,
  16. RiStarLine,
  17. } from '@remixicon/react'
  18. import Link from 'next/link'
  19. import { Menu, MenuButton, MenuItem, MenuItems, Transition } from '@headlessui/react'
  20. import Indicator from '../indicator'
  21. import AccountAbout from '../account-about'
  22. import GithubStar from '../github-star'
  23. import Support from './support'
  24. import Compliance from './compliance'
  25. import PremiumBadge from '@/app/components/base/premium-badge'
  26. import { useGetDocLanguage } from '@/context/i18n'
  27. import Avatar from '@/app/components/base/avatar'
  28. import { logout } from '@/service/common'
  29. import AppContext, { useAppContext } from '@/context/app-context'
  30. import { useProviderContext } from '@/context/provider-context'
  31. import { useModalContext } from '@/context/modal-context'
  32. import { LicenseStatus } from '@/types/feature'
  33. import { IS_CLOUD_EDITION } from '@/config'
  34. import cn from '@/utils/classnames'
  35. export default function AppSelector() {
  36. const itemClassName = `
  37. flex items-center w-full h-9 pl-3 pr-2 text-text-secondary system-md-regular
  38. rounded-lg hover:bg-state-base-hover cursor-pointer gap-1
  39. `
  40. const router = useRouter()
  41. const [aboutVisible, setAboutVisible] = useState(false)
  42. const systemFeatures = useContextSelector(AppContext, v => v.systemFeatures)
  43. const { t } = useTranslation()
  44. const { userProfile, langeniusVersionInfo, isCurrentWorkspaceOwner } = useAppContext()
  45. const { isEducationAccount } = useProviderContext()
  46. const { setShowAccountSettingModal } = useModalContext()
  47. const docLanguage = useGetDocLanguage()
  48. const handleLogout = async () => {
  49. await logout({
  50. url: '/logout',
  51. params: {},
  52. })
  53. localStorage.removeItem('setup_status')
  54. localStorage.removeItem('console_token')
  55. localStorage.removeItem('refresh_token')
  56. router.push('/signin')
  57. }
  58. return (
  59. <div className="">
  60. <Menu as="div" className="relative inline-block text-left">
  61. {
  62. ({ open }) => (
  63. <>
  64. <MenuButton className={cn('inline-flex items-center rounded-[20px] p-0.5 hover:bg-background-default-dodge', open && 'bg-background-default-dodge')}>
  65. <Avatar avatar={userProfile.avatar_url} name={userProfile.name} size={36} />
  66. </MenuButton>
  67. <Transition
  68. as={Fragment}
  69. enter="transition ease-out duration-100"
  70. enterFrom="transform opacity-0 scale-95"
  71. enterTo="transform opacity-100 scale-100"
  72. leave="transition ease-in duration-75"
  73. leaveFrom="transform opacity-100 scale-100"
  74. leaveTo="transform opacity-0 scale-95"
  75. >
  76. <MenuItems
  77. className="
  78. absolute right-0 mt-1.5 w-60 max-w-80
  79. origin-top-right divide-y divide-divider-subtle rounded-xl bg-components-panel-bg-blur
  80. shadow-lg focus:outline-none
  81. "
  82. >
  83. <MenuItem disabled>
  84. <div className='flex flex-nowrap items-center py-[13px] pl-3 pr-2'>
  85. <div className='grow'>
  86. <div className='system-md-medium break-all text-text-primary'>
  87. {userProfile.name}
  88. {isEducationAccount && (
  89. <PremiumBadge size='s' color='blue' className='ml-1 !px-2'>
  90. <RiGraduationCapFill className='mr-1 h-3 w-3' />
  91. <span className='system-2xs-medium'>EDU</span>
  92. </PremiumBadge>
  93. )}
  94. </div>
  95. <div className='system-xs-regular break-all text-text-tertiary'>{userProfile.email}</div>
  96. </div>
  97. <Avatar avatar={userProfile.avatar_url} name={userProfile.name} size={36} className='mr-3' />
  98. </div>
  99. </MenuItem>
  100. <div className="px-1 py-1">
  101. <MenuItem>
  102. <Link
  103. className={cn(itemClassName, 'group',
  104. 'data-[active]:bg-state-base-hover',
  105. )}
  106. href='/account'
  107. target='_self' rel='noopener noreferrer'>
  108. <RiAccountCircleLine className='size-4 shrink-0 text-text-tertiary' />
  109. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.account.account')}</div>
  110. <RiArrowRightUpLine className='size-[14px] shrink-0 text-text-tertiary' />
  111. </Link>
  112. </MenuItem>
  113. <MenuItem>
  114. <div className={cn(itemClassName,
  115. 'data-[active]:bg-state-base-hover',
  116. )} onClick={() => setShowAccountSettingModal({ payload: 'members' })}>
  117. <RiSettings3Line className='size-4 shrink-0 text-text-tertiary' />
  118. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.userProfile.settings')}</div>
  119. </div>
  120. </MenuItem>
  121. </div>
  122. <div className='p-1'>
  123. <MenuItem>
  124. <Link
  125. className={cn(itemClassName, 'group justify-between',
  126. 'data-[active]:bg-state-base-hover',
  127. )}
  128. href={`https://docs.dify.ai/${docLanguage}/introduction`}
  129. target='_blank' rel='noopener noreferrer'>
  130. <RiBookOpenLine className='size-4 shrink-0 text-text-tertiary' />
  131. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.userProfile.helpCenter')}</div>
  132. <RiArrowRightUpLine className='size-[14px] shrink-0 text-text-tertiary' />
  133. </Link>
  134. </MenuItem>
  135. <Support />
  136. {IS_CLOUD_EDITION && isCurrentWorkspaceOwner && <Compliance />}
  137. </div>
  138. <div className='p-1'>
  139. <MenuItem>
  140. <Link
  141. className={cn(itemClassName, 'group justify-between',
  142. 'data-[active]:bg-state-base-hover',
  143. )}
  144. href='https://roadmap.dify.ai'
  145. target='_blank' rel='noopener noreferrer'>
  146. <RiMap2Line className='size-4 shrink-0 text-text-tertiary' />
  147. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.userProfile.roadmap')}</div>
  148. <RiArrowRightUpLine className='size-[14px] shrink-0 text-text-tertiary' />
  149. </Link>
  150. </MenuItem>
  151. {systemFeatures.license.status === LicenseStatus.NONE && <MenuItem>
  152. <Link
  153. className={cn(itemClassName, 'group justify-between',
  154. 'data-[active]:bg-state-base-hover',
  155. )}
  156. href='https://github.com/langgenius/dify'
  157. target='_blank' rel='noopener noreferrer'>
  158. <RiGithubLine className='size-4 shrink-0 text-text-tertiary' />
  159. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.userProfile.github')}</div>
  160. <div className='flex items-center gap-0.5 rounded-[5px] border border-divider-deep bg-components-badge-bg-dimm px-[5px] py-[3px]'>
  161. <RiStarLine className='size-3 shrink-0 text-text-tertiary' />
  162. <GithubStar className='system-2xs-medium-uppercase text-text-tertiary' />
  163. </div>
  164. </Link>
  165. </MenuItem>}
  166. {
  167. document?.body?.getAttribute('data-public-site-about') !== 'hide' && (
  168. <MenuItem>
  169. <div className={cn(itemClassName, 'justify-between',
  170. 'data-[active]:bg-state-base-hover',
  171. )} onClick={() => setAboutVisible(true)}>
  172. <RiInformation2Line className='size-4 shrink-0 text-text-tertiary' />
  173. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.userProfile.about')}</div>
  174. <div className='flex shrink-0 items-center'>
  175. <div className='system-xs-regular mr-2 text-text-tertiary'>{langeniusVersionInfo.current_version}</div>
  176. <Indicator color={langeniusVersionInfo.current_version === langeniusVersionInfo.latest_version ? 'green' : 'orange'} />
  177. </div>
  178. </div>
  179. </MenuItem>
  180. )
  181. }
  182. </div>
  183. <MenuItem>
  184. <div className='p-1' onClick={() => handleLogout()}>
  185. <div
  186. className={cn(itemClassName, 'group justify-between',
  187. 'data-[active]:bg-state-base-hover',
  188. )}
  189. >
  190. <RiLogoutBoxRLine className='size-4 shrink-0 text-text-tertiary' />
  191. <div className='system-md-regular grow px-1 text-text-secondary'>{t('common.userProfile.logout')}</div>
  192. </div>
  193. </div>
  194. </MenuItem>
  195. </MenuItems>
  196. </Transition>
  197. </>
  198. )
  199. }
  200. </Menu>
  201. {
  202. aboutVisible && <AccountAbout onCancel={() => setAboutVisible(false)} langeniusVersionInfo={langeniusVersionInfo} />
  203. }
  204. </div >
  205. )
  206. }