index.tsx 3.7 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 s from './index.module.css'
  6. import cn from '@/utils/classnames'
  7. import { switchWorkspace } from '@/service/common'
  8. import { useWorkspacesContext } from '@/context/workspace-context'
  9. import { ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
  10. import { Check } from '@/app/components/base/icons/src/vender/line/general'
  11. import { ToastContext } from '@/app/components/base/toast'
  12. const itemClassName = `
  13. flex items-center px-3 py-2 h-10 cursor-pointer
  14. `
  15. const itemIconClassName = `
  16. shrink-0 mr-2 flex items-center justify-center w-6 h-6 bg-[#EFF4FF] rounded-md text-xs font-medium text-primary-600
  17. `
  18. const itemNameClassName = `
  19. grow mr-2 text-sm text-gray-700 text-left
  20. `
  21. const itemCheckClassName = `
  22. shrink-0 w-4 h-4 text-primary-600
  23. `
  24. const WorkplaceSelector = () => {
  25. const { t } = useTranslation()
  26. const { notify } = useContext(ToastContext)
  27. const { workspaces } = useWorkspacesContext()
  28. const currentWorkspace = workspaces.find(v => v.current)
  29. const handleSwitchWorkspace = async (tenant_id: string) => {
  30. try {
  31. if (currentWorkspace?.id === tenant_id)
  32. return
  33. await switchWorkspace({ url: '/workspaces/switch', body: { tenant_id } })
  34. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  35. location.assign(`${location.origin}`)
  36. }
  37. catch (e) {
  38. notify({ type: 'error', message: t('common.provider.saveFailed') })
  39. }
  40. }
  41. return (
  42. <Menu as="div" className="relative w-full h-full">
  43. {
  44. ({ open }) => (
  45. <>
  46. <Menu.Button className={cn(
  47. `
  48. ${itemClassName} w-full
  49. group hover:bg-gray-50 cursor-pointer ${open && 'bg-gray-50'} rounded-lg
  50. `,
  51. )}>
  52. <div className={itemIconClassName}>{currentWorkspace?.name[0].toLocaleUpperCase()}</div>
  53. <div className={`${itemNameClassName} truncate`}>{currentWorkspace?.name}</div>
  54. <ChevronRight className='shrink-0 w-[14px] h-[14px] text-gray-500' />
  55. </Menu.Button>
  56. <Transition
  57. as={Fragment}
  58. enter="transition ease-out duration-100"
  59. enterFrom="transform opacity-0 scale-95"
  60. enterTo="transform opacity-100 scale-100"
  61. leave="transition ease-in duration-75"
  62. leaveFrom="transform opacity-100 scale-100"
  63. leaveTo="transform opacity-0 scale-95"
  64. >
  65. <Menu.Items
  66. className={cn(
  67. `
  68. absolute top-[1px] min-w-[200px] max-h-[70vh] overflow-y-scroll z-10 bg-white border-[0.5px] border-gray-200
  69. divide-y divide-gray-100 origin-top-right rounded-xl
  70. `,
  71. s.popup,
  72. )}
  73. >
  74. <div className="px-1 py-1">
  75. {
  76. workspaces.map(workspace => (
  77. <div className={itemClassName} key={workspace.id} onClick={() => handleSwitchWorkspace(workspace.id)}>
  78. <div className={itemIconClassName}>{workspace.name[0].toLocaleUpperCase()}</div>
  79. <div className={itemNameClassName}>{workspace.name}</div>
  80. {workspace.current && <Check className={itemCheckClassName} />}
  81. </div>
  82. ))
  83. }
  84. </div>
  85. </Menu.Items>
  86. </Transition>
  87. </>
  88. )
  89. }
  90. </Menu>
  91. )
  92. }
  93. export default WorkplaceSelector