-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathVersionDropdown.tsx
More file actions
69 lines (56 loc) · 1.83 KB
/
VersionDropdown.tsx
File metadata and controls
69 lines (56 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use client';
import WithNoScriptSelect from '@node-core/ui-components/Common/Select/NoScriptSelect';
import { useLocale, useTranslations } from 'next-intl';
import { use } from 'react';
import { redirect, usePathname } from '#site/navigation';
import {
ReleaseContext,
ReleasesContext,
} from '#site/providers/releaseProvider';
import type { FC } from 'react';
const getDropDownStatus = (version: string, status: string) => {
if (status.endsWith('LTS')) {
return `${version} (LTS)`;
}
if (status === 'Current') {
return `${version} (Current)`;
}
return version;
};
const VersionDropdown: FC = () => {
const { releases } = use(ReleasesContext);
const { release, setVersion } = use(ReleaseContext);
const t = useTranslations();
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 setVersionOrNavigate = (version: string) => {
const release = releases.find(
({ versionWithPrefix }) => versionWithPrefix === version
);
if (release?.isLts && pathname.includes('current')) {
redirect({ href: '/download', locale });
return;
}
if (release?.status === 'Current' && !pathname.includes('current')) {
redirect({ href: '/download/current', locale });
return;
}
setVersion(version);
};
return (
<WithNoScriptSelect
ariaLabel={t('layouts.download.dropdown.version')}
values={releases.map(({ status, versionWithPrefix }) => ({
value: versionWithPrefix,
label: getDropDownStatus(versionWithPrefix, status),
}))}
defaultValue={release.versionWithPrefix}
onChange={setVersionOrNavigate}
className="min-w-36"
inline={true}
/>
);
};
export default VersionDropdown;