You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { Fragment, useState } from 'react'
  4. import { ChevronDownIcon, PlusIcon } from '@heroicons/react/24/solid'
  5. import { Menu, MenuButton, MenuItem, MenuItems, Transition } from '@headlessui/react'
  6. import { useRouter } from 'next/navigation'
  7. import Indicator from '../indicator'
  8. import type { AppDetailResponse } from '@/models/app'
  9. import CreateAppDialog from '@/app/components/app/create-app-dialog'
  10. import AppIcon from '@/app/components/base/app-icon'
  11. import { useAppContext } from '@/context/app-context'
  12. import { noop } from 'lodash-es'
  13. type IAppSelectorProps = {
  14. appItems: AppDetailResponse[]
  15. curApp: AppDetailResponse
  16. }
  17. export default function AppSelector({ appItems, curApp }: IAppSelectorProps) {
  18. const router = useRouter()
  19. const { isCurrentWorkspaceEditor } = useAppContext()
  20. const [showNewAppDialog, setShowNewAppDialog] = useState(false)
  21. const { t } = useTranslation()
  22. const itemClassName = `
  23. flex items-center w-full h-10 px-3 text-gray-700 text-[14px]
  24. rounded-lg font-normal hover:bg-gray-100 cursor-pointer
  25. `
  26. return (
  27. <div className="">
  28. <Menu as="div" className="relative inline-block text-left">
  29. <div>
  30. <MenuButton
  31. className="
  32. inline-flex h-7 w-full items-center justify-center
  33. rounded-[10px] pl-2 pr-2.5 text-[14px] font-semibold
  34. text-[#1C64F2] hover:bg-[#EBF5FF]
  35. "
  36. >
  37. {curApp?.name}
  38. <ChevronDownIcon
  39. className="ml-1 h-3 w-3"
  40. aria-hidden="true"
  41. />
  42. </MenuButton>
  43. </div>
  44. <Transition
  45. as={Fragment}
  46. enter="transition ease-out duration-100"
  47. enterFrom="transform opacity-0 scale-95"
  48. enterTo="transform opacity-100 scale-100"
  49. leave="transition ease-in duration-75"
  50. leaveFrom="transform opacity-100 scale-100"
  51. leaveTo="transform opacity-0 scale-95"
  52. >
  53. <MenuItems
  54. className="
  55. absolute -left-11 right-0 mt-1.5 w-60 max-w-80
  56. origin-top-right divide-y divide-gray-100 rounded-lg bg-white
  57. shadow-lg
  58. "
  59. >
  60. {!!appItems.length && (<div className="overflow-auto px-1 py-1" style={{ maxHeight: '50vh' }}>
  61. {
  62. appItems.map((app: AppDetailResponse) => (
  63. <MenuItem key={app.id}>
  64. <div className={itemClassName} onClick={() =>
  65. router.push(`/app/${app.id}/${isCurrentWorkspaceEditor ? 'configuration' : 'overview'}`)
  66. }>
  67. <div className='relative mr-2 h-6 w-6 rounded-[6px] bg-[#D5F5F6]'>
  68. <AppIcon size='tiny' />
  69. <div className='absolute -bottom-0.5 -right-0.5 flex h-2.5 w-2.5 items-center justify-center rounded bg-white'>
  70. <Indicator />
  71. </div>
  72. </div>
  73. {app.name}
  74. </div>
  75. </MenuItem>
  76. ))
  77. }
  78. </div>)}
  79. {isCurrentWorkspaceEditor && <MenuItem>
  80. <div className='p-1' onClick={() => setShowNewAppDialog(true)}>
  81. <div
  82. className='flex h-12 cursor-pointer items-center rounded-lg hover:bg-gray-100'
  83. >
  84. <div
  85. className='
  86. ml-4 mr-2 flex
  87. h-6 w-6 items-center justify-center rounded-[6px] border-[0.5px]
  88. border-dashed border-gray-200 bg-gray-100
  89. '
  90. >
  91. <PlusIcon className='h-4 w-4 text-gray-500' />
  92. </div>
  93. <div className='text-[14px] font-normal text-gray-700'>{t('common.menus.newApp')}</div>
  94. </div>
  95. </div>
  96. </MenuItem>
  97. }
  98. </MenuItems>
  99. </Transition>
  100. </Menu>
  101. <CreateAppDialog
  102. show={showNewAppDialog}
  103. onClose={() => setShowNewAppDialog(false)}
  104. onSuccess={noop}
  105. />
  106. </div>
  107. )
  108. }