Skip to content

Commit 5247e87

Browse files
committed
feat: streamline detectos (only run query once)
1 parent a630e91 commit 5247e87

11 files changed

Lines changed: 54 additions & 44 deletions

File tree

apps/site/components/Downloads/DownloadButton/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import classNames from 'classnames';
55
import type { FC, PropsWithChildren } from 'react';
66

77
import Button from '@/components/Common/Button';
8-
import { useDetectOS } from '@/hooks';
8+
import { useClientContext } from '@/hooks';
99
import type { NodeRelease } from '@/types';
1010
import { getNodeDownloadUrl } from '@/util/getNodeDownloadUrl';
1111
import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';
@@ -22,7 +22,8 @@ const DownloadButton: FC<PropsWithChildren<DownloadButtonProps>> = ({
2222
os,
2323
bitness: userBitness,
2424
architecture: userArchitecture,
25-
} = useDetectOS();
25+
} = useClientContext();
26+
2627
const bitness = getUserBitnessByArchitecture(userArchitecture, userBitness);
2728
const downloadLink = getNodeDownloadUrl(versionWithPrefix, os, bitness);
2829

apps/site/components/Downloads/DownloadLink.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import type { FC, PropsWithChildren } from 'react';
44

5-
import { useDetectOS } from '@/hooks';
5+
import { useClientContext } from '@/hooks';
66
import type { NodeRelease } from '@/types';
77
import { getNodeDownloadUrl } from '@/util/getNodeDownloadUrl';
88

@@ -12,7 +12,7 @@ const DownloadLink: FC<PropsWithChildren<DownloadLinkProps>> = ({
1212
release: { versionWithPrefix },
1313
children,
1414
}) => {
15-
const { os, bitness } = useDetectOS();
15+
const { os, bitness } = useClientContext();
1616
const downloadLink = getNodeDownloadUrl(versionWithPrefix, os, bitness);
1717

1818
return <a href={downloadLink}>{children}</a>;

apps/site/components/Downloads/Release/BitnessDropdown.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useEffect, useContext, useMemo } from 'react';
66
import semVer from 'semver';
77

88
import Select from '@/components/Common/Select';
9-
import { useDetectOS } from '@/hooks/react-client';
9+
import { useClientContext } from '@/hooks';
1010
import { ReleaseContext } from '@/providers/releaseProvider';
1111
import { bitnessItems, formatDropdownItems } from '@/util/downloadUtils';
1212
import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';
@@ -16,7 +16,7 @@ const parseNumericBitness = (bitness: string) =>
1616

1717
const BitnessDropdown: FC = () => {
1818
const { bitness: userBitness, architecture: userArchitecture } =
19-
useDetectOS();
19+
useClientContext();
2020
const { bitness, os, release, setBitness } = useContext(ReleaseContext);
2121
const t = useTranslations();
2222

@@ -102,6 +102,7 @@ const BitnessDropdown: FC = () => {
102102
items: bitnessItems[os],
103103
disabledItems,
104104
})}
105+
loading={os === 'LOADING'}
105106
ariaLabel={t('layouts.download.dropdown.bitness')}
106107
defaultValue={String(bitness)}
107108
onChange={bitness => setBitness(parseNumericBitness(bitness))}

