Correct tag facets handling
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -58,6 +58,11 @@ export interface AuthResponse {
|
||||
expiresIn: number;
|
||||
}
|
||||
|
||||
export interface FacetCount {
|
||||
value: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
results: Story[];
|
||||
totalHits: number;
|
||||
@@ -65,6 +70,7 @@ export interface SearchResult {
|
||||
perPage: number;
|
||||
query: string;
|
||||
searchTimeMs: number;
|
||||
facets?: Record<string, FacetCount[]>;
|
||||
}
|
||||
|
||||
export interface PagedResult<T> {
|
||||
|
||||
Reference in New Issue
Block a user