-
Notifications
You must be signed in to change notification settings - Fork 1
/
feed-list.tsx
79 lines (69 loc) · 1.73 KB
/
feed-list.tsx
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
import useSWR from "swr";
import { useDashboardContext } from "../hooks/useDashboard";
import { getFeeds } from "../lib/fetcher";
import { LoadingIndicator } from "./ui/activity-indicator";
import { classNames } from "./ui/class-names";
interface FeedItemProps {
id: string;
title: string;
selected: null | { id: string; title: string };
onSelect(id: string, title: string): void;
}
export const FeedItem = (props: FeedItemProps) => {
const handleSelect = () => {
if (props.id) {
props.onSelect(props.id, props.title);
}
};
const isSelected = props.id === props.selected?.id;
return (
<li
key={props.id}
className={classNames("cursor-pointer p-2", isSelected && "bg-sky-500/50 text-white")}>
<div className="flex justify-between">
<div onClick={handleSelect} className="flex-1">
<h2 className="text-base">{props.title}</h2>
<pre className="text-xs opacity-25">{props.id}</pre>
</div>
</div>
</li>
);
};
export const FeedList = () => {
const [{ feed: selectedFeed }, { selectFeed }] = useDashboardContext();
const { data, error } = useSWR("feeds", getFeeds);
const isLoading = !error && !data;
if (isLoading) {
return <LoadingIndicator />;
}
if (error) {
return <div>{error.toString()}</div>;
}
if (data) {
if (data.feeds.length === 0) {
return (
<div>
<p>Looks like you have no feeds.</p>
</div>
);
}
return (
<div className="absolute top-0 left-0 right-0 bottom-0 w-full">
<ul>
{data.feeds.map((feed) =>
feed === null ? null : (
<FeedItem
key={feed?.id}
id={feed?.id}
title={feed?.title}
onSelect={selectFeed}
selected={selectedFeed}
/>
)
)}
</ul>
</div>
);
}
return null;
};