Skip to content

Commit 26401b6

Browse files
committed
useEffect + useCallback
1 parent 8040345 commit 26401b6

1 file changed

Lines changed: 38 additions & 30 deletions

File tree

‎apps/site/components/Downloads/Release/VersionDropdown.tsx‎

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import WithNoScriptSelect from '@node-core/ui-components/Common/Select/NoScriptSelect';
44
import { useLocale, useTranslations } from 'next-intl';
5-
import { use } from 'react';
5+
import { use, useCallback, useEffect } from 'react';
66

77
import { redirect, usePathname } from '#site/navigation.mjs';
88
import { 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

Comments
 (0)