aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/ArticleItem.tsx
blob: 8fb00b065cf21a740b254e2665f5000b84438fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 (
		<div
			className={`group flex items-center gap-3 rounded-lg border p-3 hover:bg-gray-50 ${
				optimisticArticle.isRead
					? "border-gray-200 bg-white"
					: "border-blue-200 bg-blue-50"
			}`}
		>
			{showReadStatus && (
				<button
					type="button"
					onClick={() => handleToggleRead(article.id, optimisticArticle.isRead)}
					className={`flex-shrink-0 rounded p-1 transition-colors ${
						optimisticArticle.isRead
							? "text-gray-400 hover:text-gray-600"
							: "text-blue-600 hover:text-blue-700"
					}`}
					title={optimisticArticle.isRead ? "Mark as unread" : "Mark as read"}
				>
					<FontAwesomeIcon
						icon={optimisticArticle.isRead ? faCheck : faCircle}
						className="w-4 h-4"
					/>
				</button>
			)}
			<div className="flex-1 min-w-0">
				<button
					type="button"
					onClick={() => handleArticleClick(optimisticArticle)}
					className={`text-left w-full group-hover:text-blue-600 transition-colors ${
						optimisticArticle.isRead
							? "text-gray-700"
							: "text-gray-900 font-medium"
					}`}
				>
					<div className="flex items-center gap-2 break-words">
						{optimisticArticle.title}
					</div>
				</button>
			</div>
		</div>
	);
}