diff --git a/components/DownloadBtn.vue b/components/DownloadBtn.vue index 5976ad5..299112e 100644 --- a/components/DownloadBtn.vue +++ b/components/DownloadBtn.vue @@ -1,31 +1,66 @@ - + diff --git a/constants.ts b/constants.ts index b0bcf65..b634c29 100644 --- a/constants.ts +++ b/constants.ts @@ -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' diff --git a/getBrowserOs.ts b/getBrowserOs.ts index 470367a..9d45ac7 100644 --- a/getBrowserOs.ts +++ b/getBrowserOs.ts @@ -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 = { + iphone: /iphone/, + ipad: /ipad|macintosh/, + android: /android/, + } + const desktops: Record = { + 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 } diff --git a/getLatestRelease.ts b/getLatestRelease.ts index 09e3202..f7fc6e8 100644 --- a/getLatestRelease.ts +++ b/getLatestRelease.ts @@ -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 { - 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 = { + 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 } = + await useAsyncData('release', () => $fetch(URL_RELEASE_BASE)) + +export async function getLatestRelease({ + os, +}: { + os: string +}): Promise { + 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 = { 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 } diff --git a/locales/en.json b/locales/en.json index 800c36a..477eb19 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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}" }