aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/FeedSidebar.tsx
blob: 3a367f55ef0d1ff527f760d2364f5d1dd28f2264 (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
import { useAtomValue } from "jotai";
import { Suspense } from "react";
import { useLocation, useSearch } from "wouter";
import { feedsAtom } from "../atoms";
import { ErrorBoundary } from "./ErrorBoundary";

interface Props {
	basePath: string;
	isReadView?: boolean;
}

export function FeedSidebar({ basePath, isReadView = false }: Props) {
	const search = useSearch();
	const [, setLocation] = useLocation();
	const params = new URLSearchParams(search);
	const selectedFeedId = params.get("feed");

	const handleSelect = (feedId: string | null) => {
		if (feedId) {
			setLocation(`${basePath}?feed=${feedId}`);
		} else {
			setLocation(basePath);
		}
	};

	return (
		<nav>
			<h2 className="mb-3 text-xs font-semibold uppercase tracking-wide text-stone-400">
				Feeds
			</h2>
			<ul className="space-y-0.5">
				<li>
					<button
						type="button"
						onClick={() => handleSelect(null)}
						className={`w-full rounded-md px-3 py-1.5 text-left text-sm transition-colors ${
							!selectedFeedId
								? "bg-stone-200 font-medium text-stone-900"
								: "text-stone-600 hover:bg-stone-100"
						}`}
					>
						All feeds
					</button>
				</li>
				<ErrorBoundary>
					<Suspense>
						<FeedListItems
							isReadView={isReadView}
							selectedFeedId={selectedFeedId}
							onSelect={handleSelect}
						/>
					</Suspense>
				</ErrorBoundary>
			</ul>
		</nav>
	);
}

function FeedListItems({
	isReadView,
	selectedFeedId,
	onSelect,
}: {
	isReadView: boolean;
	selectedFeedId: string | null;
	onSelect: (feedId: string | null) => void;
}) {
	const { data: allFeeds } = useAtomValue(feedsAtom);

	const feeds = isReadView
		? allFeeds
		: allFeeds.filter((feed) => feed.unreadCount > 0);

	return (
		<>
			{feeds.map((feed) => (
				<li key={feed.id}>
					<button
						type="button"
						onClick={() => onSelect(feed.id)}
						className={`flex w-full items-center justify-between rounded-md px-3 py-1.5 text-left text-sm transition-colors ${
							selectedFeedId === feed.id
								? "bg-stone-200 font-medium text-stone-900"
								: "text-stone-600 hover:bg-stone-100"
						}`}
					>
						<span className="min-w-0 truncate">{feed.title}</span>
						{!isReadView && feed.unreadCount > 0 && (
							<span className="ml-2 shrink-0 rounded-full bg-sky-100 px-1.5 py-0.5 text-xs font-medium text-sky-700">
								{feed.unreadCount}
							</span>
						)}
					</button>
				</li>
			))}
		</>
	);
}