index.tsx 4.2 KB

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