drop-server-bak/components/AddLibraryButton.vue

154 lines
5.1 KiB
Vue
Raw Permalink Normal View History

2025-01-19 16:29:29 +11:00
<template>
2025-04-13 21:44:29 -04:00
<div
class="inline-flex w-full group hover:scale-105 transition-all duration-200"
>
2025-01-28 15:16:34 +11:00
<LoadingButton
:loading="isLibraryLoading"
:style="'none'"
class="transition w-full inline-flex items-center justify-center h-full gap-x-2 rounded-none rounded-l-md bg-white/10 hover:bg-white/20 text-zinc-100 backdrop-blur px-5 py-3 active:scale-95"
2025-04-13 21:44:29 -04:00
@click="() => toggleLibrary()"
2025-01-19 16:29:29 +11:00
>
{{ inLibrary ? $t("library.inLib") : $t("library.addToLib") }}
2025-01-28 15:16:34 +11:00
<CheckIcon v-if="inLibrary" class="-mr-0.5 h-5 w-5" aria-hidden="true" />
<PlusIcon v-else class="-mr-0.5 h-5 w-5" aria-hidden="true" />
</LoadingButton>
<!-- Collections dropdown -->
<Menu as="div" class="relative">
<MenuButton
2025-01-28 15:16:34 +11:00
as="div"
class="transition cursor-pointer inline-flex items-center rounded-r-md h-full ml-[2px] bg-white/10 hover:bg-white/20 backdrop-blur py-3.5 px-2 justify-center focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/20"
>
<ChevronDownIcon class="h-5 w-5 text-white" aria-hidden="true" />
</MenuButton>
2025-01-19 16:29:29 +11:00
<transition
enter-active-class="transition ease-out duration-100"
enter-from-class="transform opacity-0 scale-95"
enter-to-class="transform opacity-100 scale-100"
leave-active-class="transition ease-in duration-75"
leave-from-class="transform opacity-100 scale-100"
leave-to-class="transform opacity-0 scale-95"
>
2025-01-28 15:16:34 +11:00
<MenuItems
2025-03-11 11:47:35 +11:00
class="absolute right-0 z-50 mt-2 w-72 origin-top-right rounded-md bg-zinc-800/90 backdrop-blur shadow-lg focus:outline-none"
2025-01-28 15:16:34 +11:00
>
<div class="p-2">
2025-01-28 15:16:34 +11:00
<div
class="font-display uppercase px-3 py-2 text-sm font-semibold text-zinc-500"
>
{{ $t("library.collection.collections") }}
</div>
<div
class="flex flex-col gap-y-2 py-1 max-h-[150px] overflow-y-auto"
>
2025-01-28 15:16:34 +11:00
<div
v-if="collections.length === 0"
class="px-3 py-2 text-sm text-zinc-500"
2025-01-19 16:29:29 +11:00
>
{{ $t("library.collection.noCollections") }}
2025-01-28 15:16:34 +11:00
</div>
<MenuItem
v-for="(collection, collectionIdx) in collections"
:key="collection.id"
v-slot="{ active }"
>
<button
:class="[
active ? 'bg-zinc-700/90' : '',
'group flex w-full items-center justify-between rounded-md px-3 py-2 text-sm text-zinc-200',
]"
2025-01-28 16:10:42 +11:00
@click="() => toggleCollection(collection.id)"
2025-01-28 15:16:34 +11:00
>
<span>{{ collection.name }}</span>
<CheckIcon
v-if="inCollections[collectionIdx]"
class="h-5 w-5 text-blue-400"
aria-hidden="true"
/>
</button>
</MenuItem>
</div>
<div class="border-t border-zinc-700 pt-1">
<LoadingButton
:loading="false"
class="w-full"
2025-04-13 21:44:29 -04:00
@click="createCollectionModal = true"
2025-01-19 16:29:29 +11:00
>
<PlusIcon class="mr-2 h-4 w-4" />
{{ $t("library.collection.addToNew") }}
2025-01-28 15:16:34 +11:00
</LoadingButton>
</div>
2025-01-19 16:29:29 +11:00
</div>
</MenuItems>
</transition>
</Menu>
</div>
2025-01-28 15:16:34 +11:00
<ModalCreateCollection
2025-01-28 15:16:34 +11:00
v-model="createCollectionModal"
2025-04-13 21:44:29 -04:00
:game-id="props.gameId"
2025-01-28 15:16:34 +11:00
/>
2025-01-19 16:29:29 +11:00
</template>
<script setup lang="ts">
import { PlusIcon, ChevronDownIcon, CheckIcon } from "@heroicons/vue/24/solid";
import { Menu, MenuButton, MenuItems, MenuItem } from "@headlessui/vue";
const props = defineProps<{
2025-01-28 15:16:34 +11:00
gameId: string;
}>();
2025-01-28 15:16:34 +11:00
const isLibraryLoading = ref(false);
const { t } = useI18n();
2025-01-28 15:16:34 +11:00
const createCollectionModal = ref(false);
const collections = await useCollections();
const library = await useLibrary();
2025-01-28 15:16:34 +11:00
const inLibrary = computed(
2025-04-13 21:44:29 -04:00
() => library.value.entries.findIndex((e) => e.gameId == props.gameId) != -1,
2025-01-28 15:16:34 +11:00
);
const inCollections = computed(() =>
2025-01-28 16:10:42 +11:00
collections.value.map(
2025-04-13 21:44:29 -04:00
(e) => e.entries.findIndex((e) => e.gameId == props.gameId) != -1,
),
2025-01-28 15:16:34 +11:00
);
2025-01-28 15:16:34 +11:00
async function toggleLibrary() {
isLibraryLoading.value = true;
try {
2025-03-14 12:22:08 +11:00
await $dropFetch("/api/v1/collection/default/entry", {
method: inLibrary.value ? "DELETE" : "POST",
2025-01-28 15:16:34 +11:00
body: {
id: props.gameId,
},
failTitle: t("errors.library.add.title"),
2025-01-28 15:16:34 +11:00
});
await refreshLibrary();
2025-01-28 15:16:34 +11:00
} finally {
isLibraryLoading.value = false;
}
}
2025-01-28 16:10:42 +11:00
async function toggleCollection(id: string) {
try {
const collection = collections.value.find((e) => e.id == id);
if (!collection) return;
const index = collection.entries.findIndex((e) => e.gameId == props.gameId);
2025-01-28 16:10:42 +11:00
await $dropFetch(`/api/v1/collection/:id/entry`, {
method: index == -1 ? "POST" : "DELETE",
params: { id },
2025-01-28 16:10:42 +11:00
body: {
id: props.gameId,
},
failTitle: t("errors.library.add.title"),
2025-01-28 16:10:42 +11:00
});
await refreshCollection(id);
} finally {
/* empty */
2025-01-28 16:10:42 +11:00
}
}
2025-01-19 16:29:29 +11:00
</script>