avatar.tsx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. localStorage.removeItem('setup_status')
  23. localStorage.removeItem('console_token')
  24. localStorage.removeItem('refresh_token')
  25. router.push('/signin')
  26. }
  27. return (
  28. <Menu as="div" className="relative inline-block text-left">
  29. {
  30. ({ open }) => (
  31. <>
  32. <div>
  33. <Menu.Button
  34. className={`
  35. inline-flex items-center
  36. rounded-[20px] p-1x text-sm
  37. text-gray-700 hover:bg-gray-200
  38. mobile:px-1
  39. ${open && 'bg-gray-200'}
  40. `}
  41. >
  42. <Avatar name={userProfile.name} size={32} />
  43. </Menu.Button>
  44. </div>
  45. <Transition
  46. as={Fragment}
  47. enter="transition ease-out duration-100"
  48. enterFrom="transform opacity-0 scale-95"
  49. enterTo="transform opacity-100 scale-100"
  50. leave="transition ease-in duration-75"
  51. leaveFrom="transform opacity-100 scale-100"
  52. leaveTo="transform opacity-0 scale-95"
  53. >
  54. <Menu.Items
  55. className="
  56. absolute -right-2 -top-1 w-60 max-w-80
  57. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  58. shadow-lg
  59. "
  60. >
  61. <Menu.Item>
  62. <div className='p-1'>
  63. <div className='flex flex-nowrap items-center px-3 py-2'>
  64. <div className='grow'>
  65. <div className='system-md-medium text-text-primary break-all'>{userProfile.name}</div>
  66. <div className='system-xs-regular text-text-tertiary break-all'>{userProfile.email}</div>
  67. </div>
  68. <Avatar name={userProfile.name} size={32} />
  69. </div>
  70. </div>
  71. </Menu.Item>
  72. <Menu.Item>
  73. <div className='p-1' onClick={() => handleLogout()}>
  74. <div
  75. className='flex items-center justify-start h-9 px-3 rounded-lg cursor-pointer group hover:bg-gray-50'
  76. >
  77. <LogOut01 className='w-4 h-4 text-gray-500 flex mr-1' />
  78. <div className='font-normal text-[14px] text-gray-700'>{t('common.userProfile.logout')}</div>
  79. </div>
  80. </div>
  81. </Menu.Item>
  82. </Menu.Items>
  83. </Transition>
  84. </>
  85. )
  86. }
  87. </Menu>
  88. )
  89. }