index.tsx 4.2 KB

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