Correct tag facets handling

This commit is contained in:
Stefan Hardegger
2025-07-28 14:37:58 +02:00
parent 860bf02d56
commit c08082c0d6
5 changed files with 114 additions and 21 deletions

View File

@@ -2,7 +2,7 @@
import { useState, useEffect } from 'react';
import { searchApi } from '../../lib/api';
import { Story, Tag } from '../../types/api';
import { Story, Tag, FacetCount } from '../../types/api';
import AppLayout from '../../components/layout/AppLayout';
import { Input } from '../../components/ui/Input';
import Button from '../../components/ui/Button';
@@ -29,24 +29,16 @@ export default function LibraryPage() {
// Extract tags from current search results with counts
const extractTagsFromResults = (stories: Story[]): Tag[] => {
const tagCounts: { [key: string]: number } = {};
// Convert facet counts to Tag objects for the UI
const convertFacetsToTags = (facets?: Record<string, FacetCount[]>): Tag[] => {
if (!facets || !facets.tagNames) {
return [];
}
stories.forEach(story => {
story.tagNames?.forEach(tagName => {
if (tagCounts[tagName]) {
tagCounts[tagName]++;
} else {
tagCounts[tagName] = 1;
}
});
});
return Object.entries(tagCounts).map(([tagName, count]) => ({
id: tagName, // Use tag name as ID since we don't have actual IDs from search results
name: tagName,
storyCount: count
return facets.tagNames.map(facet => ({
id: facet.value, // Use tag name as ID since we don't have actual IDs from search results
name: facet.value,
storyCount: facet.count
}));
};
@@ -72,8 +64,8 @@ export default function LibraryPage() {
setTotalPages(Math.ceil((result?.totalHits || 0) / 20));
setTotalElements(result?.totalHits || 0);
// Always update tags based on current search results (including initial wildcard search)
const resultTags = extractTagsFromResults(currentStories);
// Update tags from facets - these represent all matching stories, not just current page
const resultTags = convertFacetsToTags(result?.facets);
setTags(resultTags);
} catch (error) {
console.error('Failed to load stories:', error);