-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathOperatingSystemDropdown.tsx
More file actions
66 lines (56 loc) · 2.26 KB
/
OperatingSystemDropdown.tsx
File metadata and controls
66 lines (56 loc) · 2.26 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
'use client';
import Select from '@node-core/ui-components/Common/Select';
import { useTranslations } from 'next-intl';
import { use, useEffect, useMemo } from 'react';
import useClientContext from '#site/hooks/useClientContext';
import { ReleaseContext } from '#site/providers/releaseProvider';
import { nextItem, OPERATING_SYSTEMS, parseCompat } from '#site/util/download';
import type { OperatingSystem } from '#site/types/userAgent';
import type { FC } from 'react';
type OperatingSystemDropdownProps = { exclude?: Array<OperatingSystem> };
const OperatingSystemDropdown: FC<OperatingSystemDropdownProps> = () => {
const { os } = useClientContext();
const release = use(ReleaseContext);
const t = useTranslations();
useEffect(() => {
if (os !== 'LOADING') {
release.setOS(os);
}
// Reacts on Client Context change of OS
// Only this Hook is allowed to bypass the `setOS` from above
// As this Hook is what defined the initial OS state
// eslint-disable-next-line @eslint-react/exhaustive-deps
}, [os]);
// We parse the compatibility of the dropdown items
const parsedOperatingSystems = useMemo(
() => parseCompat(OPERATING_SYSTEMS, release),
// We only want to react on the change of the Install Method and Version
// eslint-disable-next-line @eslint-react/exhaustive-deps
[release.installMethod, release.version]
);
// We set the OS to the next available OS when the current
// one is not valid anymore due to Version changes
useEffect(
() => {
if (release.os !== 'LOADING') {
release.setOS(nextItem(release.os, parsedOperatingSystems));
}
},
// We only want to react on the change of the Version, Install Method and OS
// eslint-disable-next-line @eslint-react/exhaustive-deps
[release.installMethod, release.version, release.os]
);
return (
<Select<OperatingSystem>
values={parsedOperatingSystems}
defaultValue={release.os !== 'LOADING' ? release.os : undefined}
loading={release.os === 'LOADING'}
placeholder={t('layouts.download.dropdown.unknown')}
ariaLabel={t('layouts.download.dropdown.os')}
onChange={value => release.setOS(value)}
className="min-w-[8.5rem]"
inline={true}
/>
);
};
export default OperatingSystemDropdown;