Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

feat: add basic tree view for tags

tonyaellie3e2662d4f4ef
diff --git a/frontend/components/Tag/Tree/Node.vue b/frontend/components/Tag/Tree/Node.vue
new file mode 100644
index 00000000..cf32b6ef
--- /dev/null
+++ b/frontend/components/Tag/Tree/Node.vue
@@ -0,0 +1,85 @@
+<script setup lang="ts">
+ import MdiChevronRight from "~icons/mdi/chevron-right";
+ import { getContrastTextColor } from "~/lib/utils";
+ import { getIconComponent } from "~/lib/icons";
+ import { useTreeState } from "./tree-state";
+ import type { TagTreeItem } from "./types";
+ import TagTreeNode from "./Node.vue";
+
+ type Props = {
+ treeId: string;
+ item: TagTreeItem;
+ };
+
+ const props = defineProps<Props>();
+
+ const tagIcon = computed(() => getIconComponent(props.item.icon));
+
+ const state = useTreeState(props.treeId);
+
+ const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
+
+ const sortedChildren = computed(() => {
+ return [...(props.item.children ?? [])].sort((a, b) => collator.compare(a.name, b.name));
+ });
+
+ const hasChildren = computed(() => sortedChildren.value.length > 0);
+
+ const nodeHash = computed(() => {
+ return props.item.id.replace(/-/g, "").substring(0, 8);
+ });
+
+ const openRef = computed({
+ get() {
+ return state.value[nodeHash.value] ?? false;
+ },
+ set(value: boolean) {
+ state.value[nodeHash.value] = value;
+ },
+ });
+</script>
+
+<template>
+ <div>
+ <div
+ class="flex items-center gap-1 rounded p-1"
+ :class="{
+ 'cursor-pointer hover:bg-accent hover:text-accent-foreground': hasChildren,
+ }"
+ @click="openRef = !openRef"
+ >
+ <div
+ class="mr-1 flex items-center justify-center rounded p-0.5"
+ :class="{
+ 'hover:bg-accent hover:text-accent-foreground': hasChildren,
+ }"
+ >
+ <div v-if="!hasChildren" class="size-6" />
+ <div v-else class="group/node relative size-6" :data-swap="openRef">
+ <div
+ class="absolute inset-0 flex items-center justify-center transition-transform duration-300 group-data-[swap=true]/node:rotate-90"
+ >
+ <MdiChevronRight class="size-6" />
+ </div>
+ </div>
+ </div>
+
+ <div
+ class="mr-1 flex size-5 items-center justify-center rounded-full"
+ :style="
+ item.color
+ ? { backgroundColor: item.color, color: getContrastTextColor(item.color) }
+ : { backgroundColor: 'hsl(var(--accent))', color: 'hsl(var(--accent-foreground))' }
+ "
+ >
+ <component :is="tagIcon" class="size-3" />
+ </div>
+
+ <NuxtLink class="text-lg hover:underline" :to="`/tag/${item.id}`" @click.stop>{{ item.name }}</NuxtLink>
+ </div>
+
+ <div v-if="openRef" class="ml-4">
+ <TagTreeNode v-for="child in sortedChildren" :key="child.id" :item="child" :tree-id="treeId" />
+ </div>
+ </div>
+</template>
diff --git a/frontend/components/Tag/Tree/Root.vue b/frontend/components/Tag/Tree/Root.vue
new file mode 100644
index 00000000..74b3f2fd
--- /dev/null
+++ b/frontend/components/Tag/Tree/Root.vue
@@ -0,0 +1,54 @@
+<script setup lang="ts">
+ import { Button } from "~~/components/ui/button";
+ import { useDialog } from "~/components/ui/dialog-provider";
+ import { DialogID } from "~/components/ui/dialog-provider/utils";
+ import type { TagTreeItem } from "./types";
+ import TagTreeNode from "./Node.vue";
+
+ type Props = {
+ tags: TagTreeItem[];
+ treeId: string;
+ };
+
+ const { openDialog } = useDialog();
+
+ const props = defineProps<Props>();
+
+ const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
+
+ const sortedTags = computed(() => {
+ const list = props.tags ?? [];
+ return [...list].sort((a, b) => collator.compare(a.name, b.name));
+ });
+</script>
+
+<template>
+ <div>
+ <div
+ v-if="sortedTags.length === 0"
+ class="py-6 text-center text-sm text-muted-foreground"
+ role="status"
+ aria-live="polite"
+ >
+ <p class="mx-auto max-w-xs">
+ {{ $t("tags.no_results") }}
+ </p>
+ <Button
+ class="mt-3"
+ variant="outline"
+ size="sm"
+ type="button"
+ :aria-label="$t('components.tag.create_modal.title') || $t('global.create')"
+ @click="openDialog(DialogID.CreateTag)"
+ >
+ {{ $t("components.tag.create_modal.title") || $t("global.create") }}
+ </Button>
+ </div>
+
+ <ul role="tree" :aria-labelledby="treeId" class="space-y-1">
+ <li v-for="item in sortedTags" :key="item.id" role="treeitem">
+ <TagTreeNode :item="item" :tree-id="treeId" />
+ </li>
+ </ul>
+ </div>
+</template>
diff --git a/frontend/components/Tag/Tree/tree-state.ts b/frontend/components/Tag/Tree/tree-state.ts
new file mode 100644
index 00000000..3a008978
--- /dev/null
+++ b/frontend/components/Tag/Tree/tree-state.ts
@@ -0,0 +1,13 @@
+import type { Ref } from "vue";
+
+type TreeState = Record<string, boolean>;
+
+const store: Record<string, Ref<TreeState>> = {};
+
+export function useTreeState(key: string): Ref<TreeState> {
+ if (!store[key]) {
+ store[key] = ref({});
+ }
+
+ return store[key];
+}
diff --git a/frontend/components/Tag/Tree/types.ts b/frontend/components/Tag/Tree/types.ts
new file mode 100644
index 00000000..941166ed
--- /dev/null
+++ b/frontend/components/Tag/Tree/types.ts
@@ -0,0 +1,5 @@
+import type { TagOut } from "~~/lib/api/types/data-contracts";
+
+export type TagTreeItem = Omit<TagOut, "children"> & {
+ children: TagTreeItem[];
+};
diff --git a/frontend/layouts/default.vue b/frontend/layouts/default.vue
index 9706dd87..087c8615 100644
--- a/frontend/layouts/default.vue
+++ b/frontend/layouts/default.vue
@@ -230,6 +230,7 @@
import MdiHome from "~icons/mdi/home";
import MdiFileTree from "~icons/mdi/file-tree";
+ import MdiTagMultiple from "~icons/mdi/tag-multiple";
import MdiMagnify from "~icons/mdi/magnify";
import MdiQrcodeScan from "~icons/mdi/qrcode-scan";
import MdiAccount from "~icons/mdi/account";
@@ -400,36 +401,43 @@
to: "/locations",
},
{
- icon: MdiMagnify,
+ icon: MdiTagMultiple,
id: 2,
+ active: computed(() => route.path === "/tags"),
+ name: computed(() => t("global.tags")),
+ to: "/tags",
+ },
+ {
+ icon: MdiMagnify,
+ id: 3,
active: computed(() => route.path === "/items"),
name: computed(() => t("menu.search")),
to: "/items",
},
{
icon: MdiFileDocumentMultiple,
- id: 3,
+ id: 4,
active: computed(() => route.path === "/templates"),
name: computed(() => t("menu.templates")),
to: "/templates",
},
{
icon: MdiWrench,
- id: 4,
+ id: 5,
active: computed(() => route.path === "/maintenance"),
name: computed(() => t("menu.maintenance")),
to: "/maintenance",
},
{
icon: MdiAccount,
- id: 5,
+ id: 6,
active: computed(() => route.path === "/profile"),
name: computed(() => t("menu.profile")),
to: "/profile",
},
{
icon: MdiCog,
- id: 6,
+ id: 7,
active: computed(() => route.path.includes("/collection")),
name: computed(() => t("menu.collection")),
to: "/collection/members",
diff --git a/frontend/pages/tags.vue b/frontend/pages/tags.vue
new file mode 100644
index 00000000..5f24a961
--- /dev/null
+++ b/frontend/pages/tags.vue
@@ -0,0 +1,165 @@
+<script setup lang="ts">
+ import { useI18n } from "vue-i18n";
+ import MdiCollapseAllOutline from "~icons/mdi/collapse-all-outline";
+ import MdiExpandAllOutline from "~icons/mdi/expand-all-outline";
+ import { useTreeState } from "~~/components/Tag/Tree/tree-state";
+ import { Button, ButtonGroup } from "@/components/ui/button";
+ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
+ import type { TagOut } from "~~/lib/api/types/data-contracts";
+ import BaseContainer from "@/components/Base/Container.vue";
+ import BaseSectionHeader from "@/components/Base/SectionHeader.vue";
+ import BaseCard from "@/components/Base/Card.vue";
+ import TagTreeRoot from "~/components/Tag/Tree/Root.vue";
+ import type { TagTreeItem } from "~/components/Tag/Tree/types";
+
+ definePageMeta({
+ middleware: ["auth"],
+ });
+
+ const { t } = useI18n();
+
+ useHead({
+ title: "HomeBox | " + t("global.tags"),
+ });
+
+ const api = useUserApi();
+
+ const { data: allTags } = useAsyncData(async () => {
+ const { data, error } = await api.tags.getAll();
+
+ if (error) {
+ return [];
+ }
+
+ return data;
+ });
+
+ function buildTagTree(tags: TagOut[]): TagTreeItem[] {
+ const nodes = new Map<string, TagTreeItem>();
+
+ for (const tag of tags) {
+ nodes.set(tag.id, {
+ ...tag,
+ children: [],
+ });
+ }
+
+ const roots: TagTreeItem[] = [];
+
+ for (const tag of tags) {
+ const node = nodes.get(tag.id);
+
+ if (!node) {
+ continue;
+ }
+
+ const parent = tag.parentId ? nodes.get(tag.parentId) : undefined;
+
+ if (parent && parent.id !== node.id) {
+ parent.children.push(node);
+ } else {
+ roots.push(node);
+ }
+ }
+
+ return roots;
+ }
+
+ const tree = computed(() => {
+ if (!allTags.value || !Array.isArray(allTags.value)) {
+ return [];
+ }
+
+ return buildTagTree(allTags.value);
+ });
+
+ const tagTreeId = "tagTree";
+ const treeState = useTreeState(tagTreeId);
+
+ const route = useRouter();
+
+ onMounted(() => {
+ const query = route.currentRoute.value.query;
+
+ if (query && query[tagTreeId]) {
+ const data = JSON.parse(query[tagTreeId] as string);
+
+ for (const key in data) {
+ treeState.value[key] = data[key];
+ }
+ }
+ });
+
+ watch(
+ treeState,
+ () => {
+ route.replace({
+ query: {
+ [tagTreeId]: JSON.stringify(treeState.value),
+ },
+ });
+ },
+ { deep: true }
+ );
+
+ function closeAll() {
+ for (const key in treeState.value) {
+ treeState.value[key] = false;
+ }
+ }
+
+ function openTagChildren(items: TagTreeItem[]) {
+ for (const item of items) {
+ if (item.children.length > 0) {
+ treeState.value[item.id.replace(/-/g, "").substring(0, 8)] = true;
+ openTagChildren(item.children);
+ }
+ }
+ }
+
+ function openAll() {
+ if (!tree.value) return;
+
+ openTagChildren(tree.value);
+ }
+</script>
+
+<template>
+ <BaseContainer>
+ <div class="mb-2 flex justify-between">
+ <BaseSectionHeader>{{ $t("global.tags") }}</BaseSectionHeader>
+ <div>
+ <TooltipProvider :delay-duration="0">
+ <ButtonGroup>
+ <Tooltip>
+ <TooltipTrigger>
+ <Button size="icon" variant="outline" data-pos="start" @click="openAll">
+ <MdiExpandAllOutline />
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>{{ $t("locations.expand_tree") }}</p>
+ </TooltipContent>
+ </Tooltip>
+ <Tooltip>
+ <TooltipTrigger>
+ <Button size="icon" variant="outline" data-pos="end" @click="closeAll">
+ <MdiCollapseAllOutline />
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>{{ $t("locations.collapse_tree") }}</p>
+ </TooltipContent>
+ </Tooltip>
+ </ButtonGroup>
+ </TooltipProvider>
+ </div>
+ </div>
+
+ <BaseCard>
+ <div class="p-2">
+ <TagTreeRoot v-if="tree && Array.isArray(tree)" :tags="tree" :tree-id="tagTreeId" />
+ </div>
+ </BaseCard>
+ </BaseContainer>
+</template>