index.tsx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Fragment } from 'react'
  2. import { switchWorkspace } from '@/service/common'
  3. import { Menu, Transition } from '@headlessui/react'
  4. import { ChevronRightIcon, CheckIcon } from '@heroicons/react/24/outline'
  5. import cn from 'classnames'
  6. import s from './index.module.css'
  7. import { useContext } from 'use-context-selector'
  8. import { ToastContext } from '@/app/components/base/toast'
  9. import { useTranslation } from 'react-i18next'
  10. import { useRouter } from 'next/navigation'
  11. import { useWorkspacesContext } from '@/context/workspace-context'
  12. const itemClassName = `
  13. flex items-center px-3 py-2 h-10 cursor-pointer
  14. `
  15. const itemIconClassName = `
  16. shrink-0 mr-2 w-6 h-6 bg-[#EFF4FF] rounded-md
  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 router = useRouter()
  27. const { notify } = useContext(ToastContext)
  28. const { workspaces } = useWorkspacesContext()
  29. const currentWrokspace = workspaces.filter(item => item.current)?.[0]
  30. const handleSwitchWorkspace = async (tenant_id: string) => {
  31. try {
  32. await switchWorkspace({ url: `/workspaces/switch`, body: { tenant_id } })
  33. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  34. router.replace('/apps')
  35. } catch (e) {
  36. notify({ type: 'error', message: t('common.provider.saveFailed') })
  37. } finally {
  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'}
  49. `
  50. )}>
  51. <div className={itemIconClassName} />
  52. <div className={`${itemNameClassName} truncate`}>{currentWrokspace?.name}</div>
  53. <ChevronRightIcon className='shrink-0 w-[14px] h-[14px]' />
  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} />
  78. <div className={itemNameClassName}>{workspace.name}</div>
  79. {workspace.current && <CheckIcon className={itemCheckClassName} />}
  80. </div>
  81. ))
  82. }
  83. </div>
  84. </Menu.Items>
  85. </Transition>
  86. </>
  87. )
  88. }
  89. </Menu>
  90. )
  91. }
  92. export default WorkplaceSelector