index.tsx 7.3 KB

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