-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathuseFacetSelection.ts
More file actions
276 lines (254 loc) · 8.96 KB
/
useFacetSelection.ts
File metadata and controls
276 lines (254 loc) · 8.96 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/** Facet info enriched with i18n labels */
export interface FacetInfoWithLabels extends Omit<FacetInfo, 'id'> {
id: ComparisonFacet
label: string
description: string
chartable: boolean
chartable_scatter: boolean
formatter?: (value: number) => string
}
// Get facets in a category (excluding coming soon)
function getFacetsInCategory(category: string): ComparisonFacet[] {
return ALL_FACETS.filter(f => {
const info = FACET_INFO[f]
return info.category === category && !info.comingSoon
})
}
/**
* Composable for managing comparison facet selection with URL sync.
*
* @param queryParam - The URL query parameter name to use (default: 'facets')
*/
export function useFacetSelection(queryParam = 'facets') {
const { t } = useI18n()
const compactNumberFormatter = useCompactNumberFormatter()
const bytesFormatter = useBytesFormatter()
const facetLabels = computed(
(): Record<
ComparisonFacet,
{
label: string
description: string
chartable: boolean
chartable_scatter: boolean
formatter?: (value: number) => string
}
> => ({
downloads: {
label: t(`compare.facets.items.downloads.label`),
description: t(`compare.facets.items.downloads.description`),
chartable: true, // TODO: rename to chartable_bar
chartable_scatter: true,
formatter: v => compactNumberFormatter.value.format(v),
},
totalLikes: {
label: t(`compare.facets.items.totalLikes.label`),
description: t(`compare.facets.items.totalLikes.description`),
chartable: true,
chartable_scatter: true,
formatter: v => compactNumberFormatter.value.format(v),
},
packageSize: {
label: t(`compare.facets.items.packageSize.label`),
description: t(`compare.facets.items.packageSize.description`),
chartable: true,
chartable_scatter: true,
formatter: v => bytesFormatter.format(v),
},
installSize: {
label: t(`compare.facets.items.installSize.label`),
description: t(`compare.facets.items.installSize.description`),
chartable: true,
chartable_scatter: true,
formatter: v => bytesFormatter.format(v),
},
moduleFormat: {
label: t(`compare.facets.items.moduleFormat.label`),
description: t(`compare.facets.items.moduleFormat.description`),
chartable: false,
chartable_scatter: false,
},
types: {
label: t(`compare.facets.items.types.label`),
description: t(`compare.facets.items.types.description`),
chartable: false,
chartable_scatter: false,
},
engines: {
label: t(`compare.facets.items.engines.label`),
description: t(`compare.facets.items.engines.description`),
chartable: false,
chartable_scatter: false,
},
vulnerabilities: {
label: t(`compare.facets.items.vulnerabilities.label`),
description: t(`compare.facets.items.vulnerabilities.description`),
chartable: false,
chartable_scatter: true,
formatter: v => compactNumberFormatter.value.format(v),
},
lastUpdated: {
label: t(`compare.facets.items.lastUpdated.label`),
description: t(`compare.facets.items.lastUpdated.description`),
chartable: false,
chartable_scatter: true,
},
license: {
label: t(`compare.facets.items.license.label`),
description: t(`compare.facets.items.license.description`),
chartable: false,
chartable_scatter: false,
},
dependencies: {
label: t(`compare.facets.items.dependencies.label`),
description: t(`compare.facets.items.dependencies.description`),
chartable: true,
chartable_scatter: true,
formatter: v => compactNumberFormatter.value.format(v),
},
totalDependencies: {
label: t(`compare.facets.items.totalDependencies.label`),
description: t(`compare.facets.items.totalDependencies.description`),
chartable: true,
chartable_scatter: true,
formatter: v => compactNumberFormatter.value.format(v),
},
deprecated: {
label: t(`compare.facets.items.deprecated.label`),
description: t(`compare.facets.items.deprecated.description`),
chartable: false,
chartable_scatter: false,
},
}),
)
// Helper to build facet info with i18n labels
function buildFacetInfo(facet: ComparisonFacet): FacetInfoWithLabels {
return {
id: facet,
...FACET_INFO[facet],
label: facetLabels.value[facet].label,
description: facetLabels.value[facet].description,
chartable: facetLabels.value[facet].chartable,
chartable_scatter: facetLabels.value[facet].chartable_scatter,
formatter: facetLabels.value[facet].formatter ?? undefined,
}
}
// Sync with URL query param (stable ref - doesn't change on other query changes)
const facetsParam = useRouteQuery<string>(queryParam, '', { mode: 'replace' })
// Parse facet IDs from URL or use defaults
const selectedFacetIds = computed<ComparisonFacet[]>({
get() {
if (!facetsParam.value) {
return DEFAULT_FACETS
}
// Parse comma-separated facets and filter valid, non-comingSoon ones
const parsed = facetsParam.value
.split(',')
.map(f => f.trim())
.filter(
(f): f is ComparisonFacet =>
ALL_FACETS.includes(f as ComparisonFacet) &&
!FACET_INFO[f as ComparisonFacet].comingSoon,
)
return parsed.length > 0 ? parsed : DEFAULT_FACETS
},
set(facets) {
if (facets.length === 0 || arraysEqual(facets, DEFAULT_FACETS)) {
// Remove param if using defaults
facetsParam.value = ''
} else {
facetsParam.value = facets.join(',')
}
},
})
// Selected facets with full info and i18n labels
const selectedFacets = computed<FacetInfoWithLabels[]>(() =>
selectedFacetIds.value.map(buildFacetInfo),
)
// Check if a facet is selected
function isFacetSelected(facet: ComparisonFacet): boolean {
return selectedFacetIds.value.includes(facet)
}
// Toggle a single facet
function toggleFacet(facet: ComparisonFacet): void {
const current = selectedFacetIds.value
if (current.includes(facet)) {
// Don't allow deselecting all facets
if (current.length > 1) {
selectedFacetIds.value = current.filter(f => f !== facet)
}
} else {
selectedFacetIds.value = [...current, facet]
}
}
// Select all facets in a category
function selectCategory(category: string): void {
const categoryFacets = getFacetsInCategory(category)
const current = selectedFacetIds.value
const newFacets = [...new Set([...current, ...categoryFacets])]
selectedFacetIds.value = newFacets
}
// Deselect all facets in a category
function deselectCategory(category: string): void {
const categoryFacets = getFacetsInCategory(category)
const remaining = selectedFacetIds.value.filter(f => !categoryFacets.includes(f))
// Don't allow deselecting all facets
if (remaining.length > 0) {
selectedFacetIds.value = remaining
}
}
// Select all facets globally
function selectAll(): void {
selectedFacetIds.value = DEFAULT_FACETS
}
// Deselect all facets globally (keeps first facet to ensure at least one)
function deselectAll(): void {
selectedFacetIds.value = [DEFAULT_FACETS[0] as ComparisonFacet]
}
// Check if all facets are selected
const isAllSelected = computed(() => selectedFacetIds.value.length === DEFAULT_FACETS.length)
// Check if only one facet is selected (minimum)
const isNoneSelected = computed(() => selectedFacetIds.value.length === 1)
const facetCategories = {
performance: t(`compare.facets.categories.performance`),
health: t(`compare.facets.categories.health`),
compatibility: t(`compare.facets.categories.compatibility`),
security: t(`compare.facets.categories.security`),
}
// Get translated category name
function getCategoryLabel(category: FacetInfo['category']): string {
return facetCategories[category]
}
// All facets with their info and i18n labels, grouped by category
const facetsByCategory = computed(() => {
const result: Record<string, FacetInfoWithLabels[]> = {}
for (const category of CATEGORY_ORDER) {
result[category] = FACETS_BY_CATEGORY[category].map(buildFacetInfo)
}
return result
})
return {
selectedFacets,
isFacetSelected,
toggleFacet,
selectCategory,
deselectCategory,
selectAll,
deselectAll,
isAllSelected,
isNoneSelected,
allFacets: ALL_FACETS,
facetLabels,
// Facet info with i18n
getCategoryLabel,
facetsByCategory,
categoryOrder: CATEGORY_ORDER,
}
}
// Helper to compare arrays
function arraysEqual<T>(a: T[], b: T[]): boolean {
if (a.length !== b.length) return false
const sortedA = [...a].sort()
const sortedB = [...b].sort()
return sortedA.every((val, i) => val === sortedB[i])
}