From 987f78ac25af119796c6f3830a61aafcabeb2dc1 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 22 Aug 2025 23:17:09 +0900 Subject: fix(frontend): wrong optimistic update --- frontend/src/components/ArticleItem.tsx | 38 +++++++++++---------------------- 1 file changed, 13 insertions(+), 25 deletions(-) (limited to 'frontend/src/components/ArticleItem.tsx') diff --git a/frontend/src/components/ArticleItem.tsx b/frontend/src/components/ArticleItem.tsx index faa86fe..4942518 100644 --- a/frontend/src/components/ArticleItem.tsx +++ b/frontend/src/components/ArticleItem.tsx @@ -1,6 +1,5 @@ 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, @@ -18,27 +17,19 @@ type Article = NonNullable< interface Props { article: Article; + onReadChange?: (articleId: string, isRead: boolean) => void; } -export function ArticleItem({ article }: Props) { +export function ArticleItem({ article, onReadChange }: 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); + onReadChange?.(articleId, newReadState); if (isCurrentlyRead) { await markArticleUnread({ id: articleId }); @@ -50,9 +41,8 @@ export function ArticleItem({ article }: Props) { 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); - + if (!article.isRead) { + onReadChange?.(article.id, true); await markArticleRead({ id: article.id }); } }; @@ -60,38 +50,36 @@ export function ArticleItem({ article }: Props) { return (
-- cgit v1.2.3-70-g09d2