index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { Fragment, useState } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { useContext } from 'use-context-selector'
  6. import classNames from 'classnames'
  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 WorkplaceSelector from './workplace-selector'
  12. import I18n from '@/context/i18n'
  13. import Avatar from '@/app/components/base/avatar'
  14. import { logout } from '@/service/common'
  15. import { useAppContext } from '@/context/app-context'
  16. import { ArrowUpRight, ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows'
  17. import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
  18. import { useModalContext } from '@/context/modal-context'
  19. import { LanguagesSupported } from '@/i18n/language'
  20. export type IAppSelecotr = {
  21. isMobile: boolean
  22. }
  23. export default function AppSelector({ isMobile }: IAppSelecotr) {
  24. const itemClassName = `
  25. flex items-center w-full h-9 px-3 text-gray-700 text-[14px]
  26. rounded-lg font-normal hover:bg-gray-50 cursor-pointer
  27. `
  28. const router = useRouter()
  29. const [aboutVisible, setAboutVisible] = useState(false)
  30. const { locale } = useContext(I18n)
  31. const { t } = useTranslation()
  32. const { userProfile, langeniusVersionInfo } = useAppContext()
  33. const { setShowAccountSettingModal } = useModalContext()
  34. const handleLogout = async () => {
  35. await logout({
  36. url: '/logout',
  37. params: {},
  38. })
  39. if (localStorage?.getItem('console_token'))
  40. localStorage.removeItem('console_token')
  41. router.push('/signin')
  42. }
  43. return (
  44. <div className="">
  45. <Menu as="div" className="relative inline-block text-left">
  46. {
  47. ({ open }) => (
  48. <>
  49. <div>
  50. <Menu.Button
  51. className={`
  52. inline-flex items-center
  53. rounded-[20px] py-1 pr-2.5 pl-1 text-sm
  54. text-gray-700 hover:bg-gray-200
  55. mobile:px-1
  56. ${open && 'bg-gray-200'}
  57. `}
  58. >
  59. <Avatar name={userProfile.name} className='sm:mr-2 mr-0' size={32} />
  60. {!isMobile && <>
  61. {userProfile.name}
  62. <ChevronDown className="w-3 h-3 ml-1 text-gray-700" />
  63. </>}
  64. </Menu.Button>
  65. </div>
  66. <Transition
  67. as={Fragment}
  68. enter="transition ease-out duration-100"
  69. enterFrom="transform opacity-0 scale-95"
  70. enterTo="transform opacity-100 scale-100"
  71. leave="transition ease-in duration-75"
  72. leaveFrom="transform opacity-100 scale-100"
  73. leaveTo="transform opacity-0 scale-95"
  74. >
  75. <Menu.Items
  76. className="
  77. absolute right-0 mt-1.5 w-60 max-w-80
  78. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  79. shadow-lg
  80. "
  81. >
  82. <Menu.Item>
  83. <div className='flex flex-nowrap items-center px-4 py-[13px]'>
  84. <Avatar name={userProfile.name} size={36} className='mr-3' />
  85. <div className='grow'>
  86. <div className='leading-5 font-normal text-[14px] text-gray-800 break-all'>{userProfile.name}</div>
  87. <div className='leading-[18px] text-xs font-normal text-gray-500 break-all'>{userProfile.email}</div>
  88. </div>
  89. </div>
  90. </Menu.Item>
  91. <div className='px-1 py-1'>
  92. <div className='mt-2 px-3 text-xs font-medium text-gray-500'>{t('common.userProfile.workspace')}</div>
  93. <WorkplaceSelector />
  94. </div>
  95. <div className="px-1 py-1">
  96. <Menu.Item>
  97. <div className={itemClassName} onClick={() => setShowAccountSettingModal({ payload: 'account' })}>
  98. <div>{t('common.userProfile.settings')}</div>
  99. </div>
  100. </Menu.Item>
  101. <Menu.Item>
  102. <Link
  103. className={classNames(itemClassName, 'group justify-between')}
  104. href='https://github.com/langgenius/dify/discussions/categories/feedbacks'
  105. target='_blank' rel='noopener noreferrer'>
  106. <div>{t('common.userProfile.roadmapAndFeedback')}</div>
  107. <ArrowUpRight className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  108. </Link>
  109. </Menu.Item>
  110. <Menu.Item>
  111. <Link
  112. className={classNames(itemClassName, 'group justify-between')}
  113. href='https://discord.gg/5AEfbxcd9k'
  114. target='_blank' rel='noopener noreferrer'>
  115. <div>{t('common.userProfile.community')}</div>
  116. <ArrowUpRight className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  117. </Link>
  118. </Menu.Item>
  119. <Menu.Item>
  120. <Link
  121. className={classNames(itemClassName, 'group justify-between')}
  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. <div>{t('common.userProfile.helpCenter')}</div>
  127. <ArrowUpRight className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  128. </Link>
  129. </Menu.Item>
  130. {
  131. document?.body?.getAttribute('data-public-site-about') !== 'hide' && (
  132. <Menu.Item>
  133. <div className={classNames(itemClassName, 'justify-between')} onClick={() => setAboutVisible(true)}>
  134. <div>{t('common.userProfile.about')}</div>
  135. <div className='flex items-center'>
  136. <div className='mr-2 text-xs font-normal text-gray-500'>{langeniusVersionInfo.current_version}</div>
  137. <Indicator color={langeniusVersionInfo.current_version === langeniusVersionInfo.latest_version ? 'green' : 'orange'} />
  138. </div>
  139. </div>
  140. </Menu.Item>
  141. )
  142. }
  143. </div>
  144. <Menu.Item>
  145. <div className='p-1' onClick={() => handleLogout()}>
  146. <div
  147. className='flex items-center justify-between h-9 px-3 rounded-lg cursor-pointer group hover:bg-gray-50'
  148. >
  149. <div className='font-normal text-[14px] text-gray-700'>{t('common.userProfile.logout')}</div>
  150. <LogOut01 className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  151. </div>
  152. </div>
  153. </Menu.Item>
  154. </Menu.Items>
  155. </Transition>
  156. </>
  157. )
  158. }
  159. </Menu>
  160. {
  161. aboutVisible && <AccountAbout onCancel={() => setAboutVisible(false)} langeniusVersionInfo={langeniusVersionInfo} />
  162. }
  163. </div >
  164. )
  165. }