turash/bugulma/frontend/hooks/useTimelineItems.ts
2025-12-15 10:06:41 +01:00

39 lines
1.2 KiB
TypeScript

import { mapBackendTimelineItem } from '@/lib/heritage-mapper';
import type { BackendHeritageDataResponse } from '@/schemas/backend/heritage';
import { useMemo } from 'react';
export const useTimelineItems = (
heritageData: BackendHeritageDataResponse | undefined,
selectedCategory: string,
minImportance: number
) => {
return useMemo(() => {
if (!heritageData?.timeline_items) return [];
console.debug(
'[useTimelineItems] timeline_items from backend:',
heritageData.timeline_items.length
);
return heritageData.timeline_items
.map(mapBackendTimelineItem)
.sort((a, b) => {
// Sort by time_from chronologically (oldest first)
const timeA = a.timeFrom ? new Date(a.timeFrom).getTime() : 0;
const timeB = b.timeFrom ? new Date(b.timeFrom).getTime() : 0;
return timeA - timeB;
})
.filter((item) => {
// Category filter
if (selectedCategory !== 'all' && item.category !== selectedCategory) {
return false;
}
// Importance filter
if (item.importance < minImportance) {
return false;
}
return true;
});
}, [heritageData, selectedCategory, minImportance]);
};