Skip to content

Commit

Permalink
fix(website): desktop download autoselect
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Oct 22, 2024
1 parent 7fe865b commit 9d40c9e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-terms-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'website': patch
---

Fixed an issue where the desktop download widget would not autoselect the correct OS download.
2 changes: 1 addition & 1 deletion apps/website/components/DownloadDesktopSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Props = {
}

export function DownloadDesktopSelect({ daemon, release, testnetOnly }: Props) {
const downloadLinks = getDownloadLinksDesktop(daemon, release)
const downloadLinks = getDownloadLinksDesktop(release)
const { accepted } = useTermsOfService()

const [download, setDownload] = useState(downloadLinks[0])
Expand Down
113 changes: 113 additions & 0 deletions apps/website/content/downloads.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { getTags, findUserDefaultDownload } from './downloads'

const downloadsList = [
// desktop release naming scheme
{ name: 'walletd-arm64.dmg' },
{ name: 'walletd-x64.dmg' },
{ name: 'walletd-0.12.0.Setup.exe' },
{ name: 'walletd_0.12.0_arm64.deb' },
{ name: 'walletd_0.12.0_amd64.deb' },
{ name: 'walletd-0.12.0-1.arm64.rpm' },
{ name: 'walletd-0.12.0-1.x86_64.rpm' },
// daemon release naming scheme
{ name: 'walletd_darwin_amd64.zip' },
{ name: 'walletd_darwin_arm64.zip' },
{ name: 'walletd_linux_amd64.zip' },
{ name: 'walletd_linux_arm64.zip' },
{ name: 'walletd_windows_amd64.zip' },
].map(({ name }) => ({ title: '', link: '', tags: getTags({ name }) }))

test('should return the correct tags', () => {
expect(getTags({ name: 'walletd-arm64.dmg' })).toEqual(['macos', 'arm64'])
expect(getTags({ name: 'walletd-x64.dmg' })).toEqual(['macos', 'amd64'])
expect(getTags({ name: 'walletd-0.12.0.Setup.exe' })).toEqual([
'windows',
'amd64',
])
expect(getTags({ name: 'walletd_0.12.0_arm64.deb' })).toEqual([
'linux',
'arm64',
])
expect(getTags({ name: 'walletd_0.12.0_amd64.deb' })).toEqual([
'linux',
'amd64',
])
expect(getTags({ name: 'walletd-0.12.0-1.arm64.rpm' })).toEqual([
'linux',
'arm64',
])
expect(getTags({ name: 'walletd-0.12.0-1.x86_64.rpm' })).toEqual([
'linux',
'amd64',
])
expect(getTags({ name: 'walletd_darwin_amd64.zip' })).toEqual([
'macos',
'amd64',
])
expect(getTags({ name: 'walletd_darwin_arm64.zip' })).toEqual([
'macos',
'arm64',
])
expect(getTags({ name: 'walletd_linux_amd64.zip' })).toEqual([
'linux',
'amd64',
])
expect(getTags({ name: 'walletd_linux_arm64.zip' })).toEqual([
'linux',
'arm64',
])
expect(getTags({ name: 'walletd_windows_amd64.zip' })).toEqual([
'windows',
'amd64',
])
})

describe('findUserDefaultDownload', () => {
test('any windows user-agent should return amd64', () => {
const userAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0'
jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent)
const download = findUserDefaultDownload(downloadsList)
expect(download).toEqual({
title: '',
link: '',
tags: ['windows', 'amd64'],
})
})

test('any macos user-agent should return arm64', () => {
const userAgent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent)
const download = findUserDefaultDownload(downloadsList)
expect(download).toEqual({
title: '',
link: '',
tags: ['macos', 'arm64'],
})
})

test('linux arm user-agent should return arm64', () => {
const userAgent =
'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent)
const download = findUserDefaultDownload(downloadsList)
expect(download).toEqual({
title: '',
link: '',
tags: ['linux', 'arm64'],
})
})

test('linux amd user-agent should return amd64', () => {
const userAgent =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent)
const download = findUserDefaultDownload(downloadsList)
expect(download).toEqual({
title: '',
link: '',
tags: ['linux', 'amd64'],
})
})
})
19 changes: 11 additions & 8 deletions apps/website/content/downloads.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GitHubRelease, GitHubReleaseAsset } from '@siafoundation/data-sources'
import { GitHubRelease } from '@siafoundation/data-sources'

type DownloadOption = { title: string; link: string; tags: DownloadTag[] }

Expand All @@ -25,15 +25,13 @@ export function getDownloadLinksDaemon(daemon: string, release: GitHubRelease) {
}

export function getDownloadLinksDesktop(
daemon: string,
release: GitHubRelease
): DownloadOption[] {
if (!release) {
return []
}

// Desktop releases include assets for multiple daemons, so we need to filter.
const assets = release.assets.filter((asset) => asset.name.includes(daemon))
const { assets } = release

const final = []

Expand Down Expand Up @@ -117,24 +115,29 @@ export function getDownloadLinksDesktop(

type DownloadTag = 'zen' | 'windows' | 'macos' | 'linux' | 'amd64' | 'arm64'

function getTags(asset: GitHubReleaseAsset): DownloadTag[] {
export function getTags(asset: { name: string }): DownloadTag[] {
const tags: DownloadTag[] = []
if (asset.name.includes('testnet') || asset.name.includes('zen')) {
tags.push('zen')
}
if (asset.name.includes('windows')) {
if (asset.name.includes('windows') || asset.name.includes('.exe')) {
tags.push('windows')
}
if (asset.name.includes('darwin')) {
if (asset.name.includes('darwin') || asset.name.includes('.dmg')) {
tags.push('macos')
}
if (asset.name.includes('linux')) {
if (
asset.name.includes('linux') ||
asset.name.includes('.deb') ||
asset.name.includes('.rpm')
) {
tags.push('linux')
}
if (
asset.name.includes('amd64') ||
asset.name.includes('x86_64') ||
asset.name.includes('x64')
// For now assume amd64 for Windows exe.
) {
tags.push('amd64')
}
Expand Down

0 comments on commit 9d40c9e

Please sign in to comment.