Skip to content

A self-hosted media server and file management system

11.8 KB
Raw Blame
<script setup lang="ts">
import type { HTMLAttributes } from 'vue';
import type { ImageResource } from '@/contracts/media';

import { formatFileSize, toFormattedDate, toTimeSpan } from '@/service/util';
import { CedarDelete2 } from '@/components/cedar-ui/icons';
import { ButtonIcon } from '@/components/cedar-ui/button';
import { computed } from 'vue';
import { cn } from '@aminnausin/cedar-ui';

import VideoControlWrapper from '@/components/video/VideoControlWrapper.vue';
import LazyImage from '@/components/lazy/LazyImage.vue';

import ProiconsDelete from '~icons/proicons/delete';
import ProiconsStar from '~icons/proicons/star';
import PrimeSave from '~icons/prime/save';

const props = withDefaults(
    defineProps<{
        data: ImageResource;
        isPrimary?: boolean;
        isAudio?: boolean;
        isFolder?: boolean;
        isPendingDelete?: boolean;
        isReadOnly?: boolean;
    }>(),
    {
        isPrimary: false,
        isAudio: false,
        isPendingDelete: false,
    },
);

const deletionDate = computed(() => {
    if (!props.data.replaced_at) return null;
    const replacedAt = new Date(props.data.replaced_at);
    return new Date(replacedAt.getTime() + 30 * 24 * 60 * 60 * 1000);
});

const isDisabled = computed(() => !!deletionDate.value);

const filename = computed(() => props.data.path.split('/').at(-1));
const imageDate = computed(() => {
    if (deletionDate.value) return { name: `deleted ${toTimeSpan(deletionDate.value)}`, title: toFormattedDate(deletionDate.value) };
    const dateUploadedAt = props.data.created_at ? new Date(props.data.created_at) : new Date();

    return { name: `${props.data.source} ${toTimeSpan(dateUploadedAt)}`, title: toFormattedDate(dateUploadedAt) };
});

const generatePosterStyle = (url?: string): HTMLAttributes['style'] => {
    if (!url) return {};

    // I could use blur hash instead
    return {
        backgroundImage: `url("${url}")`,
        backgroundPosition: 'center',
        backgroundSize: 'cover',
        backgroundRepeat: 'no-repeat',
    };
};

