|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, onMounted, ref } from 'vue' |
| 3 | +
|
| 4 | +type PlatformTarget = 'macOS' | 'Windows' | 'Linux' |
| 5 | +
|
| 6 | +type GithubReleaseAsset = { |
| 7 | + name: string |
| 8 | + browser_download_url: string |
| 9 | +} |
| 10 | +
|
| 11 | +type GithubLatestRelease = { |
| 12 | + html_url: string |
| 13 | + assets?: GithubReleaseAsset[] |
| 14 | +} |
| 15 | +
|
| 16 | +const latestReleaseUrl = 'https://github.com/interaapps/starquery/releases/latest' |
| 17 | +const githubLatestReleaseApi = 'https://api.github.com/repos/interaapps/starquery/releases/latest' |
| 18 | +const platform = ref<PlatformTarget | null>(null) |
| 19 | +const downloadUrl = ref(latestReleaseUrl) |
| 20 | +const isLoading = ref(false) |
| 21 | +
|
| 22 | +defineProps<{ |
| 23 | + large?: boolean |
| 24 | +}>() |
| 25 | +
|
| 26 | +const label = computed(() => { |
| 27 | + if (!platform.value) { |
| 28 | + return 'Install' |
| 29 | + } |
| 30 | +
|
| 31 | + return `Install for ${platform.value}` |
| 32 | +}) |
| 33 | +
|
| 34 | +function detectPlatform(): PlatformTarget | null { |
| 35 | + const value = `${navigator.platform} ${navigator.userAgent}`.toLowerCase() |
| 36 | +
|
| 37 | + if (value.includes('mac')) { |
| 38 | + return 'macOS' |
| 39 | + } |
| 40 | +
|
| 41 | + if (value.includes('win')) { |
| 42 | + return 'Windows' |
| 43 | + } |
| 44 | +
|
| 45 | + if (value.includes('linux')) { |
| 46 | + return 'Linux' |
| 47 | + } |
| 48 | +
|
| 49 | + return null |
| 50 | +} |
| 51 | +
|
| 52 | +function getAssetMatchers(target: PlatformTarget) { |
| 53 | + if (target === 'macOS') { |
| 54 | + return [ |
| 55 | + (name: string) => name.endsWith('.zip') && (name.includes('darwin') || name.includes('mac')), |
| 56 | + (name: string) => name.endsWith('.zip'), |
| 57 | + ] |
| 58 | + } |
| 59 | +
|
| 60 | + if (target === 'Windows') { |
| 61 | + return [ |
| 62 | + (name: string) => name.endsWith('.exe'), |
| 63 | + (name: string) => name.endsWith('.msix'), |
| 64 | + (name: string) => name.endsWith('.nupkg'), |
| 65 | + ] |
| 66 | + } |
| 67 | +
|
| 68 | + return [ |
| 69 | + (name: string) => name.endsWith('.deb'), |
| 70 | + (name: string) => name.endsWith('.rpm'), |
| 71 | + (name: string) => name.endsWith('.appimage'), |
| 72 | + (name: string) => name.endsWith('.tar.gz'), |
| 73 | + ] |
| 74 | +} |
| 75 | +
|
| 76 | +function findBestAssetUrl(target: PlatformTarget, assets: GithubReleaseAsset[]) { |
| 77 | + const normalizedAssets = assets.map((asset) => ({ |
| 78 | + ...asset, |
| 79 | + normalizedName: asset.name.toLowerCase(), |
| 80 | + })) |
| 81 | +
|
| 82 | + for (const matcher of getAssetMatchers(target)) { |
| 83 | + const match = normalizedAssets.find((asset) => matcher(asset.normalizedName)) |
| 84 | + if (match) { |
| 85 | + return match.browser_download_url |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + return null |
| 90 | +} |
| 91 | +
|
| 92 | +async function loadLatestReleaseAsset(target: PlatformTarget) { |
| 93 | + isLoading.value = true |
| 94 | +
|
| 95 | + try { |
| 96 | + const response = await fetch(githubLatestReleaseApi, { |
| 97 | + headers: { |
| 98 | + Accept: 'application/vnd.github+json', |
| 99 | + }, |
| 100 | + }) |
| 101 | +
|
| 102 | + if (!response.ok) { |
| 103 | + return |
| 104 | + } |
| 105 | +
|
| 106 | + const release = (await response.json()) as GithubLatestRelease |
| 107 | + const assetUrl = findBestAssetUrl(target, release.assets ?? []) |
| 108 | + downloadUrl.value = assetUrl || release.html_url || latestReleaseUrl |
| 109 | + } catch { |
| 110 | + downloadUrl.value = latestReleaseUrl |
| 111 | + } finally { |
| 112 | + isLoading.value = false |
| 113 | + } |
| 114 | +} |
| 115 | +
|
| 116 | +onMounted(() => { |
| 117 | + const detectedPlatform = detectPlatform() |
| 118 | + platform.value = detectedPlatform |
| 119 | +
|
| 120 | + if (detectedPlatform) { |
| 121 | + void loadLatestReleaseAsset(detectedPlatform) |
| 122 | + } |
| 123 | +}) |
| 124 | +</script> |
| 125 | + |
| 126 | +<template> |
| 127 | + <a |
| 128 | + :href="downloadUrl" |
| 129 | + class="whitespace-nowrap rounded-full border border-transparent bg-primary-500 hover:bg-primary-600 hover:scale-110 active:scale-95 transition-all text-[0.92rem] font-bold tracking-[-0.01em] !text-white !no-underline md:inline-flex md:items-center md:justify-center" |
| 130 | + :class="large ? '!text-xl py-3 px-8' : 'py-[0.3rem] px-4'" |
| 131 | + target="_blank" |
| 132 | + rel="noreferrer" |
| 133 | + :aria-busy="isLoading ? 'true' : 'false'" |
| 134 | + > |
| 135 | + {{ label }} |
| 136 | + </a> |
| 137 | +</template> |
0 commit comments