A continuation of HomeBox the inventory and organization system built for the Home User
feat: improve tag ui
tonyaellie875a4d4fa08d
diff --git a/frontend/components/Item/Card.vue b/frontend/components/Item/Card.vueindex a373869f..f65cdee3 100644--- a/frontend/components/Item/Card.vue+++ b/frontend/components/Item/Card.vue@@ -69,7 +69,7 @@</TooltipProvider><Markdown class="mb-2 line-clamp-3 text-ellipsis" :source="item.description" /><div class="-mr-1 mt-auto flex flex-wrap justify-end gap-2">- <TagChip v-for="tag in itemTags" :key="tag.id" :tag="tag" size="sm" />+ <TagChip v-for="tag in itemTags" :key="tag.id" :tag="tag" size="sm" :ancestors="tag.ancestors" /></div></div></NuxtLink>@@ -104,7 +104,7 @@});const itemTags = computed(() => {- return props.item.tags || [];+ return useTagStore().withAncestors(props.item.tags);});const props = defineProps({diff --git a/frontend/components/Item/View/table/data-table-expanded-row.vue b/frontend/components/Item/View/table/data-table-expanded-row.vueindex b588c3c3..16b4b8e3 100644--- a/frontend/components/Item/View/table/data-table-expanded-row.vue+++ b/frontend/components/Item/View/table/data-table-expanded-row.vue@@ -8,6 +8,10 @@item: ItemSummary;}>();+ const itemTags = computed(() => {+ return useTagStore().withAncestors(props.item.tags);+ });+const api = useUserApi();const imageUrl = computed(() => {@@ -35,7 +39,7 @@</NuxtLink></Badge><div class="flex flex-wrap gap-2">- <TagChip v-for="tag in item.tags" :key="tag.id" :tag="tag" size="sm" />+ <TagChip v-for="tag in itemTags" :key="tag.id" :tag="tag" size="sm" :ancestors="tag.ancestors" /></div><p class="whitespace-pre-line break-words text-sm text-muted-foreground">{{ item.description || $t("components.item.no_description") }}diff --git a/frontend/components/Tag/Chip.vue b/frontend/components/Tag/Chip.vueindex f5b16666..1261eea4 100644--- a/frontend/components/Tag/Chip.vue+++ b/frontend/components/Tag/Chip.vue@@ -18,16 +18,22 @@type: Boolean,default: false,},+ ancestors: {+ type: Boolean,+ default: false,+ },});</script><template><NuxtLink- class="group/tag-chip flex gap-2 rounded-full shadow transition duration-300 hover:bg-accent/50"+ class="group/tag-chip flex gap-2 rounded-full border shadow transition duration-300 hover:bg-accent/50":class="{'p-4 py-1 text-base': size === 'lg','p-3 py-1 text-sm': size !== 'sm' && size !== 'lg','p-2 py-0.5 text-xs': size === 'sm',+ 'border-dashed italic': ancestors,+ 'border-transparent': !ancestors,}":style="tag.colordiff --git a/frontend/components/Tag/Selector.vue b/frontend/components/Tag/Selector.vueindex 29e2efef..b9acb2f9 100644--- a/frontend/components/Tag/Selector.vue+++ b/frontend/components/Tag/Selector.vue@@ -5,10 +5,9 @@</Label><TagsInput- :model-value="modelValue"- :display-value="v => props.tags.find(t => t.id === v)?.name ?? 'Loading...'"+ v-model="modelValue"class="w-full gap-0 px-0"- @update:model-value="v => onTagsInputUpdate(v as string[])"+ :display-value="v => props.tags.find(t => t.id === v)?.name ?? 'Loading...'"><div class="flex flex-wrap items-center gap-2 overflow-hidden px-3"><TagsInputItem v-for="item in modelValue" :key="item" :value="item" class="h-auto overflow-hidden text-wrap">@@ -52,7 +51,9 @@if (ev.detail.value === 'create-item') {void createAndAdd(searchTerm);} else {- addTagWithAncestors(ev.detail.value);+ if (!modelValue.includes(ev.detail.value)) {+ modelValue = [...modelValue, ev.detail.value];+ }}searchTerm = '';}@@ -119,66 +120,6 @@const open = ref(false);const searchTerm = ref("");- const NULL_UUID = "00000000-0000-0000-0000-000000000000";-- function isValidId(id: string | null | undefined): boolean {- return !!id && id !== NULL_UUID;- }-- function getAncestorIds(tagId: string): string[] {- const ancestors: string[] = [];- let current = props.tags.find(t => t.id === tagId);- while (current && isValidId(current.parentId)) {- const parentId = current.parentId!;- ancestors.push(parentId);- current = props.tags.find(t => t.id === parentId);- }- return ancestors;- }-- function getDescendantIds(tagId: string): string[] {- const descendants: string[] = [];- const visited = new Set<string>();- const queue = [tagId];- while (queue.length > 0) {- const currentId = queue.shift()!;- if (visited.has(currentId)) continue;- visited.add(currentId);- for (const tag of props.tags) {- if (tag.parentId === currentId && isValidId(tag.id) && !visited.has(tag.id)) {- descendants.push(tag.id);- queue.push(tag.id);- }- }- }- return descendants;- }-- function addTagWithAncestors(tagId: string) {- if (!isValidId(tagId)) return;- const toAdd = [tagId, ...getAncestorIds(tagId)];- const current = new Set(modelValue.value);- for (const id of toAdd) {- current.add(id);- }- modelValue.value = [...current];- }-- function onTagsInputUpdate(newVal: string[]) {- const removed = modelValue.value.filter(id => !newVal.includes(id));- if (removed.length === 0) {- modelValue.value = newVal;- return;- }- const toRemove = new Set<string>(removed);- for (const id of removed) {- for (const descendant of getDescendantIds(id)) {- toRemove.add(descendant);- }- }- modelValue.value = modelValue.value.filter(id => !toRemove.has(id));- }-const filteredTags = computed(() => {const filtered = fuzzysort.go(searchTerm.value, props.tags, { key: "name", all: true })diff --git a/frontend/pages/item/[id]/index.vue b/frontend/pages/item/[id]/index.vueindex 4712bcbd..52a96a59 100644--- a/frontend/pages/item/[id]/index.vue+++ b/frontend/pages/item/[id]/index.vue@@ -134,6 +134,10 @@originalType?: string;};+ const itemTags = computed(() => {+ return useTagStore().withAncestors(item.value?.tags || []);+ });+const photos = computed<Photo[]>(() => {if (!item.value) {return [];@@ -684,7 +688,7 @@{{ item ? item.name : "" }}</h1><div class="flex flex-wrap gap-2 pb-1">- <TagChip v-for="tag in item?.tags || []" :key="tag.id" :tag="tag" size="sm" />+ <TagChip v-for="tag in itemTags" :key="tag.id" :tag="tag" size="sm" :ancestors="tag.ancestors" /></div><div class="flex flex-wrap gap-1 text-wrap text-xs"><div>diff --git a/frontend/stores/tags.ts b/frontend/stores/tags.tsindex 4afdb6b7..37812a7b 100644--- a/frontend/stores/tags.ts+++ b/frontend/stores/tags.ts@@ -1,5 +1,5 @@import { defineStore } from "pinia";-import type { TagOut } from "~~/lib/api/types/data-contracts";+import type { TagOut, TagSummary } from "~~/lib/api/types/data-contracts";export const useTagStore = defineStore("tags", {state: () => ({@@ -37,5 +37,41 @@ export const useTagStore = defineStore("tags", {this.allTags = result.data;return result;},+ getAncestors(tags: string[]) {+ if (this.allTags === null) {+ return [];+ }++ // recursively find all ancestors of all input tags+ const toCheck = [this.allTags.filter(t => tags.includes(t.id))];+ const ancestors: TagOut[] = [];++ while (toCheck.length > 0) {+ const next = toCheck.pop();+ if (next === undefined) {+ break;+ }+ for (const tag of next) {+ if (ancestors.includes(tag)) {+ continue;+ }+ ancestors.push(tag);+ toCheck.push(this.allTags.filter(t => t.id === tag.parentId));+ }+ }++ // filter out tags from ancestors+ return ancestors.filter(t => !tags.includes(t.id));+ },+ withAncestors(tags: TagOut[] | TagSummary[]) {+ if (!tags) {+ return [];+ }+ const ancestors = this.getAncestors(tags.map(t => t.id)).map(t => ({ ...t, ancestors: true }));++ return [...tags.map(t => ({ ...t, ancestors: false })), ...ancestors].sort((a, b) =>+ a.name.localeCompare(b.name)+ );+ },},});