-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathPlatformDropdown.tsx
More file actions
74 lines (63 loc) · 2.52 KB
/
PlatformDropdown.tsx
File metadata and controls
74 lines (63 loc) · 2.52 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
'use client';
import Select from '@node-core/ui-components/Common/Select';
import { useTranslations } from 'next-intl';
import { useEffect, use, useMemo } from 'react';
import useClientContext from '#site/hooks/useClientContext';
import { ReleaseContext } from '#site/providers/releaseProvider';
import { PLATFORMS, nextItem, parseCompat } from '#site/util/download';
import { getUserPlatform } from '#site/util/userAgent';
import type { Platform } from '#site/types/userAgent';
import type { FC } from 'react';
const PlatformDropdown: FC = () => {
const { architecture, bitness } = useClientContext();
const release = use(ReleaseContext);
const t = useTranslations();
useEffect(
() => {
if (architecture && bitness) {
const autoDetectedPlatform = getUserPlatform(architecture, bitness);
release.setPlatform(autoDetectedPlatform);
}
},
// Only react on the change of the Client Context Architecture and Bitness
// eslint-disable-next-line @eslint-react/exhaustive-deps
[architecture, bitness]
);
// We parse the compatibility of the dropdown items
const parsedPlatforms = useMemo(
() =>
// We only want to parse the compatibility when the OS has finished loading
// Otherwise, we would be parsing the compatibility of an empty array
release.os !== 'LOADING'
? parseCompat(PLATFORMS[release.os], release)
: [],
// We only want to react on the change of the OS, Platform, and Version
// eslint-disable-next-line @eslint-react/exhaustive-deps
[release.os, release.version]
);
// We set the Platform to the next available Architecture when the current
// one is not valid anymore due to OS or Version changes
useEffect(
() => {
if (release.os !== 'LOADING' && release.platform !== '') {
release.setPlatform(nextItem(release.platform, parsedPlatforms));
}
},
// We only want to react on the change of the OS and Version
// eslint-disable-next-line @eslint-react/exhaustive-deps
[release.os, release.version, release.platform]
);
return (
<Select<Platform>
values={parsedPlatforms}
value={release.platform !== '' ? release.platform : undefined}
loading={release.os === 'LOADING' || release.platform === ''}
placeholder={t('layouts.download.dropdown.unknown')}
ariaLabel={t('layouts.download.dropdown.platform')}
onChange={platform => platform && release.setPlatform(platform)}
className="min-w-28"
inline={true}
/>
);
};
export default PlatformDropdown;