Skip to content

Commit a27daa1

Browse files
committed
refactor: refactor some code smell
1 parent 1a61975 commit a27daa1

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

app/components/Package/ShareModal.vue

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ const colorParam = computed(() =>
1818
const cardUrl = computed(
1919
() => `/api/card/${props.packageName}.png?theme=${theme.value}${colorParam.value}`,
2020
)
21-
const absoluteCardUrl = computed(
22-
() => `${origin}/api/card/${props.packageName}.png?theme=${theme.value}${colorParam.value}`,
23-
)
21+
const absoluteCardUrl = computed(() => `${origin}${cardUrl.value}`)
2422
2523
// Downloads for alt text
24+
const compactFormatter = useCompactNumberFormatter()
2625
const { data: downloadsData } = usePackageDownloads(
2726
computed(() => props.packageName),
2827
'last-week',
@@ -33,9 +32,7 @@ const altText = computed(() => {
3332
const parts: string[] = [`${props.packageName} ${props.resolvedVersion} (${tag})`]
3433
const dl = downloadsData.value?.downloads
3534
if (dl && dl > 0) {
36-
parts.push(
37-
`${Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 1 }).format(dl)} weekly downloads`,
38-
)
35+
parts.push(`${compactFormatter.value.format(dl)} weekly downloads`)
3936
}
4037
if (props.license) parts.push(`${props.license} license`)
4138
parts.push('via npmx.dev')
@@ -64,20 +61,24 @@ const imgError = ref(false)
6461
watch(cardUrl, () => {
6562
imgLoaded.value = false
6663
imgError.value = false
64+
showAlt.value = false
6765
})
6866
6967
async function downloadCard() {
7068
const a = document.createElement('a')
7169
a.href = cardUrl.value
7270
a.download = `${props.packageName.replace('/', '-')}-card.png`
7371
document.body.appendChild(a)
74-
a.click()
75-
document.body.removeChild(a)
72+
try {
73+
a.click()
74+
} finally {
75+
document.body.removeChild(a)
76+
}
7677
showAlt.value = true
7778
}
7879
7980
function handleCopyLink() {
80-
copyLink(absoluteCardUrl.value)
81+
copyLink()
8182
showAlt.value = true
8283
}
8384
</script>
@@ -122,7 +123,7 @@ function handleCopyLink() {
122123
:alt="`${packageName} share card`"
123124
class="w-full h-full rounded"
124125
style="object-fit: contain; image-rendering: high-quality"
125-
:class="imgLoaded ? '' : 'hidden'"
126+
:class="{ hidden: !imgLoaded }"
126127
@load="imgLoaded = true"
127128
@error="imgError = true"
128129
/>
@@ -144,7 +145,7 @@ function handleCopyLink() {
144145
>
145146
<ButtonBase
146147
:classicon="altCopied ? 'i-lucide:check' : 'i-lucide:copy'"
147-
@click="copyAlt(altText)"
148+
@click="copyAlt()"
148149
>
149150
{{ altCopied ? 'Copied!' : 'Copy ALT' }}
150151
</ButtonBase>

0 commit comments

Comments
 (0)