'use client' import React from 'react' import { useTranslation } from 'react-i18next' import { v4 as uuid } from 'uuid' import { RiAddLine, RiDeleteBinLine } from '@remixicon/react' import Input from '@/app/components/base/input' import Button from '@/app/components/base/button' import ActionButton from '@/app/components/base/action-button' import cn from '@/utils/classnames' export type HeaderItem = { id: string key: string value: string } type Props = { headersItems: HeaderItem[] onChange: (headerItems: HeaderItem[]) => void readonly?: boolean isMasked?: boolean } const HeadersInput = ({ headersItems, onChange, readonly = false, isMasked = false, }: Props) => { const { t } = useTranslation() 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 (
{t('tools.mcp.modal.noHeaders')}
{!readonly && ( )}
) } return (
{isMasked && (
{t('tools.mcp.modal.maskedHeadersTip')}
)}
{t('tools.mcp.modal.headerKey')}
{t('tools.mcp.modal.headerValue')}
{headersItems.map((item, index) => (
handleItemChange(index, 'key', e.target.value)} placeholder={t('tools.mcp.modal.headerKeyPlaceholder')} className='rounded-none border-0' readOnly={readonly} />
handleItemChange(index, 'value', e.target.value)} placeholder={t('tools.mcp.modal.headerValuePlaceholder')} className='flex-1 rounded-none border-0' readOnly={readonly} /> {!readonly && !!headersItems.length && ( handleRemoveItem(index)} className='mr-2' > )}
))}
{!readonly && ( )}
) } export default React.memo(HeadersInput)