refac: knowledge fuzzy search

This commit is contained in:
Timothy J. Baek 2024-10-02 06:21:16 -07:00
parent 4aca1e86ad
commit 9c4b55c86a

View File

@ -1,4 +1,6 @@
<script lang="ts"> <script lang="ts">
import Fuse from 'fuse.js';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime); dayjs.extend(relativeTime);
@ -24,8 +26,16 @@
let selectedItem = null; let selectedItem = null;
let showDeleteConfirm = false; let showDeleteConfirm = false;
let filteredItems; let fuse = null;
$: filteredItems = $knowledge.filter((item) => query === '' || item.name.includes(query));
let filteredItems = [];
$: if (fuse) {
filteredItems = query
? fuse.search(query).map((e) => {
return e.item;
})
: $knowledge;
}
const deleteHandler = async (item) => { const deleteHandler = async (item) => {
const res = await deleteKnowledgeById(localStorage.token, item.id).catch((e) => { const res = await deleteKnowledgeById(localStorage.token, item.id).catch((e) => {
@ -40,6 +50,12 @@
onMount(async () => { onMount(async () => {
knowledge.set(await getKnowledgeItems(localStorage.token)); knowledge.set(await getKnowledgeItems(localStorage.token));
knowledge.subscribe((value) => {
fuse = new Fuse(value, {
keys: ['name', 'description']
});
});
}); });
</script> </script>