mirror of
https://github.com/langgenius/dify.git
synced 2026-05-13 08:57:28 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
139 lines
4.3 KiB
TypeScript
139 lines
4.3 KiB
TypeScript
'use client'
|
|
|
|
import type { NavItem } from '../nav/nav-selector'
|
|
import {
|
|
RiRobot2Fill,
|
|
RiRobot2Line,
|
|
} from '@remixicon/react'
|
|
import { flatten } from 'es-toolkit/compat'
|
|
import { produce } from 'immer'
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { useStore as useAppStore } from '@/app/components/app/store'
|
|
import { useAppContext } from '@/context/app-context'
|
|
import dynamic from '@/next/dynamic'
|
|
import { useParams } from '@/next/navigation'
|
|
import { useInfiniteAppList } from '@/service/use-apps'
|
|
import { AppModeEnum } from '@/types/app'
|
|
import Nav from '../nav'
|
|
|
|
const CreateAppTemplateDialog = dynamic(() => import('@/app/components/app/create-app-dialog'), { ssr: false })
|
|
const CreateAppModal = dynamic(() => import('@/app/components/app/create-app-modal'), { ssr: false })
|
|
const CreateFromDSLModal = dynamic(() => import('@/app/components/app/create-from-dsl-modal'), { ssr: false })
|
|
|
|
const AppNav = () => {
|
|
const { t } = useTranslation()
|
|
const { appId } = useParams()
|
|
const { isCurrentWorkspaceEditor } = useAppContext()
|
|
const appDetail = useAppStore(state => state.appDetail)
|
|
const [showNewAppDialog, setShowNewAppDialog] = useState(false)
|
|
const [showNewAppTemplateDialog, setShowNewAppTemplateDialog] = useState(false)
|
|
const [showCreateFromDSLModal, setShowCreateFromDSLModal] = useState(false)
|
|
const [navItems, setNavItems] = useState<NavItem[]>([])
|
|
|
|
const {
|
|
data: appsData,
|
|
fetchNextPage,
|
|
hasNextPage,
|
|
isFetchingNextPage,
|
|
refetch,
|
|
} = useInfiniteAppList({
|
|
page: 1,
|
|
limit: 30,
|
|
name: '',
|
|
}, { enabled: !!appId })
|
|
|
|
const handleLoadMore = useCallback(() => {
|
|
if (hasNextPage)
|
|
fetchNextPage()
|
|
}, [fetchNextPage, hasNextPage])
|
|
|
|
const openModal = (state: string) => {
|
|
if (state === 'blank')
|
|
setShowNewAppDialog(true)
|
|
if (state === 'template')
|
|
setShowNewAppTemplateDialog(true)
|
|
if (state === 'dsl')
|
|
setShowCreateFromDSLModal(true)
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (appsData) {
|
|
const appItems = flatten((appsData.pages ?? []).map(appData => appData.data))
|
|
const navItems = appItems.map((app) => {
|
|
const link = ((isCurrentWorkspaceEditor, app) => {
|
|
if (!isCurrentWorkspaceEditor) {
|
|
return `/app/${app.id}/overview`
|
|
}
|
|
else {
|
|
if (app.mode === AppModeEnum.WORKFLOW || app.mode === AppModeEnum.ADVANCED_CHAT)
|
|
return `/app/${app.id}/workflow`
|
|
else
|
|
return `/app/${app.id}/configuration`
|
|
}
|
|
})(isCurrentWorkspaceEditor, app)
|
|
return {
|
|
id: app.id,
|
|
icon_type: app.icon_type,
|
|
icon: app.icon,
|
|
icon_background: app.icon_background,
|
|
icon_url: app.icon_url,
|
|
name: app.name,
|
|
mode: app.mode,
|
|
link,
|
|
}
|
|
})
|
|
setNavItems(navItems as any)
|
|
}
|
|
}, [appsData, isCurrentWorkspaceEditor, setNavItems])
|
|
|
|
// update current app name
|
|
useEffect(() => {
|
|
if (appDetail) {
|
|
const newNavItems = produce(navItems, (draft: NavItem[]) => {
|
|
navItems.forEach((app, index) => {
|
|
if (app.id === appDetail.id)
|
|
draft[index]!.name = appDetail.name
|
|
})
|
|
})
|
|
setNavItems(newNavItems)
|
|
}
|
|
}, [appDetail, navItems])
|
|
|
|
return (
|
|
<>
|
|
<Nav
|
|
isApp
|
|
icon={<RiRobot2Line className="h-4 w-4" />}
|
|
activeIcon={<RiRobot2Fill className="h-4 w-4" />}
|
|
text={t('menus.apps', { ns: 'common' })}
|
|
activeSegment={['apps', 'app', 'snippets']}
|
|
link="/apps"
|
|
curNav={appDetail}
|
|
navigationItems={navItems}
|
|
createText={t('menus.newApp', { ns: 'common' })}
|
|
onCreate={openModal}
|
|
onLoadMore={handleLoadMore}
|
|
isLoadingMore={isFetchingNextPage}
|
|
/>
|
|
<CreateAppModal
|
|
show={showNewAppDialog}
|
|
onClose={() => setShowNewAppDialog(false)}
|
|
onSuccess={() => refetch()}
|
|
/>
|
|
<CreateAppTemplateDialog
|
|
show={showNewAppTemplateDialog}
|
|
onClose={() => setShowNewAppTemplateDialog(false)}
|
|
onSuccess={() => refetch()}
|
|
/>
|
|
<CreateFromDSLModal
|
|
show={showCreateFromDSLModal}
|
|
onClose={() => setShowCreateFromDSLModal(false)}
|
|
onSuccess={() => refetch()}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default AppNav
|