|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { FacetValue } from '#shared/types' |
| 3 | +
|
| 4 | +const props = defineProps<{ |
| 5 | + /** Facet label */ |
| 6 | + label: string |
| 7 | + /** Description/tooltip for the facet */ |
| 8 | + description?: string |
| 9 | + /** Values for each column */ |
| 10 | + values: (FacetValue | null | undefined)[] |
| 11 | + /** Whether this facet is loading (e.g., install size) */ |
| 12 | + facetLoading?: boolean |
| 13 | + /** Whether each column is loading (array matching values) */ |
| 14 | + columnLoading?: boolean[] |
| 15 | + /** Whether to show the proportional bar (defaults to true for numeric values) */ |
| 16 | + bar?: boolean |
| 17 | + /** Package headers for display */ |
| 18 | + headers: string[] |
| 19 | +}>() |
| 20 | +
|
| 21 | +// Check if all values are numeric (for bar visualization) |
| 22 | +const isNumeric = computed(() => { |
| 23 | + return props.values.every(v => v === null || v === undefined || typeof v.raw === 'number') |
| 24 | +}) |
| 25 | +
|
| 26 | +// Show bar if explicitly enabled, or if not specified and values are numeric |
| 27 | +const showBar = computed(() => { |
| 28 | + return props.bar ?? isNumeric.value |
| 29 | +}) |
| 30 | +
|
| 31 | +// Get max value for bar width calculation |
| 32 | +const maxValue = computed(() => { |
| 33 | + if (!isNumeric.value) return 0 |
| 34 | + return Math.max(...props.values.map(v => (typeof v?.raw === 'number' ? v.raw : 0))) |
| 35 | +}) |
| 36 | +
|
| 37 | +// Calculate bar width percentage for a value |
| 38 | +function getBarWidth(value: FacetValue | null | undefined): number { |
| 39 | + if (!isNumeric.value || !maxValue.value || !value || typeof value.raw !== 'number') return 0 |
| 40 | + return (value.raw / maxValue.value) * 100 |
| 41 | +} |
| 42 | +
|
| 43 | +function getStatusClass(status?: FacetValue['status']): string { |
| 44 | + switch (status) { |
| 45 | + case 'good': |
| 46 | + return 'text-emerald-400' |
| 47 | + case 'info': |
| 48 | + return 'text-blue-400' |
| 49 | + case 'warning': |
| 50 | + return 'text-amber-400' |
| 51 | + case 'bad': |
| 52 | + return 'text-red-400' |
| 53 | + default: |
| 54 | + return 'text-fg' |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +// Check if a specific cell is loading |
| 59 | +function isCellLoading(index: number): boolean { |
| 60 | + return props.facetLoading || (props.columnLoading?.[index] ?? false) |
| 61 | +} |
| 62 | +
|
| 63 | +// Get short package name (without version) for mobile display |
| 64 | +function getShortName(header: string): string { |
| 65 | + const atIndex = header.lastIndexOf('@') |
| 66 | + if (atIndex > 0) { |
| 67 | + return header.substring(0, atIndex) |
| 68 | + } |
| 69 | + return header |
| 70 | +} |
| 71 | +</script> |
| 72 | + |
| 73 | +<template> |
| 74 | + <div class="border border-border rounded-lg overflow-hidden"> |
| 75 | + <!-- Facet header --> |
| 76 | + <div class="flex items-center gap-1.5 px-3 py-2 bg-bg-subtle border-b border-border"> |
| 77 | + <span class="text-xs text-fg-muted uppercase tracking-wider font-medium">{{ label }}</span> |
| 78 | + <span |
| 79 | + v-if="description" |
| 80 | + class="i-carbon:information w-3 h-3 text-fg-subtle" |
| 81 | + :title="description" |
| 82 | + aria-hidden="true" |
| 83 | + /> |
| 84 | + </div> |
| 85 | + |
| 86 | + <!-- Package values --> |
| 87 | + <div class="divide-y divide-border"> |
| 88 | + <div |
| 89 | + v-for="(value, index) in values" |
| 90 | + :key="index" |
| 91 | + class="relative flex items-center justify-between gap-2 px-3 py-2" |
| 92 | + > |
| 93 | + <!-- Background bar for numeric values --> |
| 94 | + <div |
| 95 | + v-if="showBar && value && getBarWidth(value) > 0" |
| 96 | + class="absolute inset-y-0 inset-is-0 bg-fg/5 transition-all duration-300" |
| 97 | + :style="{ width: `${getBarWidth(value)}%` }" |
| 98 | + aria-hidden="true" |
| 99 | + /> |
| 100 | + |
| 101 | + <!-- Package name --> |
| 102 | + <span |
| 103 | + class="relative font-mono text-xs text-fg-muted truncate flex-shrink min-w-0" |
| 104 | + :title="headers[index]" |
| 105 | + > |
| 106 | + {{ getShortName(headers[index] ?? '') }} |
| 107 | + </span> |
| 108 | + |
| 109 | + <!-- Value --> |
| 110 | + <span class="relative flex-shrink-0"> |
| 111 | + <!-- Loading state --> |
| 112 | + <template v-if="isCellLoading(index)"> |
| 113 | + <span |
| 114 | + class="i-carbon:circle-dash w-4 h-4 text-fg-subtle motion-safe:animate-spin" |
| 115 | + aria-hidden="true" |
| 116 | + /> |
| 117 | + </template> |
| 118 | + |
| 119 | + <!-- No data --> |
| 120 | + <template v-else-if="!value"> |
| 121 | + <span class="text-fg-subtle text-sm">-</span> |
| 122 | + </template> |
| 123 | + |
| 124 | + <!-- Value display --> |
| 125 | + <template v-else> |
| 126 | + <span class="font-mono text-sm tabular-nums" :class="getStatusClass(value.status)"> |
| 127 | + <!-- Date values use DateTime component for i18n and user settings --> |
| 128 | + <DateTime |
| 129 | + v-if="value.type === 'date'" |
| 130 | + :datetime="value.display" |
| 131 | + date-style="medium" |
| 132 | + /> |
| 133 | + <template v-else>{{ value.display }}</template> |
| 134 | + </span> |
| 135 | + </template> |
| 136 | + </span> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | +</template> |
0 commit comments