Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/site/hooks/react-client/__tests__/useDetectOS.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ describe('useDetectOS', () => {
await waitFor(() => {
assert.deepEqual(result.current, {
os: 'MAC',
bitness: '32',
architecture: 'x86',
bitness: '64',
architecture: 'arm',
});
});
});
Expand Down
11 changes: 7 additions & 4 deletions apps/site/hooks/react-client/useDetectOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Bitness,
OperatingSystem,
} from '#site/types/userAgent';
import { getHighEntropyValues, detectOS } from '#site/util/userAgent';
import { detectOS, getHighEntropyValues } from '#site/util/userAgent';

type UserOSState = {
os: OperatingSystem | 'LOADING';
Expand All @@ -28,10 +28,12 @@ const useDetectOS = () => {
navigator.userAgent
);

const os = detectOS();

// We immediately set the OS to LOADING, and then we update it with the detected OS.
// This is due to that initial render set within the state will indicate a mismatch from
// the server-side rendering versus what the initial state is from the client-side
setUserOSState(current => ({ ...current, os: detectOS() }));
setUserOSState(current => ({ ...current, os }));

// We attempt to get the high entropy values from the Browser and set the User OS State
// based from the values we get from the Browser, if it fails we fallback to the User Agent
Expand All @@ -40,8 +42,9 @@ const useDetectOS = () => {
({
// If there is no getHighEntropyValues API on the Browser or it failed to resolve
// we attempt to fallback to what the User Agent indicates
bitness = uaIndicates64 ? '64' : '32',
architecture = 'x86',
bitness = os === 'MAC' || uaIndicates64 ? '64' : '32',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS UA's don't have "64", but 32bit macOS isn't a thing anymore since many generations of macOS/OS X.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Although I'm absolutely not a fan of os specific code, this is a small enough check tho

// we assume that MacOS has moved to arm64 by default now
architecture = os === 'MAC' ? 'arm' : 'x86',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this was incorrectly set as "arm64", but there's no such thing as "arch64", for some reason TypeScript is resolving as "string"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for looking into this - I had a lot of trouble testing this locally and decided to push it for review 😅

}) => {
setUserOSState(current => ({
...current,
Expand Down
Loading