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.

4.5 KB
Raw Blame
'use client'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Input } from '@langgenius/dify-ui/input'
import { RiAddLine, RiDeleteBinLine } from '@remixicon/react'
import * as React from 'react'
import { useTranslation } from 'react-i18next'
import { v4 as uuid } from 'uuid'

export type HeaderItem = {
  id: string
  key: string
  value: string
}

type Props = Readonly<{
  headersItems: HeaderItem[]
  onChange: (headerItems: HeaderItem[]) => void
  readonly?: boolean
  isMasked?: boolean
}>

const HeadersInput = ({ headersItems, onChange, readonly = false, isMasked = false }: Props) => {
  const { t } = useTranslation(['common', 'tools'])

  const handleItemChange = (index: number, field: 'key' | 'value', value: string) => {
    const newItems = [...headersItems]
    newItems[index] = { ...newItems[index]!, [field]: value }

    onChange(newItems)
  }

  const handleRemoveItem = (index: number) => {
    const newItems = headersItems.filter((_, i) => i !== index)

    onChange(newItems)
  }

  const handleAddItem = () => {
    const newItems = [...headersItems, { id: uuid(), key: '', value: '' }]

    onChange(newItems)
  }

  if (headersItems.length === 0) {
    return (
      <div className="space-y-2">
        <div className="body-xs-regular text-text-tertiary">
          {t(($) => $['mcp.modal.noHeaders'], { ns: 'tools' })}
        </div>
        {!readonly && (
          <Button variant="secondary" size="small" onClick={handleAddItem} className="w-full">
            <RiAddLine className="size-4" />
            {t(($) => $['mcp.modal.addHeader'], { ns: 'tools' })}
          </Button>
        )}
      </div>
    )
  }

  return (
    <div className="space-y-2">
      {isMasked && (
        <div className="body-xs-regular text-text-tertiary">
          {t(($) => $['mcp.modal.maskedHeadersTip'], { ns: 'tools' })}
        </div>
      )}
      <div className="overflow-hidden rounded-lg border border-divider-regular">
        <div className="bg-background-secondary flex h-7 items-center system-xs-medium-uppercase leading-7 text-text-tertiary">
          <div className="h-full w-1/2 border-e border-divider-regular ps-3">
            {t(($) => $['mcp.modal.headerKey'], { ns: 'tools' })}
          </div>
          <div className="h-full w-1/2 ps-3 pe-1">
            {t(($) => $['mcp.modal.headerValue'], { ns: 'tools' })}
          </div>
        </div>
        {headersItems.map((item, index) => (
          <div
            key={item.id}
            className={cn(
              'flex items-center border-divider-regular',
              index < headersItems.length - 1 && 'border-b',
            )}
          >
            <div className="w-1/2 border-e border-divider-regular">
              <Input
                aria-label={t(($) => $['mcp.modal.headerKey'], { ns: 'tools' })}
                value={item.key}
                onChange={(e) => handleItemChange(index, 'key', e.target.value)}
                placeholder={t(($) => $['mcp.modal.headerKeyPlaceholder'], { ns: 'tools' })}
                className="rounded-none border-0"
                readOnly={readonly}
              />
            </div>
            <div className="flex w-1/2 items-center">
              <Input
                aria-label={t(($) => $['mcp.modal.headerValue'], { ns: 'tools' })}
                value={item.value}
                onChange={(e) => handleItemChange(index, 'value', e.target.value)}
                placeholder={t(($) => $['mcp.modal.headerValuePlaceholder'], { ns: 'tools' })}
                className="flex-1 rounded-none border-0"
                readOnly={readonly}
              />
              {!readonly && !!headersItems.length && (
                <IconButton
                  aria-label={`${t(($) => $['operation.delete'], { ns: 'common' })} ${item.key.trim() || t(($) => $['mcp.modal.headerKey'], { ns: 'tools' })} ${index + 1}`}
                  onClick={() => handleRemoveItem(index)}
                  className="me-2"
                >
                  <RiDeleteBinLine aria-hidden className="size-4 text-text-destructive" />
                </IconButton>
              )}
            </div>
          </div>
        ))}
      </div>
      {!readonly && (
        <Button variant="secondary" size="small" onClick={handleAddItem} className="w-full">
          <RiAddLine className="size-4" />
          {t(($) => $['mcp.modal.addHeader'], { ns: 'tools' })}
        </Button>
      )}
    </div>
  )
}

export default React.memo(HeadersInput)