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.

9.2 KB
Raw Blame
import type { MouseEvent, ReactNode } from 'react'
import { cn } from '@langgenius/dify-ui/cn'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
import {
  SegmentedControl,
  SegmentedControlDivider,
  SegmentedControlItem,
} from '@langgenius/dify-ui/segmented-control'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'
import { useTranslation } from 'react-i18next'
import { useDocLink } from '@/context/i18n'
import { AgentConfigureClearSessionConfirmDialog } from '../confirm-clear-session-dialog'

type AgentConfigureRightPanelMode = 'build' | 'preview'

function AgentModeTipSection({
  iconClassName,
  title,
  children,
}: {
  iconClassName: string
  title: string
  children: ReactNode
}) {
  return (
    <div className="flex flex-col gap-1">
      <div className="flex items-start gap-1.5">
        <span aria-hidden className={cn('size-4 shrink-0 text-text-primary', iconClassName)} />
        <div className="system-sm-medium-uppercase text-text-primary">{title}</div>
      </div>
      <div className="system-xs-regular text-text-secondary">{children}</div>
    </div>
  )
}

function ModeInfoTip({ children, ariaLabel }: { children: ReactNode; ariaLabel: string }) {
  const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
    event.stopPropagation()
  }

  return (
    <Popover>
      <PopoverTrigger
        openOnHover
        delay={300}
        closeDelay={200}
        aria-label={ariaLabel}
        onClick={handleClick}
        className="inline-flex size-5 shrink-0 cursor-pointer items-center justify-center rounded-md border-0 bg-transparent p-0"
      >
        <span
          aria-hidden
          className="i-ri-question-line size-4 text-text-tertiary hover:text-text-secondary"
        />
      </PopoverTrigger>
      <PopoverContent
        sideOffset={2}
        className="w-60 max-w-60 rounded-xl bg-components-tooltip-bg px-4 py-3.5 text-start text-text-secondary backdrop-blur-[5px]"
      >
        {children}
      </PopoverContent>
    </Popover>
  )
}

function PreviewModeItem({
  previewEnabled,
  label,
  disabledTip,
  children,
}: {
  previewEnabled: boolean
  label: string
  disabledTip: string
  children: ReactNode
}) {
  const item = (
    <SegmentedControlItem<AgentConfigureRightPanelMode>
      value="preview"
      disabled={!previewEnabled}
      className="uppercase"
    >
      {children}
    </SegmentedControlItem>
  )

  if (previewEnabled) return item

  return (
    <Popover>
      <PopoverTrigger
        openOnHover
        nativeButton={false}
        aria-label={`${label}. ${disabledTip}`}
        render={<span className="inline-flex" />}
      >
        {item}
      </PopoverTrigger>
      <PopoverContent
        sideOffset={6}
        className="max-w-65 rounded-md bg-components-tooltip-bg px-3 py-2 system-xs-regular text-text-tertiary shadow-lg"
      >
        {disabledTip}
      </PopoverContent>
    </Popover>
  )
}

