From d05b99c58f5032bb850a930bf168be46a17cef5b Mon Sep 17 00:00:00 2001 From: Claudio Wunder Date: Sat, 31 Oct 2026 23:56:25 +0100 Subject: [PATCH] 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 --- .../Downloads/Release/VersionDropdown.tsx | 35 +++++++------------ apps/site/next.constants.mjs | 12 +++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/site/components/Downloads/Release/VersionDropdown.tsx b/apps/site/components/Downloads/Release/VersionDropdown.tsx index e8fca6f8878e2..2f2a989f06118 100644 --- a/apps/site/components/Downloads/Release/VersionDropdown.tsx +++ b/apps/site/components/Downloads/Release/VersionDropdown.tsx @@ -5,7 +5,7 @@ import { useLocale, useTranslations } from 'next-intl'; import { use, useEffect } from 'react'; import { redirect, usePathname } from '#site/navigation.mjs'; -import { STATUS_KIND_MAP } from '#site/next.constants.mjs'; +import { STATUS_KIND_MAP, STATUS_ROUTE_MAP } from '#site/next.constants.mjs'; import { ReleaseContext, ReleasesContext, @@ -21,27 +21,18 @@ const VersionDropdown: FC = () => { const locale = useLocale(); const pathname = usePathname(); - // Allows us to keep the route semantically correct to what the user should expect - // from the /current and non /current routes. - const getRedirectPath = (status: NodeReleaseStatus) => { - if (['LTS', 'EOL'].includes(status) && !pathname.endsWith('/download')) { - return '/download'; - } - - if (status === 'Current' && !pathname.includes('current')) { - return '/download/current'; - } + // The route a status is presented on, unless we are already on it. + const getRouteChange = (status: NodeReleaseStatus) => { + const route = STATUS_ROUTE_MAP[status]; - if (status === 'Alpha' && !pathname.includes('alpha')) { - return '/download/alpha'; - } + return route === pathname ? undefined : route; }; // Ensure the initial route is correct based on the release status. useEffect(() => { - const redirectPath = getRedirectPath(release.status); - if (redirectPath) { - redirect({ href: redirectPath, locale }); + const route = getRouteChange(release.status); + if (route) { + redirect({ href: route, locale }); } // eslint-disable-next-line @eslint-react/exhaustive-deps -- deps are correct on first render, and this should not run again. }, []); @@ -52,12 +43,10 @@ const VersionDropdown: FC = () => { ({ versionWithPrefix }) => versionWithPrefix === version ); - if (selectedRelease) { - const href = getRedirectPath(selectedRelease.status); - if (href) { - redirect({ href, locale }); - return; - } + const route = selectedRelease && getRouteChange(selectedRelease.status); + if (route) { + redirect({ href: route, locale }); + return; } setVersion(version); diff --git a/apps/site/next.constants.mjs b/apps/site/next.constants.mjs index a4019123a8805..1d7709670ee17 100644 --- a/apps/site/next.constants.mjs +++ b/apps/site/next.constants.mjs @@ -148,6 +148,18 @@ export const STATUS_KIND_MAP = { Alpha: 'warning', }; +/** + * The download route on which each release status is presented + * + * @type {Record} + */ +export const STATUS_ROUTE_MAP = { + EOL: '/download', + LTS: '/download', + Current: '/download/current', + Alpha: '/download/alpha', +}; + /** * The location of the Node.js Security Working Group Vulnerabilities data. */