-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters