-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
119 lines (107 loc) · 4.13 KB
/
page.tsx
File metadata and controls
119 lines (107 loc) · 4.13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { type Metadata } from "next";
import { redirect } from "next/navigation";
import { type JSX, Suspense } from "react";
import { type ViewType } from "react-diff-view";
import { createSimplePackageSpec } from "^/lib/createSimplePackageSpec";
import { DEFAULT_DIFF_FILES_GLOB } from "^/lib/default-diff-files";
import destination from "^/lib/destination";
import { parseQuery, type QueryParams } from "^/lib/query";
import { simplePackageSpecToString } from "^/lib/SimplePackageSpec";
import decodeParts from "^/lib/utils/decodeParts";
import specsToDiff from "^/lib/utils/specsToDiff";
import splitParts from "^/lib/utils/splitParts";
import BundlephobiaDiff from "./_page/BundlephobiaDiff";
import DiffIntro from "./_page/DiffIntro";
import NpmDiff from "./_page/NpmDiff";
import PackagephobiaDiff from "./_page/PackagephobiaDiff";
import { type DIFF_TYPE_PARAM_NAME } from "./_page/paramNames";
export interface DiffPageProps {
params: Promise<{ parts: string | string[] }>;
searchParams: Promise<QueryParams & { [DIFF_TYPE_PARAM_NAME]: ViewType }>;
}
export async function generateMetadata({
params,
}: DiffPageProps): Promise<Metadata> {
const { parts } = await params;
const specs = splitParts(decodeParts(parts));
const [a, b] = specs.map((spec) => createSimplePackageSpec(spec));
return {
title: `Comparing ${simplePackageSpecToString(a)}...${simplePackageSpecToString(b)}`,
description: `A diff between the npm packages "${simplePackageSpecToString(a)}" and "${simplePackageSpecToString(b)}"`,
};
}
const DiffPageInner = async ({
params,
searchParams,
}: DiffPageProps): Promise<JSX.Element> => {
const { parts } = await params;
const { diffFiles, ...optionsQuery } = await searchParams;
const specsOrVersions = splitParts(decodeParts(parts));
const { redirect: redirectTarget, canonicalSpecs } =
await destination(specsOrVersions);
if (redirectTarget !== false) {
const specsStr = specsToDiff(canonicalSpecs);
const searchStr = Object.entries(await searchParams)
.map(([key, value]) => `${key}=${value}`)
.join("&");
redirect(
`/${specsStr}` + (searchStr?.length > 0 ? `?${searchStr}` : ""),
);
} else {
const options = parseQuery({
// If no diffFiles is passed, use the default.
// This is done here, since we don't want a fall back in the API
diffFiles: diffFiles ?? DEFAULT_DIFF_FILES_GLOB,
...optionsQuery,
});
const [a, b] = canonicalSpecs.map((spec) =>
createSimplePackageSpec(spec),
);
return (
<>
<DiffIntro
className="self-stretch"
a={a}
b={b}
services={
<>
<BundlephobiaDiff
a={a}
b={b}
specs={canonicalSpecs}
suspenseKey={
"bundlephobia-" + canonicalSpecs.join("...")
}
/>
<PackagephobiaDiff
a={a}
b={b}
specs={canonicalSpecs}
suspenseKey={
"packagephobia-" +
canonicalSpecs.join("...")
}
/>
</>
}
options={options}
/>
<NpmDiff
a={a}
b={b}
specs={canonicalSpecs}
options={options}
suspenseKey={JSON.stringify([canonicalSpecs, options])}
/>
</>
);
}
};
const DiffPage = (props: DiffPageProps) => {
return (
<Suspense>
<DiffPageInner {...props} />
</Suspense>
);
};
export default DiffPage;