summaryrefslogtreecommitdiff
path: root/apps/web/src/app/changelog/utils.ts
blob: 3f38ab6b2657061fc2f18428291d5fcc2cd3fcc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { allChangelogs } from "content-collections";

export type Change = { type: string; text: string };
export type Release = (typeof allChangelogs)[number];

const knownSectionOrder = ["new", "improved", "fixed", "breaking"];

const knownSectionTitles: Record<string, string> = {
	new: "Features",
	improved: "Improvements",
	fixed: "Fixes",
	breaking: "Breaking Changes",
};

export function getSectionTitle({ type }: { type: string }): string {
	return (
		knownSectionTitles[type] ?? type.charAt(0).toUpperCase() + type.slice(1)
	);
}

export function groupAndOrderChanges({ changes }: { changes: Change[] }) {
	const grouped = changes.reduce<Record<string, Change[]>>((acc, change) => {
		if (!acc[change.type]) acc[change.type] = [];
		acc[change.type].push(change);
		return acc;
	}, {});

	const customTypes = Object.keys(grouped).filter(
		(type) => !knownSectionOrder.includes(type),
	);
	const orderedTypes = [
		...knownSectionOrder.filter((type) => grouped[type]?.length > 0),
		...customTypes,
	];

	return { grouped, orderedTypes };
}

function isPublishedRelease({ published }: Release) {
	return published !== false;
}

export function getSortedReleases() {
	return allChangelogs
		.filter(isPublishedRelease)
		.sort((a, b) =>
			b.version.localeCompare(a.version, undefined, { numeric: true }),
		);
}

export function getReleaseByVersion({ version }: { version: string }) {
	return getSortedReleases().find((release) => release.version === version);
}