index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 } 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 { 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 [currApp, setCurrApp] = React.useState<App | null>(null)
  44. const [isShowCreateModal, setIsShowCreateModal] = React.useState(false)
  45. const onCreate: CreateAppModalProps['onConfirm'] = async ({ name, icon, icon_background }) => {
  46. const { app_model_config: model_config } = await fetchAppDetail(currApp?.app.id as string)
  47. try {
  48. const app = await createApp({
  49. name,
  50. icon,
  51. icon_background,
  52. mode: currApp?.app.mode as AppMode,
  53. config: model_config,
  54. })
  55. setIsShowCreateModal(false)
  56. Toast.notify({
  57. type: 'success',
  58. message: t('app.newApp.appCreated'),
  59. })
  60. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  61. router.push(`/app/${app.id}/${isCurrentWorkspaceManager ? 'configuration' : 'overview'}`)
  62. }
  63. catch (e) {
  64. Toast.notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  65. }
  66. }
  67. if (!isLoaded) {
  68. return (
  69. <div className='flex h-full items-center'>
  70. <Loading type='area' />
  71. </div>
  72. )
  73. }
  74. return (
  75. <div className='h-full flex flex-col border-l border-gray-200'>
  76. <div className='shrink-0 pt-6 px-12'>
  77. <div className={`mb-1 ${s.textGradient} text-xl font-semibold`}>{t('explore.apps.title')}</div>
  78. <div className='text-gray-500 text-sm'>{t('explore.apps.description')}</div>
  79. </div>
  80. <Category
  81. className='mt-6 px-12'
  82. list={categories}
  83. value={currCategory}
  84. onChange={setCurrCategory}
  85. />
  86. <div className='relative flex flex-1 mt-6 pb-6 flex-col overflow-auto bg-gray-100 shrink-0 grow'>
  87. <nav
  88. className={`${s.appList} grid content-start gap-4 px-6 sm:px-12 shrink-0`}>
  89. {currList.map(app => (
  90. <AppCard
  91. key={app.app_id}
  92. app={app}
  93. canCreate={hasEditPermission}
  94. onCreate={() => {
  95. setCurrApp(app)
  96. setIsShowCreateModal(true)
  97. }}
  98. />
  99. ))}
  100. </nav>
  101. </div>
  102. {isShowCreateModal && (
  103. <CreateAppModal
  104. appName={currApp?.app.name || ''}
  105. show={isShowCreateModal}
  106. onConfirm={onCreate}
  107. onHide={() => setIsShowCreateModal(false)}
  108. />
  109. )}
  110. </div>
  111. )
  112. }
  113. export default React.memo(Apps)