22
33import WithNoScriptSelect from '@node-core/ui-components/Common/Select/NoScriptSelect' ;
44import { useLocale , useTranslations } from 'next-intl' ;
5- import { use } from 'react' ;
5+ import { use , useEffect } from 'react' ;
66
7- import { redirect , usePathname } from '#site/navigation' ;
7+ import { redirect , usePathname } from '#site/navigation.mjs ' ;
88import { STATUS_KIND_MAP } from '#site/next.constants.mjs' ;
99import {
1010 ReleaseContext ,
1111 ReleasesContext ,
1212} from '#site/providers/releaseProvider' ;
1313
14+ import type { NodeReleaseStatus } from '#site/types' ;
1415import type { FC } from 'react' ;
1516
1617const VersionDropdown : FC = ( ) => {
@@ -22,19 +23,41 @@ const VersionDropdown: FC = () => {
2223
2324 // Allows us to keep the route semantically correct to what the user should expect
2425 // 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+ }
34+
35+ if ( status === 'Alpha' && ! pathname . includes ( 'alpha' ) ) {
36+ return '/download/alpha' ;
37+ }
38+ } ;
39+
40+ // Ensure the initial route is correct based on the release status.
41+ useEffect ( ( ) => {
42+ const redirectPath = getRedirectPath ( release . status ) ;
43+ if ( redirectPath ) {
44+ redirect ( { href : redirectPath , locale } ) ;
45+ }
46+ // eslint-disable-next-line @eslint-react/exhaustive-deps -- deps are correct on first render, and this should not run again.
47+ } , [ ] ) ;
48+
49+ // Handle setting the new version or navigating to the correct route based on status.
2550 const setVersionOrNavigate = ( version : string ) => {
26- const release = releases . find (
51+ const selectedRelease = releases . find (
2752 ( { versionWithPrefix } ) => versionWithPrefix === version
2853 ) ;
2954
30- if ( release ?. status === 'LTS' && pathname . includes ( 'current' ) ) {
31- redirect ( { href : '/download' , locale } ) ;
32- return ;
33- }
34-
35- if ( release ?. status === 'Current' && ! pathname . includes ( 'current' ) ) {
36- redirect ( { href : '/download/current' , locale } ) ;
37- return ;
55+ if ( selectedRelease ) {
56+ const href = getRedirectPath ( selectedRelease . status ) ;
57+ if ( href ) {
58+ redirect ( { href, locale } ) ;
59+ return ;
60+ }
3861 }
3962
4063 setVersion ( version ) ;
0 commit comments