diff --git a/src/features/discover/model/discover.selectors.ts b/src/features/discover/model/discover.selectors.ts index 5e26084..49a29f7 100644 --- a/src/features/discover/model/discover.selectors.ts +++ b/src/features/discover/model/discover.selectors.ts @@ -6,25 +6,38 @@ export function sortProfiles(profiles: Profile[], currentUserId?: string): Profi return sortByCurrentUserAndName(profiles, currentUserId); } +function normalizeTag(tag: string): string { + return tag.trim().toLowerCase(); +} + export function getPopularTags(profiles: Profile[]): { topTags: string[]; allTags: string[] } { const counts: Record = {}; + const displayLabels: Record = {}; profiles.forEach((profile) => { profile.canvas?.loves?.forEach((tag) => { - counts[tag] = (counts[tag] || 0) + 1; + const normalized = normalizeTag(tag); + counts[normalized] = (counts[normalized] || 0) + 1; + if (!displayLabels[normalized]) { + displayLabels[normalized] = normalized; + } }); profile.canvas?.comfort?.forEach((tag) => { - counts[tag] = (counts[tag] || 0) + 1; + const normalized = normalizeTag(tag); + counts[normalized] = (counts[normalized] || 0) + 1; + if (!displayLabels[normalized]) { + displayLabels[normalized] = normalized; + } }); }); const sortedByCount = Object.entries(counts).sort((a, b) => b[1] - a[1]); return { - topTags: sortedByCount.slice(0, 3).map(([tag]) => tag), - allTags: Object.keys(counts).sort((a, b) => - a.localeCompare(b, "pt-BR", { sensitivity: "base" }), - ), + topTags: sortedByCount.slice(0, 3).map(([normalizedTag]) => displayLabels[normalizedTag]), + allTags: Object.keys(counts) + .map((normalizedTag) => displayLabels[normalizedTag]) + .sort((a, b) => a.localeCompare(b, "pt-BR", { sensitivity: "base" })), }; } @@ -38,6 +51,7 @@ export function filterProfiles( }, ): Profile[] { const query = filters.searchQuery.trim().toLowerCase(); + const normalizedSelectedTag = filters.selectedTag ? normalizeTag(filters.selectedTag) : ""; return profiles.filter((profile) => { const matchesSearch = @@ -56,8 +70,8 @@ export function filterProfiles( const matchesTag = !filters.selectedTag || - profile.canvas?.loves?.includes(filters.selectedTag) || - profile.canvas?.comfort?.includes(filters.selectedTag); + profile.canvas?.loves?.some((tag) => normalizeTag(tag) === normalizedSelectedTag) || + profile.canvas?.comfort?.some((tag) => normalizeTag(tag) === normalizedSelectedTag); return Boolean(matchesSearch && matchesRole && matchesStatus && matchesTag); });