Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.5k
refactor(useDownloadLink): Move useDetectOs to useDownloadLink#5376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
16975170b7e2691adac297dc5791fc854feFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const UserAgentBitness = 'user-agent-bitness'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
| import { useDetectOS } from '../useDetectOS'; | ||
| import { useDownloadLink } from '../useDownloadLink'; | ||
| const mockNavigator = { | ||
| userAgent: | ||
| @@ -11,7 +11,7 @@ const mockNavigator = { | ||
| const originalNavigator = global.navigator; | ||
| describe('useDetectOS', () => { | ||
| describe('useDownloadLink', () => { | ||
| afterEach(() => { | ||
| // Reset the navigator global to the original value | ||
| Object.defineProperty(global, 'navigator', { | ||
| @@ -27,14 +27,12 @@ describe('useDetectOS', () => { | ||
| writable: true, | ||
| }); | ||
| const { result } = renderHook(() => useDetectOS()); | ||
| const { result } = renderHook(() => | ||
| useDownloadLink({ version: 'v18.16.0' }) | ||
| ); | ||
| await waitFor(() => { | ||
| expect(result.current.userOS).toBe('WIN'); | ||
| }); | ||
| await waitFor(() => { | ||
| expect(result.current.getDownloadLink('v18.16.0')).toBe( | ||
| expect(result.current).toBe( | ||
ovflowd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 'https://nodejs.org/dist/v18.16.0/node-v18.16.0-x64.msi' | ||
| ); | ||
| }); | ||
| @@ -51,15 +49,13 @@ describe('useDetectOS', () => { | ||
| writable: true, | ||
| }); | ||
| const { result } = renderHook(() => useDetectOS()); | ||
| await waitFor(() => { | ||
| expect(result.current.userOS).toBe('OTHER'); | ||
| }); | ||
| const { result } = renderHook(() => | ||
| useDownloadLink({ version: 'v20.1.0' }) | ||
| ); | ||
| await waitFor(() => { | ||
| expect(result.current.getDownloadLink('v18.16.0')).toBe( | ||
| 'https://nodejs.org/dist/v18.16.0/node-v18.16.0.tar.gz' | ||
| expect(result.current).toBe( | ||
| 'https://nodejs.org/dist/v20.1.0/node-v20.1.0.tar.gz' | ||
| ); | ||
| }); | ||
| }); | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { useMemo } from 'react'; | ||
| import useSWR from 'swr'; | ||
| import { detectOS } from '../util/detectOS'; | ||
| import { downloadUrlByOS } from '../util/downloadUrlByOS'; | ||
| import { getBitness } from '../util/getBitness'; | ||
| import { UserAgentBitness } from '../constants/swr'; | ||
| type UseDownloadLinkArgs = { | ||
| version: string; | ||
| }; | ||
| export const useDownloadLink = ({ version }: UseDownloadLinkArgs) => { | ||
| const { data: bitness } = useSWR(UserAgentBitness, getBitness); | ||
| const downloadLink = useMemo( | ||
ovflowd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| () => | ||
| downloadUrlByOS({ | ||
| userAgent: navigator?.userAgent, | ||
| userOS: detectOS(), | ||
| version, | ||
| bitness, | ||
| }), | ||
| [bitness, version] | ||
| ); | ||
| return downloadLink; | ||
| }; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /// <reference types="user-agent-data-types" /> | ||
ovflowd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| export const getBitness = async () => { | ||
| // This is necessary to detect Windows 11 on Edge. | ||
| // [MDN](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorUAData/getHighEntropyValues) | ||
| // [MSFT](https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11) | ||
| if (typeof navigator?.userAgentData?.getHighEntropyValues === 'function') { | ||
| return navigator.userAgentData | ||
| .getHighEntropyValues(['bitness']) | ||
| .then(ua => ua.bitness); | ||
| } | ||
| return undefined; | ||
ovflowd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.