index.tsx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 cn from 'classnames'
  6. import s from './index.module.css'
  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) return
  32. await switchWorkspace({ url: '/workspaces/switch', body: { tenant_id } })
  33. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  34. location.assign(`${location.origin}`)
  35. }
  36. catch (e) {
  37. notify({ type: 'error', message: t('common.provider.saveFailed') })
  38. }
  39. }
  40. return (
  41. <Menu as="div" className="relative w-full h-full">
  42. {
  43. ({ open }) => (
  44. <>
  45. <Menu.Button className={cn(
  46. `
  47. ${itemClassName} w-full
  48. group hover:bg-gray-50 cursor-pointer ${open && 'bg-gray-50'} rounded-lg
  49. `,
  50. )}>
  51. <div className={itemIconClassName}>{currentWorkspace?.name[0].toLocaleUpperCase()}</div>
  52. <div className={`${itemNameClassName} truncate`}>{currentWorkspace?.name}</div>
  53. <ChevronRight className='shrink-0 w-[14px] h-[14px] text-gray-500' />
  54. </Menu.Button>
  55. <Transition
  56. as={Fragment}
  57. enter="transition ease-out duration-100"
  58. enterFrom="transform opacity-0 scale-95"
  59. enterTo="transform opacity-100 scale-100"
  60. leave="transition ease-in duration-75"
  61. leaveFrom="transform opacity-100 scale-100"
  62. leaveTo="transform opacity-0 scale-95"
  63. >
  64. <Menu.Items
  65. className={cn(
  66. `
  67. absolute top-[1px] min-w-[200px] z-10 bg-white border-[0.5px] border-gray-200
  68. divide-y divide-gray-100 origin-top-right rounded-xl
  69. `,
  70. s.popup,
  71. )}
  72. >
  73. <div className="px-1 py-1">
  74. {
  75. workspaces.map(workspace => (
  76. <div className={itemClassName} key={workspace.id} onClick={() => handleSwitchWorkspace(workspace.id)}>
  77. <div className={itemIconClassName}>{workspace.name[0].toLocaleUpperCase()}</div>
  78. <div className={itemNameClassName}>{workspace.name}</div>
  79. {workspace.current && <Check className={itemCheckClassName} />}
  80. </div>
  81. ))
  82. }
  83. </div>
  84. </Menu.Items>
  85. </Transition>
  86. </>
  87. )
  88. }
  89. </Menu>
  90. )
  91. }
  92. export default WorkplaceSelector