index.tsx 11 KB

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