Skip to content

Commit bc4c15d

Browse files
committed
fix top posters from post type filter
1 parent 4193305 commit bc4c15d

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/components/Feed.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import TopPosterStatusBar from "./TopPosterStatusBar";
3232
const NUM_TOP_POSTERS = 6;
3333

3434
const columnClasses: { [key: number]: string } = {
35-
1: "lg:columns-1",
36-
2: "lg:columns-2",
37-
3: "lg:columns-3",
38-
4: "lg:columns-4",
39-
5: "lg:columns-5",
35+
1: "lg:columns-1",
36+
2: "lg:columns-2",
37+
3: "lg:columns-3",
38+
4: "lg:columns-4",
39+
5: "lg:columns-5",
4040
};
4141

4242
export interface FeedProps {
@@ -56,7 +56,7 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
5656
const [hasMore, setHasMore] = useState(true);
5757
const [time, setTime] = useState<string>(initialTime);
5858
const [sort, setSort] = useState<string>(params.sort || initialSort);
59-
const [postTypeFilter, setPostTypeFilter] = useState<string>('all');
59+
const [postTypeFilter, setPostTypeFilter] = useState<string>("all");
6060
const [numColumns, setNumColumns] = useState(3);
6161
const observer = useRef<IntersectionObserver | null>(null);
6262
const sentinel = useRef(null);
@@ -65,15 +65,22 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
6565
);
6666
const [topPosters, setTopPosters] = useState<[string, number][]>([]);
6767

68+
const filteredPosts = useMemo(() => {
69+
if (postTypeFilter === "all") {
70+
return posts;
71+
}
72+
return posts.filter((post) => getPostType(post) === postTypeFilter);
73+
}, [posts, postTypeFilter]);
74+
6875
useEffect(() => {
6976
const authorCounts: { [key: string]: number } = {};
70-
posts.forEach((post) => {
77+
filteredPosts.forEach((post) => {
7178
authorCounts[post.author] = (authorCounts[post.author] || 0) + 1;
7279
});
7380

7481
const sortedAuthors = Object.entries(authorCounts).sort(([, countA], [, countB]) => countB - countA);
7582
setTopPosters(sortedAuthors.slice(0, NUM_TOP_POSTERS));
76-
}, [posts]);
83+
}, [filteredPosts]);
7784

7885
useEffect(() => {
7986
if (sort === "top") {
@@ -169,13 +176,6 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
169176
{ label: "Time", options: timeOptions },
170177
];
171178

172-
const filteredPosts = useMemo(() => {
173-
if (postTypeFilter === 'all') {
174-
return posts;
175-
}
176-
return posts.filter(post => getPostType(post) === postTypeFilter);
177-
}, [posts, postTypeFilter]);
178-
179179
return (
180180
<div className="dark:bg-custom-black dark:text-white">
181181
<div className="max-w-[95vw] mx-auto relative py-4">
@@ -242,7 +242,9 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
242242
</div>
243243
</div>
244244
<div className="hidden md:flex items-center gap-2.5 shrink-0 mb-4 w-full justify-start">
245-
<label htmlFor="columns" className="text-[11px] font-medium text-zinc-600 uppercase tracking-wide">Cols</label>
245+
<label htmlFor="columns" className="text-[11px] font-medium text-zinc-600 uppercase tracking-wide">
246+
Cols
247+
</label>
246248
<input
247249
id="columns"
248250
type="range"
@@ -251,7 +253,8 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
251253
value={numColumns}
252254
onChange={(e) => setNumColumns(Number(e.target.value))}
253255
className="w-20 h-0.75 bg-zinc-800 rounded-full appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3 [&::-webkit-slider-thumb]:bg-blue-400 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:cursor-pointer [&::-webkit-slider-thumb]:shadow-[0_0_6px_rgba(96,165,250,0.5)]"
254-
/><span className="text-xs font-mono text-zinc-400 w-4 text-center tabular-nums">{numColumns}</span>
256+
/>
257+
<span className="text-xs font-mono text-zinc-400 w-4 text-center tabular-nums">{numColumns}</span>
255258
</div>
256259
<div className={`columns-1 ${columnClasses[numColumns]} gap-4`}>
257260
{filteredPosts.map((post) => (
@@ -341,8 +344,7 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
341344
</div>
342345
))}
343346
</div>
344-
<div ref={sentinel} className="h-1"></div>{" "}
345-
<BackToTopButton />
347+
<div ref={sentinel} className="h-1"></div> <BackToTopButton />
346348
<TopPosterStatusBar topPosters={topPosters} />
347349
</div>
348350
</div>

0 commit comments

Comments
 (0)