Skip to content

Commit 46fd476

Browse files
wojtekmajdanielroeautofix-ci[bot]
authored
feat: add i18n support to compare page (#569)
Co-authored-by: Daniel Roe <daniel@roe.dev> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 0afd38b commit 46fd476

File tree

16 files changed

+853
-410
lines changed

16 files changed

+853
-410
lines changed

app/components/Filter/Panel.vue

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import type {
88
} from '#shared/types/preferences'
99
import {
1010
DOWNLOAD_RANGES,
11-
SEARCH_SCOPE_OPTIONS,
12-
SECURITY_FILTER_OPTIONS,
11+
SEARCH_SCOPE_VALUES,
12+
SECURITY_FILTER_VALUES,
1313
UPDATED_WITHIN_OPTIONS,
1414
} from '#shared/types/preferences'
1515
@@ -205,20 +205,20 @@ const hasActiveFilters = computed(() => !!filterSummary.value)
205205
:aria-label="$t('filters.search_scope')"
206206
>
207207
<button
208-
v-for="option in SEARCH_SCOPE_OPTIONS"
209-
:key="option.value"
208+
v-for="scope in SEARCH_SCOPE_VALUES"
209+
:key="scope"
210210
type="button"
211211
class="px-2 py-0.5 text-xs font-mono rounded-sm transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
212212
:class="
213-
filters.searchScope === option.value
213+
filters.searchScope === scope
214214
? 'bg-bg-muted text-fg'
215215
: 'text-fg-muted hover:text-fg'
216216
"
217-
:aria-pressed="filters.searchScope === option.value"
218-
:title="$t(getScopeDescriptionKey(option.value))"
219-
@click="emit('update:searchScope', option.value)"
217+
:aria-pressed="filters.searchScope === scope"
218+
:title="$t(getScopeDescriptionKey(scope))"
219+
@click="emit('update:searchScope', scope)"
220220
>
221-
{{ $t(getScopeLabelKey(option.value)) }}
221+
{{ $t(getScopeLabelKey(scope)) }}
222222
</button>
223223
</div>
224224
</div>
@@ -301,18 +301,18 @@ const hasActiveFilters = computed(() => !!filterSummary.value)
301301
</legend>
302302
<div class="flex flex-wrap gap-2" role="radiogroup" :aria-label="$t('filters.security')">
303303
<button
304-
v-for="option in SECURITY_FILTER_OPTIONS"
305-
:key="option.value"
304+
v-for="security in SECURITY_FILTER_VALUES"
305+
:key="security"
306306
type="button"
307307
role="radio"
308308
disabled
309-
:aria-checked="filters.security === option.value"
309+
:aria-checked="filters.security === security"
310310
class="tag transition-colors duration-200 opacity-50 cursor-not-allowed focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
311311
:class="
312-
filters.security === option.value ? 'bg-fg text-bg border-fg hover:text-bg/70' : ''
312+
filters.security === security ? 'bg-fg text-bg border-fg hover:text-bg/70' : ''
313313
"
314314
>
315-
{{ $t(getSecurityLabelKey(option.value)) }}
315+
{{ $t(getSecurityLabelKey(security)) }}
316316
</button>
317317
</div>
318318
</fieldset>

app/components/compare/FacetSelector.vue

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,36 @@
11
<script setup lang="ts">
2-
import type { ComparisonFacet } from '#shared/types'
3-
import { FACET_INFO, FACETS_BY_CATEGORY, CATEGORY_ORDER } from '#shared/types/comparison'
4-
5-
const { isFacetSelected, toggleFacet, selectCategory, deselectCategory } = useFacetSelection()
6-
7-
// Enrich facets with their info for rendering
8-
const facetsByCategory = computed(() => {
9-
const result: Record<
10-
string,
11-
{ facet: ComparisonFacet; info: (typeof FACET_INFO)[ComparisonFacet] }[]
12-
> = {}
13-
for (const category of CATEGORY_ORDER) {
14-
result[category] = FACETS_BY_CATEGORY[category].map(facet => ({
15-
facet,
16-
info: FACET_INFO[facet],
17-
}))
18-
}
19-
return result
20-
})
2+
const {
3+
isFacetSelected,
4+
toggleFacet,
5+
selectCategory,
6+
deselectCategory,
7+
facetsByCategory,
8+
categoryOrder,
9+
getCategoryLabel,
10+
} = useFacetSelection()
2111
2212
// Check if all non-comingSoon facets in a category are selected
2313
function isCategoryAllSelected(category: string): boolean {
2414
const facets = facetsByCategory.value[category] ?? []
25-
const selectableFacets = facets.filter(f => !f.info.comingSoon)
26-
return selectableFacets.length > 0 && selectableFacets.every(f => isFacetSelected(f.facet))
15+
const selectableFacets = facets.filter(f => !f.comingSoon)
16+
return selectableFacets.length > 0 && selectableFacets.every(f => isFacetSelected(f.id))
2717
}
2818
2919
// Check if no facets in a category are selected
3020
function isCategoryNoneSelected(category: string): boolean {
3121
const facets = facetsByCategory.value[category] ?? []
32-
const selectableFacets = facets.filter(f => !f.info.comingSoon)
33-
return selectableFacets.length > 0 && selectableFacets.every(f => !isFacetSelected(f.facet))
22+
const selectableFacets = facets.filter(f => !f.comingSoon)
23+
return selectableFacets.length > 0 && selectableFacets.every(f => !isFacetSelected(f.id))
3424
}
3525
</script>
3626

3727
<template>
3828
<div class="space-y-3" role="group" :aria-label="$t('compare.facets.group_label')">
39-
<div v-for="category in CATEGORY_ORDER" :key="category">
29+
<div v-for="category in categoryOrder" :key="category">
4030
<!-- Category header with all/none buttons -->
4131
<div class="flex items-center gap-2 mb-2">
4232
<span class="text-[10px] text-fg-subtle uppercase tracking-wider">
43-
{{ $t(`compare.facets.categories.${category}`) }}
33+
{{ getCategoryLabel(category) }}
4434
</span>
4535
<button
4636
type="button"
@@ -51,9 +41,7 @@ function isCategoryNoneSelected(category: string): boolean {
5141
: 'text-fg-muted/60 hover:text-fg-muted'
5242
"
5343
:aria-label="
54-
$t('compare.facets.select_category', {
55-
category: $t(`compare.facets.categories.${category}`),
56-
})
44+
$t('compare.facets.select_category', { category: getCategoryLabel(category) })
5745
"
5846
:disabled="isCategoryAllSelected(category)"
5947
@click="selectCategory(category)"
@@ -70,9 +58,7 @@ function isCategoryNoneSelected(category: string): boolean {
7058
: 'text-fg-muted/60 hover:text-fg-muted'
7159
"
7260
:aria-label="
73-
$t('compare.facets.deselect_category', {
74-
category: $t(`compare.facets.categories.${category}`),
75-
})
61+
$t('compare.facets.deselect_category', { category: getCategoryLabel(category) })
7662
"
7763
:disabled="isCategoryNoneSelected(category)"
7864
@click="deselectCategory(category)"
@@ -84,31 +70,31 @@ function isCategoryNoneSelected(category: string): boolean {
8470
<!-- Facet buttons -->
8571
<div class="flex items-center gap-1.5 flex-wrap" role="group">
8672
<button
87-
v-for="{ facet, info } in facetsByCategory[category]"
88-
:key="facet"
73+
v-for="facet in facetsByCategory[category]"
74+
:key="facet.id"
8975
type="button"
90-
:title="info.comingSoon ? $t('compare.facets.coming_soon') : info.description"
91-
:disabled="info.comingSoon"
92-
:aria-pressed="isFacetSelected(facet)"
93-
:aria-label="info.label"
76+
:title="facet.comingSoon ? $t('compare.facets.coming_soon') : facet.description"
77+
:disabled="facet.comingSoon"
78+
:aria-pressed="isFacetSelected(facet.id)"
79+
:aria-label="facet.label"
9480
class="inline-flex items-center gap-1 px-1.5 py-0.5 font-mono text-xs rounded border transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50"
9581
:class="
96-
info.comingSoon
82+
facet.comingSoon
9783
? 'text-fg-subtle/50 bg-bg-subtle border-border-subtle cursor-not-allowed'
98-
: isFacetSelected(facet)
84+
: isFacetSelected(facet.id)
9985
? 'text-fg-muted bg-bg-muted border-border'
10086
: 'text-fg-subtle bg-bg-subtle border-border-subtle hover:text-fg-muted hover:border-border'
10187
"
102-
@click="!info.comingSoon && toggleFacet(facet)"
88+
@click="!facet.comingSoon && toggleFacet(facet.id)"
10389
>
10490
<span
105-
v-if="!info.comingSoon"
91+
v-if="!facet.comingSoon"
10692
class="w-3 h-3"
107-
:class="isFacetSelected(facet) ? 'i-carbon:checkmark' : 'i-carbon:add'"
93+
:class="isFacetSelected(facet.id) ? 'i-carbon:checkmark' : 'i-carbon:add'"
10894
aria-hidden="true"
10995
/>
110-
{{ info.label }}
111-
<span v-if="info.comingSoon" class="text-[9px]"
96+
{{ facet.label }}
97+
<span v-if="facet.comingSoon" class="text-[9px]"
11298
>({{ $t('compare.facets.coming_soon') }})</span
11399
>
114100
</button>

app/composables/useFacetSelection.ts

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
1-
import type { ComparisonFacet } from '#shared/types'
2-
import { ALL_FACETS, DEFAULT_FACETS, FACET_INFO } from '#shared/types/comparison'
1+
import type { ComparisonFacet, FacetInfo } from '#shared/types'
2+
import {
3+
ALL_FACETS,
4+
CATEGORY_ORDER,
5+
DEFAULT_FACETS,
6+
FACET_INFO,
7+
FACETS_BY_CATEGORY,
8+
} from '#shared/types/comparison'
39
import { useRouteQuery } from '@vueuse/router'
410

11+
/** Facet info enriched with i18n labels */
12+
export interface FacetInfoWithLabels extends Omit<FacetInfo, 'id'> {
13+
id: ComparisonFacet
14+
label: string
15+
description: string
16+
}
17+
518
/**
619
* Composable for managing comparison facet selection with URL sync.
720
*
821
* @param queryParam - The URL query parameter name to use (default: 'facets')
922
*/
1023
export function useFacetSelection(queryParam = 'facets') {
24+
const { t } = useI18n()
25+
26+
// Helper to build facet info with i18n labels
27+
function buildFacetInfo(facet: ComparisonFacet): FacetInfoWithLabels {
28+
return {
29+
id: facet,
30+
...FACET_INFO[facet],
31+
label: t(`compare.facets.items.${facet}.label`),
32+
description: t(`compare.facets.items.${facet}.description`),
33+
}
34+
}
35+
1136
// Sync with URL query param (stable ref - doesn't change on other query changes)
1237
const facetsParam = useRouteQuery<string>(queryParam, '', { mode: 'replace' })
1338

14-
// Parse facets from URL or use defaults
15-
const selectedFacets = computed<ComparisonFacet[]>({
39+
// Parse facet IDs from URL or use defaults
40+
const selectedFacetIds = computed<ComparisonFacet[]>({
1641
get() {
1742
if (!facetsParam.value) {
1843
return DEFAULT_FACETS
@@ -40,21 +65,26 @@ export function useFacetSelection(queryParam = 'facets') {
4065
},
4166
})
4267

68+
// Selected facets with full info and i18n labels
69+
const selectedFacets = computed<FacetInfoWithLabels[]>(() =>
70+
selectedFacetIds.value.map(buildFacetInfo),
71+
)
72+
4373
// Check if a facet is selected
4474
function isFacetSelected(facet: ComparisonFacet): boolean {
45-
return selectedFacets.value.includes(facet)
75+
return selectedFacetIds.value.includes(facet)
4676
}
4777

4878
// Toggle a single facet
4979
function toggleFacet(facet: ComparisonFacet): void {
50-
const current = selectedFacets.value
80+
const current = selectedFacetIds.value
5181
if (current.includes(facet)) {
5282
// Don't allow deselecting all facets
5383
if (current.length > 1) {
54-
selectedFacets.value = current.filter(f => f !== facet)
84+
selectedFacetIds.value = current.filter(f => f !== facet)
5585
}
5686
} else {
57-
selectedFacets.value = [...current, facet]
87+
selectedFacetIds.value = [...current, facet]
5888
}
5989
}
6090

@@ -69,36 +99,50 @@ export function useFacetSelection(queryParam = 'facets') {
6999
// Select all facets in a category
70100
function selectCategory(category: string): void {
71101
const categoryFacets = getFacetsInCategory(category)
72-
const current = selectedFacets.value
102+
const current = selectedFacetIds.value
73103
const newFacets = [...new Set([...current, ...categoryFacets])]
74-
selectedFacets.value = newFacets
104+
selectedFacetIds.value = newFacets
75105
}
76106

77107
// Deselect all facets in a category
78108
function deselectCategory(category: string): void {
79109
const categoryFacets = getFacetsInCategory(category)
80-
const remaining = selectedFacets.value.filter(f => !categoryFacets.includes(f))
110+
const remaining = selectedFacetIds.value.filter(f => !categoryFacets.includes(f))
81111
// Don't allow deselecting all facets
82112
if (remaining.length > 0) {
83-
selectedFacets.value = remaining
113+
selectedFacetIds.value = remaining
84114
}
85115
}
86116

87117
// Select all facets globally
88118
function selectAll(): void {
89-
selectedFacets.value = DEFAULT_FACETS
119+
selectedFacetIds.value = DEFAULT_FACETS
90120
}
91121

92122
// Deselect all facets globally (keeps first facet to ensure at least one)
93123
function deselectAll(): void {
94-
selectedFacets.value = [DEFAULT_FACETS[0] as ComparisonFacet]
124+
selectedFacetIds.value = [DEFAULT_FACETS[0] as ComparisonFacet]
95125
}
96126

97127
// Check if all facets are selected
98-
const isAllSelected = computed(() => selectedFacets.value.length === DEFAULT_FACETS.length)
128+
const isAllSelected = computed(() => selectedFacetIds.value.length === DEFAULT_FACETS.length)
99129

100130
// Check if only one facet is selected (minimum)
101-
const isNoneSelected = computed(() => selectedFacets.value.length === 1)
131+
const isNoneSelected = computed(() => selectedFacetIds.value.length === 1)
132+
133+
// Get translated category name
134+
function getCategoryLabel(category: FacetInfo['category']): string {
135+
return t(`compare.facets.categories.${category}`)
136+
}
137+
138+
// All facets with their info and i18n labels, grouped by category
139+
const facetsByCategory = computed(() => {
140+
const result: Record<string, FacetInfoWithLabels[]> = {}
141+
for (const category of CATEGORY_ORDER) {
142+
result[category] = FACETS_BY_CATEGORY[category].map(buildFacetInfo)
143+
}
144+
return result
145+
})
102146

103147
return {
104148
selectedFacets,
@@ -111,6 +155,10 @@ export function useFacetSelection(queryParam = 'facets') {
111155
isAllSelected,
112156
isNoneSelected,
113157
allFacets: ALL_FACETS,
158+
// Facet info with i18n
159+
getCategoryLabel,
160+
facetsByCategory,
161+
categoryOrder: CATEGORY_ORDER,
114162
}
115163
}
116164

0 commit comments

Comments
 (0)