Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.6 KB
Raw Blame
'use client'

import type { ReactNode } from 'react'
import { Button } from '@langgenius/dify-ui/button'
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import { getAgentDefaultSection, getAgentSectionAccess } from '@/features/agent-v2/acl'
import { useAgentPermissions } from '@/features/agent-v2/permissions'
import useDocumentTitle from '@/hooks/use-document-title'
import { usePathname, useRouter } from '@/next/navigation'
import { isNotFoundError } from '@/service/http-error'

type AgentDetailLayoutProps = {
  agentId: string
  children: ReactNode
}

export function AgentDetailLayout({ agentId, children }: AgentDetailLayoutProps) {
  const { t } = useTranslation(['agentV2', 'agentRoster'])
  const pathname = usePathname()
  const router = useRouter()
  const { t: tCommon } = useTranslation(['common'])
  const { agentQuery, ...capabilities } = useAgentPermissions(agentId)
  const shouldRedirectToRoster = isNotFoundError(agentQuery.error)
  const section = pathname.endsWith('/access-config')
    ? 'access-config'
    : pathname.endsWith('/access')
      ? 'access'
      : pathname.endsWith('/logs')
        ? 'logs'
        : pathname.endsWith('/monitoring')
          ? 'monitoring'
          : 'configure'
  const sectionTitle = t(($) => $[`agentDetail.sections.${section}`])
  const agentTitle = agentQuery.data?.name ?? t(($) => $['agentDetail.documentTitle'])
  const canAccessSection = getAgentSectionAccess(capabilities)[section]
  const defaultSection = getAgentDefaultSection(capabilities)
  const redirectPath = shouldRedirectToRoster
    ? '/agents'
    : agentQuery.isSuccess && !canAccessSection
      ? defaultSection
        ? `/agents/${agentId}/${defaultSection}`
        : '/agents'
      : undefined

  useDocumentTitle(`${sectionTitle} · ${agentTitle}`)

  useEffect(() => {
    if (redirectPath) router.replace(redirectPath)
  }, [router, redirectPath])

  if (agentQuery.isPending) return <LoadingPlaceholder className="h-full" />
  if (redirectPath) return null
  if (agentQuery.isError) {
    return (
      <div role="alert" className="flex h-full items-center justify-center gap-3">
        <span>{t(($) => $['roster.loadingError'], { ns: 'agentRoster' })}</span>
        <Button onClick={() => void agentQuery.refetch()}>
          {tCommon(($) => $['operation.retry'])}
        </Button>
      </div>
    )
  }
  if (!canAccessSection) return null

  return (
    <div className="relative flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
      <div className="min-h-0 min-w-0 flex-1 overflow-auto">{children}</div>
    </div>
  )
}