index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 { isCurrentWorkspaceEditor } = 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. if (curNav?.id === nav.id)
  72. return
  73. setAppDetail()
  74. router.push(nav.link)
  75. }} title={nav.name}>
  76. <div className='relative w-6 h-6 mr-2 rounded-md'>
  77. <AppIcon size='tiny' icon={nav.icon} background={nav.icon_background}/>
  78. {!!nav.mode && (
  79. <span className={cn(
  80. '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',
  81. )}>
  82. {nav.mode === 'advanced-chat' && (
  83. <ChatBot className='w-2.5 h-2.5 text-[#1570EF]' />
  84. )}
  85. {nav.mode === 'agent-chat' && (
  86. <CuteRobote className='w-2.5 h-2.5 text-indigo-600' />
  87. )}
  88. {nav.mode === 'chat' && (
  89. <ChatBot className='w-2.5 h-2.5 text-[#1570EF]' />
  90. )}
  91. {nav.mode === 'completion' && (
  92. <AiText className='w-2.5 h-2.5 text-[#0E9384]' />
  93. )}
  94. {nav.mode === 'workflow' && (
  95. <Route className='w-2.5 h-2.5 text-[#f79009]' />
  96. )}
  97. </span>
  98. )}
  99. </div>
  100. <div className='truncate'>
  101. {nav.name}
  102. </div>
  103. </div>
  104. </Menu.Item>
  105. ))
  106. }
  107. </div>
  108. {!isApp && (
  109. <Menu.Button className='p-1 w-full'>
  110. <div onClick={() => onCreate('')} className={cn(
  111. 'flex items-center gap-2 px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100',
  112. )}>
  113. <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'>
  114. <Plus className='w-4 h-4 text-gray-500' />
  115. </div>
  116. <div className='grow text-left font-normal text-[14px] text-gray-700'>{createText}</div>
  117. </div>
  118. </Menu.Button>
  119. )}
  120. {isApp && isCurrentWorkspaceEditor && (
  121. <Menu as="div" className="relative w-full h-full">
  122. {({ open }) => (
  123. <>
  124. <Menu.Button className='p-1 w-full'>
  125. <div className={cn(
  126. 'flex items-center gap-2 px-3 py-[6px] rounded-lg cursor-pointer hover:bg-gray-100',
  127. open && '!bg-gray-100',
  128. )}>
  129. <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'>
  130. <Plus className='w-4 h-4 text-gray-500' />
  131. </div>
  132. <div className='grow text-left font-normal text-[14px] text-gray-700'>{createText}</div>
  133. <ChevronRight className='shrink-0 w-3.5 h-3.5 text-gray-500'/>
  134. </div>
  135. </Menu.Button>
  136. <Transition
  137. as={Fragment}
  138. enter="transition ease-out duration-100"
  139. enterFrom="transform opacity-0 scale-95"
  140. enterTo="transform opacity-100 scale-100"
  141. leave="transition ease-in duration-75"
  142. leaveFrom="transform opacity-100 scale-100"
  143. leaveTo="transform opacity-0 scale-95"
  144. >
  145. <Menu.Items className={cn(
  146. 'absolute top-[3px] right-[-198px] min-w-[200px] z-10 bg-white border-[0.5px] border-gray-200 rounded-lg shadow-lg',
  147. )}>
  148. <div className='p-1'>
  149. <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')}>
  150. <FilePlus01 className='shrink-0 mr-2 w-4 h-4 text-gray-600' />
  151. {t('app.newApp.startFromBlank')}
  152. </div>
  153. <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')}>
  154. <FilePlus02 className='shrink-0 mr-2 w-4 h-4 text-gray-600' />
  155. {t('app.newApp.startFromTemplate')}
  156. </div>
  157. </div>
  158. <div className='p-1 border-t border-gray-100'>
  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('dsl')}>
  160. <FileArrow01 className='shrink-0 mr-2 w-4 h-4 text-gray-600' />
  161. {t('app.importDSL')}
  162. </div>
  163. </div>
  164. </Menu.Items>
  165. </Transition>
  166. </>
  167. )}
  168. </Menu>
  169. )}
  170. </Menu.Items>
  171. </>
  172. )}
  173. </Menu>
  174. </div>
  175. )
  176. }
  177. export default NavSelector