From fce0d8b709db35f086e0c96dd36d8f1c7baa7fc5 Mon Sep 17 00:00:00 2001 From: Sean Hatfield Date: Mon, 12 Aug 2024 13:08:43 -0700 Subject: [PATCH] Improve search of document picker (#2097) improve search of document picker --- .../Documents/Directory/utils.js | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/Modals/ManageWorkspace/Documents/Directory/utils.js b/frontend/src/components/Modals/ManageWorkspace/Documents/Directory/utils.js index 31454085..3cce544e 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); }