import React, { useState } from 'react'; import Trash from '../assets/trash.svg'; import Arrow2 from '../assets/dropdown-arrow.svg'; import { Doc, ActiveState } from '../models/misc'; import { useDispatch } from 'react-redux'; import { useTranslation } from 'react-i18next'; import ConfirmationModal from '../modals/ConfirmationModal'; type Props = { options: Doc[] | null; selectedDocs: Doc | null; setSelectedDocs: any; isDocsListOpen: boolean; setIsDocsListOpen: React.Dispatch>; handleDeleteClick: any; handlePostDocumentSelect: any; }; function SourceDropdown({ options, setSelectedDocs, selectedDocs, setIsDocsListOpen, isDocsListOpen, handleDeleteClick, handlePostDocumentSelect, // Callback function fired after a document is selected }: Props) { const dispatch = useDispatch(); const { t } = useTranslation(); const dropdownRef = React.useRef(null); const embeddingsName = import.meta.env.VITE_EMBEDDINGS_NAME || 'huggingface_sentence-transformers/all-mpnet-base-v2'; const [deleteModalState, setDeleteModalState] = useState('INACTIVE'); const [documentToDelete, setDocumentToDelete] = useState(null); const handleEmptyDocumentSelect = () => { dispatch(setSelectedDocs(null)); setIsDocsListOpen(false); }; const handleClickOutside = (event: MouseEvent) => { if ( dropdownRef.current && !dropdownRef.current.contains(event.target as Node) ) { setIsDocsListOpen(false); } }; React.useEffect(() => { document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); const confirmDelete = (option: Doc) => { setDocumentToDelete(option); setDeleteModalState('ACTIVE'); }; const handleConfirmedDelete = () => { if (documentToDelete) { handleDeleteClick(documentToDelete); setDeleteModalState('INACTIVE'); setDocumentToDelete(null); } }; const handleCancelDelete = () => { setDeleteModalState('INACTIVE'); setDocumentToDelete(null); }; return (
{isDocsListOpen && (
{options ? ( options.map((option: any, index: number) => { if (option.model === embeddingsName) { return (
{ dispatch(setSelectedDocs(option)); setIsDocsListOpen(false); handlePostDocumentSelect(option); }} > { setIsDocsListOpen(false); }} className="ml-4 flex-1 overflow-hidden py-3 text-ellipsis whitespace-nowrap" > {option.name} {option.location === 'local' && ( Delete { event.stopPropagation(); confirmDelete(option); }} /> )}
); } }) ) : ( <> )}
{ handlePostDocumentSelect(null); }} > {t('none')}
)}
); } export default SourceDropdown;