From b72af1b4a2e14947949f986a1f24ad028c0031ec Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sat, 10 Feb 2024 02:30:54 -0800
Subject: feat(badges): fallback yotsuba

---
 src/lib/FallbackImage.svelte | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 src/lib/FallbackImage.svelte

(limited to 'src/lib/FallbackImage.svelte')

diff --git a/src/lib/FallbackImage.svelte b/src/lib/FallbackImage.svelte
new file mode 100644
index 00000000..a8f367fb
--- /dev/null
+++ b/src/lib/FallbackImage.svelte
@@ -0,0 +1,32 @@
+<script lang="ts">
+	export let source: string | undefined;
+	export let alternative: string | undefined;
+	export let fallback: string | undefined;
+	export let maxReplaceCount = 1;
+	export let replaceDelay = 1000;
+	export let error = 'http://i2.kym-cdn.com/photos/images/newsfeed/000/290/992/0aa.jpg';
+
+	let replaceCount = 0;
+
+	const delayedReplace = (event: Event, image: string | undefined) => {
+		if (replaceCount >= maxReplaceCount) return;
+
+		setTimeout(() => {
+			(event.target as HTMLImageElement).src = image || '';
+
+			replaceCount += 1;
+		}, replaceDelay);
+	};
+</script>
+
+{#if replaceCount < maxReplaceCount}
+	<img
+		src={source}
+		alt={alternative}
+		loading="lazy"
+		class="badge"
+		on:error={(e) => delayedReplace(e, fallback)}
+	/>
+{:else}
+	<img src={error} alt="Not found" loading="lazy" />
+{/if}
-- 
cgit v1.2.3

