返回 CodeWhale
page.tsx
根目录 / web / app / [locale] / changelog / page.tsx
1 import Link from "next/link";
2 import { Icon } from "@/components/icon";
3 import { PageHeader } from "@/components/page-header";
4 import { Status } from "@/components/status-badge";
5 import { EmptyState } from "@/components/surface-state";
6 import { ReleaseUpdates } from "@/components/release-updates";
7 import { getReleaseUpdates } from "@/lib/release-updates";
8 import { CHANGELOG, type ChangelogRelease } from "@/lib/changelog.generated";
9 import { changelogAnchor } from "@/scripts/changelog-lib.mjs";
10 import { getFacts } from "@/lib/facts";
11 import { compareVersions } from "@/lib/features";
12 import { fill, getChangelog, getChrome } from "@/lib/i18n/dictionaries";
13 import { REPO_RELEASES_URL, REPO_URL } from "@/lib/i18n/links";
14 import { buildPageMetadata } from "@/lib/page-meta";
15 import { bodyDateLocale } from "@/lib/i18n/fallback";
16 import { FallbackRegion } from "@/components/fallback-region";
17
18 // Published releases resolve at request time; the notes are build-time
19 // from CHANGELOG.md and link to the complete published history.
20 export const dynamic = "force-dynamic";
21
22 export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
23 const { locale } = await params;
24 const t = getChangelog(locale);
25 return buildPageMetadata({
26 path: "/changelog",
27 locale,
28 title: t.metaTitle,
29 description: t.metaDescription,
30 });
31 }
32
33 /** Link labels carry a trailing ↗ in the dictionaries; the icon draws it. */
34 function ext(label: string) {
35 return (
36 <>
37 {label.replace(/\s*↗\s*$/u, "")}
38 <Icon name="external" className="icon" />
39 </>
40 );
41 }
42
43 function formatDate(iso: string, dateLocale: string): string {
44 return new Date(iso).toLocaleDateString(dateLocale, {
45 year: "numeric",
46 month: "short",
47 day: "numeric",
48 // Dates come from ISO timestamps and YYYY-MM-DD headings; render them
49 // as the UTC day they name, not the server's local day.
50 timeZone: "UTC",
51 });
52 }
53
54 export default async function ChangelogPage({ params }: { params: Promise<{ locale: string }> }) {
55 const { locale } = await params;
56 const t = getChangelog(locale);
57 const chrome = getChrome(locale);
58 const facts = await getFacts();
59 const updates = await getReleaseUpdates(facts.latestPublishedRelease);
60 const published = updates.terminal.release;
61 const candidate = facts.version;
62 const candidateIsPublished = published !== null && candidate !== null && published.version === candidate;
63
64 return (
65 <FallbackRegion path="/changelog" locale={locale}>
66 <PageHeader kicker={t.kicker} title={t.title} lede={t.lead} pose="write">
67 <dl className="def-rows changelog-facts">
68 <div className="def-row" data-candidate={candidateIsPublished ? "published" : "unreleased"}>
69 <dt>{t.candidateLabel}</dt>
70 <dd>
71 {candidate && (
72 <Status tone={candidateIsPublished ? "ready" : "idle"}>
73 {candidateIsPublished
74 ? fill(t.candidateMatches, { version: `v${candidate}` })
75 : fill(t.candidateValue, { version: `v${candidate}` })}
76 </Status>
77 )}
78 <a href={`${REPO_URL}/blob/main/CHANGELOG.md`} target="_blank" rel="noreferrer" className="link">
79 {ext(t.fullNotes)}
80 </a>
81 </dd>
82 </div>
83 </dl>
84 </PageHeader>
85
86 <div className="page-body">
87 <ReleaseUpdates key={`${locale}:${updates.fetchedAt}`} locale={locale} initial={updates} showAllLink={false} />
88 <div className="page-body-narrow changelog-list">
89 {CHANGELOG.length === 0 ? (
90 <EmptyState
91 locale={locale}
92 title={t.emptyTitle}
93 body={t.emptyBody}
94 action={
95 <a className="btn btn-secondary" href={REPO_RELEASES_URL}>
96 {ext(t.releasesLink)}
97 </a>
98 }
99 />
100 ) : (
101 CHANGELOG.map((release) => (
102 <Release
103 key={release.version}
104 release={release}
105 locale={locale}
106 candidate={!release.unreleased && published !== null && (compareVersions(release.version, published.version) ?? 0) > 0}
107 publishedUrl={
108 published && published.version === release.version ? published.url : null
109 }
110 />
111 ))
112 )}
113 <p className="status-line">
114 <Link href={`/${locale}/docs`} className="link">
115 {chrome.footerDocs}
116 </Link>
117 <a href={REPO_RELEASES_URL} target="_blank" rel="noreferrer" className="link">
118 {ext(t.releasesLink)}
119 </a>
120 </p>
121 </div>
122 </div>
123 </FallbackRegion>
124 );
125 }
126
127 function Release({
128 release,
129 locale,
130 candidate,
131 publishedUrl,
132 }: {
133 release: ChangelogRelease;
134 locale: string;
135 candidate: boolean;
136 publishedUrl: string | null;
137 }) {
138 const t = getChangelog(locale);
139 const heading = release.unreleased ? t.unreleasedHeading : `v${release.version}`;
140 const anchor = release.unreleased ? "unreleased" : `v${release.version}`;
141 // Every entry here is clipped or capped for the web; this is the in-page
142 // path to the unabridged notes for exactly this version.
143 const notesUrl = `${REPO_URL}/blob/main/CHANGELOG.md#${changelogAnchor(release)}`;
144 const versionUrl = release.unreleased ? null : `/${locale}/changelog/${release.version}`;
145 const notesLabel = fill(t.releaseNotesLink, { version: heading });
146
147 return (
148 <article id={anchor} className="changelog-release scroll-mt-32">
149 <div className="changelog-release-head">
150 {/* Each release is a receipt: the shell marks it. */}
151 <h2>
152 <Icon name="shell" />
153 {heading}
154 </h2>
155 <div className="changelog-links">
156 {release.date && <span className="tabular">{formatDate(release.date, bodyDateLocale("/changelog", locale))}</span>}
157 {publishedUrl && (
158 <a href={publishedUrl} target="_blank" rel="noreferrer">
159 {ext(t.releasePageLink)}
160 </a>
161 )}
162 {release.compareUrl && !release.unreleased && !candidate && (
163 <a href={release.compareUrl} target="_blank" rel="noreferrer">
164 {ext(t.compareLink)}
165 </a>
166 )}
167 {versionUrl ? (
168 <Link href={versionUrl}>{notesLabel.replace(/\s*↗\s*$/u, "")}</Link>
169 ) : (
170 <a href={notesUrl} target="_blank" rel="noreferrer">
171 {ext(notesLabel)}
172 </a>
173 )}
174 </div>
175 </div>
176 {(release.unreleased || candidate) && <p className="changelog-unreleased-note">{t.unreleasedNote}</p>}
177 <div className="changelog-sections">
178 {release.sections.map((section, i) => (
179 <section key={`${section.heading}-${i}`}>
180 <h3>
181 {section.heading}
182 {section.itemCount > section.items.length &&
183 (versionUrl ? (
184 <Link href={versionUrl}>
185 {fill(t.moreEntries, { shown: section.items.length, total: section.itemCount })}
186 </Link>
187 ) : (
188 <a href={notesUrl} target="_blank" rel="noreferrer">
189 {fill(t.moreEntries, { shown: section.items.length, total: section.itemCount })}
190 </a>
191 ))}
192 </h3>
193 <ul>
194 {section.items.map((item, j) => (
195 <li key={j}>{item}</li>
196 ))}
197 </ul>
198 </section>
199 ))}
200 </div>
201 </article>
202 );
203 }
204
204 lines Plain Text