You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. 'use client'
  2. import React, { useCallback, useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import useSWR from 'swr'
  6. import { useDebounceFn } from 'ahooks'
  7. import s from './style.module.css'
  8. import cn from '@/utils/classnames'
  9. import ExploreContext from '@/context/explore-context'
  10. import type { App } from '@/models/explore'
  11. import Category from '@/app/components/explore/category'
  12. import AppCard from '@/app/components/explore/app-card'
  13. import { fetchAppDetail, fetchAppList } from '@/service/explore'
  14. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  15. import CreateAppModal from '@/app/components/explore/create-app-modal'
  16. import AppTypeSelector from '@/app/components/app/type-selector'
  17. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  18. import Loading from '@/app/components/base/loading'
  19. import Input from '@/app/components/base/input'
  20. import {
  21. DSLImportMode,
  22. } from '@/models/app'
  23. import { useImportDSL } from '@/hooks/use-import-dsl'
  24. import DSLConfirmModal from '@/app/components/app/create-from-dsl-modal/dsl-confirm-modal'
  25. type AppsProps = {
  26. pageType?: PageType
  27. onSuccess?: () => void
  28. }
  29. export enum PageType {
  30. EXPLORE = 'explore',
  31. CREATE = 'create',
  32. }
  33. const Apps = ({
  34. pageType = PageType.EXPLORE,
  35. onSuccess,
  36. }: AppsProps) => {
  37. const { t } = useTranslation()
  38. const { hasEditPermission } = useContext(ExploreContext)
  39. const allCategoriesEn = t('explore.apps.allCategories', { lng: 'en' })
  40. const [keywords, setKeywords] = useState('')
  41. const [searchKeywords, setSearchKeywords] = useState('')
  42. const { run: handleSearch } = useDebounceFn(() => {
  43. setSearchKeywords(keywords)
  44. }, { wait: 500 })
  45. const handleKeywordsChange = (value: string) => {
  46. setKeywords(value)
  47. handleSearch()
  48. }
  49. const [currentType, setCurrentType] = useState<string>('')
  50. const [currCategory, setCurrCategory] = useTabSearchParams({
  51. defaultTab: allCategoriesEn,
  52. disableSearchParams: pageType !== PageType.EXPLORE,
  53. })
  54. const {
  55. data: { categories, allList },
  56. } = useSWR(
  57. ['/explore/apps'],
  58. () =>
  59. fetchAppList().then(({ categories, recommended_apps }) => ({
  60. categories,
  61. allList: recommended_apps.sort((a, b) => a.position - b.position),
  62. })),
  63. {
  64. fallbackData: {
  65. categories: [],
  66. allList: [],
  67. },
  68. },
  69. )
  70. const filteredList = useMemo(() => {
  71. if (currCategory === allCategoriesEn) {
  72. if (!currentType)
  73. return allList
  74. else if (currentType === 'chatbot')
  75. return allList.filter(item => (item.app.mode === 'chat' || item.app.mode === 'advanced-chat'))
  76. else if (currentType === 'agent')
  77. return allList.filter(item => (item.app.mode === 'agent-chat'))
  78. else
  79. return allList.filter(item => (item.app.mode === 'workflow'))
  80. }
  81. else {
  82. if (!currentType)
  83. return allList.filter(item => item.category === currCategory)
  84. else if (currentType === 'chatbot')
  85. return allList.filter(item => (item.app.mode === 'chat' || item.app.mode === 'advanced-chat') && item.category === currCategory)
  86. else if (currentType === 'agent')
  87. return allList.filter(item => (item.app.mode === 'agent-chat') && item.category === currCategory)
  88. else
  89. return allList.filter(item => (item.app.mode === 'workflow') && item.category === currCategory)
  90. }
  91. }, [currentType, currCategory, allCategoriesEn, allList])
  92. const searchFilteredList = useMemo(() => {
  93. if (!searchKeywords || !filteredList || filteredList.length === 0)
  94. return filteredList
  95. const lowerCaseSearchKeywords = searchKeywords.toLowerCase()
  96. return filteredList.filter(item =>
  97. item.app && item.app.name && item.app.name.toLowerCase().includes(lowerCaseSearchKeywords),
  98. )
  99. }, [searchKeywords, filteredList])
  100. const [currApp, setCurrApp] = React.useState<App | null>(null)
  101. const [isShowCreateModal, setIsShowCreateModal] = React.useState(false)
  102. const {
  103. handleImportDSL,
  104. handleImportDSLConfirm,
  105. versions,
  106. isFetching,
  107. } = useImportDSL()
  108. const [showDSLConfirmModal, setShowDSLConfirmModal] = useState(false)
  109. const onCreate: CreateAppModalProps['onConfirm'] = async ({
  110. name,
  111. icon_type,
  112. icon,
  113. icon_background,
  114. description,
  115. }) => {
  116. const { export_data } = await fetchAppDetail(
  117. currApp?.app.id as string,
  118. )
  119. const payload = {
  120. mode: DSLImportMode.YAML_CONTENT,
  121. yaml_content: export_data,
  122. name,
  123. icon_type,
  124. icon,
  125. icon_background,
  126. description,
  127. }
  128. await handleImportDSL(payload, {
  129. onSuccess: () => {
  130. setIsShowCreateModal(false)
  131. },
  132. onPending: () => {
  133. setShowDSLConfirmModal(true)
  134. },
  135. })
  136. }
  137. const onConfirmDSL = useCallback(async () => {
  138. await handleImportDSLConfirm({
  139. onSuccess,
  140. })
  141. }, [handleImportDSLConfirm, onSuccess])
  142. if (!categories || categories.length === 0) {
  143. return (
  144. <div className="flex h-full items-center">
  145. <Loading type="area" />
  146. </div>
  147. )
  148. }
  149. return (
  150. <div className={cn(
  151. 'flex flex-col',
  152. pageType === PageType.EXPLORE ? 'h-full border-l border-gray-200' : 'h-[calc(100%-56px)]',
  153. )}>
  154. {pageType === PageType.EXPLORE && (
  155. <div className='shrink-0 pt-6 px-12'>
  156. <div className={`mb-1 ${s.textGradient} text-xl font-semibold`}>{t('explore.apps.title')}</div>
  157. <div className='text-gray-500 text-sm'>{t('explore.apps.description')}</div>
  158. </div>
  159. )}
  160. <div className={cn(
  161. 'flex items-center justify-between mt-6',
  162. pageType === PageType.EXPLORE ? 'px-12' : 'px-8',
  163. )}>
  164. <>
  165. {pageType !== PageType.EXPLORE && (
  166. <>
  167. <AppTypeSelector value={currentType} onChange={setCurrentType}/>
  168. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'/>
  169. </>
  170. )}
  171. <Category
  172. list={categories}
  173. value={currCategory}
  174. onChange={setCurrCategory}
  175. allCategoriesEn={allCategoriesEn}
  176. />
  177. </>
  178. <Input
  179. showLeftIcon
  180. showClearIcon
  181. wrapperClassName='w-[200px]'
  182. value={keywords}
  183. onChange={e => handleKeywordsChange(e.target.value)}
  184. onClear={() => handleKeywordsChange('')}
  185. />
  186. </div>
  187. <div className={cn(
  188. 'relative flex flex-1 pb-6 flex-col overflow-auto bg-gray-100 shrink-0 grow',
  189. pageType === PageType.EXPLORE ? 'mt-4' : 'mt-0 pt-2',
  190. )}>
  191. <nav
  192. className={cn(
  193. s.appList,
  194. 'grid content-start shrink-0',
  195. pageType === PageType.EXPLORE ? 'gap-4 px-6 sm:px-12' : 'gap-3 px-8 sm:!grid-cols-2 md:!grid-cols-3 lg:!grid-cols-4',
  196. )}>
  197. {searchFilteredList.map(app => (
  198. <AppCard
  199. key={app.app_id}
  200. isExplore={pageType === PageType.EXPLORE}
  201. app={app}
  202. canCreate={hasEditPermission}
  203. onCreate={() => {
  204. setCurrApp(app)
  205. setIsShowCreateModal(true)
  206. }}
  207. />
  208. ))}
  209. </nav>
  210. </div>
  211. {isShowCreateModal && (
  212. <CreateAppModal
  213. appIconType={currApp?.app.icon_type || 'emoji'}
  214. appIcon={currApp?.app.icon || ''}
  215. appIconBackground={currApp?.app.icon_background || ''}
  216. appIconUrl={currApp?.app.icon_url}
  217. appName={currApp?.app.name || ''}
  218. appDescription={currApp?.app.description || ''}
  219. show={isShowCreateModal}
  220. onConfirm={onCreate}
  221. confirmDisabled={isFetching}
  222. onHide={() => setIsShowCreateModal(false)}
  223. />
  224. )}
  225. {
  226. showDSLConfirmModal && (
  227. <DSLConfirmModal
  228. versions={versions}
  229. onCancel={() => setShowDSLConfirmModal(false)}
  230. onConfirm={onConfirmDSL}
  231. confirmDisabled={isFetching}
  232. />
  233. )
  234. }
  235. </div>
  236. )
  237. }
  238. export default React.memo(Apps)