import React, { useEffect, useState } from 'react' import { RiAddLine, RiArrowDownSLine, } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { useRouter } from 'next/navigation' import { ApiConnectionMod } from '@/app/components/base/icons/src/vender/solid/development' import { useModalContext } from '@/context/modal-context' import { useExternalKnowledgeApi } from '@/context/external-knowledge-api-context' type ApiItem = { value: string name: string url: string } type ExternalApiSelectProps = { items: ApiItem[] value?: string onSelect: (item: ApiItem) => void } const ExternalApiSelect: React.FC = ({ items, value, onSelect }) => { const { t } = useTranslation() const [isOpen, setIsOpen] = useState(false) const [selectedItem, setSelectedItem] = useState( items.find(item => item.value === value) || null, ) const { setShowExternalKnowledgeAPIModal } = useModalContext() const { mutateExternalKnowledgeApis } = useExternalKnowledgeApi() const router = useRouter() useEffect(() => { const newSelectedItem = items.find(item => item.value === value) || null setSelectedItem(newSelectedItem) }, [value, items]) const handleAddNewAPI = () => { setShowExternalKnowledgeAPIModal({ payload: { name: '', settings: { endpoint: '', api_key: '' } }, onSaveCallback: async () => { mutateExternalKnowledgeApis() router.refresh() }, onCancelCallback: () => { mutateExternalKnowledgeApis() }, isEditMode: false, }) } const handleSelect = (item: ApiItem) => { setSelectedItem(item) onSelect(item) setIsOpen(false) } return (
setIsOpen(!isOpen)} > {selectedItem ? (
{selectedItem.name}
) : ( {t('dataset.selectExternalKnowledgeAPI.placeholder')} )}
{isOpen && (
{items.map(item => (
handleSelect(item)} >
{item.name} {item.url}
))}
{t('dataset.createNewExternalAPI')}
)}
) } export default ExternalApiSelect