index.tsx 10 KB

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