index.tsx 4.5 KB

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