forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformDropdown.tsx
More file actions
90 lines (76 loc) · 2.93 KB
/
PlatformDropdown.tsx
File metadata and controls
90 lines (76 loc) · 2.93 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use client';
import { useTranslations } from 'next-intl';
import { useContext, useEffect, useMemo } from 'react';
import type { FC } from 'react';
import Select from '@/components/Common/Select';
import WinGet from '@/components/Icons/Platform/WinGet';
import Choco from '@/components/Icons/Platform/Choco';
import Docker from '@/components/Icons/Platform/Docker';
import FNM from '@/components/Icons/Platform/FNM';
import Homebrew from '@/components/Icons/Platform/Homebrew';
import NVM from '@/components/Icons/Platform/NVM';
import { ReleaseContext } from '@/providers/releaseProvider';
import type { PackageManager } from '@/types/release';
import { formatDropdownItems, platformItems } from '@/util/downloadUtils';
const supportedHomebrewVersions = ['LTS', 'Current'];
const PlatformDropdown: FC = () => {
const { release, os, platform, setPlatform } = useContext(ReleaseContext);
const t = useTranslations();
// @TODO: We should have a proper utility that gives
// disabled OSs, Platforms, based on specific criteria
// this can be an optimisation for the future
// to remove this logic from this component
const disabledItems = useMemo(() => {
const disabledItems = [];
if (os === 'WIN') {
disabledItems.push('BREW', 'NVM');
}
if (os === 'LINUX' || os === 'MAC') {
disabledItems.push('WINGET', 'CHOCO');
}
const releaseSupportsHomebrew = supportedHomebrewVersions.includes(
release.status
);
if (!releaseSupportsHomebrew) {
disabledItems.push('BREW');
}
return disabledItems;
}, [os, release.status]);
// @TODO: We should have a proper utility that gives
// disabled OSs, Platforms, based on specific criteria
// this can be an optimisation for the future
// to remove this logic from this component
useEffect(() => {
const currentPlatformExcluded = disabledItems.includes(platform);
const nonExcludedPlatform = platformItems
.map(({ value }) => value)
.find(platform => !disabledItems.includes(platform));
if (currentPlatformExcluded && nonExcludedPlatform) {
setPlatform(nonExcludedPlatform);
}
// we shouldn't react when "actions" change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [release.status, disabledItems, platform]);
return (
<Select
values={formatDropdownItems({
items: platformItems,
icons: {
WINGET: <WinGet width={16} height={16} />,
NVM: <NVM width={16} height={16} />,
FNM: <FNM width={16} height={16} />,
BREW: <Homebrew width={16} height={16} />,
DOCKER: <Docker width={16} height={16} />,
CHOCO: <Choco width={16} height={16} />,
},
disabledItems,
})}
ariaLabel={t('layouts.download.dropdown.platform')}
defaultValue={platform}
onChange={platform => setPlatform(platform as PackageManager)}
className="w-28"
inline={true}
/>
);
};
export default PlatformDropdown;