apps/site/components/Downloads/Release/OperatingSystemDropdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Apple from '@/components/Icons/Platform/Apple';
99
import Aix from '@/components/Icons/Platform/Generic';
1010
import Linux from '@/components/Icons/Platform/Linux';
1111
import Microsoft from '@/components/Icons/Platform/Microsoft';
12-
import { useDetectOS } from '@/hooks/react-client';
12+
import { useClientContext } from '@/hooks';
1313
import { ReleaseContext } from '@/providers/releaseProvider';
1414
import type { UserOS } from '@/types/userOS';
1515
import {
@@ -22,7 +22,7 @@ type OperatingSystemDropdownProps = { exclude?: Array<UserOS> };
2222
const OperatingSystemDropdown: FC<OperatingSystemDropdownProps> = ({
2323
exclude = [],
2424
}) => {
25-
const { os: userOS } = useDetectOS();
25+
const { os: userOS } = useClientContext();
2626
const { os, setOS } = useContext(ReleaseContext);
2727
const t = useTranslations();
2828

apps/site/components/Downloads/Release/ReleaseCodeBox.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { useContext, useMemo } from 'react';
66
import semVer from 'semver';
77

88
import Banner from '@/components/Common/Banner';
9-
import Skeleton from '@/components/Common/Skeleton';
109
import JSXCodeBox from '@/components/JSX/CodeBox';
1110
import { ESP_SUPPORT_THRESHOLD_VERSION } from '@/next.constants.mjs';
1211
import { createSval } from '@/next.jsx.compiler.mjs';
@@ -40,11 +39,9 @@ const ReleaseCodeBox: FC = () => {
4039
</Banner>
4140
)}
4241

43-
<Skeleton loading={os === 'LOADING'}>
44-
<JSXCodeBox language={codeLanguage} className="min-h-[12rem]">
45-
{parsedSnippet}
46-
</JSXCodeBox>
47-
</Skeleton>
42+
<JSXCodeBox language={codeLanguage} className="min-h-[12rem]">
43+
{parsedSnippet}
44+
</JSXCodeBox>
4845

4946
<span className="text-center text-xs text-neutral-800 dark:text-neutral-200">
5047
{t('layouts.download.codeBox.communityWarning')}

apps/site/components/withCurrentOS.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

apps/site/hooks/react-client/useClientContext.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ import { MatterContext } from '@/providers/matterProvider';
66
import type { ClientSharedServerContext } from '@/types';
77

88
const useClientContext = (): ClientSharedServerContext => {
9-
const { frontmatter, pathname, headings, readingTime, filename } =
10-
useContext(MatterContext);
9+
const {
10+
frontmatter,
11+
pathname,
12+
headings,
13+
readingTime,
14+
filename,
15+
os,
16+
architecture,
17+
bitness,
18+
} = useContext(MatterContext);
1119

12-
return { pathname, frontmatter, headings, readingTime, filename };
20+
return {
21+
pathname,
22+
frontmatter,
23+
headings,
24+
readingTime,
25+
filename,
26+
os,
27+
architecture,
28+
bitness,
29+
};
1330
};
1431

1532
export default useClientContext;

apps/site/hooks/react-client/useDetectOS.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ type UserOSState = {
1515

1616
const useDetectOS = () => {
1717
const [userOSState, setUserOSState] = useState<UserOSState>({
18-
os: 'LOADING',
19-
bitness: 86,
20-
architecture: 'ARM',
18+
os: detectOS(),
19+
bitness: 64,
20+
architecture: 'x64',
2121
});
2222

2323
useEffect(() => {
2424
Promise.all([getBitness(), getArchitecture()]).then(
2525
([bitness, architecture]) => {
2626
const userAgent: string | undefined =
2727
(typeof navigator === 'object' && navigator.userAgent) || '';
28+
2829
// Default bitness if unable to determine
2930
const defaultBitness: number = 86;
3031
// Regex to detect 64-bit architecture in user agent

apps/site/providers/matterProvider.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { createContext } from 'react';
44
import type { FC, PropsWithChildren } from 'react';
55

6+
import { useDetectOS } from '@/hooks';
67
import type { ClientSharedServerContext } from '@/types';
78
import { assignClientContext } from '@/util/assignClientContext';
89

@@ -17,8 +18,12 @@ type MatterProviderProps = PropsWithChildren<
1718
export const MatterProvider: FC<MatterProviderProps> = ({
1819
children,
1920
...data
20-
}) => (
21-
<MatterContext.Provider value={assignClientContext(data)}>
22-
{children}
23-
</MatterContext.Provider>
24-
);
21+
}) => {
22+
const os = useDetectOS();
23+
24+
return (
25+
<MatterContext.Provider value={assignClientContext({ ...os, ...data })}>
26+
{children}
27+
</MatterContext.Provider>
28+
);
29+
};

apps/site/types/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { Heading } from '@vcarl/remark-headings';
22
import type { ReadTimeResults } from 'reading-time';
33

4-
import type { LegacyFrontMatter } from './frontmatter';
4+
import type { useDetectOS } from '@/hooks';
5+
import type { LegacyFrontMatter } from '@/types/frontmatter';
56

6-
export interface ClientSharedServerContext {
7+
export interface ClientSharedServerContext
8+
extends ReturnType<typeof useDetectOS> {
79
frontmatter: LegacyFrontMatter;
810
headings: Array<Heading>;
911
pathname: string;

0 commit comments

Comments
 (0)