index.tsx 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 { useProviderContext } from '@/context/provider-context'
  10. import { ToastContext } from '@/app/components/base/toast'
  11. import PremiumBadge from '@/app/components/base/premium-badge'
  12. const WorkplaceSelector = () => {
  13. const { t } = useTranslation()
  14. const { plan } = useProviderContext()
  15. const { notify } = useContext(ToastContext)
  16. const { workspaces } = useWorkspacesContext()
  17. const currentWorkspace = workspaces.find(v => v.current)
  18. const isFreePlan = plan.type === 'sandbox'
  19. const handleSwitchWorkspace = async (tenant_id: string) => {
  20. try {
  21. if (currentWorkspace?.id === tenant_id)
  22. return
  23. await switchWorkspace({ url: '/workspaces/switch', body: { tenant_id } })
  24. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  25. location.assign(`${location.origin}`)
  26. }
  27. catch (e) {
  28. notify({ type: 'error', message: t('common.provider.saveFailed') })
  29. }
  30. }
  31. return (
  32. <Menu as="div" className="relative w-full h-full">
  33. {
  34. ({ open }) => (
  35. <>
  36. <Menu.Button className={cn(
  37. `
  38. flex items-center p-0.5 gap-1.5 w-full
  39. group hover:bg-state-base-hover cursor-pointer ${open && 'bg-state-base-hover'} rounded-[10px]
  40. `,
  41. )}>
  42. <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>
  43. <div className='flex flex-row'>
  44. <div className={'truncate max-w-[80px] text-text-secondary system-sm-medium'}>{currentWorkspace?.name}</div>
  45. <RiArrowDownSLine className='w-4 h-4 text-text-secondary' />
  46. </div>
  47. </Menu.Button>
  48. <Transition
  49. as={Fragment}
  50. enter="transition ease-out duration-100"
  51. enterFrom="transform opacity-0 scale-95"
  52. enterTo="transform opacity-100 scale-100"
  53. leave="transition ease-in duration-75"
  54. leaveFrom="transform opacity-100 scale-100"
  55. leaveTo="transform opacity-0 scale-95"
  56. >
  57. <Menu.Items
  58. className={cn(
  59. `
  60. flex w-[280px] flex-col items-start absolute left-[-15px] mt-1 rounded-xl shadows-shadow-lg
  61. `,
  62. )}
  63. >
  64. <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 ">
  65. <div className='flex px-3 pt-1 pb-0.5 items-start self-stretch'>
  66. <span className='flex-1 text-text-tertiary system-xs-medium-uppercase'>{t('common.userProfile.workspace')}</span>
  67. </div>
  68. {
  69. workspaces.map(workspace => (
  70. <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)}>
  71. <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>
  72. <div className='line-clamp-1 grow overflow-hidden text-text-secondary text-ellipsis system-md-regular cursor-pointer'>{workspace.name}</div>
  73. {
  74. <PremiumBadge size='s' color='gray' allowHover={false}>
  75. <div className='system-2xs-medium'>
  76. <span className='p-[2px]'>
  77. {plan.type === 'professional' ? 'PRO' : plan.type.toUpperCase()}
  78. </span>
  79. </div>
  80. </PremiumBadge>
  81. }
  82. </div>
  83. ))
  84. }
  85. </div>
  86. </Menu.Items>
  87. </Transition>
  88. </>
  89. )
  90. }
  91. </Menu>
  92. )
  93. }
  94. export default WorkplaceSelector