index.tsx 4.0 KB

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