Skip to content

Commit d05b99c

Browse files
committed
refactor: simplify the route handling of the version dropdown
The route of each release status is now a STATUS_ROUTE_MAP next to STATUS_KIND_MAP, instead of string checks on the pathname (includes, endsWith and a status list). A single getRouteChange helper returns the route to move to, or nothing when already on it, and is shared by the redirect on load and the version change handler. There is no behaviour change, except that the pathname is now compared exactly. Assisted-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 86d0195 commit d05b99c

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

‎apps/site/components/Downloads/Release/VersionDropdown.tsx‎

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useLocale, useTranslations } from 'next-intl';
55
import { use, useEffect } from 'react';
66

77
import { redirect, usePathname } from '#site/navigation.mjs';
8-
import { STATUS_KIND_MAP } from '#site/next.constants.mjs';
8+
import { STATUS_KIND_MAP, STATUS_ROUTE_MAP } from '#site/next.constants.mjs';
99
import {
1010
ReleaseContext,
1111
ReleasesContext,
@@ -21,27 +21,18 @@ const VersionDropdown: FC = () => {
2121
const locale = useLocale();
2222
const pathname = usePathname();
2323

24-
// Allows us to keep the route semantically correct to what the user should expect
25-
// from the /current and non /current routes.
26-
const getRedirectPath = (status: NodeReleaseStatus) => {
27-
if (['LTS', 'EOL'].includes(status) && !pathname.endsWith('/download')) {
28-
return '/download';
29-
}
30-
31-
if (status === 'Current' && !pathname.includes('current')) {
32-
return '/download/current';
33-
}
24+
// The route a status is presented on, unless we are already on it.
25+
const getRouteChange = (status: NodeReleaseStatus) => {
26+
const route = STATUS_ROUTE_MAP[status];
3427

35-
if (status === 'Alpha' && !pathname.includes('alpha')) {
36-
return '/download/alpha';
37-
}
28+
return route === pathname ? undefined : route;
3829
};
3930

4031
// Ensure the initial route is correct based on the release status.
4132
useEffect(() => {
42-
const redirectPath = getRedirectPath(release.status);
43-
if (redirectPath) {
44-
redirect({ href: redirectPath, locale });
33+
const route = getRouteChange(release.status);
34+
if (route) {
35+
redirect({ href: route, locale });
4536
}
4637
// eslint-disable-next-line @eslint-react/exhaustive-deps -- deps are correct on first render, and this should not run again.
4738
}, []);
@@ -52,12 +43,10 @@ const VersionDropdown: FC = () => {
5243
({ versionWithPrefix }) => versionWithPrefix === version
5344
);
5445

55-
if (selectedRelease) {
56-
const href = getRedirectPath(selectedRelease.status);
57-
if (href) {
58-
redirect({ href, locale });
59-
return;
60-
}
46+
const route = selectedRelease && getRouteChange(selectedRelease.status);
47+
if (route) {
48+
redirect({ href: route, locale });
49+
return;
6150
}
6251

6352
setVersion(version);

‎apps/site/next.constants.mjs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ export const STATUS_KIND_MAP = {
148148
Alpha: 'warning',
149149
};
150150

151+
/**
152+
* The download route on which each release status is presented
153+
*
154+
* @type {Record<import('./types/releases').NodeReleaseStatus, string>}
155+
*/
156+
export const STATUS_ROUTE_MAP = {
157+
EOL: '/download',
158+
LTS: '/download',
159+
Current: '/download/current',
160+
Alpha: '/download/alpha',
161+
};
162+
151163
/**
152164
* The location of the Node.js Security Working Group Vulnerabilities data.
153165
*/

0 commit comments

Comments
 (0)