Skip to content

Commit 85cb043

Browse files
committed
add top communities status bar
1 parent e00aaef commit 85cb043

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
3+
interface TopCommunitiesStatusBarProps {
4+
topCommunities: [string, number][];
5+
}
6+
7+
const TopCommunitiesStatusBar: React.FC<TopCommunitiesStatusBarProps> = ({ topCommunities }) => {
8+
if (topCommunities.length === 0) {
9+
return null;
10+
}
11+
12+
return (
13+
<div className="fixed bottom-0 left-0 right-0 bg-slate-200 dark:bg-neutral-900 py-2 border-t border-gray-300 dark:border-gray-700 text-sm z-40 shadow-lg hidden md:block">
14+
<div className="max-w-[95vw] mx-auto flex items-center justify-center gap-x-4 px-4 overflow-x-auto whitespace-nowrap hide-scrollbar">
15+
<span className="font-bold text-gray-800 dark:text-gray-200 shrink-0">Top Communities:</span>
16+
{topCommunities.map(([community, count]) => (
17+
<React.Fragment key={community}>
18+
<a href={`/r/${community}`} className="shrink-0 flex items-center gap-1 px-2 py-0.5 rounded bg-zinc-300/70 dark:bg-zinc-800/70 text-[11px] text-zinc-700 dark:text-zinc-400 hover:text-white hover:bg-zinc-700/70 transition-colors">
19+
<span>r/</span>
20+
<span>{community}</span>
21+
<span className="text-zinc-600 text-[10px]">{count}</span>
22+
</a>
23+
</React.Fragment>
24+
))}
25+
</div>
26+
</div>
27+
);
28+
};
29+
30+
export default TopCommunitiesStatusBar;

src/components/UserPost.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import SecureMediaEmbed from "./SecureMediaEmbed";
1919
import Thumbnail from "./Thumbnail";
2020
import { postTypeOptions } from "../utils/timeOptions";
2121
import SegmentedControl from "./SegmentedControl";
22+
import TopCommunitiesStatusBar from "./TopCommunitiesStatusBar";
23+
24+
const NUM_TOP_COMMUNITIES = 6;
2225

2326
const columnClasses: { [key: number]: string } = {
2427
1: "lg:columns-1",
@@ -52,6 +55,19 @@ const UserPost: React.FC<UserPostProps> = memo(({ username }) => {
5255
return posts.filter((post) => getPostType(post) === postTypeFilter);
5356
}, [posts, comments, postTypeFilter, activeTab]);
5457

58+
const topCommunities = useMemo(() => {
59+
const communityCounts: { [key: string]: number } = {};
60+
61+
[...posts, ...comments].forEach((item) => {
62+
if (item.subreddit) {
63+
communityCounts[item.subreddit] = (communityCounts[item.subreddit] || 0) + 1;
64+
}
65+
});
66+
67+
const sortedCommunities = Object.entries(communityCounts).sort(([, countA], [, countB]) => countB - countA);
68+
return sortedCommunities.slice(0, NUM_TOP_COMMUNITIES);
69+
}, [posts, comments]);
70+
5571
const fetchPosts = useCallback(() => {
5672
if (!hasMore) return;
5773

@@ -271,6 +287,7 @@ const UserPost: React.FC<UserPostProps> = memo(({ username }) => {
271287
))}
272288
</div>
273289
<div ref={sentinel} className="h-1"></div>
290+
<TopCommunitiesStatusBar topCommunities={topCommunities} />
274291
</div>
275292
</div>
276293
);

0 commit comments

Comments
 (0)