index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use client'
  2. import { useCallback, useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useParams } from 'next/navigation'
  5. import useSWRInfinite from 'swr/infinite'
  6. import { flatten } from 'lodash-es'
  7. import produce from 'immer'
  8. import Nav from '../nav'
  9. import { type NavItem } from '../nav/nav-selector'
  10. import { Robot, RobotActive } from '../../base/icons/src/public/header-nav/studio'
  11. import { fetchAppList } from '@/service/apps'
  12. import CreateAppTemplateDialog from '@/app/components/app/create-app-dialog'
  13. import CreateAppModal from '@/app/components/app/create-app-modal'
  14. import CreateFromDSLModal from '@/app/components/app/create-from-dsl-modal'
  15. import type { AppListResponse } from '@/models/app'
  16. import { useAppContext } from '@/context/app-context'
  17. import { useStore as useAppStore } from '@/app/components/app/store'
  18. const getKey = (
  19. pageIndex: number,
  20. previousPageData: AppListResponse,
  21. activeTab: string,
  22. keywords: string,
  23. ) => {
  24. if (!pageIndex || previousPageData.has_more) {
  25. const params: any = { url: 'apps', params: { page: pageIndex + 1, limit: 30, name: keywords } }
  26. if (activeTab !== 'all')
  27. params.params.mode = activeTab
  28. else
  29. delete params.params.mode
  30. return params
  31. }
  32. return null
  33. }
  34. const AppNav = () => {
  35. const { t } = useTranslation()
  36. const { appId } = useParams()
  37. const { isCurrentWorkspaceManager } = useAppContext()
  38. const { appDetail } = useAppStore()
  39. const [showNewAppDialog, setShowNewAppDialog] = useState(false)
  40. const [showNewAppTemplateDialog, setShowNewAppTemplateDialog] = useState(false)
  41. const [showCreateFromDSLModal, setShowCreateFromDSLModal] = useState(false)
  42. const [navItems, setNavItems] = useState<NavItem[]>([])
  43. const { data: appsData, setSize, mutate } = useSWRInfinite(
  44. appId
  45. ? (pageIndex: number, previousPageData: AppListResponse) => getKey(pageIndex, previousPageData, 'all', '')
  46. : () => null,
  47. fetchAppList,
  48. { revalidateFirstPage: false },
  49. )
  50. const handleLoadmore = useCallback(() => {
  51. setSize(size => size + 1)
  52. }, [setSize])
  53. const openModal = (state: string) => {
  54. if (state === 'blank')
  55. setShowNewAppDialog(true)
  56. if (state === 'template')
  57. setShowNewAppTemplateDialog(true)
  58. if (state === 'dsl')
  59. setShowCreateFromDSLModal(true)
  60. }
  61. useEffect(() => {
  62. if (appsData) {
  63. const appItems = flatten(appsData?.map(appData => appData.data))
  64. const navItems = appItems.map((app) => {
  65. const link = ((isCurrentWorkspaceManager, app) => {
  66. if (!isCurrentWorkspaceManager) {
  67. return `/app/${app.id}/overview`
  68. }
  69. else {
  70. if (app.mode === 'workflow' || app.mode === 'advanced-chat')
  71. return `/app/${app.id}/workflow`
  72. else
  73. return `/app/${app.id}/configuration`
  74. }
  75. })(isCurrentWorkspaceManager, app)
  76. return {
  77. id: app.id,
  78. icon: app.icon,
  79. icon_background: app.icon_background,
  80. name: app.name,
  81. mode: app.mode,
  82. link,
  83. }
  84. })
  85. setNavItems(navItems)
  86. }
  87. }, [appsData, isCurrentWorkspaceManager, setNavItems])
  88. // update current app name
  89. useEffect(() => {
  90. if (appDetail) {
  91. const newNavItems = produce(navItems, (draft: NavItem[]) => {
  92. navItems.forEach((app, index) => {
  93. if (app.id === appDetail.id)
  94. draft[index].name = appDetail.name
  95. })
  96. })
  97. setNavItems(newNavItems)
  98. }
  99. }, [appDetail, navItems])
  100. return (
  101. <>
  102. <Nav
  103. isApp
  104. icon={<Robot className='w-4 h-4' />}
  105. activeIcon={<RobotActive className='w-4 h-4' />}
  106. text={t('common.menus.apps')}
  107. activeSegment={['apps', 'app']}
  108. link='/apps'
  109. curNav={appDetail}
  110. navs={navItems}
  111. createText={t('common.menus.newApp')}
  112. onCreate={openModal}
  113. onLoadmore={handleLoadmore}
  114. />
  115. <CreateAppModal
  116. show={showNewAppDialog}
  117. onClose={() => setShowNewAppDialog(false)}
  118. onSuccess={() => mutate()}
  119. />
  120. <CreateAppTemplateDialog
  121. show={showNewAppTemplateDialog}
  122. onClose={() => setShowNewAppTemplateDialog(false)}
  123. onSuccess={() => mutate()}
  124. />
  125. <CreateFromDSLModal
  126. show={showCreateFromDSLModal}
  127. onClose={() => setShowCreateFromDSLModal(false)}
  128. onSuccess={() => mutate()}
  129. />
  130. </>
  131. )
  132. }
  133. export default AppNav