33<script setup lang="ts">
44import { KeyboardSensor , PointerSensor , useDraggable } from ' @dnd-kit/vue'
55import { CheckIcon , ClipboardCopyIcon , EditIcon , MoreHorizontalIcon } from ' @modrinth/assets'
6- import { defineMessages , IconButton , useFormatDateTime , useVIntl } from ' @modrinth/ui'
7- import { computed , onMounted , ref , watch } from ' vue'
6+ import {
7+ defineMessages ,
8+ IconButton ,
9+ useDebugLogger ,
10+ useFormatDateTime ,
11+ useVIntl ,
12+ } from ' @modrinth/ui'
13+ import { computed , onBeforeUnmount , onMounted , ref , watch } from ' vue'
814
915import type { InstanceScreenshot } from ' @/helpers/instance'
10- const loadedScreenshotUrls = new Set <string >()
1116
1217const props = defineProps <{
1318 screenshot: InstanceScreenshot
@@ -29,9 +34,12 @@ const emit = defineEmits<{
2934
3035const card = ref <HTMLElement >()
3136const image = ref <HTMLImageElement >()
32- const loaded = ref (loadedScreenshotUrls . has ( props . screenshot . url ) )
37+ const imageReady = ref (false )
3338const { formatMessage } = useVIntl ()
39+ const debugImage = useDebugLogger (' Screenshots:Card' )
3440const formatTime = useFormatDateTime ({ dateStyle: ' medium' , timeStyle: ' short' })
41+ let loadStartedAt = performance .now ()
42+ let loadGeneration = 0
3543const messages = defineMessages ({
3644 select: { id: ' app.screenshots.select' , defaultMessage: ' Select {name}' },
3745 deselect: { id: ' app.screenshots.deselect' , defaultMessage: ' Deselect {name}' },
@@ -69,19 +77,86 @@ function activate(event: MouseEvent | KeyboardEvent) {
6977 emit (' activate' , event )
7078}
7179
72- function markImageLoaded() {
73- loadedScreenshotUrls .add (props .screenshot .url )
74- loaded .value = true
80+ async function markImageLoaded() {
81+ const loadedImage = image .value
82+ const loadedUrl = props .screenshot .url
83+ const generation = loadGeneration
84+ if (! loadedImage ) return
85+
86+ try {
87+ await loadedImage .decode ()
88+ } catch {
89+ if (! loadedImage .complete || loadedImage .naturalWidth === 0 ) return
90+ }
91+
92+ await new Promise <void >((resolve ) => {
93+ window .requestAnimationFrame (() => window .requestAnimationFrame (() => resolve ()))
94+ })
95+
96+ if (
97+ generation !== loadGeneration ||
98+ image .value !== loadedImage ||
99+ props .screenshot .url !== loadedUrl
100+ ) {
101+ return
102+ }
103+
104+ const wasReady = imageReady .value
105+ imageReady .value = true
106+ if (! wasReady ) {
107+ debugImage (' image loaded' , {
108+ id: props .screenshot .id ,
109+ fileName: props .screenshot .file_name ,
110+ url: props .screenshot .url ,
111+ loadDurationMs: performance .now () - loadStartedAt ,
112+ naturalWidth: image .value ?.naturalWidth ,
113+ naturalHeight: image .value ?.naturalHeight ,
114+ })
115+ }
116+ }
117+
118+ function markImageFailed(event : Event ) {
119+ debugImage (' image failed' , {
120+ id: props .screenshot .id ,
121+ fileName: props .screenshot .file_name ,
122+ url: props .screenshot .url ,
123+ loadDurationMs: performance .now () - loadStartedAt ,
124+ event ,
125+ })
75126}
76127
77128onMounted (() => {
129+ debugImage (' mounted' , {
130+ id: props .screenshot .id ,
131+ fileName: props .screenshot .file_name ,
132+ url: props .screenshot .url ,
133+ complete: image .value ?.complete ,
134+ })
78135 if (image .value ?.complete && image .value .naturalWidth > 0 ) markImageLoaded ()
79136})
80137
138+ onBeforeUnmount (() => {
139+ loadGeneration += 1
140+ debugImage (' unmounted' , {
141+ id: props .screenshot .id ,
142+ fileName: props .screenshot .file_name ,
143+ url: props .screenshot .url ,
144+ loaded: imageReady .value ,
145+ })
146+ })
147+
81148watch (
82149 () => props .screenshot .url ,
83- (url ) => {
84- loaded .value = loadedScreenshotUrls .has (url )
150+ (url , previousUrl ) => {
151+ loadGeneration += 1
152+ loadStartedAt = performance .now ()
153+ imageReady .value = false
154+ debugImage (' source changed' , {
155+ id: props .screenshot .id ,
156+ fileName: props .screenshot .file_name ,
157+ previousUrl ,
158+ url ,
159+ })
85160 },
86161)
87162 </script >
@@ -91,7 +166,7 @@ watch(
91166 ref =" card"
92167 role =" button"
93168 tabindex =" 0"
94- class =" group relative aspect-video min-w-0 cursor-pointer overflow-hidden rounded-xl border border-solid border-surface-5 bg-surface-2 p-0 text-left shadow-sm transition-[filter] hover:brightness-110 focus-visible:outline focus-visible:outline-2 focus-visible:outline-brand"
169+ class =" group relative isolate aspect-video min-w-0 cursor-pointer overflow-hidden rounded-xl border border-solid border-surface-5 bg-surface-2 p-0 text-left shadow-sm transition-[filter] hover:brightness-110 focus-visible:outline focus-visible:outline-2 focus-visible:outline-brand"
95170 :class =" {
96171 '!border-contrast brightness-110': selected,
97172 '!border-brand ring-2 ring-brand animate-pulse': highlighted,
@@ -115,7 +190,7 @@ watch(
115190 >
116191 <button
117192 type =" button"
118- class =" selection-button group/selection absolute right-0.5 top-0 z-[2 ] flex size-[50px] cursor-pointer items-start justify-center border-0 bg-transparent p-0 pt-4"
193+ class =" selection-button group/selection absolute right-0.5 top-0 z-[3 ] flex size-[50px] cursor-pointer items-start justify-center border-0 bg-transparent p-0 pt-4"
119194 :aria-label ="
120195 formatMessage(selected ? messages.deselect : messages.select, {
121196 name: screenshot.file_name,
@@ -134,19 +209,25 @@ watch(
134209 <CheckIcon v-if =" selected " class="relative size-4 invert [stroke-width:3 ]" />
135210 </span >
136211 </button >
137- <div v-if =" !loaded" class =" absolute inset-0 animate-pulse bg-surface-3" />
138212 <img
139213 ref =" image"
140214 :src =" screenshot.url"
141215 :alt =" screenshot.file_name"
142- loading =" lazy"
216+ loading =" eager"
217+ decoding =" async"
143218 draggable =" false"
144- class =" h-full w-full object-cover transition duration-200 "
145- :class =" loaded ? 'opacity-100' : 'opacity-0'"
219+ class =" screenshot-card-fade absolute inset-0 z-[1] h-full w-full object-cover"
220+ :class =" imageReady ? 'opacity-100' : 'opacity-0'"
146221 @load =" markImageLoaded"
222+ @error =" markImageFailed"
147223 />
148224 <div
149- class =" absolute inset-x-0 bottom-0 flex items-end justify-between gap-2 bg-gradient-to-t from-surface-1 to-transparent p-3 pt-[120px] text-contrast opacity-0 transition-opacity duration-200 group-hover:opacity-100 group-focus-within:opacity-100"
225+ aria-hidden =" true"
226+ class =" pointer-events-none absolute inset-0 bg-button-bg"
227+ :class =" { 'animate-pulse': !imageReady }"
228+ />
229+ <div
230+ class =" absolute inset-x-0 bottom-0 z-[2] flex items-end justify-between gap-2 bg-gradient-to-t from-surface-1 to-transparent p-3 pt-[120px] text-contrast opacity-0 transition-opacity duration-200 group-hover:opacity-100 group-focus-within:opacity-100"
150231 >
151232 <div class =" min-w-0" >
152233 <div v-tooltip =" screenshot.file_name" class =" truncate text-sm font-semibold" >
@@ -193,3 +274,9 @@ watch(
193274 </div >
194275 </article >
195276</template >
277+
278+ <style scoped>
279+ .screenshot-card-fade {
280+ transition : opacity 350ms ease-in-out ;
281+ }
282+ </style >
0 commit comments