index.tsx 4.1 KB

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