web/src/components/things/LatestUpdates.astro

176 lines · 4101 bytes

  1---
  2import { fetchChangelog } from '@utils/fetch-changelog';
  3import { fetchData, slugify } from '@utils/fetch-data';
  4import ServiceIcon from './ServiceIcon.astro';
  5import type { Category, Service } from '../../types/Service';
  6
  7interface Props {
  8	limit?: number;
  9	categories?: Category[];
 10}
 11
 12const { limit = 12, categories: categoriesProp } = Astro.props;
 13
 14const categories = categoriesProp ?? ((await fetchData())?.categories || []);
 15const { entries } = await fetchChangelog();
 16
 17const findService = (
 18	categoryName: string,
 19	sectionName: string,
 20	serviceName: string,
 21): Service | undefined => {
 22	const c = categories.find((cat) => cat.name === categoryName);
 23	const s = c?.sections.find((sec) => sec.name === sectionName);
 24	return s?.services?.find((sv) => sv.name === serviceName);
 25};
 26
 27const additions = entries
 28	.slice()
 29	.sort((a, b) => (a.date < b.date ? 1 : -1))
 30	.flatMap((e) =>
 31		(e.changes?.services?.added || []).map((s) => ({
 32			...s,
 33			date: e.date,
 34			author: e.pr?.author,
 35			authorUrl: e.pr?.author ? `https://github.com/${e.pr.author}` : null,
 36			service: findService(s.category, s.section, s.name),
 37		})),
 38	)
 39	.slice(0, limit);
 40
 41const formatDate = (iso: string) =>
 42	new Date(iso).toLocaleDateString('en-GB', {
 43		day: 'numeric',
 44		month: 'short',
 45		year: 'numeric',
 46	});
 47---
 48
 49<div class="latest-updates">
 50	{
 51		additions.length > 0 ? (
 52			<>
 53				<p class="intro">
 54					Newest privacy-respecting software and services added to the
 55					directory.
 56				</p>
 57				<ul>
 58					{additions.map((a) => (
 59						<li>
 60							<a
 61								class="service-link"
 62								href={`/${slugify(a.category)}/${slugify(a.section)}/${slugify(a.name)}`}
 63							>
 64								{a.service && (
 65									<ServiceIcon
 66										name={a.name}
 67										url={a.service.url}
 68										icon={a.service.icon}
 69										size={32}
 70									/>
 71								)}
 72								<span class="name">{a.name}</span>
 73							</a>
 74							<p
 75								class="meta"
 76								title={`In ${a.category}${a.author ? ` submitted by @${a.author}` : ''} on ${formatDate(a.date)}`}
 77							>
 78								In{' '}
 79								<a class="inline" href={`/${slugify(a.category)}`}>
 80									{a.category}
 81								</a>
 82								{a.author && (
 83									<>
 84										{' '}
 85										submitted by{' '}
 86										<a
 87											class="inline"
 88											href={a.authorUrl!}
 89											target="_blank"
 90											rel="noopener noreferrer"
 91										>
 92											@{a.author}
 93										</a>
 94									</>
 95								)}{' '}
 96								on <time datetime={a.date}>{formatDate(a.date)}</time>
 97							</p>
 98						</li>
 99					))}
100				</ul>
101				<div class="footer">
102					<a class="small-button" href="/changelog">
103						View full changelog &rarr;
104					</a>
105				</div>
106			</>
107		) : (
108			<p class="intro">No recent updates to show.</p>
109		)
110	}
111</div>
112
113<style lang="scss">
114	.latest-updates {
115		background: var(--accent-fg);
116		border: var(--border-heavy);
117		border-radius: var(--curve-sm);
118		box-shadow: var(--shadow-md);
119		padding: var(--space-md);
120		width: 100%;
121		box-sizing: border-box;
122		margin: 0 auto;
123		.intro {
124			margin: 0 0 var(--space-sm) 0;
125		}
126		ul {
127			list-style: none;
128			padding: 0;
129			margin: 0;
130			display: grid;
131			grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
132			gap: var(--space-sm) var(--space-md);
133			li {
134				display: flex;
135				flex-direction: column;
136				min-width: 0;
137				padding: var(--space-xs) 0;
138				.service-link {
139					display: flex;
140					align-items: center;
141					gap: var(--space-sm);
142					text-decoration: none;
143					color: var(--foreground);
144					min-width: 0;
145					&:hover .name {
146						color: var(--accent);
147					}
148					.name {
149						font-weight: bold;
150						overflow: hidden;
151						text-overflow: ellipsis;
152						white-space: nowrap;
153					}
154				}
155				.meta {
156					font-size: var(--text-sm);
157					opacity: var(--opacity-muted);
158					margin: 2px 0 0 0;
159					max-width: 100%;
160					overflow: hidden;
161					text-overflow: ellipsis;
162					white-space: nowrap;
163					a.inline {
164						color: inherit;
165						text-decoration: underline;
166					}
167				}
168			}
169		}
170		.footer {
171			display: flex;
172			justify-content: flex-end;
173			margin-top: var(--space-md);
174		}
175	}
176</style>