|
3 | 3 | import { useTranslations } from 'next-intl';
|
4 | 4 | import type { FC } from 'react';
|
5 | 5 | import { useEffect, useContext, useMemo } from 'react';
|
6 |
| -import semVer from 'semver'; |
7 | 6 |
|
8 | 7 | import Select from '@/components/Common/Select';
|
9 | 8 | import { useClientContext } from '@/hooks';
|
10 | 9 | import { ReleaseContext } from '@/providers/releaseProvider';
|
11 |
| -import { bitnessItems, formatDropdownItems } from '@/util/downloadUtils'; |
| 10 | +import { ARCHITECTURES, parseCompatibility } from '@/util/downloadUtils'; |
12 | 11 | import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';
|
13 | 12 |
|
14 | 13 | const parseNumericBitness = (bitness: string) =>
|
15 | 14 | /^\d+$/.test(bitness) ? Number(bitness) : bitness;
|
16 | 15 |
|
17 | 16 | const BitnessDropdown: FC = () => {
|
18 |
| - const { bitness: userBitness, architecture: userArchitecture } = |
19 |
| - useClientContext(); |
20 |
| - const { bitness, os, release, setBitness } = useContext(ReleaseContext); |
| 17 | + const { architecture, bitness } = useClientContext(); |
| 18 | + |
| 19 | + const release = useContext(ReleaseContext); |
21 | 20 | const t = useTranslations();
|
22 | 21 |
|
23 | 22 | useEffect(() => {
|
24 |
| - setBitness(getUserBitnessByArchitecture(userArchitecture, userBitness)); |
25 |
| - // we shouldn't update the effect on setter state change |
| 23 | + release.setBitness(getUserBitnessByArchitecture(architecture, bitness)); |
| 24 | + // Only react on the change of the Client Context Architecture and Bitness |
26 | 25 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
27 |
| - }, [userArchitecture, userBitness]); |
28 |
| - |
29 |
| - // @TODO: We should have a proper utility that gives |
30 |
| - // disabled OSs, Platforms, based on specific criteria |
31 |
| - // this can be an optimisation for the future |
32 |
| - // to remove this logic from this component |
33 |
| - const disabledItems = useMemo(() => { |
34 |
| - const disabledItems = []; |
| 26 | + }, [architecture, bitness]); |
35 | 27 |
|
36 |
| - if (os === 'WIN' && semVer.satisfies(release.version, '< 19.9.0')) { |
37 |
| - disabledItems.push('arm64'); |
38 |
| - } |
39 |
| - |
40 |
| - if (os === 'WIN' && semVer.satisfies(release.version, '>= 23.0.0')) { |
41 |
| - disabledItems.push('86'); |
42 |
| - } |
43 |
| - |
44 |
| - if (os === 'LINUX' && semVer.satisfies(release.version, '< 4.0.0')) { |
45 |
| - disabledItems.push('arm64', 'armv7l'); |
46 |
| - } |
47 |
| - |
48 |
| - if (os === 'LINUX' && semVer.satisfies(release.version, '< 4.4.0')) { |
49 |
| - disabledItems.push('ppc64le'); |
50 |
| - } |
51 |
| - |
52 |
| - if (os === 'LINUX' && semVer.satisfies(release.version, '< 6.6.0')) { |
53 |
| - disabledItems.push('s390x'); |
54 |
| - } |
55 |
| - |
56 |
| - if (os === 'AIX' && semVer.satisfies(release.version, '< 6.7.0')) { |
57 |
| - disabledItems.push('ppc64'); |
58 |
| - } |
59 |
| - |
60 |
| - return disabledItems; |
61 |
| - }, [os, release.version]); |
| 28 | + // We parse the compatibility of the dropdown items |
| 29 | + const parsedArchitectures = useMemo( |
| 30 | + () => parseCompatibility(ARCHITECTURES[release.os], release), |
| 31 | + // We only want to react on the change of the OS, Bitness, and Version |
| 32 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 33 | + [release.os, release.bitness, release.version] |
| 34 | + ); |
62 | 35 |
|
63 |
| - // @TODO: We should have a proper utility that gives |
64 |
| - // disabled OSs, Platforms, based on specific criteria |
65 |
| - // this can be an optimisation for the future |
66 |
| - // to remove this logic from this component |
67 | 36 | useEffect(() => {
|
68 |
| - const mappedBitnessValues = bitnessItems[os].map(({ value }) => value); |
| 37 | + const currentBitnessItem = parsedArchitectures.find( |
| 38 | + ({ value }) => value === String(release.bitness) |
| 39 | + ); |
69 | 40 |
|
70 | 41 | const currentBitnessExcluded =
|
71 |
| - // Different OSs support different Bitnessess, hence we should also check |
72 |
| - // if besides the current bitness not being supported for a given release version |
73 |
| - // we also should check if it is not supported by the OS |
74 |
| - disabledItems.includes(String(bitness)) || |
75 |
| - !mappedBitnessValues.includes(String(bitness)); |
| 42 | + !currentBitnessItem || currentBitnessItem.disabled; |
76 | 43 |
|
77 |
| - const nonExcludedBitness = mappedBitnessValues.find( |
78 |
| - bitness => !disabledItems.includes(bitness) |
| 44 | + const nonExcludedBitness = parsedArchitectures.find( |
| 45 | + ({ disabled }) => !disabled |
79 | 46 | );
|
80 | 47 |
|
81 | 48 | if (currentBitnessExcluded && nonExcludedBitness) {
|
82 | 49 | // We set it as a Number for cases where it is 64 or 86 otherwise we are
|
83 | 50 | // setting it as a string (ARMv7, ARMv6, etc.)
|
84 | 51 | const numericBitness = Number(nonExcludedBitness);
|
85 | 52 |
|
86 |
| - setBitness( |
87 |
| - numericBitness.toString() === nonExcludedBitness |
| 53 | + release.setBitness( |
| 54 | + numericBitness.toString() === nonExcludedBitness.value |
88 | 55 | ? numericBitness
|
89 |
| - : nonExcludedBitness |
| 56 | + : nonExcludedBitness.value |
90 | 57 | );
|
91 | 58 | }
|
92 |
| - // we shouldn't react when "actions" change |
| 59 | + // Only react on the change of the current OS and Version |
93 | 60 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
94 |
| - }, [os, disabledItems]); |
| 61 | + }, [release.os, release.version]); |
95 | 62 |
|
96 | 63 | return (
|
97 | 64 | <Select
|
98 |
| - values={formatDropdownItems({ |
99 |
| - items: bitnessItems[os], |
100 |
| - disabledItems, |
101 |
| - })} |
102 |
| - loading={os === 'LOADING'} |
| 65 | + values={parsedArchitectures} |
| 66 | + loading={release.os === 'LOADING'} |
103 | 67 | ariaLabel={t('layouts.download.dropdown.bitness')}
|
104 |
| - defaultValue={String(bitness)} |
105 |
| - onChange={bitness => setBitness(parseNumericBitness(bitness))} |
| 68 | + defaultValue={String(release.bitness)} |
| 69 | + onChange={bitness => release.setBitness(parseNumericBitness(bitness))} |
106 | 70 | className="min-w-28"
|
107 | 71 | inline={true}
|
108 | 72 | />
|
|
0 commit comments