index.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use client'
  2. import React, { useMemo, useState } from 'react'
  3. import cn from 'classnames'
  4. import { useRouter } from 'next/navigation'
  5. import { useTranslation } from 'react-i18next'
  6. import { useContext } from 'use-context-selector'
  7. import useSWR from 'swr'
  8. import Toast from '../../base/toast'
  9. import s from './style.module.css'
  10. import ExploreContext from '@/context/explore-context'
  11. import type { App } from '@/models/explore'
  12. import Category from '@/app/components/explore/category'
  13. import AppCard from '@/app/components/explore/app-card'
  14. import { fetchAppDetail, fetchAppList } from '@/service/explore'
  15. import { importApp } from '@/service/apps'
  16. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  17. import CreateAppModal from '@/app/components/explore/create-app-modal'
  18. import AppTypeSelector from '@/app/components/app/type-selector'
  19. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  20. import Loading from '@/app/components/base/loading'
  21. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  22. import { useAppContext } from '@/context/app-context'
  23. import { getRedirection } from '@/utils/app-redirection'
  24. type AppsProps = {
  25. pageType?: PageType
  26. onSuccess?: () => void
  27. }
  28. export enum PageType {
  29. EXPLORE = 'explore',
  30. CREATE = 'create',
  31. }
  32. const Apps = ({
  33. pageType = PageType.EXPLORE,
  34. onSuccess,
  35. }: AppsProps) => {
  36. const { t } = useTranslation()
  37. const { isCurrentWorkspaceEditor } = useAppContext()
  38. const { push } = useRouter()
  39. const { hasEditPermission } = useContext(ExploreContext)
  40. const allCategoriesEn = t('explore.apps.allCategories', { lng: 'en' })
  41. const [currentType, setCurrentType] = useState<string>('')
  42. const [currCategory, setCurrCategory] = useTabSearchParams({
  43. defaultTab: allCategoriesEn,
  44. disableSearchParams: pageType !== PageType.EXPLORE,
  45. })
  46. const {
  47. data: { categories, allList },
  48. } = useSWR(
  49. ['/explore/apps'],
  50. () =>
  51. fetchAppList().then(({ categories, recommended_apps }) => ({
  52. categories,
  53. allList: recommended_apps.sort((a, b) => a.position - b.position),
  54. })),
  55. {
  56. fallbackData: {
  57. categories: [],
  58. allList: [],
  59. },
  60. },
  61. )
  62. const filteredList = useMemo(() => {
  63. if (currCategory === allCategoriesEn) {
  64. if (!currentType)
  65. return allList
  66. else if (currentType === 'chatbot')
  67. return allList.filter(item => (item.app.mode === 'chat' || item.app.mode === 'advanced-chat'))
  68. else if (currentType === 'agent')
  69. return allList.filter(item => (item.app.mode === 'agent-chat'))
  70. else
  71. return allList.filter(item => (item.app.mode === 'workflow'))
  72. }
  73. else {
  74. if (!currentType)
  75. return allList.filter(item => item.category === currCategory)
  76. else if (currentType === 'chatbot')
  77. return allList.filter(item => (item.app.mode === 'chat' || item.app.mode === 'advanced-chat') && item.category === currCategory)
  78. else if (currentType === 'agent')
  79. return allList.filter(item => (item.app.mode === 'agent-chat') && item.category === currCategory)
  80. else
  81. return allList.filter(item => (item.app.mode === 'workflow') && item.category === currCategory)
  82. }
  83. }, [currentType, currCategory, allCategoriesEn, allList])
  84. const [currApp, setCurrApp] = React.useState<App | null>(null)
  85. const [isShowCreateModal, setIsShowCreateModal] = React.useState(false)
  86. const onCreate: CreateAppModalProps['onConfirm'] = async ({
  87. name,
  88. icon,
  89. icon_background,
  90. description,
  91. }) => {
  92. const { export_data } = await fetchAppDetail(
  93. currApp?.app.id as string,
  94. )
  95. try {
  96. const app = await importApp({
  97. data: export_data,
  98. name,
  99. icon,
  100. icon_background,
  101. description,
  102. })
  103. setIsShowCreateModal(false)
  104. Toast.notify({
  105. type: 'success',
  106. message: t('app.newApp.appCreated'),
  107. })
  108. if (onSuccess)
  109. onSuccess()
  110. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  111. getRedirection(isCurrentWorkspaceEditor, app, push)
  112. }
  113. catch (e) {
  114. Toast.notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  115. }
  116. }
  117. if (!categories) {
  118. return (
  119. <div className="flex h-full items-center">
  120. <Loading type="area" />
  121. </div>
  122. )
  123. }
  124. return (
  125. <div className={cn(
  126. 'flex flex-col',
  127. pageType === PageType.EXPLORE ? 'h-full border-l border-gray-200' : 'h-[calc(100%-56px)]',
  128. )}>
  129. {pageType === PageType.EXPLORE && (
  130. <div className='shrink-0 pt-6 px-12'>
  131. <div className={`mb-1 ${s.textGradient} text-xl font-semibold`}>{t('explore.apps.title')}</div>
  132. <div className='text-gray-500 text-sm'>{t('explore.apps.description')}</div>
  133. </div>
  134. )}
  135. <div className={cn(
  136. 'flex items-center mt-6',
  137. pageType === PageType.EXPLORE ? 'px-12' : 'px-8',
  138. )}>
  139. {pageType !== PageType.EXPLORE && (
  140. <>
  141. <AppTypeSelector value={currentType} onChange={setCurrentType} />
  142. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'/>
  143. </>
  144. )}
  145. <Category
  146. list={categories}
  147. value={currCategory}
  148. onChange={setCurrCategory}
  149. allCategoriesEn={allCategoriesEn}
  150. />
  151. </div>
  152. <div className={cn(
  153. 'relative flex flex-1 pb-6 flex-col overflow-auto bg-gray-100 shrink-0 grow',
  154. pageType === PageType.EXPLORE ? 'mt-6' : 'mt-0 pt-2',
  155. )}>
  156. <nav
  157. className={cn(
  158. s.appList,
  159. 'grid content-start shrink-0',
  160. 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',
  161. )}>
  162. {filteredList.map(app => (
  163. <AppCard
  164. key={app.app_id}
  165. isExplore={pageType === PageType.EXPLORE}
  166. app={app}
  167. canCreate={hasEditPermission}
  168. onCreate={() => {
  169. setCurrApp(app)
  170. setIsShowCreateModal(true)
  171. }}
  172. />
  173. ))}
  174. </nav>
  175. </div>
  176. {isShowCreateModal && (
  177. <CreateAppModal
  178. appIcon={currApp?.app.icon || ''}
  179. appIconBackground={currApp?.app.icon_background || ''}
  180. appName={currApp?.app.name || ''}
  181. appDescription={currApp?.app.description || ''}
  182. show={isShowCreateModal}
  183. onConfirm={onCreate}
  184. onHide={() => setIsShowCreateModal(false)}
  185. />
  186. )}
  187. </div>
  188. )
  189. }
  190. export default React.memo(Apps)