A continuation of HomeBox the inventory and organization system built for the Home User
feat: improve search matching (#800)
Haydenb655cfab2864
diff --git a/frontend/components/Form/Autocomplete2.vue b/frontend/components/Form/Autocomplete2.vueindex 9b207589..f356a371 100644--- a/frontend/components/Form/Autocomplete2.vue+++ b/frontend/components/Form/Autocomplete2.vue@@ -61,6 +61,7 @@</template><script setup lang="ts">+ import lunr from "lunr";import {Combobox,ComboboxInput,@@ -126,42 +127,37 @@return "";}- const computedItems = computed<ComboItem[]>(() => {- const list: ComboItem[] = [];+ function lunrFactory() {+ return lunr(function () {+ this.ref("id");+ this.field("display");- for (let i = 0; i < props.items.length; i++) {- const item = props.items[i];-- const out: Partial<ComboItem> = {- id: i,- value: item,- };-- switch (typeof item) {- case "string":- out.display = item;- break;- case "object":- // @ts-ignore - up to the user to provide a valid display key- out.display = item[props.display] as string;- break;- default:- out.display = "";- break;+ for (let i = 0; i < props.items.length; i++) {+ const item = props.items[i];+ const display = extractDisplay(item);+ this.add({ id: i, display });}+ });+ }- if (search.value && out.display) {- const foldSearch = search.value.toLowerCase();- const foldDisplay = out.display.toLowerCase();+ const index = ref<ReturnType<typeof lunrFactory>>(lunrFactory());- if (foldDisplay.startsWith(foldSearch)) {- list.push(out as ComboItem);- }+ watchEffect(() => {+ if (props.items) {+ index.value = lunrFactory();+ }+ });- continue;- }+ const computedItems = computed<ComboItem[]>(() => {+ const list: ComboItem[] = [];++ const matches = index.value.search("*" + search.value + "*");- list.push(out as ComboItem);+ for (let i = 0; i < matches.length; i++) {+ const match = matches[i];+ const item = props.items[parseInt(match.ref)];+ const display = extractDisplay(item);+ list.push({ id: i, display, value: item });}return list;diff --git a/frontend/components/Location/Selector.vue b/frontend/components/Location/Selector.vueindex 82b79549..4a738b5e 100644--- a/frontend/components/Location/Selector.vue+++ b/frontend/components/Location/Selector.vue@@ -37,7 +37,7 @@const props = defineProps<Props>();const value = useVModel(props, "modelValue");- const locations = await useFlatLocations();+ const locations = useFlatLocations();const form = ref({parent: null as LocationSummary | null,search: "",diff --git a/frontend/composables/use-location-helpers.ts b/frontend/composables/use-location-helpers.tsindex 5d4d2451..91e056d3 100644--- a/frontend/composables/use-location-helpers.ts+++ b/frontend/composables/use-location-helpers.ts@@ -7,8 +7,8 @@ export interface FlatTreeItem {treeString: string;}-export function flatTree(tree: TreeItem[]): Ref<FlatTreeItem[]> {- const v = ref<FlatTreeItem[]>([]);+function flatTree(tree: TreeItem[]): FlatTreeItem[] {+ const v = [] as FlatTreeItem[];// turns the nested items into a flat items array where// the display is a string of the tree hierarchy separated by breadcrumbs@@ -19,7 +19,7 @@ export function flatTree(tree: TreeItem[]): Ref<FlatTreeItem[]> {}for (const item of items) {- v.value.push({+ v.push({id: item.id,name: item.name,treeString: display + item.name,@@ -35,14 +35,18 @@ export function flatTree(tree: TreeItem[]): Ref<FlatTreeItem[]> {return v;}-export async function useFlatLocations(): Promise<Ref<FlatTreeItem[]>> {- const api = useUserApi();+export function useFlatLocations(): Ref<FlatTreeItem[]> {+ const locations = useLocationStore();- const locations = await api.locations.getTree();-- if (!locations) {- return ref([]);+ if (locations.tree === null) {+ locations.refreshTree();}- return flatTree(locations.data);+ return computed(() => {+ if (locations.tree === null) {+ return [];+ }++ return flatTree(locations.tree);+ });}diff --git a/frontend/layouts/default.vue b/frontend/layouts/default.vueindex 769606b6..fa342961 100644--- a/frontend/layouts/default.vue+++ b/frontend/layouts/default.vue@@ -185,6 +185,7 @@onServerEvent(ServerEvent.LocationMutation, () => {locationStore.refreshChildren();locationStore.refreshParents();+ locationStore.refreshTree();});onServerEvent(ServerEvent.ItemMutation, () => {diff --git a/frontend/package.json b/frontend/package.jsonindex 8eb8a950..cbbd11c2 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -41,6 +41,7 @@"@tailwindcss/aspect-ratio": "^0.4.0","@tailwindcss/forms": "^0.5.2","@tailwindcss/typography": "^0.5.4",+ "@types/lunr": "^2.3.7","@vuepic/vue-datepicker": "^8.1.1","@vueuse/nuxt": "^10.0.0","@vueuse/router": "^10.0.0",@@ -50,6 +51,7 @@"dompurify": "^3.0.0","h3": "^1.7.1","http-proxy": "^1.18.1",+ "lunr": "^2.3.9","markdown-it": "^14.0.0","pinia": "^2.0.21","postcss": "^8.4.16",diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yamlindex 1958dc63..633e817a 100644--- a/frontend/pnpm-lock.yaml+++ b/frontend/pnpm-lock.yaml@@ -26,6 +26,9 @@ dependencies:'@tailwindcss/typography':specifier: ^0.5.4version: 0.5.8([email protected])+ '@types/lunr':+ specifier: ^2.3.7+ version: 2.3.7'@vuepic/vue-datepicker':specifier: ^8.1.1version: 8.1.1([email protected])@@ -53,6 +56,9 @@ dependencies:http-proxy:specifier: ^1.18.1version: 1.18.1+ lunr:+ specifier: ^2.3.9+ version: 2.3.9markdown-it:specifier: ^14.0.0version: 14.0.0@@ -2797,6 +2803,10 @@ packages:resolution: {integrity: sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==}dev: true+ /@types/[email protected]:+ resolution: {integrity: sha512-Tb/kUm38e8gmjahQzdCKhbdsvQ9/ppzHFfsJ0dMs3ckqQsRj+P5IkSAwFTBrBxdyr3E/LoMUUrZngjDYAjiE3A==}+ dev: false+/@types/[email protected]:resolution: {integrity: sha512-Tla7hH9oeXHOlJyBFdoqV61xWE9FZf/y2g+gFVwQ2vE1/eBzjUno5JCd3Hdb5oATve5OF6xNjZ/4VIZhVVx+hA==}dependencies:@@ -6586,6 +6596,10 @@ packages:dependencies:yallist: 4.0.0+ resolution: {integrity: sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==}+ dev: false+resolution: {integrity: sha512-P53AZrzq7hclCU6HWj88xNZHmP15DKjMmK/vBytO1qnpYP3ul4IEZlyCE0aU3JRnmgWmZPmoTKj4Bls7v0pMyA==}engines: {node: '>=14.19.0'}diff --git a/frontend/stores/locations.ts b/frontend/stores/locations.tsindex 2a5c83d3..94bf7a40 100644--- a/frontend/stores/locations.ts+++ b/frontend/stores/locations.ts@@ -1,12 +1,13 @@import { defineStore } from "pinia";import { LocationsApi } from "~~/lib/api/classes/locations";-import { LocationOutCount } from "~~/lib/api/types/data-contracts";+import { LocationOutCount, TreeItem } from "~~/lib/api/types/data-contracts";export const useLocationStore = defineStore("locations", {state: () => ({parents: null as LocationOutCount[] | null,Locations: null as LocationOutCount[] | null,client: useUserApi(),+ tree: null as TreeItem[] | null,}),getters: {/**@@ -60,5 +61,14 @@ export const useLocationStore = defineStore("locations", {this.Locations = result.data;return result;},+ async refreshTree(): ReturnType<LocationsApi["getTree"]> {+ const result = await this.client.locations.getTree();+ if (result.error) {+ return result;+ }++ this.tree = result.data;+ return result;+ },},});