From 0069462034ba6022ff241358691166db87c533bd Mon Sep 17 00:00:00 2001 From: Marlon Erler <109803887+marlon-erler@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:48:04 +0000 Subject: [PATCH] fix search --- dist/index.js | 32 ++++++++++++++++------------ src/Views/Objects/objectPane.tsx | 36 ++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0740784a..8518ae39 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2096,39 +2096,45 @@ } function applyFilter() { appliedFilter.value = filterInput.value; - const allObjects = [...chat.objects.value.entries()]; - const matchingObjects = allObjects.filter((entry) => { - return checkMatchesFilter(entry[1]); + const allObjects = [...chat.objects.value.values()]; + allObjects.forEach((object, i) => { + const doesMatch = checkIfMatchesFilter(object); + if (doesMatch) { + showingMessageObjects.set(object.id, object); + } else { + showingMessageObjects.remove(object.id); + } }); - resultCount.value = matchingObjects.length; - showingMessageObjects.clear(); - matchingObjects.forEach((entry) => showingMessageObjects.set(...entry)); + resultCount.value = showingMessageObjects.value.size; } chat.objects.handleAddition((messageObject) => { - if (checkMatchesFilter(messageObject) == false) return; + if (checkIfMatchesFilter(messageObject) == false) return; showingMessageObjects.set(messageObject.id, messageObject); chat.objects.handleRemoval(messageObject, () => { showingMessageObjects.remove(messageObject.id); }); }); - function checkMatchesFilter(messageObject) { + function checkIfMatchesFilter(messageObject) { if (isFilterEmpty.value == true) return true; - const wordsOfObject = []; + const stringsInObject = []; const objectTitle = messageObject.title || translation.untitledObject; - wordsOfObject.push(...objectTitle.toLowerCase().split(" ")); + stringsInObject.push(objectTitle); const latest = chat.getMostRecentContent(messageObject); if (latest) { Object.values(latest).forEach((value) => { - wordsOfObject.push(value.toLowerCase()); + if (typeof value != "string") return; + stringsInObject.push(value); }); } + const wordsOfObject = stringsInObject.map((string) => string.toLowerCase().split(" ")).flat(); const wordsOfSearchTerm = appliedFilter.value.toLowerCase().split(" "); for (const word of wordsOfSearchTerm) { if (word[0] == "-") { const wordContent = word.substring(1); - console.log(wordContent); if (wordsOfObject.includes(wordContent)) return false; - } else if (!wordsOfObject.includes(word)) return false; + } else { + return wordsOfObject.includes(word); + } } return true; } diff --git a/src/Views/Objects/objectPane.tsx b/src/Views/Objects/objectPane.tsx index baa29b8d..24a4b7d3 100644 --- a/src/Views/Objects/objectPane.tsx +++ b/src/Views/Objects/objectPane.tsx @@ -58,48 +58,58 @@ export function ObjectPane(chat: Chat) { filterInput.value = ""; applyFilter(); } + function applyFilter() { appliedFilter.value = filterInput.value; - const allObjects = [...chat.objects.value.entries()]; - const matchingObjects = allObjects.filter((entry) => { - return checkMatchesFilter(entry[1]); + const allObjects = [...chat.objects.value.values()]; + allObjects.forEach((object, i) => { + const doesMatch = checkIfMatchesFilter(object); + if (doesMatch) { + showingMessageObjects.set(object.id, object); + } else { + showingMessageObjects.remove(object.id); + } }); - resultCount.value = matchingObjects.length; - showingMessageObjects.clear(); - matchingObjects.forEach((entry) => showingMessageObjects.set(...entry)); + resultCount.value = showingMessageObjects.value.size; } chat.objects.handleAddition((messageObject) => { - if (checkMatchesFilter(messageObject) == false) return; + if (checkIfMatchesFilter(messageObject) == false) return; showingMessageObjects.set(messageObject.id, messageObject); chat.objects.handleRemoval(messageObject, () => { showingMessageObjects.remove(messageObject.id); }); }); - function checkMatchesFilter(messageObject: MessageObject) { + function checkIfMatchesFilter(messageObject: MessageObject) { if (isFilterEmpty.value == true) return true; - const wordsOfObject: string[] = []; + const stringsInObject: string[] = []; const objectTitle = messageObject.title || translation.untitledObject; - wordsOfObject.push(...objectTitle.toLowerCase().split(" ")); + stringsInObject.push(objectTitle); const latest = chat.getMostRecentContent(messageObject); if (latest) { Object.values(latest).forEach((value) => { - wordsOfObject.push(value.toLowerCase()); + if (typeof value != "string") return; + stringsInObject.push(value); }); } + const wordsOfObject = stringsInObject + .map((string) => string.toLowerCase().split(" ")) + .flat(); + const wordsOfSearchTerm = appliedFilter.value.toLowerCase().split(" "); for (const word of wordsOfSearchTerm) { if (word[0] == "-") { // exclusion const wordContent = word.substring(1); - console.log(wordContent); if (wordsOfObject.includes(wordContent)) return false; - } else if (!wordsOfObject.includes(word)) return false; + } else { + return wordsOfObject.includes(word); + } } return true;