Files
ghatreh_user/src/components/ProductImage.vue
T

67 lines
1.3 KiB
Vue
Raw Normal View History

2026-06-13 12:20:11 +03:30
<template>
<img
:src="currentSrc"
:class="[imgClass, dimmed && 'product-image--dimmed']"
:alt="alt"
@error="handleError"
/>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
import {
getProductFallbackImage,
resolveProductImageSrc,
type ProductImageSource
} from '@/utils/productImage'
const props = withDefaults(
defineProps<{
product?: ProductImageSource
imgClass?: string
alt?: string
dimmed?: boolean
}>(),
{
imgClass: 'max-h-full max-w-full object-contain',
alt: '',
dimmed: false
}
)
function productImageKey(product?: ProductImageSource): string {
return [
product?.file_id ?? '',
product?.file?.id ?? '',
product?.file?.full_src ?? '',
product?.file?.src ?? '',
product?.file?.url ?? '',
product?.img ?? '',
].join('|')
}
2026-06-13 12:20:11 +03:30
const currentSrc = ref(resolveProductImageSrc(props.product))
const usingFallback = ref(false)
watch(
() => productImageKey(props.product),
() => {
2026-06-13 12:20:11 +03:30
usingFallback.value = false
currentSrc.value = resolveProductImageSrc(props.product)
2026-06-13 12:20:11 +03:30
},
)
function handleError() {
if (usingFallback.value) return
usingFallback.value = true
currentSrc.value = getProductFallbackImage(props.product)
}
</script>
<style scoped>
.product-image--dimmed {
opacity: 0.45;
filter: grayscale(1);
}
</style>