MCPcopy Index your code
hub / github.com/nodejs/nodejs.org / useDetectOS

Function useDetectOS

apps/site/hooks/useDetectOS.ts:19–61  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17};
18
19const useDetectOS = () => {
20 const [userOSState, setUserOSState] = useState<UserOSState>({
21 os: 'LOADING',
22 bitness: '',
23 architecture: '',
24 });
25
26 useEffect(() => {
27 // If the navigator User Agent indicates a 64-bit OS, we can assume the bitness is 64.
28 const uaIndicates64 = /WOW64|Win64|x86_64|x86-64|x64_64|x64;|AMD64/.test(
29 navigator.userAgent
30 );
31
32 const os = detectOS();
33
34 // We immediately set the OS to LOADING, and then we update it with the detected OS.
35 // This is due to that initial render set within the state will indicate a mismatch from
36 // the server-side rendering versus what the initial state is from the client-side
37 // eslint-disable-next-line @eslint-react/set-state-in-effect
38 setUserOSState(current => ({ ...current, os }));
39
40 // We attempt to get the high entropy values from the Browser and set the User OS State
41 // based from the values we get from the Browser, if it fails we fallback to the User Agent
42 // to determine the bitness and architecture of the User OS.
43 getHighEntropyValues(['bitness', 'architecture']).then(
44 ({
45 // If there is no getHighEntropyValues API on the Browser or it failed to resolve
46 // we attempt to fallback to what the User Agent indicates
47 bitness = os === 'MAC' || uaIndicates64 ? '64' : '32',
48 // we assume that MacOS has moved to arm64 by default now
49 architecture = os === 'MAC' ? 'arm' : 'x86',
50 }) => {
51 setUserOSState(current => ({
52 ...current,
53 bitness: bitness as Bitness,
54 architecture: architecture as Architecture,
55 }));
56 }
57 );
58 }, []);
59
60 return userOSState;
61};
62
63export default useDetectOS;

Callers 2

MatterProviderFunction · 0.85

Calls 2

detectOSFunction · 0.90
getHighEntropyValuesFunction · 0.90

Tested by

no test coverage detected