index.tsx 5.6 KB

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