web/src/components/things/ServiceCard.astro

92 lines · 2648 bytes

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