index.tsx 8.9 KB

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