Skip to content

Commit

Permalink
fix: update get latest release url
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Nov 20, 2024
1 parent e9248e9 commit 2f57d09
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 40 deletions.
59 changes: 47 additions & 12 deletions components/DownloadBtn.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,66 @@
<template>
<a
v-if="release.platform"
v-if="releaseContent && releaseContent.downloadName"
class="link"
:href="release.releaseUrl"
:href="releaseContent.releaseUrl"
target="_blank"
:download="release.downloadName"
:download="releaseContent.downloadName"
>
<ElButton class="btn" type="primary">
<i18n-t keypath="download" tag="span">
<button class="btn">
<i18n-t keypath="downloadLink" tag="span">
<template #platform>
<span>{{ release.platform }}</span>
<span>{{ releaseContent.platform }}</span>
</template>
</i18n-t>
</ElButton>
</button>
</a>
<a
v-else
class="link"
href="https://github.com/witnet/my-wit-wallet/releases/latest"
:href="releaseContent ? releaseContent.releaseUrl : GITHUB_RELEASE_URL"
>
<ElButton class="btn" type="primary">{{ $t('head.title') }}</ElButton>
<button class="btn">
<i18n-t v-if="releaseContent" keypath="downloadLink" tag="span">
<template #platform>
<span>{{ releaseContent?.platform }}</span>
</template>
</i18n-t>
<span v-else>{{ t('githubLink') }}</span>
</button>
</a>
</template>

<script setup>
import { getLatestRelease } from '../getLatestRelease'
const release = await getLatestRelease(navigator)
import { getLatestRelease, getStoreRelease } from '@/getLatestRelease'
import { getBrowserOs } from '@/getBrowserOs'
import { useI18n } from 'vue-i18n'
import { GITHUB_RELEASE_URL } from '@/constants'
import { onMounted, ref } from 'vue'
const { t } = useI18n()

const releaseContent = ref(null)

onMounted(async () => {
releaseContent.value = await release()
})

async function release() {
const os = getBrowserOs(navigator)
const storeRelease = getStoreRelease({ os })
if (!os) {
return null
}
if (storeRelease) {
return storeRelease
}
return await getLatestRelease({ os })
}
</script>

<style></style>
<style lang="scss">
.link {
width: max-content;
height: auto;
display: flex;
}
</style>
2 changes: 2 additions & 0 deletions constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const languages = [
export const fallbackLocale = 'en'

export const DEFAULT_OS = 'Linux'
export const GITHUB_RELEASE_URL =
'https://github.com/witnet/sheikah/releases/latest'

export const URL_RELEASE_BASE =
'https://api.github.com/repos/witnet/my-wit-wallet/releases/latest'
34 changes: 29 additions & 5 deletions getBrowserOs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import { DEFAULT_OS } from './constants'
export function getBrowserOs(navigator: Navigator) {
return navigator.userAgentData?.platform
? detectPlatformWithUserAgentData(navigator)
: detectPlatform(navigator)
}

const detectPlatformWithUserAgentData = (navigator: Navigator) => {
const userAgentData: NavigatorUAData | undefined = navigator.userAgentData
return userAgentData?.platform.toLowerCase()
}

export function getBrowserOs(navigator: any) {
const supportedOs = ['Win', 'Mac', 'Linux', 'MacIntel']
const platform = navigator.platform
return supportedOs.find((os) => platform.includes(os)) || DEFAULT_OS
const detectPlatform = (navigator: Navigator): string | undefined => {
const ua = navigator.userAgent.toLowerCase().replace(/^mozilla\/\d\.\d\W/, '')
const mobiles: Record<string, RegExp> = {
iphone: /iphone/,
ipad: /ipad|macintosh/,
android: /android/,
}
const desktops: Record<string, RegExp> = {
windows: /win/,
macos: /macintosh/,
linux: /linux/,
}
// Determine the operating system
const mobileOS = Object.keys(mobiles).find(
(os) => mobiles[os].test(ua) && navigator.maxTouchPoints >= 1,
)
const desktopOS = Object.keys(desktops).find((os) => desktops[os].test(ua))
const os = mobileOS || desktopOS
return os
}
83 changes: 61 additions & 22 deletions getLatestRelease.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,78 @@
import axios from 'axios'
import { URL_RELEASE_BASE } from './constants'
import { getBrowserOs } from '@/getBrowserOs'
import { GITHUB_RELEASE_URL, URL_RELEASE_BASE } from './constants'

type Release = {
platform: String
releaseUrl: String
downloadName: String
platform: string
releaseUrl: string
downloadName: string | null
}

export async function getLatestRelease(navigator: any): Promise<Release> {
return await axios.get(URL_RELEASE_BASE).then(async (result: any) => {
const os = await getBrowserOs(navigator).toLowerCase()
const macRelease = await result.data.assets.find((asset: any) => {
return asset.browser_download_url.includes('myWitWallet.dmg')
})
const linuxRelease = await result.data.assets.find((asset: any) =>
asset.browser_download_url.includes('linux.tar.gz')
)
type LatestReleaseResponse = {
assets: []
}

type ReleaseAsset = {
browser_download_url: string
name: string | null
}

export function getStoreRelease({ os }: { os: string }): Release | undefined {
const iosLink = {
platform: 'iOS',
releaseUrl: 'https://apps.apple.com/cl/app/mywitwallet/id6449979271',
downloadName: null,
}
const storeReleases: Record<string, Release> = {
iphone: iosLink,
ipad: iosLink,
android: {
platform: 'Android',
releaseUrl:
'https://play.google.com/store/apps/details?id=io.witnet.myWitWallet&hl=es_419&pli=1',
downloadName: null,
},
windows: {
platform: 'Windows',
releaseUrl: 'https://apps.microsoft.com/detail/9PN09DKWPL57',
downloadName: null,
},
}
if (storeReleases[os]) {
return storeReleases[os]
}
}

const { data }: { data: Ref<LatestReleaseResponse | undefined> } =
await useAsyncData('release', () => $fetch(URL_RELEASE_BASE))

export async function getLatestRelease({
os,
}: {
os: string
}): Promise<Release | null> {
if (data.value) {
const macRelease: ReleaseAsset = data.value.assets.find(
(asset: ReleaseAsset) => {
return asset.browser_download_url.includes('myWitWallet.dmg')
},
) ?? { browser_download_url: GITHUB_RELEASE_URL, name: null }
const linuxRelease: ReleaseAsset = data.value.assets.find(
(asset: ReleaseAsset) =>
asset.browser_download_url.includes('linux.tar.gz'),
) ?? { browser_download_url: GITHUB_RELEASE_URL, name: null }

const release: Record<string, Release> = {
linux: {
platform: 'GNU / Linux',
releaseUrl: linuxRelease.browser_download_url,
downloadName: linuxRelease.name,
},
win: {
platform: 'Windows',
releaseUrl: 'https://apps.microsoft.com/detail/9PN09DKWPL57',
downloadName: 'myWitWallet',
},
mac: {
macos: {
platform: 'Mac OS',
releaseUrl: macRelease.browser_download_url,
downloadName: macRelease.name,
},
}
return release[os]
})
}
return null
}
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"my_wit_wallet": "myWitWallet"
},
"title": "Get myWitWallet,{0} your Witnet light wallet",
"download": "Download myWitWallet for {platform}",
"downloadLink": "Download myWitWallet for {platform}",
"githubLink": "Download myWitWallet on GitHub",
"source_code": "Check out the source code on {github}"
}

0 comments on commit 2f57d09

Please sign in to comment.