import { useState } from "react"; import type { GetReadArticlesQuery, GetUnreadArticlesQuery, } from "../graphql/generated/graphql"; import { ArticleItem } from "./ArticleItem"; interface Props { articles: NonNullable< | GetUnreadArticlesQuery["unreadArticles"] | GetReadArticlesQuery["readArticles"] >; isReadView?: boolean; } export function ArticleList({ articles, isReadView }: Props) { const [hiddenArticleIds, setHiddenArticleIds] = useState>( new Set(), ); const handleArticleReadChange = (articleId: string, isRead: boolean) => { if (isReadView !== isRead) { setHiddenArticleIds((prev) => new Set(prev).add(articleId)); } }; const visibleArticles = articles.filter( (article) => !hiddenArticleIds.has(article.id), ); if (visibleArticles.length === 0) { return (
No articles found.
); } // Group articles by feed const articlesByFeed = visibleArticles.reduce( (acc, article) => { const feedId = article.feed.id; if (!acc[feedId]) { acc[feedId] = { feed: article.feed, articles: [], }; } acc[feedId].articles.push(article); return acc; }, {} as Record< string, { feed: { id: string; title: string }; articles: typeof articles } >, ); return (
{Object.values(articlesByFeed).map(({ feed, articles: feedArticles }) => (

{feed.title} ({feedArticles.length} article {feedArticles.length !== 1 ? "s" : ""})

{feedArticles.map((article) => ( ))}
))}
); }