fix search

This commit is contained in:
Marlon Erler
2024-07-17 15:48:04 +00:00
parent e41bbd5acb
commit 0069462034
2 changed files with 42 additions and 26 deletions
+19 -13
View File
@@ -2096,39 +2096,45 @@
} }
function applyFilter() { function applyFilter() {
appliedFilter.value = filterInput.value; appliedFilter.value = filterInput.value;
const allObjects = [...chat.objects.value.entries()]; const allObjects = [...chat.objects.value.values()];
const matchingObjects = allObjects.filter((entry) => { allObjects.forEach((object, i) => {
return checkMatchesFilter(entry[1]); const doesMatch = checkIfMatchesFilter(object);
if (doesMatch) {
showingMessageObjects.set(object.id, object);
} else {
showingMessageObjects.remove(object.id);
}
}); });
resultCount.value = matchingObjects.length; resultCount.value = showingMessageObjects.value.size;
showingMessageObjects.clear();
matchingObjects.forEach((entry) => showingMessageObjects.set(...entry));
} }
chat.objects.handleAddition((messageObject) => { chat.objects.handleAddition((messageObject) => {
if (checkMatchesFilter(messageObject) == false) return; if (checkIfMatchesFilter(messageObject) == false) return;
showingMessageObjects.set(messageObject.id, messageObject); showingMessageObjects.set(messageObject.id, messageObject);
chat.objects.handleRemoval(messageObject, () => { chat.objects.handleRemoval(messageObject, () => {
showingMessageObjects.remove(messageObject.id); showingMessageObjects.remove(messageObject.id);
}); });
}); });
function checkMatchesFilter(messageObject) { function checkIfMatchesFilter(messageObject) {
if (isFilterEmpty.value == true) return true; if (isFilterEmpty.value == true) return true;
const wordsOfObject = []; const stringsInObject = [];
const objectTitle = messageObject.title || translation.untitledObject; const objectTitle = messageObject.title || translation.untitledObject;
wordsOfObject.push(...objectTitle.toLowerCase().split(" ")); stringsInObject.push(objectTitle);
const latest = chat.getMostRecentContent(messageObject); const latest = chat.getMostRecentContent(messageObject);
if (latest) { if (latest) {
Object.values(latest).forEach((value) => { 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(" "); const wordsOfSearchTerm = appliedFilter.value.toLowerCase().split(" ");
for (const word of wordsOfSearchTerm) { for (const word of wordsOfSearchTerm) {
if (word[0] == "-") { if (word[0] == "-") {
const wordContent = word.substring(1); const wordContent = word.substring(1);
console.log(wordContent);
if (wordsOfObject.includes(wordContent)) return false; if (wordsOfObject.includes(wordContent)) return false;
} else if (!wordsOfObject.includes(word)) return false; } else {
return wordsOfObject.includes(word);
}
} }
return true; return true;
} }
+23 -13
View File
@@ -58,48 +58,58 @@ export function ObjectPane(chat: Chat) {
filterInput.value = ""; filterInput.value = "";
applyFilter(); applyFilter();
} }
function applyFilter() { function applyFilter() {
appliedFilter.value = filterInput.value; appliedFilter.value = filterInput.value;
const allObjects = [...chat.objects.value.entries()]; const allObjects = [...chat.objects.value.values()];
const matchingObjects = allObjects.filter((entry) => { allObjects.forEach((object, i) => {
return checkMatchesFilter(entry[1]); const doesMatch = checkIfMatchesFilter(object);
if (doesMatch) {
showingMessageObjects.set(object.id, object);
} else {
showingMessageObjects.remove(object.id);
}
}); });
resultCount.value = matchingObjects.length; resultCount.value = showingMessageObjects.value.size;
showingMessageObjects.clear();
matchingObjects.forEach((entry) => showingMessageObjects.set(...entry));
} }
chat.objects.handleAddition((messageObject) => { chat.objects.handleAddition((messageObject) => {
if (checkMatchesFilter(messageObject) == false) return; if (checkIfMatchesFilter(messageObject) == false) return;
showingMessageObjects.set(messageObject.id, messageObject); showingMessageObjects.set(messageObject.id, messageObject);
chat.objects.handleRemoval(messageObject, () => { chat.objects.handleRemoval(messageObject, () => {
showingMessageObjects.remove(messageObject.id); showingMessageObjects.remove(messageObject.id);
}); });
}); });
function checkMatchesFilter(messageObject: MessageObject) { function checkIfMatchesFilter(messageObject: MessageObject) {
if (isFilterEmpty.value == true) return true; if (isFilterEmpty.value == true) return true;
const wordsOfObject: string[] = []; const stringsInObject: string[] = [];
const objectTitle = messageObject.title || translation.untitledObject; const objectTitle = messageObject.title || translation.untitledObject;
wordsOfObject.push(...objectTitle.toLowerCase().split(" ")); stringsInObject.push(objectTitle);
const latest = chat.getMostRecentContent(messageObject); const latest = chat.getMostRecentContent(messageObject);
if (latest) { if (latest) {
Object.values(latest).forEach((value) => { 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(" "); const wordsOfSearchTerm = appliedFilter.value.toLowerCase().split(" ");
for (const word of wordsOfSearchTerm) { for (const word of wordsOfSearchTerm) {
if (word[0] == "-") { if (word[0] == "-") {
// exclusion // exclusion
const wordContent = word.substring(1); const wordContent = word.substring(1);
console.log(wordContent);
if (wordsOfObject.includes(wordContent)) return false; if (wordsOfObject.includes(wordContent)) return false;
} else if (!wordsOfObject.includes(word)) return false; } else {
return wordsOfObject.includes(word);
}
} }
return true; return true;