index.tsx 7.7 KB

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