Skip to content

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

feat: start tag parent ui

tonyaellieb7a03c916c7a
diff --git a/frontend/components/Tag/Chip.vue b/frontend/components/Tag/Chip.vue
index dadc79f5..f5b16666 100644
--- a/frontend/components/Tag/Chip.vue
+++ b/frontend/components/Tag/Chip.vue
@@ -14,6 +14,10 @@
type: String as () => sizes,
default: "md",
},
+ hideIcon: {
+ type: Boolean,
+ default: false,
+ },
});
</script>
@@ -32,16 +36,18 @@
"
:to="`/tag/${tag.id}`"
>
- <div class="relative">
- <MdiTagOutline class="invisible" /><!-- hack to ensure the size is correct -->
+ <template v-if="!hideIcon">
+ <div class="relative">
+ <MdiTagOutline class="invisible" /><!-- hack to ensure the size is correct -->
- <div
- class="absolute inset-0 flex items-center justify-center transition-transform duration-300 group-hover/tag-chip:rotate-90"
- >
- <MdiTagOutline class="group-hover/tag-chip:hidden" />
- <MdiArrowUp class="hidden group-hover/tag-chip:block" />
+ <div
+ class="absolute inset-0 flex items-center justify-center transition-transform duration-300 group-hover/tag-chip:rotate-90"
+ >
+ <MdiTagOutline class="group-hover/tag-chip:hidden" />
+ <MdiArrowUp class="hidden group-hover/tag-chip:block" />
+ </div>
</div>
- </div>
+ </template>
{{ tag.name }}
</NuxtLink>
</template>
diff --git a/frontend/components/Tag/CreateModal.vue b/frontend/components/Tag/CreateModal.vue
index 9b87ede7..637b01c7 100644
--- a/frontend/components/Tag/CreateModal.vue
+++ b/frontend/components/Tag/CreateModal.vue
@@ -14,6 +14,11 @@
:label="$t('components.tag.create_modal.tag_description')"
:max-length="1000"
/>
+ <TagSingleSelector
+ v-model="form.parentTag"
+ :tags="availableTags"
+ :name="$t('components.tag.create_modal.tag_parent')"
+ />
<ColorSelector v-model="form.color" :label="$t('components.tag.create_modal.tag_color')" :show-hex="true" />
<div class="mt-4 flex flex-row-reverse">
<ButtonGroup>
@@ -37,10 +42,14 @@
import FormTextField from "~/components/Form/TextField.vue";
import FormTextArea from "~/components/Form/TextArea.vue";
import { Button, ButtonGroup } from "~/components/ui/button";
+ import TagSingleSelector from "~/components/Tag/SingleSelector.vue";
+ import type { TagOut } from "~~/lib/api/types/data-contracts";
+ import { useTagStore } from "~/stores/tags";
const { t } = useI18n();
const { closeDialog } = useDialog();
+ const tagStore = useTagStore();
useDialogHotkey(DialogID.CreateTag, { code: "Digit2", shift: true });
@@ -49,13 +58,23 @@
const form = reactive({
name: "",
description: "",
- color: "", // Future!
+ color: "",
+ parentTag: null as TagOut | null,
+ });
+
+ onMounted(async () => {
+ await tagStore.ensureAllTagsFetched();
+ });
+
+ const availableTags = computed(() => {
+ return tagStore.tags;
});
function reset() {
form.name = "";
form.description = "";
form.color = "";
+ form.parentTag = null;
focused.value = false;
loading.value = false;
}
@@ -77,7 +96,13 @@
if (shift?.value) close = false;
- const { error, data } = await api.tags.create(form);
+ const { error, data } = await api.tags.create({
+ name: form.name,
+ description: form.description,
+ color: form.color,
+ icon: "",
+ parentId: form.parentTag?.id,
+ });
if (error) {
toast.error(t("components.tag.create_modal.toast.create_failed"));
@@ -90,7 +115,7 @@
if (close) {
closeDialog(DialogID.CreateTag);
- navigateTo(`/tag/${data.id}`);
+ await navigateTo(`/tag/${data.id}`);
}
}
</script>
diff --git a/frontend/components/Tag/SingleSelector.vue b/frontend/components/Tag/SingleSelector.vue
new file mode 100644
index 00000000..85daf848
--- /dev/null
+++ b/frontend/components/Tag/SingleSelector.vue
@@ -0,0 +1,119 @@
+<template>
+ <div class="flex flex-col gap-1">
+ <Label :for="id" class="px-1">
+ {{ props.name ?? $t("global.tags") }}
+ </Label>
+
+ <Popover v-model:open="open">
+ <PopoverTrigger as-child>
+ <Button :id="id" variant="outline" role="combobox" :aria-expanded="open" class="w-full justify-between">
+ <span class="flex min-w-0 flex-auto items-center gap-2 truncate text-left">
+ <span
+ v-if="value?.color"
+ class="shrink-0 rounded-full"
+ :style="{ width: '1rem', height: '1rem', backgroundColor: value.color }"
+ />
+ <span class="truncate">
+ {{ value && value.name ? value.name : $t("components.tag.selector.select_tags") }}
+ </span>
+ </span>
+
+ <span class="ml-2 flex items-center">
+ <button
+ v-if="value"
+ type="button"
+ class="shrink-0 rounded p-1 hover:bg-primary/20"
+ :aria-label="$t('components.tag.selector.clear')"
+ @click.stop.prevent="clearSelection"
+ >
+ <X class="size-4" />
+ </button>
+
+ <ChevronsUpDown class="ml-2 size-4 shrink-0 opacity-50" />
+ </span>
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent class="w-[--reka-popper-anchor-width] p-0">
+ <Command :ignore-filter="true">
+ <CommandInput
+ v-model="search"
+ :placeholder="$t('components.tag.selector.select_tags')"
+ :display-value="_ => ''"
+ />
+ <CommandEmpty>{{ $t("components.tag.selector.no_tags_found") }}</CommandEmpty>
+ <CommandList>
+ <CommandGroup>
+ <CommandItem v-for="tag in filteredTags" :key="tag.id" :value="tag.id" @select="selectTag(tag)">
+ <Check :class="cn('mr-2 h-4 w-4', value?.id === tag.id ? 'opacity-100' : 'opacity-0')" />
+ <span
+ v-if="tag.color"
+ class="mr-2 size-4 shrink-0 rounded-full"
+ :style="{ backgroundColor: tag.color }"
+ />
+ <div>
+ {{ tag.name }}
+ </div>
+ </CommandItem>
+ </CommandGroup>
+ </CommandList>
+ </Command>
+ </PopoverContent>
+ </Popover>
+ </div>
+</template>
+
+<script setup lang="ts">
+ import { Check, ChevronsUpDown, X } from "lucide-vue-next";
+ import fuzzysort from "fuzzysort";
+ import { Button } from "~/components/ui/button";
+ import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "~/components/ui/command";
+ import { Label } from "~/components/ui/label";
+ import { Popover, PopoverContent, PopoverTrigger } from "~/components/ui/popover";
+ import { cn } from "~/lib/utils";
+ import type { TagOut } from "~~/lib/api/types/data-contracts";
+
+ type Props = {
+ modelValue?: TagOut | null;
+ tags: TagOut[];
+ name?: string;
+ };
+
+ const props = defineProps<Props>();
+ const emit = defineEmits(["update:modelValue"]);
+
+ const open = ref(false);
+ const search = ref("");
+ const id = useId();
+ const value = useVModel(props, "modelValue", emit);
+
+ function selectTag(tag: TagOut) {
+ if (value.value?.id !== tag.id) {
+ value.value = tag;
+ } else {
+ value.value = null;
+ }
+ open.value = false;
+ }
+
+ function clearSelection() {
+ value.value = null;
+ search.value = "";
+ open.value = false;
+ }
+
+ const filteredTags = computed(() => {
+ const filtered = fuzzysort.go(search.value, props.tags, { key: "name", all: true }).map(i => i.obj);
+
+ return filtered;
+ });
+
+ // Reset search when value is cleared
+ watch(
+ () => value.value,
+ () => {
+ if (!value.value) {
+ search.value = "";
+ }
+ }
+ );
+</script>
diff --git a/frontend/locales/en.json b/frontend/locales/en.json
index 8706273d..c41b7b59 100644
--- a/frontend/locales/en.json
+++ b/frontend/locales/en.json
@@ -313,6 +313,7 @@
"tag_color": "Tag Color",
"tag_description": "Tag Description",
"tag_name": "Tag Name",
+ "tag_parent": "Parent Tag",
"title": "Create Tag",
"toast": {
"already_creating": "Already creating a tag",
@@ -322,7 +323,9 @@
}
},
"selector": {
- "select_tags": "Select Tags"
+ "select_tags": "Select Tags",
+ "no_tags_found": "No tags found",
+ "clear": "Clear"
}
},
"template": {
diff --git a/frontend/pages/tag/[id].vue b/frontend/pages/tag/[id].vue
index 2012e75f..9f7614ce 100644
--- a/frontend/pages/tag/[id].vue
+++ b/frontend/pages/tag/[id].vue
@@ -21,6 +21,10 @@
import PageQRCode from "~/components/global/PageQRCode.vue";
import Markdown from "~/components/global/Markdown.vue";
import ItemViewSelectable from "~/components/Item/View/Selectable.vue";
+ import TagSingleSelector from "~/components/Tag/SingleSelector.vue";
+ import TagChip from "~/components/Tag/Chip.vue";
+ import type { TagOut } from "~~/lib/api/types/data-contracts";
+ import { useTagStore } from "~/stores/tags";
definePageMeta({
middleware: ["auth"],
@@ -30,6 +34,8 @@
const { openDialog, closeDialog } = useDialog();
+ const tagStore = useTagStore();
+
const route = useRoute();
const api = useUserApi();
@@ -69,18 +75,98 @@
name: "",
description: "",
color: "",
+ parentTag: null as TagOut | null,
+ });
+
+ function wouldCreateCircular(potentialParent: TagOut, currentTagId: string): boolean {
+ let current: TagOut | undefined = potentialParent;
+ const visited = new Set<string>();
+
+ while (current) {
+ if (current.id === currentTagId) {
+ return true;
+ }
+ if (visited.has(current.id)) {
+ break;
+ }
+ visited.add(current.id);
+
+ if (current.parentId) {
+ current = tagStore.tags.find(t => t.id === current?.parentId);
+ } else {
+ break;
+ }
+ }
+ return false;
+ }
+
+ const availableParentTags = computed(() => {
+ return tagStore.tags.filter(t => {
+ if (t.id === tagId.value) {
+ return false;
+ }
+ if (wouldCreateCircular(t, tagId.value)) {
+ return false;
+ }
+ return true;
+ });
+ });
+
+ onMounted(async () => {
+ await tagStore.ensureAllTagsFetched();
});
+ function getBreadcrumbPath() {
+ if (!tag.value || !tag.value.parentId) {
+ return [];
+ }
+
+ const path: TagOut[] = [];
+ let currentId: string | null = tag.value.parentId;
+ const maxDepth = 5;
+ let depth = 0;
+
+ while (currentId && depth < maxDepth) {
+ const current = tagStore.tags.find(t => t.id === currentId);
+ if (current) {
+ path.unshift(current);
+ currentId = current.parentId || null;
+ } else {
+ break;
+ }
+ depth++;
+ }
+
+ return path;
+ }
+
function openUpdate() {
updateData.name = tag.value?.name || "";
updateData.description = tag.value?.description || "";
updateData.color = "";
+ if (tag.value?.parent) {
+ const parent = tagStore.tags.find(t => t.id === tag.value?.parentId);
+ updateData.parentTag = parent || null;
+ } else {
+ updateData.parentTag = null;
+ }
openDialog(DialogID.UpdateTag);
}
async function update() {
+ if (!updateData.name || updateData.name.trim().length === 0) {
+ toast.error(t("components.tag.create_modal.toast.tag_name_too_long"));
+ return;
+ }
+
updating.value = true;
- const { error, data } = await api.tags.update(tagId.value, updateData);
+ const { error, data } = await api.tags.update(tagId.value, {
+ name: updateData.name,
+ description: updateData.description,
+ color: updateData.color,
+ icon: "",
+ parentId: updateData.parentTag?.id,
+ });
if (error) {
updating.value = false;
@@ -90,6 +176,7 @@
toast.success(t("tags.toast.tag_updated"));
tag.value = data;
+
closeDialog(DialogID.UpdateTag);
updating.value = false;
}
@@ -145,6 +232,11 @@
:label="$t('components.tag.create_modal.tag_description')"
:max-length="1000"
/>
+ <TagSingleSelector
+ v-model="updateData.parentTag"
+ :tags="availableParentTags"
+ :name="$t('components.tag.create_modal.tag_parent')"
+ />
<ColorSelector
v-model="updateData.color"
:label="$t('components.tag.create_modal.tag_color')"
@@ -176,6 +268,13 @@
<MdiPackageVariant class="size-7" />
</div>
<div>
+ <div v-if="tag?.parentId" class="flex flex-wrap items-center gap-2">
+ <template v-for="parent in getBreadcrumbPath()" :key="parent.id">
+ <TagChip :tag="parent" size="sm" />
+ <span class="text-foreground/40">/</span>
+ </template>
+ <TagChip :tag="tag" size="sm" hide-icon />
+ </div>
<h1 class="flex items-center gap-3 pb-1 text-2xl">
{{ tag ? tag.name : "" }}
<Badge v-if="items && items.totalPrice" variant="secondary" class="ml-2">