avatar.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { Fragment } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { Menu, Transition } from '@headlessui/react'
  6. import Avatar from '@/app/components/base/avatar'
  7. import { logout } from '@/service/common'
  8. import { useAppContext } from '@/context/app-context'
  9. import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
  10. export type IAppSelector = {
  11. isMobile: boolean
  12. }
  13. export default function AppSelector() {
  14. const router = useRouter()
  15. const { t } = useTranslation()
  16. const { userProfile } = useAppContext()
  17. const handleLogout = async () => {
  18. await logout({
  19. url: '/logout',
  20. params: {},
  21. })
  22. if (localStorage?.getItem('console_token'))
  23. localStorage.removeItem('console_token')
  24. router.push('/signin')
  25. }
  26. return (
  27. <Menu as="div" className="relative inline-block text-left">
  28. {
  29. ({ open }) => (
  30. <>
  31. <div>
  32. <Menu.Button
  33. className={`
  34. inline-flex items-center
  35. rounded-[20px] p-1x text-sm
  36. text-gray-700 hover:bg-gray-200
  37. mobile:px-1
  38. ${open && 'bg-gray-200'}
  39. `}
  40. >
  41. <Avatar name={userProfile.name} size={32} />
  42. </Menu.Button>
  43. </div>
  44. <Transition
  45. as={Fragment}
  46. enter="transition ease-out duration-100"
  47. enterFrom="transform opacity-0 scale-95"
  48. enterTo="transform opacity-100 scale-100"
  49. leave="transition ease-in duration-75"
  50. leaveFrom="transform opacity-100 scale-100"
  51. leaveTo="transform opacity-0 scale-95"
  52. >
  53. <Menu.Items
  54. className="
  55. absolute -right-2 -top-1 w-60 max-w-80
  56. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  57. shadow-lg
  58. "
  59. >
  60. <Menu.Item>
  61. <div className='p-1'>
  62. <div className='flex flex-nowrap items-center px-3 py-2'>
  63. <div className='grow'>
  64. <div className='system-md-medium text-text-primary break-all'>{userProfile.name}</div>
  65. <div className='system-xs-regular text-text-tertiary break-all'>{userProfile.email}</div>
  66. </div>
  67. <Avatar name={userProfile.name} size={32} />
  68. </div>
  69. </div>
  70. </Menu.Item>
  71. <Menu.Item>
  72. <div className='p-1' onClick={() => handleLogout()}>
  73. <div
  74. className='flex items-center justify-start h-9 px-3 rounded-lg cursor-pointer group hover:bg-gray-50'
  75. >
  76. <LogOut01 className='w-4 h-4 text-gray-500 flex mr-1' />
  77. <div className='font-normal text-[14px] text-gray-700'>{t('common.userProfile.logout')}</div>
  78. </div>
  79. </div>
  80. </Menu.Item>
  81. </Menu.Items>
  82. </Transition>
  83. </>
  84. )
  85. }
  86. </Menu>
  87. )
  88. }