import React, { useState, useMemo } from 'react'; import { CurrentView, Post } from '../types'; import { SECTION_IDS, STORIES_ALL_CONTENT } from '../constants'; import Button from './Button'; import { ArrowUturnLeftIcon } from './icons'; import PostCard from './PostCard'; import FilterSortBar, { ViewMode, SortOptionKey, SortOption } from './FilterSortBar'; interface StoriesAllSectionProps { posts: Post[]; setCurrentView: (view: CurrentView) => void; setSelectedItemId: (id: string | null) => void; } const sortOptions: SortOption[] = [ { key: 'newest', label: 'Сначала новые' }, { key: 'oldest', label: 'Сначала старые' }, { key: 'alphabetical', label: 'По алфавиту (А-Я)' }, ]; const StoriesAllSection: React.FC = ({ posts, setCurrentView, setSelectedItemId }) => { const [viewMode, setViewMode] = useState('grid'); const [selectedCategories, setSelectedCategories] = useState([]); const [selectedSort, setSelectedSort] = useState('newest'); const handleBackClick = () => { setCurrentView('main'); setSelectedItemId(null); setTimeout(() => { document.getElementById(SECTION_IDS.story)?.scrollIntoView({ behavior: 'smooth' }); }, 0); }; const availableCategories = useMemo(() => { if (!posts) return []; return Array.from(new Set(posts.map(p => p.category))).sort(); }, [posts]); const handleCategoryChange = (category: string) => { setSelectedCategories(prev => prev.includes(category) ? prev.filter(c => c !== category) : [...prev, category] ); }; const filteredAndSortedPosts = useMemo(() => { if (!posts) return []; const filtered = selectedCategories.length > 0 ? posts.filter(p => selectedCategories.includes(p.category)) : posts; return [...filtered].sort((a, b) => { switch (selectedSort) { case 'newest': return new Date(b.publishedAt || 0).getTime() - new Date(a.publishedAt || 0).getTime(); case 'oldest': return new Date(a.publishedAt || 0).getTime() - new Date(b.publishedAt || 0).getTime(); case 'alphabetical': return a.title.localeCompare(b.title); default: return 0; } }); }, [posts, selectedCategories, selectedSort]); if (!posts) { return (

Загрузка проектов...

); } const layoutClasses = viewMode === 'grid' ? "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 md:gap-8" : "flex flex-col gap-4"; return (

{STORIES_ALL_CONTENT.title}

{STORIES_ALL_CONTENT.subtitle}

setSelectedCategories([])} sortOptions={sortOptions} selectedSort={selectedSort} onSortChange={setSelectedSort} /> {filteredAndSortedPosts.length > 0 ? (
{filteredAndSortedPosts.map(post => ( ))}
) : (

{STORIES_ALL_CONTENT.emptyStateTitle}

{STORIES_ALL_CONTENT.emptyStateMessage}

)}
); }; export default StoriesAllSection;