This commit is contained in:
MisbehavedNinjaRadiator 2025-06-18 17:17:57 +02:00 committed by MisbehavedNinjaRadiator
parent 0edae08cd3
commit 32311fe6db
8 changed files with 20 additions and 39 deletions

View file

@ -0,0 +1,64 @@
import React, { useState, useEffect, useRef } from "react";
import TestPost from "../../TestPost";
import "./feed.css";
import Post from "../Post";
function Feed() {
const [posts, setPosts] = useState<number[]>([]);
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(true);
const feedRef = useRef<HTMLDivElement | null>(null);
const PAGE_SIZE = 10;
const fetchPosts = () => {
if (loading) return;
setLoading(true);
//random shit to differentiate test posts
setTimeout(() => {
setPosts((prev) => {
const newPosts = [];
for (let i = 0; i < PAGE_SIZE; i++) {
newPosts.push(Math.floor(Math.random() * 100) + 1);
}
return [...prev, ...newPosts];
});
setLoading(false);
}, 800);
};
useEffect(() => {
fetchPosts();
}, []);
useEffect(() => {
const onScroll = () => {
const feed = feedRef.current;
if (!feed || loading || !hasMore) return;
if (feed.scrollTop + feed.clientHeight >= feed.scrollHeight - 100) {
fetchPosts();
}
};
const feed = feedRef.current;
feed?.addEventListener("scroll", onScroll);
return () => {
feed?.removeEventListener("scroll", onScroll);
};
}, [loading, hasMore]);
return (
<div className="feedContainer">
<main className="feedContent" ref={feedRef}>^
<Post/>
{posts.map((postId, idx) => (
<TestPost key={idx} postId={postId} />
))}
{loading && <div className="loading">Loading more posts...</div>}
</main>
</div>
);
}
export default Feed;

View file

@ -0,0 +1,36 @@
.feedContainer {
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
background-color: #f9f9f9;
}
.feedContent {
flex: 1;
overflow-y: auto;
padding: 1rem;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 1rem;
}
.loading {
width: 100%;
text-align: center;
margin-top: 1rem;
font-weight: bold;
color: #333;
}
/* Desktop responsive behavior */
@media (min-width: 768px) {
.feedContent {
width: 600px;
margin: 0 auto;
padding: 2rem 0;
}
}

View file

@ -0,0 +1,17 @@
import "./header.css";
function Header() {
return (
<header className="base-header">
<div className="base-header-icon"> <img src='/assets/icons/feather_black.svg' alt="featherIcon" /> </div>
<p className="header-title small-title">
Feather Feed
</p>
<div className="base-header-icon"> <img src="/assets/icons/three_menu_stripes_black.svg" alt="menuIcon" /> </div>
</header>
);
}
export default Header;