index.tsx 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Fragment } from 'react'
  2. import { useContext } from 'use-context-selector'
  3. import { useTranslation } from 'react-i18next'
  4. import { Menu, Transition } from '@headlessui/react'
  5. import { RiArrowDownSLine } from '@remixicon/react'
  6. import cn from '@/utils/classnames'
  7. import { switchWorkspace } from '@/service/common'
  8. import { useWorkspacesContext } from '@/context/workspace-context'
  9. import { ToastContext } from '@/app/components/base/toast'
  10. const WorkplaceSelector = () => {
  11. const { t } = useTranslation()
  12. const { notify } = useContext(ToastContext)
  13. const { workspaces } = useWorkspacesContext()
  14. const currentWorkspace = workspaces.find(v => v.current)
  15. const handleSwitchWorkspace = async (tenant_id: string) => {
  16. try {
  17. if (currentWorkspace?.id === tenant_id)
  18. return
  19. await switchWorkspace({ url: '/workspaces/switch', body: { tenant_id } })
  20. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  21. location.assign(`${location.origin}`)
  22. }
  23. catch (e) {
  24. notify({ type: 'error', message: t('common.provider.saveFailed') })
  25. }
  26. }
  27. return (
  28. <Menu as="div" className="relative w-full h-full">
  29. {
  30. ({ open }) => (
  31. <>
  32. <Menu.Button className={cn(
  33. `
  34. flex items-center p-0.5 gap-1.5 w-full
  35. group hover:bg-state-base-hover cursor-pointer ${open && 'bg-state-base-hover'} rounded-[10px]
  36. `,
  37. )}>
  38. <div className='flex items-center justify-center w-7 h-7 bg-[#EFF4FF] rounded-lg text-xs font-medium text-primary-600'>{currentWorkspace?.name[0].toLocaleUpperCase()}</div>
  39. <div className='flex flex-row'>
  40. <div className={'truncate max-w-[80px] text-text-secondary system-sm-medium'}>{currentWorkspace?.name}</div>
  41. <RiArrowDownSLine className='w-4 h-4 text-text-secondary' />
  42. </div>
  43. </Menu.Button>
  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={cn(
  55. `
  56. flex w-[280px] flex-col items-start absolute left-[-15px] mt-1 rounded-xl shadows-shadow-lg
  57. `,
  58. )}
  59. >
  60. <div className="flex flex-col p-1 pb-2 items-start self-stretch w-full rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-lg ">
  61. <div className='flex px-3 pt-1 pb-0.5 items-start self-stretch'>
  62. <span className='flex-1 text-text-tertiary system-xs-medium-uppercase'>{t('common.userProfile.workspace')}</span>
  63. </div>
  64. {
  65. workspaces.map(workspace => (
  66. <div className='flex py-1 pl-3 pr-2 items-center gap-2 self-stretch hover:bg-state-base-hover rounded-lg' key={workspace.id} onClick={() => handleSwitchWorkspace(workspace.id)}>
  67. <div className='flex items-center justify-center w-6 h-6 bg-[#EFF4FF] rounded-md text-xs font-medium text-primary-600'>{workspace.name[0].toLocaleUpperCase()}</div>
  68. <div className='line-clamp-1 grow overflow-hidden text-text-secondary text-ellipsis system-md-regular cursor-pointer'>{workspace.name}</div>
  69. </div>
  70. ))
  71. }
  72. </div>
  73. </Menu.Items>
  74. </Transition>
  75. </>
  76. )
  77. }
  78. </Menu>
  79. )
  80. }
  81. export default WorkplaceSelector