Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/features/discover/model/discover.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> = {};
const displayLabels: Record<string, string> = {};

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" })),
};
}

Expand All @@ -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 =
Expand All @@ -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);
});
Expand Down
Loading