index.tsx 3.9 KB

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