From 27165b3f4a956184cf8a99ec169e4bb14b6a2f27 Mon Sep 17 00:00:00 2001 From: Yash-1511 Date: Sun, 12 Jan 2025 15:12:14 +0530 Subject: [PATCH] fix: review suggestions --- src/interface/web/app/search/page.tsx | 120 +++++++++++++------------- src/khoj/routers/api.py | 16 ---- 2 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/interface/web/app/search/page.tsx b/src/interface/web/app/search/page.tsx index 1c099a7a..902b9631 100644 --- a/src/interface/web/app/search/page.tsx +++ b/src/interface/web/app/search/page.tsx @@ -174,10 +174,9 @@ export default function Search() { const [focusSearchResult, setFocusSearchResult] = useState(null); const [exampleQuery, setExampleQuery] = useState(""); const [fileSuggestions, setFileSuggestions] = useState([]); + const [allFiles, setAllFiles] = useState([]); const [showSuggestions, setShowSuggestions] = useState(false); const searchTimeoutRef = useRef(null); - const suggestionsTimeoutRef = useRef(null); - const isMobileWidth = useIsMobileWidth(); useEffect(() => { @@ -186,8 +185,68 @@ export default function Search() { Math.floor(Math.random() * naturalLanguageSearchQueryExamples.length) ], ); + + // Load all files once on page load + fetch('/api/content/computer', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }) + .then(response => response.json()) + .then(data => { + setAllFiles(data); + }) + .catch(error => { + console.error('Error loading files:', error); + }); }, []); + function getFileSuggestions(query: string) { + const fileFilterMatch = query.match(/file:([^"\s]*|"[^"]*")?/); + if (!fileFilterMatch) { + setFileSuggestions([]); + setShowSuggestions(false); + return; + } + + const filePrefix = fileFilterMatch[1]?.replace(/^"|"$/g, '').trim() || ''; + const filteredSuggestions = allFiles + .filter(file => file.toLowerCase().includes(filePrefix.toLowerCase())) + .sort() + .slice(0, 10); + + setFileSuggestions(filteredSuggestions); + setShowSuggestions(true); + } + + function handleSearchInputChange(value: string) { + setSearchQuery(value); + + // Clear previous search timeout + if (searchTimeoutRef.current) { + clearTimeout(searchTimeoutRef.current); + } + + // Get file suggestions immediately + getFileSuggestions(value); + + // Debounce search + if (value.trim()) { + searchTimeoutRef.current = setTimeout(() => { + search(); + }, 750); + } + } + + function applySuggestion(suggestion: string) { + // Replace the file: filter with the selected suggestion + const newQuery = searchQuery.replace(/file:([^"\s]*|"[^"]*")?/, `file:"${suggestion}"`); + setSearchQuery(newQuery); + setShowSuggestions(false); + search(); + } + function search() { if (searchResultsLoading || !searchQuery.trim()) return; @@ -208,63 +267,6 @@ export default function Search() { }); } - function getFileSuggestions(query: string) { - // Get suggestions only if query starts with "file:" - if (!query.toLowerCase().startsWith("file:")) { - setFileSuggestions([]); - setShowSuggestions(false); - return; - } - - const filePrefix = query.substring(5).trim(); // Remove "file:" prefix - const apiUrl = `/api/file-suggestions?q=${encodeURIComponent(filePrefix)}`; - - fetch(apiUrl, { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - }) - .then((response) => response.json()) - .then((data) => { - setFileSuggestions(data); - setShowSuggestions(true); - }) - .catch((error) => { - console.error("Error:", error); - }); - } - - function handleSearchInputChange(value: string) { - setSearchQuery(value); - - // Clear previous timeouts - if (searchTimeoutRef.current) { - clearTimeout(searchTimeoutRef.current); - } - if (suggestionsTimeoutRef.current) { - clearTimeout(suggestionsTimeoutRef.current); - } - - // Get file suggestions immediately - suggestionsTimeoutRef.current = setTimeout(() => { - getFileSuggestions(value); - }, 100); - - // Debounce search - if (value.trim()) { - searchTimeoutRef.current = setTimeout(() => { - search(); - }, 750); - } - } - - function applySuggestion(suggestion: string) { - setSearchQuery(`file:${suggestion}`); - setShowSuggestions(false); - search(); - } - return ( diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index d5cb060d..a29e993a 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -757,19 +757,3 @@ def edit_job( # Return modified automation information as a JSON response return Response(content=json.dumps(automation_info), media_type="application/json", status_code=200) - - -@api.get("/file-suggestions", response_class=Response) -@requires(["authenticated"]) -def get_file_suggestions(request: Request, q: str = ""): - """Get file suggestions for autocompletion based on the query prefix.""" - user = request.user.object - file_list = EntryAdapters.get_all_filenames_by_source(user, "computer") - - # Filter files based on query prefix - suggestions = [f for f in file_list if f.lower().startswith(q.lower())] - - # Sort suggestions alphabetically and limit to top 10 - suggestions = sorted(suggestions)[:10] - - return Response(content=json.dumps(suggestions), media_type="application/json")