Feed filters
Code ReviewSenior
- Target
- find the 3 must-fix problems (4 in all)
- Your best
- Not played yet
Pull request
Feed: tags, replies, back to top · feature/feed-v2
The feed grows to a few hundred posts a day, so this pull request:
- sorts it newest first and adds tag filters;
- adds a reply box to every card (the draft stays in the card);
- shrinks the cards below the fold while scrolling, and shows a Back to top button further down.
Your teammate wrote it with an AI assistant and checked it on a feed of twenty posts. Review it before it is merged: comment on every line that has to change, and give each comment a category.
Hints
Your comments
Scoring: a comment finds a problem when it is on the problem's lines (or one line around them) and names its category. The score is the share of problems found, minus half a problem for each comment that finds nothing new. Find every must-fix problem, with no more such comments than there are problems, to complete the challenge. Only your first review is scored.
11import { useEffect, useState } from "react";
122import { PostCard } from "./PostCard.jsx";
233
33export function Feed({ posts }) {
44export function Feed({ posts, tags }) {
55 const [tag, setTag] = useState("all");
66 const [showTop, setShowTop] = useState(false);
77
88 // Newest first, then only the chosen tag.
99 const sorted = posts.sort((a, b) => b.createdAt - a.createdAt);
1010 const visible = tag === "all" ? sorted : sorted.filter((post) => post.tags.includes(tag));
1111
1212 useEffect(() => {
1313 function onScroll() {
1414 // Cards below the fold show only their title until the user gets there.
1515 for (const card of document.querySelectorAll(".post-card")) {
1616 card.style.height = card.getBoundingClientRect().top > window.innerHeight ? "3rem" : "auto";
1717 }
1818 // Show "Back to top" once the user has scrolled down.
1919 if (window.scrollY > 800 !== showTop) setShowTop(!showTop);
2020 }
2121 window.addEventListener("scroll", onScroll, { passive: true });
2222 return () => window.removeEventListener("scroll", onScroll);
2323 }, []);
2424
42525 return (
55 <ul className="feed">
66 {posts.map((post) => (
77 <PostCard key={post.id} post={post} />
88 ))}
99 </ul>
2626 <>
2727 <div className="tags" role="group" aria-label="Filter by tag">
2828 {["all", ...tags].map((name) => (
2929 <button key={name} aria-pressed={tag === name} onClick={() => setTag(name)}>
3030 {name}
3131 </button>
3232 ))}
3333 </div>
3434 <ul className="feed">
3535 {visible.map((post, index) => (
3636 <PostCard key={index} post={post} />
3737 ))}
3838 </ul>
3939 {showTop && (
4040 <button className="back-to-top" onClick={() => window.scrollTo({ top: 0 })}>
4141 Back to top
4242 </button>
4343 )}
4444 </>
104545 );
114646}