-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathCard.vue
More file actions
181 lines (170 loc) · 6.5 KB
/
Card.vue
File metadata and controls
181 lines (170 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<script setup lang="ts">
import type { StructuredFilters } from '#shared/types/preferences'
const props = defineProps<{
/** The search result object containing package data */
result: NpmSearchResult
/** Heading level for the package name (h2 for search, h3 for lists) */
headingLevel?: 'h2' | 'h3'
/** Whether to show the publisher username */
showPublisher?: boolean
prefetch?: boolean
index?: number
/** Filters to apply to the results */
filters?: StructuredFilters
/** Search query for highlighting exact matches */
searchQuery?: string
}>()
const { isPackageSelected, togglePackageSelection, canSelectMore } = usePackageSelection()
const isSelected = computed<boolean>(() => {
return isPackageSelected(props.result.package.name)
})
const emit = defineEmits<{
clickKeyword: [keyword: string]
}>()
/** Check if this package is an exact match for the search query */
const isExactMatch = computed(() => {
if (!props.searchQuery) return false
const query = props.searchQuery.trim().toLowerCase()
const name = props.result.package.name.toLowerCase()
return query === name
})
// Process package description
const pkgDescription = useMarkdown(() => ({
text: props.result.package.description ?? '',
plain: true,
}))
const numberFormatter = useNumberFormatter()
</script>
<template>
<BaseCard :selected="isSelected" :isExactMatch="isExactMatch" :index="index">
<header class="mb-4 flex items-baseline justify-between gap-2">
<component
:is="headingLevel ?? 'h3'"
class="font-mono text-sm sm:text-base font-medium text-fg group-hover:text-fg transition-colors duration-200 min-w-0 break-all"
>
<NuxtLink
:to="packageRoute(result.package.name)"
:prefetch-on="prefetch ? 'visibility' : 'interaction'"
class="decoration-none after:content-[''] after:absolute after:inset-0"
dir="ltr"
>{{ result.package.name }}</NuxtLink
>
<span
v-if="isExactMatch"
class="text-xs px-1.5 py-0.5 ms-2 rounded bg-bg-elevated border border-border-hover text-fg"
>{{ $t('search.exact_match') }}</span
>
</component>
<PackageSelectionCheckbox
:package-name="result.package.name"
:disabled="!canSelectMore && !isSelected"
:checked="isSelected"
@change="togglePackageSelection"
/>
</header>
<div class="flex flex-col sm:flex-row sm:justify-start sm:items-start gap-6 sm:gap-8">
<div class="min-w-0 w-full">
<p v-if="pkgDescription" class="text-fg-muted text-xs sm:text-sm line-clamp-2 mb-2 sm:mb-3">
<span v-html="pkgDescription" />
</p>
<div class="flex flex-wrap items-center gap-x-3 sm:gap-x-4 gap-y-2 text-xs text-fg-muted">
<dl v-if="showPublisher || result.package.date" class="flex items-center gap-4 m-0">
<div
v-if="showPublisher && result.package.publisher?.username"
class="flex items-center gap-1.5"
>
<dt class="sr-only">{{ $t('package.card.publisher') }}</dt>
<dd class="font-mono">{{ result.package.publisher.username }}</dd>
</div>
<div v-if="result.package.date" class="flex items-center gap-1.5">
<dt class="sr-only">{{ $t('package.card.published') }}</dt>
<dd>
<DateTime
:datetime="result.package.date"
year="numeric"
month="short"
day="numeric"
/>
</dd>
</div>
<div v-if="result.package.license" class="flex items-center gap-1.5">
<dt class="sr-only">{{ $t('package.card.license') }}</dt>
<dd>{{ result.package.license }}</dd>
</div>
</dl>
</div>
<!-- Mobile: downloads on separate row -->
<dl
v-if="result.downloads?.weekly"
class="sm:hidden flex items-center gap-4 mt-2 text-xs text-fg-muted m-0"
>
<div class="flex items-center gap-1.5">
<dt class="sr-only">{{ $t('package.card.weekly_downloads') }}</dt>
<dd class="flex items-center gap-1.5">
<span class="i-lucide:chart-line w-3.5 h-3.5" aria-hidden="true" />
<span class="font-mono">{{ $n(result.downloads.weekly) }}/w</span>
</dd>
</div>
</dl>
</div>
<div class="flex flex-col gap-2 shrink-0">
<div class="text-fg-subtle flex items-start gap-2 sm:justify-end">
<span
v-if="result.package.version"
class="font-mono text-xs truncate max-w-32"
:title="result.package.version"
>
v{{ result.package.version }}
</span>
<div
v-if="result.package.publisher?.trustedPublisher"
class="flex items-center gap-1.5 shrink-0 max-w-32"
>
<ProvenanceBadge
:provider="result.package.publisher.trustedPublisher.id"
:package-name="result.package.name"
:version="result.package.version"
:linked="false"
compact
/>
</div>
</div>
<div
v-if="result.downloads?.weekly"
class="text-fg-subtle gap-2 flex items-center sm:justify-end"
>
<span class="i-lucide:chart-line w-3.5 h-3.5" aria-hidden="true" />
<span class="font-mono text-xs">
{{ $n(result.downloads.weekly) }} {{ $t('common.per_week') }}
</span>
</div>
</div>
</div>
<ul
v-if="result.package.keywords?.length"
:aria-label="$t('package.card.keywords')"
class="relative z-10 flex flex-wrap gap-1.5 mt-3 pt-3 border-t border-border list-none m-0 p-0 pointer-events-none items-center"
>
<li v-for="keyword in result.package.keywords.slice(0, 5)" :key="keyword">
<ButtonBase
class="pointer-events-auto"
size="sm"
:aria-pressed="props.filters?.keywords.includes(keyword)"
:title="`Filter by ${keyword}`"
@click.stop="emit('clickKeyword', keyword)"
>
{{ keyword }}
</ButtonBase>
</li>
<li>
<span
v-if="result.package.keywords.length > 5"
class="text-fg-subtle text-xs pointer-events-auto"
:title="result.package.keywords.slice(5).join(', ')"
>
+{{ numberFormatter.format(result.package.keywords.length - 5) }}
</span>
</li>
</ul>
</BaseCard>
</template>