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.

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