|
| 1 | +<script setup lang="ts"> |
| 2 | +import { usePackageDependents } from '~/composables/useNpmRegistry' |
| 3 | +import { formatCompactNumber } from '~/utils/formatters' |
| 4 | +
|
| 5 | +const props = defineProps<{ |
| 6 | + packageName: string |
| 7 | +}>() |
| 8 | +
|
| 9 | +const { data, status } = usePackageDependents(() => props.packageName) |
| 10 | +
|
| 11 | +const dependents = computed(() => data.value?.objects ?? []) |
| 12 | +const total = computed(() => data.value?.total ?? 0) |
| 13 | +
|
| 14 | +// Expanded state for showing all dependents |
| 15 | +const expanded = ref(false) |
| 16 | +
|
| 17 | +// Show first 10 by default, all when expanded |
| 18 | +const visibleDependents = computed(() => { |
| 19 | + if (expanded.value) { |
| 20 | + return dependents.value |
| 21 | + } |
| 22 | + return dependents.value.slice(0, 10) |
| 23 | +}) |
| 24 | +
|
| 25 | +// Show section only when we have dependents or are loading |
| 26 | +const showSection = computed(() => { |
| 27 | + return status.value === 'pending' || dependents.value.length > 0 |
| 28 | +}) |
| 29 | +</script> |
| 30 | + |
| 31 | +<template> |
| 32 | + <section v-if="showSection" aria-labelledby="dependents-heading"> |
| 33 | + <h2 id="dependents-heading" class="text-xs text-fg-subtle uppercase tracking-wider mb-3"> |
| 34 | + Dependents |
| 35 | + <span v-if="status === 'success' && total > 0">({{ total.toLocaleString() }})</span> |
| 36 | + </h2> |
| 37 | + |
| 38 | + <!-- Loading state --> |
| 39 | + <div v-if="status === 'pending'" class="space-y-2"> |
| 40 | + <div v-for="i in 5" :key="i" class="flex items-center justify-between py-1"> |
| 41 | + <div class="skeleton h-4 rounded" :style="{ width: `${60 + (i % 3) * 20}px` }" /> |
| 42 | + <div class="skeleton h-3 w-12 rounded" /> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + |
| 46 | + <!-- Dependents list --> |
| 47 | + <ul |
| 48 | + v-else-if="dependents.length > 0" |
| 49 | + class="space-y-1 list-none m-0 p-0" |
| 50 | + aria-label="Packages that depend on this package" |
| 51 | + > |
| 52 | + <li |
| 53 | + v-for="dependent in visibleDependents" |
| 54 | + :key="dependent.package.name" |
| 55 | + class="flex items-center justify-between py-1 text-sm gap-2" |
| 56 | + > |
| 57 | + <NuxtLink |
| 58 | + :to="{ name: 'package', params: { package: dependent.package.name.split('/') } }" |
| 59 | + class="font-mono text-fg-muted hover:text-fg transition-colors duration-200 truncate min-w-0" |
| 60 | + > |
| 61 | + {{ dependent.package.name }} |
| 62 | + </NuxtLink> |
| 63 | + <span |
| 64 | + v-if="dependent.downloads?.weekly" |
| 65 | + class="font-mono text-xs text-fg-subtle shrink-0 flex items-center gap-1" |
| 66 | + :title="`${dependent.downloads.weekly.toLocaleString()} weekly downloads`" |
| 67 | + > |
| 68 | + <span class="i-carbon-download w-3 h-3" aria-hidden="true" /> |
| 69 | + {{ formatCompactNumber(dependent.downloads.weekly, { decimals: 1 }) }} |
| 70 | + </span> |
| 71 | + </li> |
| 72 | + </ul> |
| 73 | + |
| 74 | + <!-- Expand button --> |
| 75 | + <button |
| 76 | + v-if="dependents.length > 10 && !expanded" |
| 77 | + type="button" |
| 78 | + class="mt-2 font-mono text-xs text-fg-muted hover:text-fg transition-colors duration-200 rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50" |
| 79 | + @click="expanded = true" |
| 80 | + > |
| 81 | + show top {{ dependents.length }} |
| 82 | + </button> |
| 83 | + </section> |
| 84 | +</template> |
0 commit comments