web/src/components/things/ServiceCard.astro

96 lines · 2485 bytes

 1---
 2import { formatLink } from '@utils/parse-markdown';
 3import type { Service } from 'src/types/Service';
 4import FontAwesome from '@components/form/FontAwesome.svelte';
 5import SaveListing from '@components/things/SaveListing.svelte';
 6import { slugify } from '@utils/fetch-data';
 7
 8interface Props {
 9	service: Service;
10	categoryName: string;
11	sectionName: string;
12}
13
14const { service, sectionName, categoryName } = Astro.props;
15---
16
17<script>
18	document.addEventListener('DOMContentLoaded', () => {
19		const serviceIcons =
20			document.querySelectorAll<HTMLImageElement>('.service-icon');
21		const broke = '/broken-image.png';
22
23		serviceIcons.forEach(function (icon) {
24			icon.onerror = function () {
25				const imgElement = this as HTMLImageElement;
26				const serviceUrl = imgElement.getAttribute('data-service-url');
27				const newSrcAttribute = imgElement.src.includes('on.ho')
28					? broke
29					: `https://icon.horse/icon/${serviceUrl}`;
30				imgElement.src =
31					imgElement.src !== newSrcAttribute ? newSrcAttribute : broke;
32				imgElement.onerror = null;
33			};
34		});
35	});
36</script>
37
38<div class="service" id={slugify(service.name)}>
39	<div class="service-head">
40		<a
41			class="service-title"
42			href={`/${slugify(categoryName)}/${slugify(sectionName)}/${slugify(service.name)}`}
43		>
44			<h4>{service.name}</h4>
45		</a>
46		{service.followWith && <p class="follow-with">({service.followWith})</p>}
47	</div>
48
49	<div class="save-listing">
50		<SaveListing
51			client:visible
52			categoryName={categoryName}
53			sectionName={sectionName}
54			serviceName={service.name}
55		/>
56	</div>
57
58	<div class="service-body">
59		<img
60			width="40"
61			height="40"
62			loading="lazy"
63			decoding="async"
64			class="service-icon"
65			alt={`${service.name} Icon`}
66			data-service-url={formatLink(service.url)}
67			src={service.icon || `https://icon.horse/icon/${formatLink(service.url)}`}
68		/>
69		<div class="service-body">
70			<p set:html={service.description} />
71		</div>
72	</div>
73
74	<div class="service-links">
75		<a class="link" href={service.url}>
76			<FontAwesome iconName="website" />
77			<span>{formatLink(service.url)}</span>
78		</a>
79		{
80			service.github && (
81				<a class="link" href={`https://github.com/${service.github}`}>
82					<FontAwesome iconName="sourceCode" /> GitHub
83				</a>
84			)
85		}
86		<a
87			href={`/${slugify(categoryName)}/${slugify(sectionName)}/${slugify(service.name)}`}
88		>
89			<FontAwesome iconName="viewReport" /> View Report ➔
90		</a>
91	</div>
92</div>
93
94<style lang="scss">
95	@use './service-card.scss';
96</style>