index.tsx 5.4 KB

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