diff --git a/frontend/src/components/Modals/ManageWorkspace/Documents/Directory/utils.js b/frontend/src/components/Modals/ManageWorkspace/Documents/Directory/utils.js index 3145408593103a7808637b6f3bd6b872fbe4ca4b..3cce544eaf52a868aa4d8ea5ca6d286261778570 100644 --- a/frontend/src/components/Modals/ManageWorkspace/Documents/Directory/utils.js +++ b/frontend/src/components/Modals/ManageWorkspace/Documents/Directory/utils.js @@ -1,6 +1,6 @@ import strDistance from "js-levenshtein"; -const LEVENSHTEIN_MIN = 8; +const LEVENSHTEIN_MIN = 2; // Regular expression pattern to match the v4 UUID and the ending .json const uuidPattern = @@ -18,20 +18,33 @@ export const stripUuidAndJsonFromString = (input = "") => { export function filterFileSearchResults(files = [], searchTerm = "") { if (!searchTerm) return files?.items || []; + const normalizedSearchTerm = searchTerm.toLowerCase().trim(); + const searchResult = []; for (const folder of files?.items) { - // If folder is a good match then add all its children - if (strDistance(folder.name, searchTerm) <= LEVENSHTEIN_MIN) { + const folderNameNormalized = folder.name.toLowerCase(); + + // Check for exact match first, then fuzzy match + if (folderNameNormalized.includes(normalizedSearchTerm)) { searchResult.push(folder); continue; } - // Otherwise check children for good results + // Check children for matches const fileSearchResults = []; for (const file of folder?.items) { - if ( - strDistance(stripUuidAndJsonFromString(file.name), searchTerm) <= - LEVENSHTEIN_MIN + const fileNameNormalized = stripUuidAndJsonFromString( + file.name + ).toLowerCase(); + + // Exact match check + if (fileNameNormalized.includes(normalizedSearchTerm)) { + fileSearchResults.push(file); + } + // Fuzzy match only if no exact matches found + else if ( + fileSearchResults.length === 0 && + strDistance(fileNameNormalized, normalizedSearchTerm) <= LEVENSHTEIN_MIN ) { fileSearchResults.push(file); }