const emit = defineEmits({
    select: () => true,
    deselect: () => true,
    delete: () => true,
    restore: () => true,
});
</script>
<template>
    <div
        :class="
            cn('group relative flex w-full flex-col items-start rounded-lg text-xs shadow-sm transition', 'data-card ring-2 ring-transparent', {
                'pointer-events-none! opacity-50': isDisabled,
                'ring-primary-active/60': isPrimary,
            })
        "
    >
        <div :class="cn('relative flex max-h-48 w-full items-center overflow-clip rounded-t-lg select-none sm:max-h-80')">
            <div
                :class="
                    cn(
                        {
                            'aspect-40/21!': data.type === 'preview',
                            'aspect-2-3!': data.type === 'poster' && isFolder,
                            'aspect-square': isAudio,
                            'aspect-video': !isAudio,
                        },
                        'size-full',
                        $attrs.class,
                    )
                "
            >
                <div class="absolute inset-0 scale-120 blur-sm" :style="generatePosterStyle(data.path)"></div>

                <LazyImage
                    :src="data.path"
                    alt="poster"
                    :animate="false"
                    loading="lazy"
                    :wrapper-class="cn('transition-opacity duration-input')"
                    :class="cn('absolute inset-0 size-full object-contain', { 'aspect-40/21': data.type === 'preview' })"
                />
            </div>

            <!-- Overlay -->
            <div :class="cn('duration-input pointer-events-none absolute inset-0 z-3 flex flex-wrap items-start justify-between gap-1 p-2 transition-[translate,margin]')">
                <Transition
                    enter-from-class="opacity-0"
                    enter-to-class="opacity-100"
                    enter-active-class="transition-opacity duration-100 ease-out"
                    leave-from-class="opacity-100"
                    leave-to-class="opacity-0"
                    leave-active-class="transition-opacity duration-100 ease-in"
                >
                    <VideoControlWrapper v-if="isPrimary" :class="cn('w-fit')">
                        <p :class="cn('pointer-events-auto px-1 text-white text-shadow-lg')">Primary</p>
                    </VideoControlWrapper>
                </Transition>

                <div
                    class="text-foreground-i dark:text-foreground-0 pointer-events-auto ms-auto flex gap-1 opacity-60 transition-opacity duration-200 group-hover:opacity-100"
                    v-if="data.type !== 'preview' && !isReadOnly"
                >
                    <ButtonIcon v-if="isPendingDelete" class="overlay-button hover:ring-1" :type="'button'" title="Undo delete" :variant="'ghost'" @click="$emit('restore')">
                        <template #icon>
                            <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" class="size-4">
                                <path d="M0 0h24v24H0z" fill="none" />
                                <path
                                    fill="none"
                                    stroke="currentColor"
                                    stroke-linecap="round"
                                    stroke-linejoin="round"
                                    stroke-width="1.5"
                                    d="M9.25 12L5.957 8.707A1 1 0 0 1 5.664 8M9.25 4L5.957 7.293A1 1 0 0 0 5.664 8M7.25 19.5h6.25a5.75 5.75 0 0 0 0-11.5H5.664"
                                />
                            </svg>
                        </template>
                    </ButtonIcon>
                    <template v-else-if="!isDisabled">
                        <ButtonIcon
                            class="overlay-button hover:ring-1"
                            :type="'button'"
                            :variant="'ghost'"
                            :title="isPrimary ? 'Already Selected' : 'Select as primary'"
                            :disabled="isPrimary"
                            v-if="!isPrimary"
                            @click="$emit('select')"
                        >
                            <template #icon>
                                <PrimeSave class="size-4" />
                            </template>
                        </ButtonIcon>
                        <ButtonIcon v-else class="overlay-button hover:ring-1" :type="'button'" title="Remove Selection" :variant="'ghost'" @click="$emit('deselect')">
                            <template #icon>
                                <CedarDelete2 class="size-4" />
                            </template>
                        </ButtonIcon>
                        <ButtonIcon
                            v-if="data.source !== 'generated' && (!data.blur_hash || (data.blur_hash && data.user_id))"
                            class="overlay-button hover:ring-1"
                            :type="'button'"
                            title="Delete image"
                            :variant="'ghost'"
                            @click="$emit('delete')"
                        >
                            <template #icon>
                                <ProiconsDelete class="size-4" />
                            </template>
                        </ButtonIcon>
                    </template>
                </div>
                <div class="mt-auto flex w-full">
                    <div class="w-full flex-1">
                        <RouterLink
                            :to="`/profile/${data.user.id}`"
                            :target="'_blank'"
                            class="pointer-events-auto flex h-4 w-fit items-center opacity-60 transition-opacity duration-200 group-hover:opacity-100"
                            v-if="data.user"
                        >
                            <div class="peer h-4 w-6 shrink-0">
                                <LazyImage
                                    :wrapper-class="'w-fit shrink-0 relative size-4'"
                                    class="hover:ring-primary-muted aspect-square rounded-full object-cover ring-1 ring-transparent transition-shadow duration-200"
                                    :src="`https://ui-avatars.com/api/?name=${data.user?.name[0] ?? 'S'}&amp;color=7F9CF5&amp;background=random`"
                                    alt="user"
                                />
                            </div>
                            <div
                                :class="
                                    cn(
                                        'pointer-events-auto -ms-2 flex h-4 items-center overflow-clip rounded-full bg-neutral-900/60 lowercase drop-shadow-md duration-200',
                                        'max-w-0 origin-left transition-[max-width,padding,margin] ease-out',
                                        'peer-hover:-ms-1 peer-hover:max-w-32 peer-hover:px-1.5 peer-hover:ease-in',
                                        'hover:-ms-1 hover:max-w-32 hover:px-1.5 hover:ease-in',
                                    )
                                "
                            >
                                <span class="w-full truncate">
                                    {{ data.user?.name ?? 'system' }}
                                </span>
                            </div>
                        </RouterLink>
                    </div>
                    <div
                        v-if="props.data.width && props.data.height"
                        class="3xl:text-xs pointer-events-none absolute right-2 bottom-2 rounded-full bg-neutral-900/60 px-1.5 py-0.5 font-mono text-[10px] text-white/90 tabular-nums opacity-60 backdrop-blur-sm transition-opacity duration-200 group-hover:opacity-100"
                    >
                        {{ props.data.width }}x{{ props.data.height }}
                    </div>
                </div>
            </div>
        </div>
        <div class="text-foreground-1 flex h-full w-full flex-1 flex-col items-start gap-x-4 gap-y-2 p-3 dark:text-inherit">
            <div class="flex w-full items-center gap-1">
                <p class="w-full truncate sm:text-sm" :title="filename">{{ filename }}</p>

                <Transition
                    enter-from-class="opacity-0"
                    enter-to-class="opacity-100"
                    enter-active-class="transition-opacity duration-100 ease-out"
                    leave-from-class="opacity-100"
                    leave-to-class="opacity-0"
                    leave-active-class="transition-opacity duration-100 ease-in"
                >
                    <ProiconsStar v-if="isPrimary" :class="cn('ml-auto size-4')" />
                </Transition>
            </div>

            <div class="text-foreground-2 -mt-1 flex w-full items-baseline gap-2">
                <span v-if="imageDate" class="min-w-0 flex-1 truncate" :title="imageDate.title">{{ imageDate.name }}</span>
                <span v-if="props.data.size" class="text-foreground-3 shrink-0 font-mono tabular-nums">{{ formatFileSize(props.data.size) }}</span>
            </div>
        </div>
    </div>
</template>
<style scoped lang="css">
.overlay-button {
    --tw-ring-color: var(--color-primary) /* var(--color-purple-600) */;

    width: calc(var(--spacing) * 6) /* 1.5rem = 24px */;
    height: calc(var(--spacing) * 6) /* 1.5rem = 24px */;
    padding: calc(var(--spacing) * 1) /* 0.25rem = 4px */;
    background-color: color-mix(in oklab, var(--color-neutral-900) /* oklch(20.5% 0 0) = #171717 */ 40%, transparent);

    transition-property: text-decoration-color, background-color, box-shadow;

    &:hover {
        @media (hover: hover) {
            background-color: color-mix(in oklab, var(--color-neutral-900) /* oklch(20.5% 0 0) = #171717 */ 60%, transparent);
        }
    }
}
</style>