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 , useCallback , useEffect } from 'react' ;
66
77import { redirect , usePathname } from '#site/navigation.mjs' ;
88import { STATUS_KIND_MAP } from '#site/next.constants.mjs' ;
@@ -23,43 +23,51 @@ const VersionDropdown: FC = () => {
2323
2424 // Allows us to keep the route semantically correct to what the user should expect
2525 // from the /current and non /current routes.
26- const getRedirectPath = ( status ?: NodeReleaseStatus ) => {
27- if (
28- ( status === 'LTS' || status === 'EOL' ) &&
29- ( pathname . includes ( 'current' ) || pathname . includes ( 'alpha' ) )
30- ) {
31- return '/download' ;
32- }
26+ const getRedirectPath = useCallback (
27+ ( status ?: NodeReleaseStatus ) => {
28+ if (
29+ ( status === 'LTS' || status === 'EOL' ) &&
30+ ( pathname . includes ( 'current' ) || pathname . includes ( 'alpha' ) )
31+ ) {
32+ return '/download' ;
33+ }
3334
34- if ( status === 'Current' && ! pathname . includes ( 'current' ) ) {
35- return '/download/current' ;
36- }
35+ if ( status === 'Current' && ! pathname . includes ( 'current' ) ) {
36+ return '/download/current' ;
37+ }
3738
38- if ( status === 'Alpha' && ! pathname . includes ( 'alpha' ) ) {
39- return '/download/alpha' ;
40- }
41- } ;
39+ if ( status === 'Alpha' && ! pathname . includes ( 'alpha' ) ) {
40+ return '/download/alpha' ;
41+ }
42+ } ,
43+ [ pathname ]
44+ ) ;
4245
4346 // Ensure the initial route is correct based on the release status.
44- const redirectPath = getRedirectPath ( release . status ) ;
45- if ( redirectPath ) {
46- redirect ( { href : redirectPath , locale } ) ;
47- }
47+ useEffect ( ( ) => {
48+ const redirectPath = getRedirectPath ( release . status ) ;
49+ if ( redirectPath ) {
50+ redirect ( { href : redirectPath , locale } ) ;
51+ }
52+ } , [ getRedirectPath , release . status , locale ] ) ;
4853
4954 // Handle setting the new version or navigating to the correct route based on status.
50- const setVersionOrNavigate = ( version : string ) => {
51- const selectedRelease = releases . find (
52- ( { versionWithPrefix } ) => versionWithPrefix === version
53- ) ;
55+ const setVersionOrNavigate = useCallback (
56+ ( version : string ) => {
57+ const selectedRelease = releases . find (
58+ ( { versionWithPrefix } ) => versionWithPrefix === version
59+ ) ;
5460
55- const href = getRedirectPath ( selectedRelease ?. status ) ;
56- if ( href ) {
57- redirect ( { href, locale } ) ;
58- return ;
59- }
61+ const href = getRedirectPath ( selectedRelease ?. status ) ;
62+ if ( href ) {
63+ redirect ( { href, locale } ) ;
64+ return ;
65+ }
6066
61- setVersion ( version ) ;
62- } ;
67+ setVersion ( version ) ;
68+ } ,
69+ [ getRedirectPath , releases , setVersion , locale ]
70+ ) ;
6371
6472 return (
6573 < WithNoScriptSelect
0 commit comments