Skip to content

Commit d663ec6

Browse files
authored
fix: missing fallback icon to failed service images (#116)
1 parent 48cfb83 commit d663ec6

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/lib/components/icons/icons.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@
6666
social: UsersRound,
6767
media: Film,
6868
creativity: Paintbrush,
69-
categoryFallback: Circle
69+
fallback: Box
7070
} as const;
7171
</script>

src/lib/features/awesome-privacy/components/category-icon.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
const slug = $derived(awesomePrivacy.slugify(category));
3030
3131
const IconComponent = $derived(
32-
(categoryIcons as Record<string, typeof Icons.categoryFallback>)[slug] ?? Icons.categoryFallback
32+
(categoryIcons as Record<string, typeof Icons.fallback>)[slug] ?? Icons.fallback
3333
);
3434
</script>
3535

src/lib/features/awesome-privacy/components/service-logo.svelte

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
import type { Service } from '../types';
55
6+
import { Icons } from '$lib/components/icons/icons.svelte';
67
import { cn } from '$lib/utils/cn';
78
89
type ServiceLogoProps = HTMLAttributes<HTMLImageElement> & {
@@ -11,17 +12,28 @@
1112
1213
let { service, class: klass, ...props }: ServiceLogoProps = $props();
1314
15+
let error = $state(false);
16+
1417
const fallback = $derived(`https://icon.horse/icon/${service.url}`);
1518
</script>
1619

17-
<img
18-
referrerpolicy="no-referrer"
19-
src={service.icon ?? fallback}
20-
onerror={(e) => {
21-
const target = e.target as HTMLImageElement;
22-
if (target.src !== fallback) target.src = fallback;
23-
}}
24-
alt={service.name}
25-
class={cn('shrink-0 object-contain', klass)}
26-
{...props}
27-
/>
20+
{#if error}
21+
<Icons.fallback class={cn('shrink-0', klass)} />
22+
{:else}
23+
<img
24+
referrerpolicy="no-referrer"
25+
src={service.icon ?? fallback}
26+
onerror={(e) => {
27+
const target = e.target as HTMLImageElement;
28+
29+
if (target.src !== fallback) {
30+
target.src = fallback;
31+
} else if (target.src === fallback) {
32+
error = true;
33+
}
34+
}}
35+
alt={service.name}
36+
class={cn('shrink-0 object-contain', klass)}
37+
{...props}
38+
/>
39+
{/if}

0 commit comments

Comments
 (0)