mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-08 05:39:13 +00:00
fix: review suggestions
This commit is contained in:
@@ -174,10 +174,9 @@ export default function Search() {
|
|||||||
const [focusSearchResult, setFocusSearchResult] = useState<SearchResult | null>(null);
|
const [focusSearchResult, setFocusSearchResult] = useState<SearchResult | null>(null);
|
||||||
const [exampleQuery, setExampleQuery] = useState("");
|
const [exampleQuery, setExampleQuery] = useState("");
|
||||||
const [fileSuggestions, setFileSuggestions] = useState<string[]>([]);
|
const [fileSuggestions, setFileSuggestions] = useState<string[]>([]);
|
||||||
|
const [allFiles, setAllFiles] = useState<string[]>([]);
|
||||||
const [showSuggestions, setShowSuggestions] = useState(false);
|
const [showSuggestions, setShowSuggestions] = useState(false);
|
||||||
const searchTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const searchTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
const suggestionsTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
||||||
|
|
||||||
const isMobileWidth = useIsMobileWidth();
|
const isMobileWidth = useIsMobileWidth();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -186,8 +185,68 @@ export default function Search() {
|
|||||||
Math.floor(Math.random() * naturalLanguageSearchQueryExamples.length)
|
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() {
|
function search() {
|
||||||
if (searchResultsLoading || !searchQuery.trim()) return;
|
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 (
|
return (
|
||||||
<SidebarProvider>
|
<SidebarProvider>
|
||||||
<AppSidebar conversationId={""} />
|
<AppSidebar conversationId={""} />
|
||||||
|
|||||||
@@ -757,19 +757,3 @@ def edit_job(
|
|||||||
|
|
||||||
# Return modified automation information as a JSON response
|
# Return modified automation information as a JSON response
|
||||||
return Response(content=json.dumps(automation_info), media_type="application/json", status_code=200)
|
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")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user