index.tsx 9.0 KB

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