index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { useTranslation } from 'react-i18next'
  6. import { useContext } from 'use-context-selector'
  7. import Toast from '../../base/toast'
  8. import s from './style.module.css'
  9. import ExploreContext from '@/context/explore-context'
  10. import type { App, AppCategory } from '@/models/explore'
  11. import Category from '@/app/components/explore/category'
  12. import AppCard from '@/app/components/explore/app-card'
  13. import { fetchAppDetail, fetchAppList, installApp } from '@/service/explore'
  14. import { createApp } from '@/service/apps'
  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 { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  19. import { type AppMode } from '@/types/app'
  20. import { useAppContext } from '@/context/app-context'
  21. const Apps: FC = () => {
  22. const { t } = useTranslation()
  23. const { isCurrentWorkspaceManager } = useAppContext()
  24. const router = useRouter()
  25. const { setControlUpdateInstalledApps, hasEditPermission } = useContext(ExploreContext)
  26. const [currCategory, setCurrCategory] = React.useState<AppCategory | ''>('')
  27. const [allList, setAllList] = React.useState<App[]>([])
  28. const [isLoaded, setIsLoaded] = React.useState(false)
  29. const currList = (() => {
  30. if (currCategory === '')
  31. return allList
  32. return allList.filter(item => item.category === currCategory)
  33. })()
  34. const [categories, setCategories] = React.useState<AppCategory[]>([])
  35. useEffect(() => {
  36. (async () => {
  37. const { categories, recommended_apps }: any = await fetchAppList()
  38. setCategories(categories)
  39. setAllList(recommended_apps)
  40. setIsLoaded(true)
  41. })()
  42. }, [])
  43. const handleAddToWorkspace = async (appId: string) => {
  44. await installApp(appId)
  45. Toast.notify({
  46. type: 'success',
  47. message: t('common.api.success'),
  48. })
  49. setControlUpdateInstalledApps(Date.now())
  50. }
  51. const [currApp, setCurrApp] = React.useState<App | null>(null)
  52. const [isShowCreateModal, setIsShowCreateModal] = React.useState(false)
  53. const onCreate: CreateAppModalProps['onConfirm'] = async ({ name, icon, icon_background }) => {
  54. const { app_model_config: model_config } = await fetchAppDetail(currApp?.app.id as string)
  55. try {
  56. const app = await createApp({
  57. name,
  58. icon,
  59. icon_background,
  60. mode: currApp?.app.mode as AppMode,
  61. config: model_config,
  62. })
  63. setIsShowCreateModal(false)
  64. Toast.notify({
  65. type: 'success',
  66. message: t('app.newApp.appCreated'),
  67. })
  68. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  69. router.push(`/app/${app.id}/${isCurrentWorkspaceManager ? 'configuration' : 'overview'}`)
  70. }
  71. catch (e) {
  72. Toast.notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  73. }
  74. }
  75. if (!isLoaded) {
  76. return (
  77. <div className='flex h-full items-center'>
  78. <Loading type='area' />
  79. </div>
  80. )
  81. }
  82. return (
  83. <div className='h-full flex flex-col border-l border-gray-200'>
  84. <div className='shrink-0 pt-6 px-12'>
  85. <div className={`mb-1 ${s.textGradient} text-xl font-semibold`}>{t('explore.apps.title')}</div>
  86. <div className='text-gray-500 text-sm'>{t('explore.apps.description')}</div>
  87. </div>
  88. <Category
  89. className='mt-6 px-12'
  90. list={categories}
  91. value={currCategory}
  92. onChange={setCurrCategory}
  93. />
  94. <div className='relative flex flex-1 mt-6 pb-6 flex-col overflow-auto bg-gray-100 shrink-0 grow'>
  95. <nav
  96. className={`${s.appList} grid content-start gap-4 px-6 sm:px-12 shrink-0`}>
  97. {currList.map(app => (
  98. <AppCard
  99. key={app.app_id}
  100. app={app}
  101. canCreate={hasEditPermission}
  102. onCreate={() => {
  103. setCurrApp(app)
  104. setIsShowCreateModal(true)
  105. }}
  106. onAddToWorkspace={handleAddToWorkspace}
  107. />
  108. ))}
  109. </nav>
  110. </div>
  111. {isShowCreateModal && (
  112. <CreateAppModal
  113. appName={currApp?.app.name || ''}
  114. show={isShowCreateModal}
  115. onConfirm={onCreate}
  116. onHide={() => setIsShowCreateModal(false)}
  117. />
  118. )}
  119. </div>
  120. )
  121. }
  122. export default React.memo(Apps)