"use client"; import React, { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Search } from 'lucide-react'; import { showAlert } from '@/components/ui/alert-system'; import SearchOptionsDialog from './SearchOptionsDialog'; import SearchResultCard from './SearchResultCard'; import { SearchResult, SearchOptions, Folder } from '@/components/types'; interface SearchSectionProps { apiBaseUrl: string; authToken: string | null; } const SearchSection: React.FC = ({ apiBaseUrl, authToken }) => { const [searchQuery, setSearchQuery] = useState(''); const [searchResults, setSearchResults] = useState([]); const [loading, setLoading] = useState(false); const [showSearchAdvanced, setShowSearchAdvanced] = useState(false); const [folders, setFolders] = useState([]); const [searchOptions, setSearchOptions] = useState({ filters: '{}', k: 4, min_score: 0, use_reranking: false, use_colpali: true }); // Update search options const updateSearchOption = (key: K, value: SearchOptions[K]) => { setSearchOptions(prev => ({ ...prev, [key]: value })); }; // Fetch folders and reset search results when auth token or API URL changes useEffect(() => { console.log('SearchSection: Token or API URL changed, resetting results'); setSearchResults([]); // Fetch available folders const fetchFolders = async () => { try { const response = await fetch(`${apiBaseUrl}/folders`, { headers: authToken ? { 'Authorization': `Bearer ${authToken}` } : {} }); if (response.ok) { const folderData = await response.json(); setFolders(folderData); } else { console.error('Failed to fetch folders', response.statusText); } } catch (error) { console.error('Error fetching folders:', error); } }; if (authToken || apiBaseUrl.includes('localhost')) { fetchFolders(); } }, [authToken, apiBaseUrl]); // Handle search const handleSearch = async () => { if (!searchQuery.trim()) { showAlert('Please enter a search query', { type: 'error', duration: 3000 }); return; } try { setLoading(true); const response = await fetch(`${apiBaseUrl}/retrieve/chunks`, { method: 'POST', headers: { 'Authorization': authToken ? `Bearer ${authToken}` : '', 'Content-Type': 'application/json' }, body: JSON.stringify({ query: searchQuery, filters: JSON.parse(searchOptions.filters || '{}'), k: searchOptions.k, min_score: searchOptions.min_score, use_reranking: searchOptions.use_reranking, use_colpali: searchOptions.use_colpali }) }); if (!response.ok) { throw new Error(`Search failed: ${response.statusText}`); } const data = await response.json(); setSearchResults(data); if (data.length === 0) { showAlert("No search results found for the query", { type: "info", duration: 3000 }); } } catch (err) { const errorMsg = err instanceof Error ? err.message : 'An unknown error occurred'; showAlert(errorMsg, { type: 'error', title: 'Search Failed', duration: 5000 }); } finally { setLoading(false); } }; return ( Search Documents Search across your documents to find relevant information.
setSearchQuery(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') handleSearch(); }} />
{searchResults.length > 0 ? (

Results ({searchResults.length})

{searchResults.map((result) => ( ))}
) : (

{searchQuery.trim() ? 'No results found. Try a different query.' : 'Enter a query to search your documents.'}

)}
); }; export default SearchSection;