Improve search of document picker (#2097)

improve search of document picker
This commit is contained in:
Sean Hatfield 2024-08-12 13:08:43 -07:00 committed by GitHub
parent 26959563e5
commit fce0d8b709
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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);
}