export function AgentPreviewHeader({
  mode,
  previewEnabled,
  chatFeatures,
  buildEnabled = true,
  onModeChange,
  onOpenWorkingDirectory,
  onRefresh,
  refreshDisabled,
  showWorkingDirectoryAction = false,
}: {
  mode: AgentConfigureRightPanelMode
  previewEnabled: boolean
  chatFeatures?: { open: boolean; onToggle: () => void }
  buildEnabled?: boolean
  onModeChange: (mode: AgentConfigureRightPanelMode) => void
  onOpenWorkingDirectory: () => void
  onRefresh: () => void
  refreshDisabled?: boolean
  showWorkingDirectoryAction?: boolean
}) {
  const { t } = useTranslation(['agentV2'])
  const docLink = useDocLink()
  const buildLabel = t(($) => $['agentDetail.configure.rightPanel.build'])
  const buildTipBody = t(($) => $['agentDetail.configure.rightPanel.buildTipBody'])
  const previewLabel = t(($) => $['agentDetail.configure.rightPanel.preview'])
  const previewTipBody = t(($) => $['agentDetail.configure.rightPanel.previewTipBody'])
  const previewDisabledTip = t(($) => $['agentDetail.configure.rightPanel.previewDisabledTip'])
  const learnMoreLabel = t(($) => $['agentDetail.configure.rightPanel.learnMore'])
  const restartLabel = t(($) => $['agentDetail.configure.preview.restart'])
  const modeTip = `${buildLabel}. ${buildTipBody} ${learnMoreLabel} ${previewLabel}. ${previewTipBody}`
  const restartButton = (
    <IconButton
      aria-label={restartLabel}
      disabled={refreshDisabled}
      onClick={mode === 'preview' ? onRefresh : undefined}
    >
      <span aria-hidden className="i-custom-vender-other-replay-line size-4" />
    </IconButton>
  )

  return (
    <div className="relative z-1 flex h-12 shrink-0 items-center justify-between gap-3 px-4 py-2 pr-(--agent-preview-header-end,1rem) max-sm:h-auto max-sm:flex-wrap max-sm:gap-1 max-sm:px-2 max-sm:pr-(--agent-preview-header-end,0.5rem)">
      <div className="flex min-w-0 flex-1 items-center gap-2 max-sm:w-full max-sm:flex-none">
        <SegmentedControl<AgentConfigureRightPanelMode>
          value={mode}
          onValueChange={(value) => onModeChange(value)}
          aria-label={t(($) => $['agentDetail.configure.rightPanel.modeLabel'])}
        >
          <SegmentedControlItem<AgentConfigureRightPanelMode>
            value="build"
            disabled={!buildEnabled}
            className="uppercase"
          >
            <span aria-hidden className="i-custom-vender-agent-v2-configure-build size-4" />
            {t(($) => $['agentDetail.configure.rightPanel.build'])}
          </SegmentedControlItem>
          <PreviewModeItem
            previewEnabled={previewEnabled}
            label={previewLabel}
            disabledTip={previewDisabledTip}
          >
            <span aria-hidden className="i-custom-vender-agent-v2-configure-preview size-4" />
            {t(($) => $['agentDetail.configure.rightPanel.preview'])}
          </PreviewModeItem>
        </SegmentedControl>
        <ModeInfoTip ariaLabel={modeTip}>
          <div className="flex flex-col gap-2">
            <div className="flex flex-col gap-3">
              <AgentModeTipSection
                iconClassName="i-custom-vender-agent-v2-configure-build"
                title={buildLabel}
              >
                <>
                  {buildTipBody}{' '}
                  <a
                    href={docLink('/use-dify/build/new-agent/build#build-by-chatting')}
                    target="_blank"
                    rel="noopener noreferrer"
                    className="body-xs-regular text-text-accent hover:underline"
                  >
                    {learnMoreLabel}
                  </a>
                </>
              </AgentModeTipSection>
              <AgentModeTipSection
                iconClassName="i-custom-vender-agent-v2-configure-preview"
                title={previewLabel}
              >
                {previewTipBody}
              </AgentModeTipSection>
            </div>
          </div>
        </ModeInfoTip>
      </div>
      <div className="flex shrink-0 items-center max-sm:w-full max-sm:flex-wrap max-sm:justify-end">
        <div className="flex items-center gap-2 max-sm:flex-wrap">
          <Tooltip>
            <TooltipTrigger
              render={
                <span className="inline-flex">
                  {mode === 'preview' ? (
                    restartButton
                  ) : (
                    <AgentConfigureClearSessionConfirmDialog onConfirm={onRefresh}>
                      {restartButton}
                    </AgentConfigureClearSessionConfirmDialog>
                  )}
                </span>
              }
            />
            <TooltipContent>{restartLabel}</TooltipContent>
          </Tooltip>
          {mode === 'build' && showWorkingDirectoryAction && (
            <button
              type="button"
              onClick={onOpenWorkingDirectory}
              className="flex h-8 items-center justify-center gap-0.5 rounded-lg px-3 py-2 text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden"
            >
              <span aria-hidden className="i-ri-folder-3-line size-4" />
              <span className="px-0.5 system-sm-medium">
                {t(($) => $['agentDetail.configure.workingDirectory.fileSystem'])}
              </span>
            </button>
          )}
        </div>
        {chatFeatures && (
          <>
            <SegmentedControlDivider className="mx-3 max-sm:hidden" />
            <button
              type="button"
              aria-expanded={chatFeatures.open}
              onClick={chatFeatures.onToggle}
              className={cn(
                'flex h-8 items-center justify-center gap-1 rounded-lg px-2 py-2 text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden',
                chatFeatures.open && 'bg-state-base-hover text-text-secondary',
              )}
            >
              <span aria-hidden className="i-ri-chat-settings-line size-4" />
              <span className="px-0.5 system-sm-medium">
                {t(($) => $['agentDetail.configure.preview.chatFeatures'])}
              </span>
            </button>
          </>
        )}
      </div>
    </div>
  )
}