All files / src/components ImageCarousel.vue

81.81% Statements 54/66
68.88% Branches 31/45
81.25% Functions 13/16
86.66% Lines 52/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324  61x   2x           2x                                                                                     86x         1x                     24x         24x 24x 24x 24x     2x 24x     24x 80x 80x 1x   79x 79x 76x 76x 76x   3x 3x         24x 26x 26x 26x 79x       26x       24x 36x       7x 7x         1x 1x         1x       1x 1x 1x 1x 1x 1x 1x 1x 1x         24x                                 24x 24x       24x 26x   2x 2x                                                                                                                                                                                                                                                                                                              
<template>
  <div class="image-carousel">
    <div v-if="loading" class="carousel-container loading-state">
      <div class="loading-spinner">
        <font-awesome-icon icon="fa-solid fa-spinner" spin />
        <span class="ms-2">Loading images...</span>
      </div>
    </div>
    <div v-else-if="!currentImageUrl" class="carousel-container error-state">
      <div class="error-message">
        <font-awesome-icon icon="fa-solid fa-exclamation-circle" />
        <span class="ms-2">Image not available</span>
      </div>
    </div>
    <div
      v-else
      class="carousel-container"
      @touchstart.passive="onTouchStart"
      @touchend.passive="onTouchEnd"
    >
      <img
        :src="currentImageUrl"
        :alt="`Image ${currentIndex + 1}`"
        class="carousel-image"
        @error="handleImageError"
      />
 
      <!-- Navigation arrows -->
      <button
        v-if="images.length > 1"
        :disabled="currentIndex === 0"
        class="carousel-arrow carousel-arrow-left"
        @click.stop="previousImage"
      >
        <font-awesome-icon icon="fa-solid fa-chevron-left" />
      </button>
      <button
        v-if="images.length > 1"
        :disabled="currentIndex === images.length - 1"
        class="carousel-arrow carousel-arrow-right"
        @click.stop="nextImage"
      >
        <font-awesome-icon icon="fa-solid fa-chevron-right" />
      </button>
 
      <!-- Counter -->
      <div v-if="images.length > 1" class="carousel-counter" @click.stop>
        {{ currentIndex + 1 }} / {{ images.length }}
      </div>
 
      <!-- Dots indicator -->
      <div v-if="images.length > 1 && images.length <= 10" class="carousel-dots">
        <button
          v-for="(_, idx) in images"
          :key="idx"
          class="carousel-dot"
          :class="{ active: idx === currentIndex }"
          @click.stop="goToImage(idx)"
        />
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import api from '@/services/api'
 
const props = defineProps<{
  images: string[]
  itemId: string
}>()
 
const currentIndex = ref(0)
const imageUrls = ref<string[]>([])
const loading = ref(true)
const retriedKeys = ref<Set<string>>(new Set())
 
// Cache presigned URLs with TTL (Lambda URLs expire in 1 hour — refresh at 55 min)
const PRESIGNED_URL_TTL_MS = 55 * 60 * 1000
const urlCache = new Map<string, { url: string; expiresAt: number }>()
 
// Get presigned read URL from API, with TTL cache
const getPresignedReadUrl = async (key: string): Promise<string> => {
  const cached = urlCache.get(key)
  if (cached && Date.now() < cached.expiresAt) {
    return cached.url
  }
  try {
    const response = await api.post('/v1/upload/presigned-read-url', { key })
    const url: string = response.data.readUrl
    urlCache.set(key, { url, expiresAt: Date.now() + PRESIGNED_URL_TTL_MS })
    return url
  } catch (error) {
    console.error('Error getting presigned read URL for key:', key, error)
    return ''
  }
}
 
// Load all presigned URLs
const loadImageUrls = async () => {
  loading.value = true
  retriedKeys.value.clear()
  try {
    imageUrls.value = await Promise.all(props.images.map((key) => getPresignedReadUrl(key)))
  } catch (error) {
    console.error('Error loading image URLs:', error)
  } finally {
    loading.value = false
  }
}
 
const currentImageUrl = computed(() => {
  return imageUrls.value[currentIndex.value] || ''
})
 
function nextImage() {
  Eif (currentIndex.value < props.images.length - 1) {
    currentIndex.value++
  }
}
 
function previousImage() {
  Eif (currentIndex.value > 0) {
    currentIndex.value--
  }
}
 
function goToImage(index: number) {
  currentIndex.value = index
}
 
async function handleImageError() {
  const key = props.images[currentIndex.value]
  Iif (!key || retriedKeys.value.has(key)) return // already retried, accept error state
  retriedKeys.value.add(key)
  urlCache.delete(key)
  const newUrl = await getPresignedReadUrl(key)
  Eif (newUrl) {
    const updated = [...imageUrls.value]
    updated[currentIndex.value] = newUrl
    imageUrls.value = updated
  }
}
 
// Touch swipe support
const touchStartX = ref<number | null>(null)
 
function onTouchStart(event: TouchEvent) {
  if (event.changedTouches.length > 0) {
    touchStartX.value = event.changedTouches[0]!.clientX
  }
}
 
function onTouchEnd(event: TouchEvent) {
  if (touchStartX.value === null || event.changedTouches.length === 0) return
  const delta = event.changedTouches[0]!.clientX - touchStartX.value
  touchStartX.value = null
  if (delta < -50) nextImage()
  else if (delta > 50) previousImage()
}
 
// Load URLs on mount
onMounted(() => {
  loadImageUrls()
})
 
// Reset index and reload URLs when images change
watch(
  () => props.images,
  () => {
    currentIndex.value = 0
    loadImageUrls()
  }
)
</script>
 
<style scoped>
.image-carousel {
  width: 100%;
  max-width: 100%;
}
 
.carousel-container {
  position: relative;
  width: 100%;
  padding-bottom: 75%; /* 4:3 aspect ratio */
  background-color: var(--bw-surface-muted);
  border-radius: 0.25rem;
  overflow: hidden;
}
 
.carousel-container.loading-state,
.carousel-container.error-state {
  display: flex;
  align-items: center;
  justify-content: center;
}
 
.loading-spinner,
.error-message {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  color: #6c757d;
  font-size: 0.875rem;
}
 
.carousel-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: opacity 0.3s ease;
}
 
.carousel-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: all 0.2s;
  padding: 0;
}
 
.carousel-arrow:hover:not(:disabled) {
  background-color: rgba(0, 0, 0, 0.8);
  transform: translateY(-50%) scale(1.1);
}
 
.carousel-arrow:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}
 
.carousel-arrow-left {
  left: 8px;
}
 
.carousel-arrow-right {
  right: 8px;
}
 
.carousel-counter {
  position: absolute;
  top: 8px;
  right: 8px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 500;
  z-index: 10;
}
 
.carousel-dots {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 10;
}
 
.carousel-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.8);
  background-color: transparent;
  cursor: pointer;
  padding: 0;
  transition: all 0.2s;
}
 
.carousel-dot:hover {
  background-color: rgba(255, 255, 255, 0.6);
  transform: scale(1.2);
}
 
.carousel-dot.active {
  background-color: rgba(255, 255, 255, 0.9);
  border-color: white;
}
 
/* Mobile adjustments */
@media (max-width: 767px) {
  .carousel-arrow {
    width: 32px;
    height: 32px;
  }
 
  .carousel-arrow-left {
    left: 4px;
  }
 
  .carousel-arrow-right {
    right: 4px;
  }
 
  .carousel-counter {
    font-size: 0.7rem;
    padding: 3px 10px;
  }
}
</style>