Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions apps/site/components/Downloads/Release/VersionDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
}, []);
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions apps/site/next.constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ export const STATUS_KIND_MAP = {
Alpha: 'warning',
};

/**
* The download route on which each release status is presented
*
* @type {Record<import('./types/releases').NodeReleaseStatus, string>}
*/
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.
*/
Expand Down
Loading