-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathVersionDropdown.tsx
More file actions
59 lines (49 loc) · 1.71 KB
/
VersionDropdown.tsx
File metadata and controls
59 lines (49 loc) · 1.71 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
'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 { STATUS_KIND_MAP } from '#site/next.constants.mjs';
import {
ReleaseContext,
ReleasesContext,
} from '#site/providers/releaseProvider';
import type { FC } from 'react';
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?.status === 'LTS' && 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: versionWithPrefix,
badge: { label: status, kind: STATUS_KIND_MAP[status] },
}))}
defaultValue={release.versionWithPrefix}
onChange={setVersionOrNavigate}
className="min-w-36"
inline={true}
/>
);
};
export default VersionDropdown;