Skip to content
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

fix(website): desktop download autoselect #792

Merged
merged 1 commit into from
Oct 24, 2024
Merged
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
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
114 changes: 114 additions & 0 deletions apps/website/content/downloads.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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 }) }))

describe('getTags', () => {
test('desktop releases 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',
])
})
test('daemon releases should return the correct tags', () => {
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'],
})
})
})
21 changes: 14 additions & 7 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,18 +115,27 @@ 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')) {
tags.push('windows')
}
if (asset.name.includes('darwin')) {
if (asset.name.includes('Setup.exe')) {
// For now assume amd64 for Windows exe.
tags.push('windows')
tags.push('amd64')
}
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 (
Expand Down
Loading