|
| 1 | +<script setup> |
| 2 | +// noinspection NpmUsedModulesInstalled |
| 3 | +import { ref } from 'vue' |
| 4 | +import VPButton from 'vitepress/dist/client/theme-default/components/VPButton.vue' |
| 5 | +
|
| 6 | +const props = defineProps({ |
| 7 | + repo: { type: String, required: true }, |
| 8 | +}) |
| 9 | +const href = `https://github.com/${props.repo}/releases/latest` |
| 10 | +const src = `https://img.shields.io/github/v/release/${props.repo}?style=for-the-badge&logo=github&label=latest%20version` |
| 11 | +
|
| 12 | +const btnTheme = ref('brand') |
| 13 | +const shaOutput = ref(null) |
| 14 | +const myButton = ref(null) |
| 15 | +
|
| 16 | +function handleOutputClick(event) { |
| 17 | + if (event.target.value) { |
| 18 | + event.target.select() |
| 19 | + navigator.clipboard.writeText(event.target.value) |
| 20 | + } |
| 21 | +} |
| 22 | +
|
| 23 | +async function generateHash(event) { |
| 24 | + console.log('event:', event) |
| 25 | + console.log('myButton:', myButton) |
| 26 | + const el = myButton.value?.$el |
| 27 | + console.log('el:', el) |
| 28 | + if (!el) return console.error('Unable to access button element.') |
| 29 | + el.disabled = true |
| 30 | + btnTheme.value = 'alt' |
| 31 | + const release = await fetchData(`https://api.github.com/repos/${props.repo}/releases/latest`) |
| 32 | + console.log('release:', release) |
| 33 | + if (!release) return console.error('Unable to fetch latest release.') |
| 34 | + console.log('release.tag_name:', release.tag_name) |
| 35 | + const tag = await fetchData(`https://api.github.com/repos/${props.repo}/git/ref/tags/${release.tag_name}`) |
| 36 | + console.log('tag:', tag) |
| 37 | + if (!tag?.object?.sha) return console.error('Unable to fetch tag/sha.') |
| 38 | + console.log('tag.object.sha:', tag.object.sha) |
| 39 | + shaOutput.value = tag.object.sha |
| 40 | + await navigator.clipboard.writeText(tag.object.sha) |
| 41 | +} |
| 42 | +
|
| 43 | +/** |
| 44 | + * Fetch Data |
| 45 | + * @param {String} url |
| 46 | + * @return {Promise<Object>} |
| 47 | + */ |
| 48 | +async function fetchData(url) { |
| 49 | + const options = { headers: { 'X-GitHub-Api-Version': '2022-11-28' } } |
| 50 | + const response = await fetch(url, options) |
| 51 | + console.log('response.status:', response.status) |
| 52 | + if (!response.ok) return |
| 53 | + return await response.json() |
| 54 | +} |
| 55 | +</script> |
| 56 | +
|
| 57 | +<template> |
| 58 | + <div id="buttons"> |
| 59 | + <VPButton text="Generate Commit Hash" @click="generateHash" :theme="btnTheme" ref="myButton" /> |
| 60 | + <div> |
| 61 | + <a :href="href" target="_blank" rel="noopener"> |
| 62 | + <img alt="GitHub Release" :src="src" /> |
| 63 | + </a> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + <div :style="{ display: shaOutput ? 'block' : 'none' }"> |
| 67 | + <input |
| 68 | + id="encodeOutput" |
| 69 | + placeholder="Encoded Results" |
| 70 | + v-model="shaOutput" |
| 71 | + type="text" |
| 72 | + @click="handleOutputClick" |
| 73 | + readonly |
| 74 | + /> |
| 75 | + <p>Hash Copied to Clipboard</p> |
| 76 | + </div> |
| 77 | +</template> |
| 78 | +
|
| 79 | +<style scoped> |
| 80 | +#buttons > * { |
| 81 | + margin-bottom: 0.5rem; |
| 82 | +} |
| 83 | +
|
| 84 | +@media (min-width: 640px) { |
| 85 | + #buttons { |
| 86 | + display: flex; |
| 87 | + align-items: center; |
| 88 | + justify-content: space-between; |
| 89 | + } |
| 90 | +} |
| 91 | +img { |
| 92 | + display: inline-block; |
| 93 | +} |
| 94 | +input { |
| 95 | + border: 1px solid var(--vp-c-gray-1); |
| 96 | + border-radius: 8px; |
| 97 | + padding: 6px; |
| 98 | + width: 100%; |
| 99 | +} |
| 100 | +p { |
| 101 | + color: var(--vp-c-success-1); |
| 102 | + margin: 0 0 8px 8px; |
| 103 | + font-weight: 300; |
| 104 | +} |
| 105 | +</style> |
0 commit comments