index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 { RiArrowDownSLine } from '@remixicon/react'
  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 { mailToSupport } from '../utils/util'
  12. import WorkplaceSelector from './workplace-selector'
  13. import classNames from '@/utils/classnames'
  14. import I18n from '@/context/i18n'
  15. import Avatar from '@/app/components/base/avatar'
  16. import { logout } from '@/service/common'
  17. import { useAppContext } from '@/context/app-context'
  18. import { ArrowUpRight } from '@/app/components/base/icons/src/vender/line/arrows'
  19. import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
  20. import { useModalContext } from '@/context/modal-context'
  21. import { LanguagesSupported } from '@/i18n/language'
  22. import { useProviderContext } from '@/context/provider-context'
  23. import { Plan } from '@/app/components/billing/type'
  24. export type IAppSelector = {
  25. isMobile: boolean
  26. }
  27. export default function AppSelector({ isMobile }: IAppSelector) {
  28. const itemClassName = `
  29. flex items-center w-full h-9 px-3 text-gray-700 text-[14px]
  30. rounded-lg font-normal hover:bg-gray-50 cursor-pointer
  31. `
  32. const router = useRouter()
  33. const [aboutVisible, setAboutVisible] = useState(false)
  34. const { locale } = useContext(I18n)
  35. const { t } = useTranslation()
  36. const { userProfile, langeniusVersionInfo } = useAppContext()
  37. const { setShowAccountSettingModal } = useModalContext()
  38. const { plan } = useProviderContext()
  39. const canEmailSupport = plan.type === Plan.professional || plan.type === Plan.team || plan.type === Plan.enterprise
  40. const handleLogout = async () => {
  41. await logout({
  42. url: '/logout',
  43. params: {},
  44. })
  45. localStorage.removeItem('setup_status')
  46. localStorage.removeItem('console_token')
  47. localStorage.removeItem('refresh_token')
  48. router.push('/signin')
  49. }
  50. return (
  51. <div className="">
  52. <Menu as="div" className="relative inline-block text-left">
  53. {
  54. ({ open }) => (
  55. <>
  56. <div>
  57. <Menu.Button
  58. className={`
  59. inline-flex items-center
  60. rounded-[20px] py-1 pr-2.5 pl-1 text-sm
  61. text-gray-700 hover:bg-gray-200
  62. mobile:px-1
  63. ${open && 'bg-gray-200'}
  64. `}
  65. >
  66. <Avatar name={userProfile.name} className='sm:mr-2 mr-0' size={32} />
  67. {!isMobile && <>
  68. {userProfile.name}
  69. <RiArrowDownSLine className="w-3 h-3 ml-1 text-gray-700" />
  70. </>}
  71. </Menu.Button>
  72. </div>
  73. <Transition
  74. as={Fragment}
  75. enter="transition ease-out duration-100"
  76. enterFrom="transform opacity-0 scale-95"
  77. enterTo="transform opacity-100 scale-100"
  78. leave="transition ease-in duration-75"
  79. leaveFrom="transform opacity-100 scale-100"
  80. leaveTo="transform opacity-0 scale-95"
  81. >
  82. <Menu.Items
  83. className="
  84. absolute right-0 mt-1.5 w-60 max-w-80
  85. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  86. shadow-lg
  87. "
  88. >
  89. <Menu.Item>
  90. <div className='flex flex-nowrap items-center px-4 py-[13px]'>
  91. <Avatar name={userProfile.name} size={36} className='mr-3' />
  92. <div className='grow'>
  93. <div className='leading-5 font-normal text-[14px] text-gray-800 break-all'>{userProfile.name}</div>
  94. <div className='leading-[18px] text-xs font-normal text-gray-500 break-all'>{userProfile.email}</div>
  95. </div>
  96. </div>
  97. </Menu.Item>
  98. <div className='px-1 py-1'>
  99. {/* <div className='mt-2 px-3 text-xs font-medium text-gray-500'>{t('common.userProfile.workspace')}</div> */}
  100. <WorkplaceSelector />
  101. </div>
  102. <div className="px-1 py-1">
  103. <Menu.Item>
  104. <Link
  105. className={classNames(itemClassName, 'group justify-between')}
  106. href='/account'
  107. target='_self' rel='noopener noreferrer'>
  108. <div>{t('common.account.account')}</div>
  109. <ArrowUpRight className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  110. </Link>
  111. </Menu.Item>
  112. <Menu.Item>
  113. <div className={itemClassName} onClick={() => setShowAccountSettingModal({ payload: 'members' })}>
  114. <div>{t('common.userProfile.settings')}</div>
  115. </div>
  116. </Menu.Item>
  117. {canEmailSupport && <Menu.Item>
  118. <a
  119. className={classNames(itemClassName, 'group justify-between')}
  120. href={mailToSupport(userProfile.email, plan.type, langeniusVersionInfo.current_version)}
  121. target='_blank' rel='noopener noreferrer'>
  122. <div>{t('common.userProfile.emailSupport')}</div>
  123. <ArrowUpRight className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  124. </a>
  125. </Menu.Item>}
  126. </div>
  127. <Menu.Item>
  128. <div className='p-1' onClick={() => handleLogout()}>
  129. <div
  130. className='flex items-center justify-between h-9 px-3 rounded-lg cursor-pointer group hover:bg-gray-50'
  131. >
  132. <div className='font-normal text-[14px] text-gray-700'>{t('common.userProfile.logout')}</div>
  133. <LogOut01 className='hidden w-[14px] h-[14px] text-gray-500 group-hover:flex' />
  134. </div>
  135. </div>
  136. </Menu.Item>
  137. </Menu.Items>
  138. </Transition>
  139. </>
  140. )
  141. }
  142. </Menu>
  143. {
  144. aboutVisible && <AccountAbout onCancel={() => setAboutVisible(false)} langeniusVersionInfo={langeniusVersionInfo} />
  145. }
  146. </div >
  147. )
  148. }