import { faCheck, faCircle } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { useOptimistic } from "react"; import { useMutation } from "urql"; import type { GetReadArticlesQuery, GetUnreadArticlesQuery, } from "../graphql/generated/graphql"; import { MarkArticleReadDocument, MarkArticleUnreadDocument, } from "../graphql/generated/graphql"; type Article = NonNullable< | GetUnreadArticlesQuery["unreadArticles"] | GetReadArticlesQuery["readArticles"] >[0]; interface Props { article: Article; showReadStatus?: boolean; } export function ArticleItem({ article, showReadStatus = true }: Props) { const [, markArticleRead] = useMutation(MarkArticleReadDocument); const [, markArticleUnread] = useMutation(MarkArticleUnreadDocument); const [optimisticArticle, setOptimisticArticle] = useOptimistic( article, (currentArticle, newReadState: boolean) => ({ ...currentArticle, isRead: newReadState, }), ); const handleToggleRead = async ( articleId: string, isCurrentlyRead: boolean, ) => { const newReadState = !isCurrentlyRead; setOptimisticArticle(newReadState); if (isCurrentlyRead) { await markArticleUnread({ id: articleId }); } else { await markArticleRead({ id: articleId }); } }; const handleArticleClick = async (article: Article) => { // Open article in new tab and mark as read if it's unread window.open(article.url, "_blank", "noreferrer"); if (!optimisticArticle.isRead) { setOptimisticArticle(true); await markArticleRead({ id: article.id }); } }; return (