index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 AccountSetting from '../account-setting'
  11. import AccountAbout from '../account-about'
  12. import WorkplaceSelector from './workplace-selector'
  13. import I18n from '@/context/i18n'
  14. import Avatar from '@/app/components/base/avatar'
  15. import { logout } from '@/service/common'
  16. import { useAppContext } from '@/context/app-context'
  17. import { ArrowUpRight, ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows'
  18. import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
  19. export default function AppSelector() {
  20. const itemClassName = `
  21. flex items-center w-full h-9 px-3 text-gray-700 text-[14px]
  22. rounded-lg font-normal hover:bg-gray-50 cursor-pointer
  23. `
  24. const router = useRouter()
  25. const [settingVisible, setSettingVisible] = useState(false)
  26. const [aboutVisible, setAboutVisible] = useState(false)
  27. const { locale } = useContext(I18n)
  28. const { t } = useTranslation()
  29. const { userProfile, langeniusVersionInfo } = useAppContext()
  30. const handleLogout = async () => {
  31. await logout({
  32. url: '/logout',
  33. params: {},
  34. })
  35. router.push('/signin')
  36. }
  37. return (
  38. <div className="">
  39. <Menu as="div" className="relative inline-block text-left">
  40. {
  41. ({ open }) => (
  42. <>
  43. <div>
  44. <Menu.Button
  45. className={`
  46. inline-flex items-center
  47. rounded-[20px] py-1 pr-2.5 pl-1 text-sm
  48. text-gray-700 hover:bg-gray-200
  49. ${open && 'bg-gray-200'}
  50. `}
  51. >
  52. <Avatar name={userProfile.name} className='mr-2' size={32} />
  53. {userProfile.name}
  54. <ChevronDown className="w-3 h-3 ml-1 text-gray-700"/>
  55. </Menu.Button>
  56. </div>
  57. <Transition
  58. as={Fragment}
  59. enter="transition ease-out duration-100"
  60. enterFrom="transform opacity-0 scale-95"
  61. enterTo="transform opacity-100 scale-100"
  62. leave="transition ease-in duration-75"
  63. leaveFrom="transform opacity-100 scale-100"
  64. leaveTo="transform opacity-0 scale-95"
  65. >
  66. <Menu.Items
  67. className="
  68. absolute right-0 mt-1.5 w-60 max-w-80
  69. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  70. shadow-lg
  71. "
  72. >
  73. <Menu.Item>
  74. <div className='flex flex-nowrap items-center px-4 py-[13px]'>
  75. <Avatar name={userProfile.name} size={36} className='mr-3' />
  76. <div className='grow'>
  77. <div className='leading-5 font-normal text-[14px] text-gray-800 break-all'>{userProfile.name}</div>
  78. <div className='leading-[18px] text-xs font-normal text-gray-500 break-all'>{userProfile.email}</div>
  79. </div>
  80. </div>
  81. </Menu.Item>
  82. <div className='px-1 py-1'>
  83. <div className='mt-2 px-3 text-xs font-medium text-gray-500'>{t('common.userProfile.workspace')}</div>
  84. <WorkplaceSelector />
  85. </div>
  86. <div className="px-1 py-1">
  87. <Menu.Item>
  88. <div className={itemClassName} onClick={() => setSettingVisible(true)}>
  89. <div>{t('common.userProfile.settings')}</div>
  90. </div>
  91. </Menu.Item>
  92. <Menu.Item>
  93. <Link
  94. className={classNames(itemClassName, 'group justify-between')}
  95. href={
  96. locale === 'en' ? 'https://docs.dify.ai/' : `https://docs.dify.ai/v/${locale.toLowerCase()}/`
  97. }
  98. target='_blank'>
  99. <div>{t('common.userProfile.helpCenter')}</div>
  100. <ArrowUpRight className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  101. </Link>
  102. </Menu.Item>
  103. <Menu.Item>
  104. <div className={classNames(itemClassName, 'justify-between')} onClick={() => setAboutVisible(true)}>
  105. <div>{t('common.userProfile.about')}</div>
  106. <div className='flex items-center'>
  107. <div className='mr-2 text-xs font-normal text-gray-500'>{langeniusVersionInfo.current_version}</div>
  108. <Indicator color={langeniusVersionInfo.current_version === langeniusVersionInfo.latest_version ? 'green' : 'orange'} />
  109. </div>
  110. </div>
  111. </Menu.Item>
  112. </div>
  113. <Menu.Item>
  114. <div className='p-1' onClick={() => handleLogout()}>
  115. <div
  116. className='flex items-center justify-between h-9 px-3 rounded-lg cursor-pointer group hover:bg-gray-50'
  117. >
  118. <div className='font-normal text-[14px] text-gray-700'>{t('common.userProfile.logout')}</div>
  119. <LogOut01 className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  120. </div>
  121. </div>
  122. </Menu.Item>
  123. </Menu.Items>
  124. </Transition>
  125. </>
  126. )
  127. }
  128. </Menu>
  129. {
  130. settingVisible && <AccountSetting onCancel={() => setSettingVisible(false)} />
  131. }
  132. {
  133. aboutVisible && <AccountAbout onCancel={() => setAboutVisible(false)} langeniusVersionInfo={langeniusVersionInfo} />
  134. }
  135. </div >
  136. )
  137. }