import { useEffect, useRef, useState, type SyntheticEvent } from 'react' import '../ui/Modal.scss' import { type PagedSearchResponse, type ServiceItem, type SearchRequest, type SyncRequest } from '../../types/search' import { syncVectorData, search } from '../../api/searches' import type { ServiceType } from '../../types/connection' import { uiText } from '../../text/uiText' interface Props { serviceType: ServiceType label: string appUrl: string username: string onClose: () => void } export function SearchModal({ serviceType, label, appUrl, username, onClose }: Readonly) { const [query, setQuery] = useState('') const [results, setResults] = useState | null>(null) const [loading, setLoading] = useState(false) const [searchError, setSearchError] = useState(null) const [syncError, setSyncError] = useState(null) //TODO: implement aiSearch const [aiSearch, setAiSearch] = useState(false) const firstInputRef = useRef(null) const dialogRef = useRef(null) useEffect(() => { const dialog = dialogRef.current if (!dialog) return dialog.showModal() firstInputRef.current?.focus() const handleCancel = (e: Event) => { e.preventDefault() onClose() } dialog.addEventListener('cancel', handleCancel) return () => dialog.removeEventListener('cancel', handleCancel) }, [onClose]) const handleSubmit = async (e: SyntheticEvent) => { e.preventDefault() setSearchError(null) setLoading(true) try { const req: SearchRequest = { appUrl, serviceType, username, query, aiSearch} const res = await search(req) setResults(res) } catch (err) { setSearchError(err instanceof Error ? err.message : uiText.search.searchFailed) } finally { setLoading(false) } } const handleSync = async () => { setSyncError(null) setLoading(true) try { const req: SyncRequest = { appUrl, serviceType, username} await syncVectorData(req) } catch (err) { setSyncError(err instanceof Error ? err.message: uiText.search.syncFailed) } finally { setLoading(false) } } return (
setQuery(e.target.value)} />
{searchError &&

{searchError}

} {syncError &&

{syncError}

}
{results && (

{results.totalElements}

{results.content.map((item) => (

{item.title}

{item.description &&

{item.description}

}
))}
)}
) }