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.

6.4 KB
Raw Blame
'use client'

import type { AgentReferencingWorkflowResponse } from '@dify/contracts/api/console/agent/types.gen'
import type { ReactNode } from 'react'
import { Button } from '@langgenius/dify-ui/button'
import { useQuery } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import AppIcon from '@/app/components/base/app-icon'
import useTimestamp from '@/hooks/use-timestamp'
import Link from '@/next/link'
import { consoleQuery } from '@/service/console'

type WorkflowReferencesTableProps = {
  agentId: string
  enabled?: boolean
}

const workflowTableColSpan = 5

const getWorkflowReferenceHref = (reference: AgentReferencingWorkflowResponse) =>
  `/app/${reference.app_id}/workflow`

export function WorkflowReferencesTable({ agentId, enabled = true }: WorkflowReferencesTableProps) {
  const { t } = useTranslation(['agentV2'])
  const { t: tCommon } = useTranslation(['common'])
  const workflowReferencesQuery = useQuery(
    consoleQuery.agent.byAgentId.referencingWorkflows.get.queryOptions({
      input: {
        params: {
          agent_id: agentId,
        },
      },
      enabled,
    }),
  )
  const workflowReferences = workflowReferencesQuery.data?.data ?? []

  return (
    <div className="min-w-0 overflow-x-auto">
      <table className="w-full min-w-303 table-fixed border-collapse">
        <colgroup>
          <col className="w-143" />
          <col className="w-40" />
          <col className="w-32" />
          <col className="w-51" />
          <col className="w-36" />
        </colgroup>
        <thead>
          <tr className="h-7 rounded-lg bg-background-section-burn text-start system-xs-semibold-uppercase text-text-tertiary">
            <th scope="col" className="rounded-s-lg px-3 text-start font-semibold">
              {t(($) => $['agentDetail.access.workflow.table.name'])}
            </th>
            <th scope="col" className="px-3 text-start font-semibold">
              {t(($) => $['agentDetail.access.workflow.table.version'])}
            </th>
            <th scope="col" className="px-3 text-start font-semibold">
              {t(($) => $['agentDetail.access.workflow.table.nodes'])}
            </th>
            <th scope="col" className="px-3 text-start font-semibold">
              {t(($) => $['agentDetail.access.workflow.table.lastUpdated'])}
            </th>
            <th scope="col" className="rounded-e-lg px-3 text-start font-semibold">
              {t(($) => $['agentDetail.access.workflow.table.actions'])}
            </th>
          </tr>
        </thead>
        <tbody className="system-sm-regular text-text-secondary">
          {enabled && workflowReferencesQuery.isPending && (
            <WorkflowAccessStateRow>
              {t(($) => $['agentDetail.access.workflow.loading'])}
            </WorkflowAccessStateRow>
          )}
          {enabled && workflowReferencesQuery.isError && (
            <WorkflowAccessStateRow>
              <div className="flex items-center justify-center gap-2">
                <span>{t(($) => $['agentDetail.access.workflow.loadFailed'])}</span>
                <Button
                  variant="secondary"
                  size="small"
                  onClick={() => {
                    void workflowReferencesQuery.refetch()
                  }}
                >
                  {tCommon(($) => $['operation.retry'])}
                </Button>
              </div>
            </WorkflowAccessStateRow>
          )}
          {enabled && workflowReferencesQuery.isSuccess && workflowReferences.length === 0 && (
            <WorkflowAccessStateRow>
              {t(($) => $['agentDetail.access.workflow.empty'])}
            </WorkflowAccessStateRow>
          )}
          {enabled &&
            workflowReferencesQuery.isSuccess &&
            workflowReferences.map((reference) => (
              <WorkflowAccessRow
                key={`${reference.app_id}:${reference.workflow_id}`}
                reference={reference}
              />
            ))}
        </tbody>
      </table>
    </div>
  )
}

function WorkflowAccessRow({ reference }: { reference: AgentReferencingWorkflowResponse }) {
  const { t } = useTranslation(['agentV2', 'agentRoster'])
  const { formatTime } = useTimestamp()
  const updatedAt =
    reference.app_updated_at != null
      ? formatTime(
          reference.app_updated_at,
          t(($) => $['roster.dateTimeFormat'], { ns: 'agentRoster' }),
        )
      : t(($) => $['agentDetail.access.workflow.notAvailable'])
  const nodeCount = reference.node_ids?.length ?? 0

  return (
    <tr className="h-10 border-b border-divider-subtle hover:bg-background-default-hover">
      <td className="px-3">
        <div className="flex min-w-0 items-center gap-2.5">
          <span aria-hidden className="shrink-0">
            <AppIcon
              size="small"
              shape="circle"
              icon={{
                icon_type: reference.app_icon_type,
                icon: reference.app_icon,
                icon_url: reference.app_icon,
                icon_background: reference.app_icon_background,
              }}
            />
          </span>
          <span className="truncate">{reference.app_name}</span>
        </div>
      </td>
      <td className="truncate px-3" translate="no">
        {reference.workflow_version}
      </td>
      <td className="px-3 tabular-nums">
        {t(($) => $['agentDetail.access.workflow.nodeCount'], { count: nodeCount })}
      </td>
      <td className="px-3 tabular-nums" translate="no">
        {updatedAt}
      </td>
      <td className="px-3">
        <Link
          href={getWorkflowReferenceHref(reference)}
          target="_blank"
          rel="noopener noreferrer"
          className="inline-flex items-center gap-0.5 rounded-sm text-text-secondary hover:text-text-accent hover:underline focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden"
        >
          {t(($) => $['agentDetail.access.workflow.openInStudio'])}
          <span className="sr-only">{reference.app_name}</span>
          <span aria-hidden className="i-ri-external-link-line size-4" />
        </Link>
      </td>
    </tr>
  )
}

function WorkflowAccessStateRow({ children }: { children: ReactNode }) {
  return (
    <tr className="h-20 border-b border-divider-subtle">
      <td
        colSpan={workflowTableColSpan}
        aria-live="polite"
        className="px-3 text-center text-text-tertiary"
      >
        {children}
      </td>
    </tr>
  )
}