forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListToolbar.vue
More file actions
257 lines (233 loc) · 8.21 KB
/
ListToolbar.vue
File metadata and controls
257 lines (233 loc) · 8.21 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<script setup lang="ts">
import type {
ColumnConfig,
ColumnId,
DownloadRange,
FilterChip,
PageSize,
PaginationMode,
SearchScope,
SecurityFilter,
SortKey,
SortOption,
StructuredFilters,
UpdatedWithin,
ViewMode,
} from '#shared/types/preferences'
import {
buildSortOption,
parseSortOption,
SORT_KEYS,
toggleDirection,
} from '#shared/types/preferences'
const props = defineProps<{
filters: StructuredFilters
columns: ColumnConfig[]
totalCount: number
filteredCount: number
availableKeywords?: string[]
activeFilters: FilterChip[]
/** When true, shows search-specific UI (relevance sort, no filters) */
searchContext?: boolean
/** Sort keys to force-disable (e.g. when the current provider doesn't support them) */
disabledSortKeys?: SortKey[]
}>()
const { t } = useI18n()
const sortOption = defineModel<SortOption>('sortOption', { required: true })
const viewMode = defineModel<ViewMode>('viewMode', { required: true })
const paginationMode = defineModel<PaginationMode>('paginationMode', { required: true })
const pageSize = defineModel<PageSize>('pageSize', { required: true })
const emit = defineEmits<{
'toggleColumn': [columnId: ColumnId]
'toggleSelection': []
'resetColumns': []
'clearFilter': [chip: FilterChip]
'clearAllFilters': []
'update:text': [value: string]
'update:searchScope': [value: SearchScope]
'update:downloadRange': [value: DownloadRange]
'update:security': [value: SecurityFilter]
'update:updatedWithin': [value: UpdatedWithin]
'toggleKeyword': [keyword: string]
}>()
const showingFiltered = computed(() => props.filteredCount !== props.totalCount)
// Parse current sort option into key and direction
const currentSort = computed(() => parseSortOption(sortOption.value))
// Get available sort keys based on context
const disabledSet = computed(() => new Set(props.disabledSortKeys ?? []))
const availableSortKeys = computed(() => {
const applyDisabled = (k: (typeof SORT_KEYS)[number]) => ({
...k,
disabled: k.disabled || disabledSet.value.has(k.key),
})
if (props.searchContext) {
// In search context: show relevance + non-disabled sorts (downloads, updated, name)
return SORT_KEYS.filter(k => !k.searchOnly || k.key === 'relevance').map(applyDisabled)
}
// In org/user context: hide search-only sorts
return SORT_KEYS.filter(k => !k.searchOnly).map(applyDisabled)
})
// Handle sort key change from dropdown
const sortKeyModel = computed<SortKey>({
get: () => currentSort.value.key,
set: newKey => {
const config = SORT_KEYS.find(k => k.key === newKey)
const direction = config?.defaultDirection ?? 'desc'
sortOption.value = buildSortOption(newKey, direction)
},
})
// Toggle sort direction
function handleToggleDirection() {
const { key, direction } = currentSort.value
sortOption.value = buildSortOption(key, toggleDirection(direction))
}
// Map sort key to i18n key
const sortKeyLabelKeys = computed<Record<SortKey, string>>(() => ({
'relevance': t('filters.sort.relevance'),
'downloads-week': t('filters.sort.downloads_week'),
'downloads-day': t('filters.sort.downloads_day'),
'downloads-month': t('filters.sort.downloads_month'),
'downloads-year': t('filters.sort.downloads_year'),
'updated': t('filters.sort.published'),
'name': t('filters.sort.name'),
}))
function getSortKeyLabelKey(key: SortKey): string {
return sortKeyLabelKeys.value[key]
}
const { selectedPackages, clearSelectedPackages } = usePackageSelection()
</script>
<template>
<div class="space-y-3 mb-6">
<!-- Main toolbar row -->
<div class="flex flex-col sm:flex-row sm:items-center gap-3">
<!-- Count display (infinite scroll mode only) -->
<div
v-if="viewMode === 'cards' && paginationMode === 'infinite' && !searchContext"
class="text-sm font-mono text-fg-muted"
>
<template v-if="showingFiltered">
{{
$t(
'filters.count.showing_filtered',
{
filtered: $n(filteredCount),
count: $n(totalCount),
},
totalCount,
)
}}
</template>
<template v-else>
{{ $t('filters.count.showing_all', { count: $n(totalCount) }, totalCount) }}
</template>
</div>
<!-- Count display (paginated/table mode only) -->
<div
v-if="(viewMode === 'table' || paginationMode === 'paginated') && !searchContext"
class="text-sm font-mono text-fg-muted"
>
{{
$t(
'filters.count.showing_paginated',
{
pageSize: Math.min(pageSize, filteredCount),
count: $n(filteredCount),
},
filteredCount,
)
}}
</div>
<div class="flex-1" />
<div class="flex flex-col sm:flex-row items-start sm:items-center gap-3">
<!-- Sort controls -->
<div class="flex items-center gap-1 shrink-0">
<!-- Sort key dropdown -->
<SelectField
:label="$t('filters.sort.label')"
hidden-label
id="sort-select"
v-model="sortKeyModel"
:items="
availableSortKeys.map(keyConfig => ({
label: getSortKeyLabelKey(keyConfig.key),
value: keyConfig.key,
disabled: keyConfig.disabled,
}))
"
/>
<!-- Sort direction toggle -->
<button
v-if="!searchContext || currentSort.key !== 'relevance'"
type="button"
class="p-2.5 rounded-md border border-border bg-bg-subtle text-fg-muted hover:text-fg hover:border-border-hover transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-2 focus-visible:ring-offset-bg"
:aria-label="$t('filters.sort.toggle_direction')"
:title="
currentSort.direction === 'asc'
? $t('filters.sort.ascending')
: $t('filters.sort.descending')
"
@click="handleToggleDirection"
>
<span
class="w-4 h-4 block transition-transform duration-200"
:class="
currentSort.direction === 'asc'
? 'i-lucide:arrow-down-narrow-wide'
: 'i-lucide:arrow-down-wide-narrow'
"
aria-hidden="true"
/>
</button>
</div>
<!-- View mode toggle - mobile (left side, row 2) -->
<div class="flex flex-row-reverse sm:flex-row items-center gap-1">
<ColumnPicker
v-if="viewMode === 'table'"
:columns="columns"
@toggle="emit('toggleColumn', $event)"
@reset="emit('resetColumns')"
/>
<ViewModeToggle v-model="viewMode" />
</div>
<div
class="flex items-center order-3 sm:border-is sm:border-fg-subtle/20 sm:ps-3"
v-if="selectedPackages.length"
>
<ButtonBase
variant="secondary"
@click="emit('toggleSelection')"
classicon="i-lucide:package-check"
>
{{ t('filters.view_selected') }} ({{ selectedPackages.length }})
</ButtonBase>
<button
@click="clearSelectedPackages"
:aria-label="$t('filters.clear_selected_label')"
class="flex items-center ms-2"
>
<span class="i-lucide:x text-sm" />
</button>
</div>
</div>
</div>
<!-- Filter panel (hidden in search context) -->
<FilterPanel
v-if="!searchContext"
:filters="filters"
:available-keywords="availableKeywords"
@update:text="emit('update:text', $event)"
@update:search-scope="emit('update:searchScope', $event)"
@update:download-range="emit('update:downloadRange', $event)"
@update:security="emit('update:security', $event)"
@update:updated-within="emit('update:updatedWithin', $event)"
@toggle-keyword="emit('toggleKeyword', $event)"
/>
<!-- Active filter chips (hidden in search context) -->
<FilterChips
v-if="!searchContext"
:chips="activeFilters"
@remove="emit('clearFilter', $event)"
@clear-all="emit('clearAllFilters')"
/>
</div>
</template>