index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { Fragment, useCallback } from 'react'
  4. import cn from 'classnames'
  5. import { Menu, Transition } from '@headlessui/react'
  6. import { useRouter } from 'next/navigation'
  7. import { debounce } from 'lodash-es'
  8. import AppIcon from '@/app/components/base/app-icon'
  9. import { AiText, ChatBot, CuteRobote } from '@/app/components/base/icons/src/vender/solid/communication'
  10. import { Route } from '@/app/components/base/icons/src/vender/solid/mapsAndTravel'
  11. import { useAppContext } from '@/context/app-context'
  12. import { useStore as useAppStore } from '@/app/components/app/store'
  13. import { ChevronDown, ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
  14. import { FileArrow01, FilePlus01, FilePlus02 } from '@/app/components/base/icons/src/vender/line/files'
  15. import { Plus } from '@/app/components/base/icons/src/vender/line/general'
  16. export type NavItem = {
  17. id: string
  18. name: string
  19. link: string
  20. icon: string
  21. icon_background: string
  22. mode: string
  23. }
  24. export type INavSelectorProps = {
  25. navs: NavItem[]
  26. curNav?: Omit<NavItem, 'link'>
  27. createText: string
  28. isApp: boolean
  29. onCreate: (state: string) => void
  30. onLoadmore?: () => void
  31. }
  32. const NavSelector = ({ curNav, navs, createText, isApp, onCreate, onLoadmore }: INavSelectorProps) => {
  33. const { t } = useTranslation()
  34. const router = useRouter()
  35. const { isCurrentWorkspaceManager } = useAppContext()
  36. const setAppDetail = useAppStore(state => state.setAppDetail)
  37. const handleScroll = useCallback(debounce((e) => {
  38. if (typeof onLoadmore === 'function') {
  39. const { clientHeight, scrollHeight, scrollTop } = e.target
  40. if (clientHeight + scrollTop > scrollHeight - 50)
  41. onLoadmore()
  42. }
  43. }, 50), [])
  44. return (
  45. <div className="">
  46. <Menu as="div" className="relative inline-block text-left">
  47. {({ open }) => (
  48. <>
  49. <Menu.Button className={cn(
  50. 'group inline-flex items-center w-full h-7 justify-center rounded-[10px] pl-2 pr-2.5 text-[14px] font-semibold text-primary-600 hover:bg-primary-50',
  51. open && 'bg-primary-50',
  52. )}>
  53. <div className='max-w-[180px] truncate' title={curNav?.name}>{curNav?.name}</div>
  54. <ChevronDown
  55. className={cn('shrink-0 w-3 h-3 ml-1 opacity-50 group-hover:opacity-100', open && '!opacity-100')}
  56. aria-hidden="true"
  57. />
  58. </Menu.Button>
  59. <Menu.Items
  60. className="
  61. absolute -left-11 right-0 mt-1.5 w-60 max-w-80
  62. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  63. shadow-lg
  64. "
  65. >
  66. <div className="px-1 py-1 overflow-auto" style={{ maxHeight: '50vh' }} onScroll={handleScroll}>
  67. {
  68. navs.map(nav => (
  69. <Menu.Item key={nav.id}>
  70. <div className='flex items-center w-full px-3 py-[6px] text-gray-700 text-[14px] rounded-lg font-normal hover:bg-gray-100 cursor-pointer truncate' onClick={() => {
  71. setAppDetail()
  72. router.push(nav.link)
  73. }} title={nav.name}>
  74. <div className='relative w-6 h-6 mr-2 rounded-md'>
  75. <AppIcon size='tiny' icon={nav.icon} background={nav.icon_background}/>
  76. {!!nav.mode && (
  77. <span className={cn(
  78. 'absolute w-3.5 h-3.5 -bottom-0.5 -right-0.5 p-0.5 bg-white rounded border-[0.5px] border-[rgba(0,0,0,0.02)] shadow-sm',
  79. )}>
  80. {nav.mode === 'advanced-chat' && (
  81. <ChatBot className='w-2.5 h-2.5 text-[#1570EF]' />
  82. )}
  83. {nav.mode === 'agent-chat' && (
  84. <CuteRobote className='w-2.5 h-2.5 text-indigo-600' />
  85. )}
  86. {nav.mode === 'chat' && (
  87. <ChatBot className='w-2.5 h-2.5 text-[#1570EF]' />
  88. )}
  89. {nav.mode === 'completion' && (
  90. <AiText className='w-2.5 h-2.5 text-[#0E9384]' />
  91. )}
  92. {nav.mode === 'workflow' && (
  93. <Route className='w-2.5 h-2.5 text-[#f79009]' />
  94. )}
  95. </span>
  96. )}
  97. </div>
  98. <div className='truncate'>
  99. {nav.name}
  100. </div>
  101. </div>
  102. </Menu.Item>
  103. ))
  104. }
  105. </div>
  106. {!isApp && (
  107. <Menu.Button className='p-1 w-full'>
  108. <div onClick={() => onCreate('')} className={cn(
  109. 'flex items-center gap-2 px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100',
  110. )}>
  111. <div className='shrink-0 flex justify-center items-center w-6 h-6 bg-gray-50 rounded-[6px] border-[0.5px] border-gray-200 border'>
  112. <Plus className='w-4 h-4 text-gray-500' />
  113. </div>
  114. <div className='grow text-left font-normal text-[14px] text-gray-700'>{createText}</div>
  115. </div>
  116. </Menu.Button>
  117. )}
  118. {isApp && isCurrentWorkspaceManager && (
  119. <Menu as="div" className="relative w-full h-full">
  120. {({ open }) => (
  121. <>
  122. <Menu.Button className='p-1 w-full'>
  123. <div className={cn(
  124. 'flex items-center gap-2 px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100',
  125. open && '!bg-gray-100',
  126. )}>
  127. <div className='shrink-0 flex justify-center items-center w-6 h-6 bg-gray-50 rounded-[6px] border-[0.5px] border-gray-200 border'>
  128. <Plus className='w-4 h-4 text-gray-500' />
  129. </div>
  130. <div className='grow text-left font-normal text-[14px] text-gray-700'>{createText}</div>
  131. <ChevronRight className='shrink-0 w-3.5 h-3.5 text-gray-500'/>
  132. </div>
  133. </Menu.Button>
  134. <Transition
  135. as={Fragment}
  136. enter="transition ease-out duration-100"
  137. enterFrom="transform opacity-0 scale-95"
  138. enterTo="transform opacity-100 scale-100"
  139. leave="transition ease-in duration-75"
  140. leaveFrom="transform opacity-100 scale-100"
  141. leaveTo="transform opacity-0 scale-95"
  142. >
  143. <Menu.Items className={cn(
  144. 'absolute top-[3px] right-[-198px] min-w-[200px] z-10 bg-white border-[0.5px] border-gray-200 rounded-lg shadow-lg',
  145. )}>
  146. <div className='p-1'>
  147. <div className={cn('flex items-center px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100 text-gray-700 font-normal')} onClick={() => onCreate('blank')}>
  148. <FilePlus01 className='shrink-0 mr-2 w-4 h-4 text-gray-600' />
  149. {t('app.newApp.startFromBlank')}
  150. </div>
  151. <div className={cn('flex items-center px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100 text-gray-700 font-normal')} onClick={() => onCreate('template')}>
  152. <FilePlus02 className='shrink-0 mr-2 w-4 h-4 text-gray-600' />
  153. {t('app.newApp.startFromTemplate')}
  154. </div>
  155. </div>
  156. <div className='p-1 border-t border-gray-100'>
  157. <div className={cn('flex items-center px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100 text-gray-700 font-normal')} onClick={() => onCreate('dsl')}>
  158. <FileArrow01 className='shrink-0 mr-2 w-4 h-4 text-gray-600' />
  159. {t('app.importDSL')}
  160. </div>
  161. </div>
  162. </Menu.Items>
  163. </Transition>
  164. </>
  165. )}
  166. </Menu>
  167. )}
  168. </Menu.Items>
  169. </>
  170. )}
  171. </Menu>
  172. </div>
  173. )
  174. }
  175. export default NavSelector