Some checks failed
ci / ci (22, ubuntu-latest) (push) Has been cancelled
Nuxt 4 + Supabase + Flightics API. Incluye búsqueda de vuelos, inspiraciones, watchlist, tracking de precios y mapa interactivo. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
33 lines
1007 B
TypeScript
33 lines
1007 B
TypeScript
const imageCache = reactive<Record<string, { thumb_url: string; image_url: string; photographer: string; photographer_url: string } | null>>({})
|
|
const pending = new Set<string>()
|
|
|
|
export function useDestinationImages() {
|
|
async function fetchImage(cityName: string) {
|
|
const key = cityName.trim().toLowerCase()
|
|
if (key in imageCache || pending.has(key)) return
|
|
pending.add(key)
|
|
|
|
try {
|
|
const data = await $fetch<{ thumb_url: string; image_url: string; photographer: string; photographer_url: string } | null>('/api/destination-image', {
|
|
query: { city: cityName },
|
|
})
|
|
imageCache[key] = data
|
|
} catch {
|
|
imageCache[key] = null
|
|
} finally {
|
|
pending.delete(key)
|
|
}
|
|
}
|
|
|
|
function getImage(cityName: string) {
|
|
const key = cityName.trim().toLowerCase()
|
|
return imageCache[key] ?? null
|
|
}
|
|
|
|
function prefetch(cityNames: string[]) {
|
|
cityNames.forEach(name => fetchImage(name))
|
|
}
|
|
|
|
return { fetchImage, getImage, prefetch, imageCache }
|
|
}
|