import type {
GetReadArticlesQuery,
GetUnreadArticlesQuery,
} from "../graphql/generated/graphql";
import { ArticleItem } from "./ArticleItem";
interface Props {
articles: NonNullable<
| GetUnreadArticlesQuery["unreadArticles"]
| GetReadArticlesQuery["readArticles"]
>;
}
export function ArticleList({ articles }: Props) {
if (articles.length === 0) {
return (
No articles found.
);
}
// Group articles by feed
const articlesByFeed = articles.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) => (
))}
))}
);
}