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]); };