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. */